LXD VM You Can Mount a Drive

In my series on LXD Virtual Machines we learned several interesting techniques.

It is also possible to create additional virtual disks, called storage volumes which can be attached to a LXD VM. In this presentation, we learn how to work with LXD VM storage volumes. This includes creating a disk, attaching a disk, formatting a disk and mounting the disk in our Ubuntu LXD VM.

This assumes you have followed the previous tutorials on creating an Ubuntu LXD VM.

To create a storage volume on your LXD host:

lxc storage volume create default mydisk size=10GB --type=block

To add the new storage volume to the Ubuntu “test-vm” LXD VM:

lxc config device add test-vm mydisk disk pool=default source=mydisk

We entered the console to access the LXD VM:

lxc console test-vm --type=vga

I installed the Gnome partition manager in Ubuntu at the terminal:

sudo apt install gparted

We created a partition table on the new drive using gparted. We then formatted an ext4 partition in gparted.

We created a mount folder, allowed all access to the folder and mounted the new storage:

sudo mkdir /mnt/mydisk
sudo chmod 777 /mnt/mydisk
sudo mount /dev/sdb1 /mnt/mydisk

To dismount the storage:

sudo umount /dev/sdb1

To mount the drive when the system boots, edit the file systems table:

sudo nano /etc/fstab

Use gparted to find the UUID of the new partition and make an entry in the fstab adjusted for your UUID.

UUID=a71531e2-46d4-41f3-aec8-c55611541c39 /mnt/mydisk ext4 _netdev 0 0

Save the file out of the editor and reboot. The drive should be mounted automatically.

Reverse the process by removing the fstab entry and then shutdown the Ubuntu LXD VM.

To remove the mydisk volume attachment from your LXD VM:

lxc config device remove test-vm mydisk

The storage volume is staill intact at this point and can be attached to another LXD VM if desired.

Commands to list the storage pools you have on your LXD host and your volumes:

lxc storage list
lxc storage volume list default

To permanently delete a volume from your LXD host:

lxc storage volume delete default mydisk