Can i add a unix-block device to a virtual machine?

Is there a way to attach a block device to a virtual machine?
I tried this:

zfs create -V 4gb tank/vol
lxc config device add ubuntu1 b1 unix-block source=/dev/zvol/tank/vol

And got this error:

Error: Invalid devices: Device validation failed for “b1”: Unsupported device type

I wanted to do this in order to create zfs filesystems inside the virtual machine.

You can use the disk type for this.

https://linuxcontainers.org/lxd/docs/master/instances/#type-disk

You just specify a source and not a path.

The unix-block device type only supports containers as it is for creating block devices with specific major and minor numbers which cannot be done with VMS.

Arbitrary unix-block/unix-char devices no, but we do support passing in additional disks through the disk type.

In your case you could use lxc config device add ubuntu b1 disk source=/dev/zvol/tank/vol.

Though as you just allocated it, it’d actually be cleaner with:

  • lxc storage volume create default my-vol --type=block size=20GiB
  • lxc config device add ubuntu b1 disk pool=default source=my-vol

Ok, both worked. I noticed that the path of the device in the container is /dev/sd?, as opposed to unix-block devices in containers that get the same path as the source property.

1 Like

Yes we cannot control where the VMS os will create the block device unlike containers.

We do however set some metadata through QEMU so if you inspect /dev/disk/ you should find some symlinks that let you tie your LXD device name to the disk.

1 Like