Ubuntu LXD Development environment with LVM

You don’t need to use the ZFS filesystem for LXD because it has the option of using LVM (Logical Volume Manager) as the storage driver. Note: do not delete the ‘default’ storage volume once it is using your system Volume Group. Please note a slight concern:

One supported volume type is a thin pool, which allows over-committing the resources by creating thinly provisioned volumes whose total allowed maximum size (quota) is larger than the available physical storage.

https://documentation.ubuntu.com/lxd/latest/reference/storage_lvm/#lvm-driver-in-lxd

When you have a spare drive

If you have a spare drive, then this is much more preferable than the second senario.

Format the drive, using with the ‘Disk’ software: the ‘Format Disk’ option. Then create a partition. Note the device PATH of the partition (it might look like /dev/sdd1).

Going to a Terminal, set the partition to an LVM Physical Volume:

pvcreate /dev/sdd1

Create a Volume Group:

vgcreate lxd-vg /dev/sdd1

Initialise LXD and YES to configure a new storage pool, specifying the name of the Volume Group (‘lxd-vg’ here) as the ‘pool’.

sudo lxd init

Now just to LXD Development Profile.

Laptop with only one drive

Unless you have already install Ubuntu with LVM, then you will need to create a Live USB with Ubuntu Desktop, backup your existing installation, and install Ubuntu – use the encrypted LVM option. BTW is LUKS encryption required? Just make sure it is using LVM. Reboot into Live USB again.

In the live USB environment, open a terminal.

Use lsblk to find the name of the (most likely third) partition on the system drive – mine was nvme0n1p3, but yours may be sda3. It needs to be the encrypted partition, and note the size of the partition, so you can decide how much to allocate to LXD.

lsblk

Use cryptsetup to open your Ubuntu system drive:

cryptsetup open /dev/nvme0n1p3 systemroot

If there is an error regarding this partition not being a LUKS partition, then try another.

If this worked, then you should be able to now see the Physical Volume, Volume Group, and Logical Volume that were created by the Ubuntu install:

sudo pvs && sudo vgs && sudo lvs

The defaults during install for Volume Group and Logical Volume will be ubuntu-vg and ubuntu-lv

Mount the opened drive:

sudo mount /dev/mapper/systemroot /mnt

Change root to this mount point

sudo mount --bind /dev /mnt/dev && sudo mount --bind /dev/pts /mnt/dev/pts && sudo mount --bind /sys /mnt/sys && sudo mount --bind /proc /proc && chroot /mnt

Now I recommend viewing your volume group and logical volume again:

vgs && lvs

The defaults for an Ubuntu install are: ubuntu-vg and ubuntu-lv. We are going to resize the volume ubuntu-lv. Here I am resizing it by minus 60g (Gigabytes). Note the option -r to resize the filesystem within the Logical Volume.

lvresize -r --size -60g <vg>/<lv>

If you need another Logical Volume for some other reason, create it now, because LXD will use the remaining space for itself. Now you have 60g of unused ‘extents’ on the Volume Group ubuntu-vg.

Exit the changed root:

exit

Un-mount all the mounts made above:

umount -R /mnt

Reboot:

reboot

Now you should have a running system except that the system Logical Volume is 40g smaller. You can check:

df -h

Check the line with /dev/mapper/ubuntu...

Install LXD

sudo snap install lxd

Initialise LXD. When choosing the option ‘Configure a new storage pool’ – choose NO.

sudo lxd init

Creating the default ‘pool’

In the above senario, we skipped the configuration of a new storage pool. We will create the ‘default’ pool using your existing Volume Group ubuntu-vg:

sudo lxc storage create default lvm volume.lvm.vg.force_reuse volume.lvm.vg_name=ubuntu-vg

Now we will edit the default LXC Profile to ensure that all new Instances are using this ‘default’ pool:

sudo lxc profile edit default

You need to make sure these lines are present:

devices:
    ...
    root:
        path: /
        pool: default
        type: disk

Now when you create an instance, it will use the ‘default’ storage pool which is using the Volume Group ubuntu-vg. To see the storage:

sudo lxc storage list

LXD Development Profile

Now to add a Profile to LXD that will have give access to a sub-directory of your home directory. I have a home directory name ‘Coder’ (the source) and the destination I have chosen is ‘coder’. The ‘shift’ option is to make this disk device have use the same uid and gid as the host machine’s source directory.

sudo lxc profile create coder
sudo lxc profile device add coder coderdisk disk source=/home/andrew/Coder path=/home/ubuntu/coder shift=true

Now when you create an Instance, in this case called ‘dart’, add BOTH the default and coder profiles:

sudo lxc launch ubuntu:26.04 dart -p default -p coder
sudo lxc exec dart --user 1000 --cwd /home/ubuntu/coder /bin/bash

You can add an alias for the container ‘dart’ in your .bashrc file:

alias lxd_dart='sudo lxc exec dart --user 1000 --cwd /home/ubuntu/coder /bin/bash'

Questions?

Leave a Reply

Your email address will not be published. Required fields are marked *