Is there a way to specify the disk size to create the LXD VM to? Currently, it sets to a default image of 9.3GB for an Ubuntu VM, but I need this to be larger, and I need to be able to configure it at creation time or otherwise expand it after launch.
Using the LXD 4.0 snap with a ZFS storage pool, and need to expand the disk space for the VM. How do I go about that?
Heh, actually I got more info from the IRC channel heh.
Got a set of two commands - one to initialize the device and one to override disk size before starting. With a cloud-init setup using growpart it lets me spin Ubuntu VMs easily with LXD.
This is the ‘vm’ profile which controls deployment and base user/system config:
config:
user.user-data: |
#cloud-config
ssh_pwauth: yes
users:
- name: teward
passwd: "snip" # Use a pw in /etc/shadow format!
lock_passwd: false
groups: lxd
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
growpart:
mode: auto
devices: ['/']
ignore_growroot_disabled: false
description: LXD Profile for Virtual Machines
devices:
config:
source: cloud-init:config
type: disk
eth0:
name: eth0
nictype: bridged
parent: lxdbr0
type: nic
root:
path: /
pool: lxd-qemu
type: disk
name: vm
With this, the following two commands work (example using Ubuntu 18.04):
(You can replace ‘50GB’ with any size that makes sense)
Or because VMs I deploy will need different sizes, I wrote this into a script which executes the relevant commands and prompts for the disk size (in GB), and uses the vm profile for init:
#!/bin/bash
if [ $(($#)) -lt 2 ]; then
echo "Invalid number of arguments provided. Require: imagepath vmname"
exit 1
fi
image=$1
name=$2
lxc init ${image} ${name} --vm --profile=vm
if [ $? -ne 0 ]; then
echo "lxc init failed, please check output."
exit 2;
fi
echo -n "Enter VM disk size in GB (min=20): "
read inputsize
size=$((inputsize))
if [ $((size)) -lt 20 ]; then
echo "Size is smaller than 20GB, defaulting to 20GB."
size=20
fi
lxc config device override ${name} root "size=${size}GB"
if [ $? -ne 0 ]; then
echo "Unable to override VM disk size, please check error output."
exit 3
fi
echo ""
echo "------"
echo ""
echo "LXD/QEMU-backed Virtual Machine ${name} created with disk of ${size}GB."
echo ""
echo "You may start your VM with:"
echo ""
echo " lxc start ${name}"
echo ""
echo "...and access its console by using:"
echo ""
echo " lxc console ${name}"
echo ""
… which when in the $PATH can be executed as lxc-launch-vm ubuntu:18.04 bionic-vm (where the first is the link to the image, and the second is the name - I only confirmed this works on Ubuntu VMs via LXD at the moment though…)