You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Request]: vminit API for block device operations, initially for freeze/thaw. #660
Adding a guest agent interface call for control operations (typically ioctl()) on block devices can enable some valuable storage features for CZ-based apps:
Is this issue going to be only focusing on the implementation of freeze/thaw? Or as you mentioned, we want one API that does: freeze, thaw, trim?
Any tips on how to get started into understanding the containerization side of things? Here is what I am thinking below:
Start by looking into Vminitd and understanding how the API works with what exists now.
Next, I will take a look into how the Ext4 Filesystem works and see how it is being used currently.
Finally, look into the ContainerizationOS package and understanding how syscall or sysctl works? I feel I need to implement something similar for the ioctl calls (FSTRIM, FSFREEZE).
I want to make sure I gain a solid understanding of things before I even think of coming up with an API design 🚀.
In the main project, VirtualMachineAgent (the protocol for the client), Vminitd (the implementation over the generated gRPC client), SandboxContext.proto (the protobuf definition, see also the make protos target).
In the vminitd subproject, really the main thing you need to look at is Server+GRPC.swift, you'll implement the server side of the call there. Looking at the other code will give you a sense for how to make system calls in Swift.
You don't need to know anything about ext4fs for this. These ioctls operate at the virtual file system level.
I don't think you'll need to worry about ContainerizationOS here. This is Linux swift code so you can just call ioctl() (see RunCommand.swift in the vminitd subproject).
The API will definitely need a pathname to the block device, let's call it devicePath for now. I've done the trim function bit in a past life so I can spell out what it will look like in terms of pseudocode; the freeze/thaw case is similar and the device validation code will be shared for whatever operations get performed.
// #define FITRIM _IOWR('X', 121, struct fstrim_range)
FITRIM = UInt32(0xc0185879)
trimBlockDevice(path: FilePath):
if not path.isAbsolute():
throw "filesystem trim requires an absolute path"
finfo = stat()
rc = lstat(path, finfo)
if rc != 0:
throw "filesystem trim stat failed: \(errno)"
if finfo.mode & MODE_SYMLINK != 0:
throw"filesystem trim refuses to process a symlink"
fd = open(r.Path)
if fd == -1:
throw "filesystem trim open failed: \(errno)"
defer fd.close()
trange = fstrim_range
trange.start = 0
trange.len = UInt64.max
trange.minlen = 0
rc = ioctl(fd, FITRIM, &trange)
if rc == -1:
throw "filesystem trim failed: \(errno)
In the vminitd subproject, really the main thing you need to look at is Server+GRPC.swift, you'll implement the server side of the call there. Looking at the other code will give you a sense for how to make system calls in Swift.
@jglogan 😆 My xcode was bugging and the vminitd package was completely missing. I was so confused, as I thought there was some sort of server side call completely missing. I was reviewing your work on #669, when I saw the package! Just as you responded here, I figured out the things you mentioned above (the RPC stuff) and have one clarifying question.
I understand that vminitd creates a gRPC server inside the VM listening on a vsock port. So these APIs are created to simply interact with the guest agent (make modifications, etc.). But how does container even use them and is there a good command to follow in order to see how it all comes together?
As John said, container is just a consumer of this library, it doesn't care about vminitd. The library has a host-side (macOS) and a guest-side (Linux VM). They communicate over gRPC on vsock port 1024 which you found.
vminitd (vminitd/Sources/vminitd/) — the guest agent. It boots inside the VM, mounts
essential filesystems, and starts a gRPC server on yee olde vsock port.
Vminitd client (Sources/Containerization/Vminitd.swift) — the host-side gRPC client stub that
wraps all the RPC calls.
The gRPC service is called SandboxContext (defined in Sources/Containerization/SandboxContext/SandboxContext.proto) and exposes a bunch of RPCs (we will add more I promise LOL) for process lifecycle, mounts, networking, sysctl, etc. Essentially everything to setup the system such that it's ready to take workloads (containers).
Following the Container Create + Start Flow
The best command to trace is create → start on LinuxContainer:
configureDNS(), configureHosts(): writes /etc/resolv.conf and /etc/hosts
LinuxContainer.start()
Dials the agent again
Creates a LinuxProcess which sets up vsock listeners for stdio (stdin/stdout/stderr each get
their own vsock port)
Sends CreateProcess RPC: vminitd decodes the OCI spec and creates a ManagedContainer
Sends StartProcess RPC: vminitd launches the actual process (via vmexec or runc), returns
the PID.
On the vminitd side (vminitd/Sources/vminitd/Server+GRPC.swift)
createProcess(): decodes the OCI spec, applies alterations (uid/gid resolution,
PATH/HOME setup), creates a ManagedContainer
startProcess(): calls ManagedContainer.start() which launches the process
Virtualization.framework
All the virtualization bits right now are backed by interactions to Virtualization.framework. All of this code is mostly shoved in VZVirtualMachineInstance. We made a protocol (VirtualMachineInstance) such that you could kind of plug in any virtualization stack, so long as they support the features we need under the hood (vsock, virtio-block, virtiofs, virtio-net) to do IO. Related to Virtualization.framework, how vminitd even gets in the VM is probably worth mentioning:
vminitd and vmexec binaries are compiled as static Linux binaries (musl libc, cross-compiled via Swift's static Linux SDK; see vminitd/Makefile). They get packaged into an EXT4 disk image that becomes the VM's root filesystem.
The flow:
cctl rootfs create (Sources/cctl/RootfsCommand.swift) builds a tar.gz archive containing a minimal rootfs: /sbin/vminitd, /sbin/vmexec, and standard directories (bin, dev, sys, proc, run, tmp, mnt, var).
That tar.gz is unpacked into a sparse EXT4 filesystem image via EXT4Unpacker (Sources/Containerization/Image/Unpacker/EXT4Unpacker.swift). The library has its own pure-Swift EXT4 formatter in ContainerizationEXT4.
The Makefile orchestrates this with make init (Makefile).
Attaching It to the VM
In Mount.swift, when a mount has runtimeOptions: .virtioblk, it's attached as a VZVirtioBlockDeviceConfiguration backed by a VZDiskImageStorageDeviceAttachment. The init filesystem gets the first block device allocation (/dev/vda) via AttachedFilesystem, which uses a simple character allocator ('a', 'b', 'c'...).
Kernel Command Line
The key piece for asking the guest to run vminitd as its init process is in Kernel.swift, linuxCommandline(initialFilesystem:):
args.append("init=/sbin/vminitd") // run vminitd as PID 1
args.append("ro") // mount root read-only
// for ext4 specifically:
args.append("rootfstype=ext4")
args.append("root=/dev/vda") // the init block device
The default args also include console=hvc0 (so we can get serial console logs) and tsc=reliable.
This gets wired into the boot loader in VZVirtualMachineInstance.swift.
@dcantah Thank you for the in-depth explanation! Definitely lots to wrap my head around, but I started with focusing on the connection between the guest-agent/gRPC client and the Virtualization Framework. It is starting to make a lot more sense, but naturally as I dig deeper, I have more questions (that I hope you have answered in the other sections). I am sure as I continue tackling issues in this project, it will all come together nicely 💯 .
The design is based off the assumption that this will be a generic API for filesystem-level operations on block devices. For the first iteration, the API focuses on freeze and thaw only.
Name
Spent a good time mulling on this and trying to come up with a generic name but am open to ideas here 🤔 .
filesystemOperations - This makes sense if we expect to add more operations past freeze/thaw/trim. manageFilesystem - Similar to the one above and only works with more operations. freezeFilesystem - This one is good as its scope is just for freeze/thaw (could maybe work with the addition of trim).
Request Body
devicePath: string # Path to the filesystem
duration: int64 # How long the filesystem should remain frozen (ms)
The duration will be defaulted to a reasonable value.
For an initial passthrough lets start with a single device path and not worry about multiple paths.
If no device paths are specified, the command initiates freeze/thaw on all mounts for the running container.
Response Body
success: bool # Whether the filesystem freeze succeeded
frozenAt: int64 # Timestamp when freeze was applied
thawedAt: int64 # Timestamp when thaw was applied
error: string # Error message if freeze/thaw failed (empty if success)
EDIT: The timestamp fields do not add much value and can be omitted
Pseudocode
I did some extensive research into all the ioctl operations and found a good way to intertwine the operations.
Perform device validation (from the code John pasted above)
Freeze the filesystem
Based on the duration, wait, and then thaw the filesystem
Further Enhancements with Trim
If we wanted to perform the trim operation, we could just add an optional flag to the request body. If the flag is set to true, perform trim first and then skip the freeze/thaw operations.
Other Design Decisions
I had an idea of making one of the request fields an array of strings that specifies the operations to be performed. I can see there potentially being some overlap in functionality, but it also feels like it could get quite complicated. I know we don't want multiple APIs for filesystem operations BUT I think splitting up freeze/thaw and trim might make the most sense.
I think we'll want one RPC, so I agree on that, but protobuf supports a union-like functionality called oneof that we can use to take in different parameters for the various fs ops we may want to do. For example, something like this:
The data that is required for all requests (the path to the fs) lives outside of the oneof, and then the operation specific data lives inside. I didn't see that linux takes in any extra data for freeze or thaw, so left them as empty, but we could just define empty message FiFreezeParams if we want to to be safe so we could add fields later on without it being a breaking change.
I think we'll want one RPC, so I agree on that, but protobuf supports a union-like functionality called oneof
I wasn’t aware of oneof and can see how it allows us to accept different parameters for filesystem operations. I think I initially had the right idea of representing multiple operations, but wasn’t sure how that would translate into RPC! I also like how that only allows for a SINGLE operation per request, so it makes things simpler. Looks like I had the right idea about what we could name the request, response, RPC but I was overthinking this entire design 😅 .
I didn't see that linux takes in any extra data for freeze or thaw, so left them as empty, but we could just define empty message FiFreezeParams
Are there any params we want to add now, even though linux does not require anything? My only thought would be duration, so that there does not have to be two separate requests in order to execute a freeze and the immediately after, a thaw.
This is a good issue and I learnt about freeze/thaw in detail. While navigating, more than couple of things just crossed my thoughts:
1/ does other container frameworks provide a timeout, like what if host crashed, then what happens to guest fs? And how relatable (if at all) to the scope of this issue?
2/ what if freeze called multiple times or thaw called without freeze? Is it even possible?
3/ how ownership boundary works? For example who owns freeze or thaw?
Commented on one of the container issues, but I think we're likely going about this wrong. Instead of freezing the blockdev, why don't we just freeze the container itself? This is a supported option in cgroups, it will just place all processes running in the cgroup into a suspended state. This has essentially the same outcome, but to me it's much easier to reason about. I'm not as ecstatic about designing a pause/resume api at the moment though..
yea, I was almost thinking in the similar lines. Having a control plane around container itself probably makes a lot of sense given that the outcome is same. Having said that, (and to my very limited knowledge into it) are there any trade-offs to it? Like especially ownership, retry and recovery.
This is a supported option in cgroups, it will just place all processes running in the cgroup into a suspended state. This has essentially the same outcome
This is all new domain to me, so I am for digging into how to use the cgroup options in order to freeze the container itself. Again, I just want to make sure that I have a solid understanding of things before I continue down that path. Any specific things to look into?
I'm not as ecstatic about designing a pause/resume api at the moment though..
Any other reasons why you don't want to go this route? I thought we had a solid API design and it seems simple enough to me (unless you see this ballooning out of scope during implementation).
Could we also look into the pause functionality with the Virtualization.Framework?
TLDR: Don't worry about the container freezing part I'll work out some API we can use, I think this is still worth getting in after looking more.
I think the reason I didn't want to re-explore pause yet is because I can see two forms of pause being useful. Pausing the container in the guest solely, or pausing the entire VM. I was originally looking at save/restore support before, as I have pause/resume on VZVirtualMachineInstance right now and that is a prerequisite. But it required reversing the order that we do IO so it was a rather large lift.
Regardless, not something you need to worry about on further thought. I think we'll want both after digging in a bit more. We can freeze the container, then the blockdev, then you can clonefile the block on the host and we should be good. I think freezing the block dev only would be a tad odd as write(2)'s will hang was my concern, but freezing the blockdev also sync's any pending data https://github.com/torvalds/linux/blob/master/fs/super.c#L2122. We could sync explicitly to get the same thing, but that would sync all filesystems.
Activity
saehejkang commentedon Apr 8, 2026
@jglogan Couple of things
Is this issue going to be only focusing on the implementation of freeze/thaw? Or as you mentioned, we want one API that does: freeze, thaw, trim?
Any tips on how to get started into understanding the
containerizationside of things? Here is what I am thinking below:Vminitdand understanding how the API works with what exists now.Ext4 Filesystemworks and see how it is being used currently.ContainerizationOSpackage and understanding howsyscallorsysctlworks? I feel I need to implement something similar for theioctlcalls (FSTRIM, FSFREEZE).I want to make sure I gain a solid understanding of things before I even think of coming up with an API design 🚀.
CC: @dcantah in case you had any tips :)
jglogan commentedon Apr 9, 2026
@saehejkang thanks!
VirtualMachineAgent(the protocol for the client),Vminitd(the implementation over the generated gRPC client),SandboxContext.proto(the protobuf definition, see also themake protostarget).vminitdsubproject, really the main thing you need to look at isServer+GRPC.swift, you'll implement the server side of the call there. Looking at the other code will give you a sense for how to make system calls in Swift.ioctl()(seeRunCommand.swiftin the vminitd subproject).The API will definitely need a pathname to the block device, let's call it
devicePathfor now. I've done the trim function bit in a past life so I can spell out what it will look like in terms of pseudocode; the freeze/thaw case is similar and the device validation code will be shared for whatever operations get performed.saehejkang commentedon Apr 9, 2026
@jglogan 😆 My
xcodewas bugging and thevminitdpackage was completely missing. I was so confused, as I thought there was some sort of server side call completely missing. I was reviewing your work on #669, when I saw the package! Just as you responded here, I figured out the things you mentioned above (the RPC stuff) and have one clarifying question.I understand that
vminitdcreates agRPC serverinside the VM listening on avsockport. So these APIs are created to simply interact with the guest agent (make modifications, etc.). But how doescontainereven use them and is there a good command to follow in order to see how it all comes together?jglogan commentedon Apr 9, 2026
Container doesn't know about vminitd.
You need to look for things like this in Containerization: https://github.com/apple/containerization/blob/main/Sources/Containerization/LinuxContainer.swift#L558.
The call site for that create func in container is: https://github.com/apple/container/blob/main/Sources/Services/ContainerSandboxService/Server/SandboxService.swift#L249
dcantah commentedon Apr 9, 2026
As John said, container is just a consumer of this library, it doesn't care about vminitd. The library has a host-side (macOS) and a guest-side (Linux VM). They communicate over gRPC on vsock port 1024 which you found.
essential filesystems, and starts a gRPC server on yee olde vsock port.
wraps all the RPC calls.
The gRPC service is called SandboxContext (defined in Sources/Containerization/SandboxContext/SandboxContext.proto) and exposes a bunch of RPCs (we will add more I promise LOL) for process lifecycle, mounts, networking, sysctl, etc. Essentially everything to setup the system such that it's ready to take workloads (containers).
Following the Container Create + Start Flow
The best command to trace is create → start on LinuxContainer:
their own vsock port)
the PID.
PATH/HOME setup), creates a ManagedContainer
Virtualization.framework
All the virtualization bits right now are backed by interactions to Virtualization.framework. All of this code is mostly shoved in
VZVirtualMachineInstance. We made a protocol (VirtualMachineInstance) such that you could kind of plug in any virtualization stack, so long as they support the features we need under the hood (vsock, virtio-block, virtiofs, virtio-net) to do IO. Related to Virtualization.framework, how vminitd even gets in the VM is probably worth mentioning:vminitd and vmexec binaries are compiled as static Linux binaries (musl libc, cross-compiled via Swift's static Linux SDK; see vminitd/Makefile). They get packaged into an EXT4 disk image that becomes the VM's root filesystem.
The flow:
cctl rootfs create(Sources/cctl/RootfsCommand.swift) builds a tar.gz archive containing a minimal rootfs: /sbin/vminitd, /sbin/vmexec, and standard directories (bin, dev, sys, proc, run, tmp, mnt, var).Attaching It to the VM
In Mount.swift, when a mount has runtimeOptions: .virtioblk, it's attached as a
VZVirtioBlockDeviceConfigurationbacked by aVZDiskImageStorageDeviceAttachment. The init filesystem gets the first block device allocation (/dev/vda) viaAttachedFilesystem, which uses a simple character allocator ('a', 'b', 'c'...).Kernel Command Line
The key piece for asking the guest to run vminitd as its init process is in Kernel.swift, linuxCommandline(initialFilesystem:):
The default args also include console=hvc0 (so we can get serial console logs) and tsc=reliable.
This gets wired into the boot loader in VZVirtualMachineInstance.swift.
saehejkang commentedon Apr 10, 2026
@dcantah Thank you for the in-depth explanation! Definitely lots to wrap my head around, but I started with focusing on the connection between the guest-agent/gRPC client and the Virtualization Framework. It is starting to make a lot more sense, but naturally as I dig deeper, I have more questions (that I hope you have answered in the other sections). I am sure as I continue tackling issues in this project, it will all come together nicely 💯 .
saehejkang commentedon Apr 11, 2026
@jglogan @dcantah
API Design for Freeze/Thaw
The design is based off the assumption that this will be a generic API for filesystem-level operations on block devices. For the first iteration, the API focuses on freeze and thaw only.
Name
Spent a good time mulling on this and trying to come up with a generic name but am open to ideas here 🤔 .
filesystemOperations- This makes sense if we expect to add more operations past freeze/thaw/trim.manageFilesystem- Similar to the one above and only works with more operations.freezeFilesystem- This one is good as its scope is just for freeze/thaw (could maybe work with the addition of trim).Request Body
Response Body
EDIT: The timestamp fields do not add much value and can be omitted
Pseudocode
I did some extensive research into all the
ioctloperations and found a good way to intertwine the operations.Further Enhancements with Trim
If we wanted to perform the trim operation, we could just add an optional flag to the request body. If the flag is set to
true, perform trim first and then skip the freeze/thaw operations.Other Design Decisions
I had an idea of making one of the request fields an array of strings that specifies the operations to be performed. I can see there potentially being some overlap in functionality, but it also feels like it could get quite complicated. I know we don't want multiple APIs for filesystem operations BUT I think splitting up freeze/thaw and trim might make the most sense.
dcantah commentedon Apr 11, 2026
I think we'll want one RPC, so I agree on that, but protobuf supports a union-like functionality called oneof that we can use to take in different parameters for the various fs ops we may want to do. For example, something like this:
The data that is required for all requests (the path to the fs) lives outside of the oneof, and then the operation specific data lives inside. I didn't see that linux takes in any extra data for freeze or thaw, so left them as empty, but we could just define empty
message FiFreezeParamsif we want to to be safe so we could add fields later on without it being a breaking change.saehejkang commentedon Apr 12, 2026
I wasn’t aware of
oneofand can see how it allows us to accept different parameters for filesystem operations. I think I initially had the right idea of representing multiple operations, but wasn’t sure how that would translate into RPC! I also like how that only allows for a SINGLE operation per request, so it makes things simpler. Looks like I had the right idea about what we could name the request, response, RPC but I was overthinking this entire design 😅 .Are there any params we want to add now, even though linux does not require anything? My only thought would be duration, so that there does not have to be two separate requests in order to execute a freeze and the immediately after, a thaw.
manojmahapatra commentedon Apr 15, 2026
This is a good issue and I learnt about freeze/thaw in detail. While navigating, more than couple of things just crossed my thoughts:
1/ does other container frameworks provide a timeout, like what if host crashed, then what happens to guest fs? And how relatable (if at all) to the scope of this issue?
2/ what if freeze called multiple times or thaw called without freeze? Is it even possible?
3/ how ownership boundary works? For example who owns freeze or thaw?
dcantah commentedon Apr 15, 2026
Commented on one of the container issues, but I think we're likely going about this wrong. Instead of freezing the blockdev, why don't we just freeze the container itself? This is a supported option in cgroups, it will just place all processes running in the cgroup into a suspended state. This has essentially the same outcome, but to me it's much easier to reason about. I'm not as ecstatic about designing a pause/resume api at the moment though..
manojmahapatra commentedon Apr 16, 2026
yea, I was almost thinking in the similar lines. Having a control plane around container itself probably makes a lot of sense given that the outcome is same. Having said that, (and to my very limited knowledge into it) are there any trade-offs to it? Like especially ownership, retry and recovery.
saehejkang commentedon Apr 17, 2026
This is all new domain to me, so I am for digging into how to use the
cgroupoptions in order to freeze the container itself. Again, I just want to make sure that I have a solid understanding of things before I continue down that path. Any specific things to look into?Any other reasons why you don't want to go this route? I thought we had a solid API design and it seems simple enough to me (unless you see this ballooning out of scope during implementation).
Could we also look into the
pausefunctionality with theVirtualization.Framework?https://developer.apple.com/documentation/virtualization/vzvirtualmachine/pause(completionhandler:)
dcantah commentedon Apr 20, 2026
TLDR: Don't worry about the container freezing part I'll work out some API we can use, I think this is still worth getting in after looking more.
I think the reason I didn't want to re-explore pause yet is because I can see two forms of pause being useful. Pausing the container in the guest solely, or pausing the entire VM. I was originally looking at save/restore support before, as I have pause/resume on VZVirtualMachineInstance right now and that is a prerequisite. But it required reversing the order that we do IO so it was a rather large lift.
Regardless, not something you need to worry about on further thought. I think we'll want both after digging in a bit more. We can freeze the container, then the blockdev, then you can
clonefilethe block on the host and we should be good. I think freezing the block dev only would be a tad odd as write(2)'s will hang was my concern, but freezing the blockdev also sync's any pending data https://github.com/torvalds/linux/blob/master/fs/super.c#L2122. We couldsyncexplicitly to get the same thing, but that would sync all filesystems.saehejkang commentedon Apr 22, 2026
Sounds good! Will get back to the implementation tomorrow.
[vminitd]: api for freeze/thaw filesystem operations (apple#685)
[vminitd]: api for freeze/thaw filesystem operations (apple#685)
feat(commit): support stopped service image commits