There are various benefits to using a ZFS backed block storage device, or ZVOL, as opposed to large file on disk. In this article I look at how virtual machines can be made using ZFS backed block storage, as opposed to regular Virtualbox files.
When creating a regular file to back a virtual machine, whenever a change has been made to that large file, a new copy must be made and stored whenever it is snapshotted. Using a ZVOL means the storage for the VM can be kept outside of regular datasets, and thus excluded from unneccesary snapshots. In addition to this, it is also possible to use ZFS snapshots instead of Virtualbox snapshots. While this does have some pitfalls, including the fact that if a snapshot is made while the VM is currently running the consistency of the file system could be in jeopardy; however, as long as snapshots are only made while the VM is off there shouldn’t be a problem.
Create a ZVOL Link to heading
Create a 25GB VM called
[root]# zfs create -V 25G tank/zvol-archlinux
Permissions Link to heading
Now the problem is accessing the ZVOL as a regular user. In order to create a disk in Virtualbox as an unprivledged user, set the owner and group.
[root]# chown john:disk /dev/zvol/tank/zvol-archlinux
For the short-term, using
chown to obtain ownership of the ZVOL works, but once the computer has been restarted, ownership will be lost. In order to make it permenant, add a udev rule for the ZVOL, by creating a new file
/etc/udev/rules.d/ that contains the following:
# /etc/udev/rules.d/99-local-zvol.rules # Give persistant ownership of ZVOL to user KERNEL=="zd*" SUBSYSTEM=="block" ACTION=="add|change" PROGRAM="/lib/udev/zvol_id /dev/%k" RESULT=="tank/zvol-archlinux" OWNER="john" GROUP="disk" MODE="0750"
This will make the ZVOL at
tank/zvol-archlinux owned by user john, in group disk, and with permissions
After applying the new rule, refresh the rules with
udevadm control --reload.
Create The Disk Link to heading
Now create the “wrapper” disk for Virtualbox. Specify a location for the resulting Virtualbox file, and the ZVOL being used.
[root]# VBoxManage internalcommands createrawvmdk \ -filename /home/john/VMs/disks/archlinux.vmdk \ -rawdisk /dev/zvol/tank/zvol-archlinux
It should say:
RAW host disk access VMDK file /home/john/VMs/disks/archlinux.vmdk created successfully.
This will create a new file “archlinux.vmdk” that is backed by the ZVOL.