Skip to content

Instantly share code, notes, and snippets.

@shantanoo-desai
Last active September 15, 2022 15:24
Show Gist options
  • Save shantanoo-desai/e628103b9c69c3e5ff90a491ab6390f4 to your computer and use it in GitHub Desktop.
Save shantanoo-desai/e628103b9c69c3e5ff90a491ab6390f4 to your computer and use it in GitHub Desktop.
Creating a Root Filesystem for Ubuntu 20.04 ARM64 using Hashicorp Packer + Docker

From Docker Container to Bootable Disk Images but with Packer

Source based on Blog Post @iximiuz (Ivan Velichko)

Putting it together

  1. Create the Filesystem Tarball with Kernel and SystemD

    packer build -only="filesystem.*" docker-ubuntu-focal-amd64.pkr.hcl
    
  2. Create a Bootable Disk Image using:

    packer build -only="partition.*" docker-ubuntu-jammy-amd64.pkr.hcl
    

The linux.img will be available in the working directory

source "docker" "fs-base" {
image = "ubuntu:jammy"
platform = "linux/amd64"
# Tar ball of the container's filesystem will be available in working directory
export_path = "./linux.tar"
}
source "docker" "partition-container" {
image = "ubuntu:jammy"
pull = false
platform = "linux/amd64"
cap_add = [ "SYS_ADMIN" ]
device = ["/dev/loop0"]
volumes = { "/tmp/docker-os-amd64/": "/os"}
}
build {
# Install Linux Kernel and SystemD packages for filesystem
name = "filesystem"
sources = [ "source.docker.fs-base" ]
provisioner "shell" {
inline = [
"apt-get update",
"apt-get install -y --no-install-recommends linux-image-virtual",
"apt-get install -y --no-install-recommends systemd-sysv"
]
}
}
build {
# Create Blank Image, Create Boot Partition, Root Partition, Final Image in Container
name = "partition"
sources = [ "source.docker.partition-container" ]
provisioner "shell" {
scripts = [
"./partition.sh"
]
}
}
#!/bin/bash
set -e
echo "[Creating Blank Image]"
IMG_SIZE=$(expr 1024 \* 1024 \* 1024)
dd if=/dev/zero of=/os/linux.img bs=${IMG_SIZE} count=1
sfdisk /os/linux.img <<EOF
label: dos
label-id: 0x5d8b75fc
device: new.img
unit: sectors
linux.img1 : start=2048, size=2095104, type=83, bootable
EOF
echo "[Mounting Image and making it EXT4]"
OFFSET=$(expr 512 \* 2048)
losetup -o ${OFFSET} /dev/loop0 /os/linux.img
mkfs.ext4 /dev/loop0
mkdir /os/mnt
mount -t auto /dev/loop0 /os/mnt/
echo "[Mounting the Filesystem]"
tar -xvf /os/linux.tar -C /os/mnt/
echo "[Install EXTLinux Bootloader]"
apt-get install -y extlinux
echo "[Creating Boot Partition]"
extlinux --install /os/mnt/boot/
cat > /os/mnt/boot/syslinux.cfg <<EOF
DEFAULT linux
SAY Now booting the kernel from SYSLINUX...
LABEL linux
KERNEL /vmlinuz
APPEND ro root=/dev/sda1 initrd=/initrd.img
EOF
echo "[Creating Final Image]"
dd if=/usr/lib/syslinux/mbr/mbr.bin of=/os/linux.img bs=440 count=1 conv=notrunc
umount /os/mnt
losetup -D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment