Skip to content

Instantly share code, notes, and snippets.

@joedborg
Last active April 25, 2024 15:46
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save joedborg/2ad747923104273c79dda76fe5063ce2 to your computer and use it in GitHub Desktop.
Save joedborg/2ad747923104273c79dda76fe5063ce2 to your computer and use it in GitHub Desktop.
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
config:
user.user-data: |
apt_mirror: http://us.archive.ubuntu.com/ubuntu/
ssh_pwauth: yes
users:
- name: ubuntu
passwd: "\$6\$s.wXDkoGmU5md\$d.vxMQSvtcs1I7wUG4SLgUhmarY7BR.5lusJq1D9U9EnHK2LJx18x90ipsg0g3Jcomfp0EoGAZYfgvT22qGFl/"
lock_passwd: false
groups: lxd
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
growpart:
mode: auto
devices:
- '/'
- '/dev/sda'
- '/dev/sda2'
ignore_growroot_disabled: false
description: VN profile
devices:
config:
source: cloud-init:config
type: disk
eth0:
nictype: bridged
parent: lxdbr0
type: nic
root:
path: /
pool: default
size: 100GB
type: disk
name: vm
@tgeek77
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?

@mattkoehne
Copy link

For newer LXD versions (5.8 and LTS 5.0.2) you can use the -d flag to increase the disk size of LXD virtual machines:

lxc launch "images:ubuntu/jammy/cloud" jammy --vm -c limits.cpu=4 -c limits.memory=8GiB -d root,size=50GiB

I tested this with version 5.0.2-838e1b2 👍

See also:

@cli0
Copy link

cli0 commented Feb 18, 2023

When I try to apply the above mentioned profile sudo lxc launch images:ubuntu/22.04 w1 -p myprofile the commands inside user.user-data do not get applied. My custom limits like cpus and RAM is respected but root size and installing packages defined within user.user-data do not work. I am using lxd 5.10. Do you guys have any experience with this kind of error?

@maworsham
Copy link

Is there a way in the profile to have the VM be configured with LVM partitioning?

Something like this (as an example):

profiles:
  - name: lma-vm
    config:
      limits.cpu: "2"
      limits.memory: 16GB
      security.secureboot: "false"
    description: "LVM Configuration for LMA Platform"
    devices:
      root:
        path: /
        pool: default
        size: 80GB
        type: disk
      eth0:
        name: eth0
        nictype: bridged
        parent: lxdbr0
        type: nic
      storage:
        config:
          ptable: gpt
          path: /dev/sda
          wipe: superblock-recursive
          preserve: false
          grub_device: true
          type: disk
          id: disk-sda
          partitions:
            - number: 1
              device: disk-sda
              flag: bios_grub
              size: 1M
              id: bios-grub
              type: partition
            - number: 2
              device: disk-sda
              flag: boot
              size: 1G
              id: sda-boot
              type: partition
            - number: 3
              device: disk-sda
              size: -1
              id: sda-lvm
              type: lvm_volgroup
              name: vg00
              devices: [sda-lvm]
              partitions:
                - id: lvroot
                  name: lvroot
                  size: 20G
                  type: lvm_partition
                  volgroup: vg00

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