Skip to content

LXD Virtual Machines



LXD is a great hypervisor to manage system containers, and pretty much anything you can use a virtual machine for you can use a system container instead. However, LXD is also able to manage virtual machines, and has been able to do so for more than a year now.

While virtual machines have been a part of LXD for quite a while, using them has been a bit challenging. Much of that has since been sorted out, and using virtual machines under LXD is now as easy to do as using system containers.

This page is pretty much my 'cheat sheet' for LXD virtual machines, where I can keep information and notes for future reference.

Note for remote LXD hosts

  • all of the below can be performed on a remote LXD host by appending the remote name to the vm
    • ie, "starbug:u2004v"

Basic launch of vm

$ lxc launch images:ubuntu/focal --vm u2004v

Launch vm and connect to console

  • console in shell
$ lxc launch images:ubuntu/focal --vm u2004v --console
  • console in remote-viewer
$ lxc launch images:ubuntu/focal --vm u2004v --console=vga

Connect to vm using lxd-agent

  • LXD containers have the lxd-agent built in, but for VM's it is an additional package named 'lxd-agent-loader'
  • It seems that the 'lxd-agent-loader' package is included in most of the LXD VM images in both the images: and ubuntu: remote image repositories
$ lxd exec u2004v bash

Launch vm with bridged profile

$ lxc launch images:ubuntu/focal --vm --profile bridged u2004v

Launch vm with specified cpu and memory

$ lxc launch images:ubuntu/focal --vm -c limits.cpu=2 -c limits.memory=4GiB u2004v

Connecting to console of running vm

  • connect to console in shell
$ lxc console u2004v
  • connect to console using remote-viewer
$ lxc console u2004v --type=vga

Create blank vm to install via ISO

  • NOTE: The ISO needs to be on a local filesystem. You'll likely receive a permission issue if the ISO is on a network filesystem.
$ lxc init --vm --empty -c limits.cpu=2 -c limits.memory=4GiB -c security.secureboot=false u2004v
$ lxc config device override u2004v root size=20GiB
$ lxc config device add u2004v iso disk source=/usr/local/ISOs/ubuntu-20.04-legacy-server-amd64.iso boot.priority=10
$ lxc start u2004v --console=vga

- perform the vm installation, as per normal
- after the install, when prompted to remove cd;

$ lxc stop -f u2004v
$ lxc config device remove u2004v iso
$ lxc start u2004v

Change size of disk for stock vm images


  • changing the disk size on a stock image vm is a two part process
  • first, init the vm and set the desired disk size
$ lxc init --vm images:ubuntu/focal u2004v
$ lxc config device override u2004v root size=20GiB
  • next, start the vm, and connect to it using 'lxc exec u2004v bash'
  • from within the vm, run the following (as root if entering as a non-root user)
    • (assumes the '/' root filesystem is on /dev/sda2)
  • (Note: some vm images automatically detect new disk size and resize '/' appropriately)
# growpart /dev/sda 2
# resize2fs /dev/sda2

If the VM has used LVM for the disk partitions, you'll likely need to perform an lvextend instead of the growpart;

(assuming lv is at /dev/mapper/ubuntu--vg-ubuntu--lv)

# lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
# resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

Add second nic to LXD VM

lxc config device add u2004v eth1 nic name=eth1 nictype=bridged parent=lxdbr0

Add second disk to LXD VM

  • create 100G disk in default storage pool and add it to vm named 'u2004v'
$ lxc storage volume create default u2004v-sdb size=100GiB --type=block
$ lxc config device add u2004v u2004v-sdb disk pool=default source=u2004v-sdb

Please note that the new disk won't be deleted if you delete the VM. In this case you need to delete the disk manually after deletion of the VM.

$ lxc storage volume delete default u2004v-sdb

Created: 2021-09-26 16:50
Last update: 2023-05-01 17:19