AngeloC
AngeloC

Reputation: 3533

How to create a volume in a ZFS dataset and attach to a LXD container?

I'd like to create a volume in a zfs dataset:

sudo zfs create mypool/maildir
sudo lxc storage volume create mypool/maldir custom1

and got

error: not found

my create is sure to be wrong, what I intended to do is:

  1. create a zfs dataset
  2. use the dataset as disk and mount to a container

so if I re-install the system, I can create a container and attach the same dataset to the new container,

is this possible? Thanks

Upvotes: 0

Views: 4526

Answers (1)

code_dredd
code_dredd

Reputation: 6095

Your post does not really show what your pool or datasets look like, but here's one way you can do it.

Generally, you want to:

  1. create the ZFS pool using ZFS directly, as you (probably) did with the zpool create ... command;
  2. create a dataset for LXD to use as its assigned pool, as you seem to be doing with zfs create ..., using the lxd init step to make this assignment;
  3. use LXC to create volumes within the LXD pool;
  4. use the attach-profile sub-command to assign volumes to container profiles

I don't have enough information to use your data as an example, so I'll show you how I did mine, using LXD/LXC 3.0:

Create the ZFS Pool and Dataset

$ sudo zpool create -m /mnt/tank tank raidz3 <vdev1> <vdev2> ...
$ sudo zfs create tank/lxd
$ sudo zfs list
NAME       USED  AVAIL  REFER  MOUNTPOINT
tank       954M  1.31T  41.7K  /mnt/tank
tank/lxd   953M  1.31T  41.7K  /mnt/tank/lxd

Give LXD its Dataset as a Pool

$ lxd init
...
Name of the new storage pool [default=default]: lxd-pool
Name of the storage backend to use (btrfs, dir, lvm, zfs) [default=zfs]:
Create a new ZFS pool? (yes/no) [default=yes]: no
Name of the existing ZFS pool or dataset: tank/lxd
...

Use LXC to Create the Volumes

The following command creates a new volume called dev-volume within the pool called lxd-pool that we assigned above, and sets a size of 512GB. (You may use whatever numbers you need in your case.)

$ lxc storage volume create lxd-pool dev-volume size=512GB

If you're getting an error: not found you'll need to make sure you're using the commands correctly, that the arguments are in the correct order so that you reference things that actually exist, and so on. Using lxc help <subcommand> is very helpful.

Attach Volume to Container Profile

You'll want to assign the volume to a profile that will be used by a container. To do this, simply rely on the following command:

$ lxc storage volume attach-profile lxd-pool dev-volume dev-profile tank /mnt/tank

The command above attaches the volume dev-volume within the lxd-pool to a profile called dev-profile as a device called tank that gets mounted at file system path /mnt/tank within the container.

I hope this helps.

Upvotes: 2

Related Questions