Skip to content

Instantly share code, notes, and snippets.

@Howard20181
Last active November 18, 2024 06:35
Show Gist options
  • Save Howard20181/b9d44935a146d7a84fc18c4c63d01b26 to your computer and use it in GitHub Desktop.
Save Howard20181/b9d44935a146d7a84fc18c4c63d01b26 to your computer and use it in GitHub Desktop.
Booting a Ubuntu base in qemu

How to build a Minimal System

  • Create filesystems

    1. Create a raw image file of 1G size

      dd if=/dev/zero of=ubuntu_base.img bs=1G count=1
    2. Installing EXT4 file system using mkfs

      mkfs.ext4 ubuntu_base.img
  • Mount

    1. Create a folder for mounting

      sudo mkdir /mnt/loopback
    2. Mount it

      sudo mount ubuntu_base.img /mnt/loopback
  • Unpacking system files

    sudo tar -xpf /mnt/e/Downloads/ubuntu-base-22.04.1-base-amd64.tar.gz -C /mnt/loopback

    Ubuntu base can be downloaded from https://cdimage.ubuntu.com/ubuntu-base/releases

  • Installation using arch-chroot

    sudo apt install arch-install-scripts
    sudo arch-chroot /mnt/loopback
  • A few adjustments to the system

    tee /etc/default/locale <<EOF
    LANGUAGE=en_US.UTF-8
    LANG=en_US.UTF-8
    LC_CTYPE=en_US.UTF-8
    EOF
    
    echo "LANG=en_US.UTF-8" > /etc/local.conf
    
    tee -a /etc/fstab <<EOF
    tmpfs /tmp tmpfs rw,nosuid,nodev,size=99%
    tmpfs /var/log tmpfs rw,nosuid,nodev
    EOF
    
    tee /etc/hosts <<EOF
    127.0.0.1       localhost localhost.localdomain
    127.0.1.1       localhost.localdomain
    EOF
  • Update software sources

    apt update && apt upgrade -y
  • Install the required packages

    apt install -y linux-image-virtual extlinux setools lzip patchelf e2fsprogs python3-minimal sudo finit-sysv qemu-guest-agent
  • Install bootloader

    mkdir -p /boot/syslinux
    
    tee /boot/syslinux/syslinux.cfg <<EOF
    PROMPT 0
    TIMEOUT 0
    DEFAULT Base
    
    LABEL Base
            LINUX ../vmlinuz
            APPEND root=/dev/sda rw
            INITRD ../initrd.img
    EOF
    
    extlinux --install /boot/syslinux
  • Cheanup

    apt clean
  • Other additional work, such as creating users, etc

    echo \
    '#!/bin/sh
    . /usr/share/initramfs-tools/hook-functions
    copy_exec /sbin/e2fsck /sbin
    copy_exec /sbin/fsck /sbin
    copy_exec /sbin/fsck.ext2 /sbin
    copy_exec /sbin/fsck.ext4 /sbin
    copy_exec /sbin/logsave /sbin' \
    | dd of=/etc/initramfs-tools/hooks/e2fsck.sh
    
    chmod +x /etc/initramfs-tools/hooks/e2fsck.sh
    
    update-initramfs -u
    
    adduser --disabled-password lsposed
    passwd -d lsposed
    adduser lsposed adm
    adduser lsposed sudo
    chmod u+w /etc/sudoers
    echo "lsposed ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
    chmod u-w /etc/sudoers
    
    tee /sbin/autologin <<EOF
    #!/bin/sh
    exec /usr/bin/login -f lsposed
    EOF
    
    chmod +x /sbin/autologin
    
    echo "tty [123456] /sbin/getty -L -n -l /sbin/autologin 0 /dev/tty1 linux nowait noclear" >> /etc/finit.conf
    
    dd if=/dev/zero of=/swapfile  bs=1M count=200
    chmod 0600 /swapfile
    mkswap /swapfile
    tee -a /etc/fstab <<EOF
    /swapfile    none    swap    sw    0   0
    EOF
  • Exit chroot

    exit
  • Unmounting the image

    sudo umount /mnt/loopback
  • Shrinking the image

    sudo e2fsck -pf ubuntu_base.img
    resize2fs -M ubuntu_base.img
  • Use qemu-img to shrink the image

    qemu-img convert -f raw -c -O qcow2 ubuntu_base.img ubuntu_base.qcow2
  • Boot in qemu

    qemu-system-x86_64 ubuntu_base.qcow2 -m 6g -smp 12 --accel whpx --snapshot
@Howard20181
Copy link
Author

It is not convenient to use bash as init, consider using finit-sysv

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