Skip to content

Instantly share code, notes, and snippets.

@mckees
Last active February 12, 2024 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mckees/51f8f000d5f92851d76d17a1e12b1aac to your computer and use it in GitHub Desktop.
Save mckees/51f8f000d5f92851d76d17a1e12b1aac to your computer and use it in GitHub Desktop.
Building an Ubuntu MAAS image with Packer

Packer Usage

Install build deps

sudo apt-get install virt-manager libvirt-daemon ovmf cloud-image-utils libslirp-dev nbdkit nbdfuse fuse2fs ninja-build meson pkg-config libglib2.0-dev libpixman-1-dev flex bison

Install QEMU

Download from https://www.qemu.org/download
cd qemu_dir
./configure --enable-slirp
make -j`nproc`
sudo make install

Install Packer:

https://developer.hashicorp.com/packer/downloads

Create image

git clone https://github.com/canonical/packer-maas
cd packer-maas/ubuntu/
vim ubuntu-cloudimg.pkr.hcl and change the -machine to "pc,accel=kvm" in qemuargs
cloud-localds seeds-cloudimg.iso user-data-cloudimg meta-data
packer init .

user-data-cloudimg

To add ssh keys or preserve your /etc/apt/sources.list, you might want to make changes like I have here:

#cloud-config
users:
  - name: root
    lock_passwd: false
    hashed_passwd: "<your_hashed_password"
    ssh_redirect_user: false
    ssh_import_id:
      - lp:mckeesh
ssh_pwauth: True
disable_root: false
preserve_hostname: true
apt:
  preserve_sources_list: true
runcmd:
  - sed -i -e '/^[#]*PermitRootLogin/s/^.*$/PermitRootLogin yes/' /etc/ssh/sshd_config
  - systemctl restart ssh

CHOICE 1: Build image without a kernel

PACKER_LOG=1 packer build -var ubuntu_series=jammy -only='cloudimg.*' .

CHOICE 2: Build an image with a kernel (My usual way)

PACKER_LOG=1 packer build -var kernel=linux-image-generic -var ubuntu_series=lunar -only='cloudimg.*' .

CHOICE 3: Build image with your own custom script

PACKER_LOG=1 packer build -var customize_script=custom-script.sh -var ubuntu_series=lunar -only='cloudimg.*' .

CHOICE 4: Build using make (less control over build options)

make custom-cloudimg.tar.gz

Build apt sources preservation into image

tar xvf custom-cloudimg.tar.gz -C image_contents

Edit image_contents/etc/cloud/cloud.cfg and add the following line:
apt_preserve_sources_list: true

gzip -d custom-cloudimg.tar.gz
pushd image_contents
tar -uf ../custom-cloudimg.tar etc/cloud/cloud.cfg
popd
gzip custom-cloudimg.tar

Upload to MAAS

Step 1: Get admin access to your MAAS server
Step 2: maas login <username> <MAAS URL>
Step 2.5: In the MAAS UI, click <username> -> API keys to get your API key to log in with
Step 3: maas your_username boot-resources create name='whatever_imagename_identifier' title='Image Display Name' architecture='amd64/generic' filetype='tgz' content@=custom-cloudimg.tar.gz

@hector-cao
Copy link

i think you could use md suffix to make markdown rendering

@vpa1977
Copy link

vpa1977 commented Feb 12, 2024

For the custom image apt sources we can explicitly specify them in apt section of user-data-cloud:

apt:
  primary:
    - arches: [default]
      uri: <your-archive-uri>
  disable_suites: [backports, security, updates]

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