Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
LXD Ubuntu VM launch and disk resize
#!/bin/env bash
set -e
readonly VM="banana"
readonly CPU="8"
readonly MEM="8GB"
readonly DSK="120GB"
lxc init images:ubuntu/focal ${VM} -p default -p vm --vm
lxc config set ${VM} limits.cpu ${CPU}
lxc config set ${VM} limits.memory ${MEM}
lxc config device override ${VM} root size=${DISK}
lxc start ${VM}
sleep 10 # `lxc start` needs a `--wait`.
lxc exec ${VM} -- apt update
lxc exec ${VM} -- apt install cloud-initramfs-growroot -y
lxc exec ${VM} -- growpart /dev/sda 2
lxc exec ${VM} -- resize2fs /dev/sda2
user.user-data: |
ssh_pwauth: yes
- name: ubuntu
passwd: "\$6\$s.wXDkoGmU5md\$d.vxMQSvtcs1I7wUG4SLgUhmarY7BR.5lusJq1D9U9EnHK2LJx18x90ipsg0g3Jcomfp0EoGAZYfgvT22qGFl/"
lock_passwd: false
groups: lxd
shell: /bin/bash
mode: auto
- '/'
- '/dev/sda'
- '/dev/sda2'
ignore_growroot_disabled: false
description: VN profile
source: cloud-init:config
type: disk
nictype: bridged
parent: lxdbr0
type: nic
path: /
pool: default
size: 100GB
type: disk
name: vm
Copy link

bmullan commented Sep 25, 2021

Thought I'd add a little info for those that haven't created or edited new LXD Profiles before.

Using the above ".sh" and ".yaml" contents...

First, set your EDITOR environment to whatever text editor you use (vi, nano etc)

$ export EDITOR=nano

To add a new "profile" to LXD you can Create a new empty profile:

$ lxc profile create myprofile (note: you don't have to call the new profile "myprofile")

then edit the new myprofile

$ lxc profile edit myprofile

Note: there will be 5-6 default lines at the bottom... you can delete those then
"Copy & Paste" the text content from the vm-lxd-profile.yaml above to the new profile.

Save the file & quit your text editor.

You can see if LXD knows about your new "profile" by...

$ lxc profile list
| myprofile | VN profile | 1 |
| default | Default LXD profile | 2 |

Finally, edit your copy of "" in the following Line:

$ lxc init images:ubuntu/focal ${VM} -p default -p vm --vm

and change the " -p vm " above to whatever you called your new LXD Profile

lxc init images:ubuntu/focal ${VM} -p default -p myprofile --vm

then run the script ""

$ chmod +x ./
$ ./

when its done you can see your new VM using:

$ lxc list

For further reference: LXD Profiles

Copy link

jamochl commented Oct 23, 2021

Great stuff! Wish this was better documented on the LXD docs

Copy link

tgeek77 commented Dec 1, 2021

Agreed. I spent all morning looking for this!

I do have one question though. I changed the user from "ubuntu" to "tux" because that's my preferred generic user, but tux isn't created but there is still an ubuntu user. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment