Skip to content

Instantly share code, notes, and snippets.

@rumpelsepp
Created September 21, 2017 12:22
Show Gist options
  • Save rumpelsepp/7f9aeb08f3e9b6ae261e027119034840 to your computer and use it in GitHub Desktop.
Save rumpelsepp/7f9aeb08f3e9b6ae261e027119034840 to your computer and use it in GitHub Desktop.
Create a debian qemu image
#!/bin/bash
# Creates a raw image which can be booted with:
#
# $ kvm -kernel PATHTOKERNEL/bzImage \
# -drive file=$HOME/sid-amd64.img,media=disk,format=raw,readonly=off \
# -append "root=/dev/sda console=ttyS0" \
# -fsdev local,id=fs1,path=$HOME,security_model=none \
# -device virtio-9p-pci,fsdev=fs1,mount_tag=host-home \
# -nographic
#
# Thanks to device virtio-9p-pci the home directory can be mounted in qemu with:
#
# # mkdir /mnt/host
# # mount -t p9 host-home /mnt/host
set -e
# Adjust this to your needs.
FS="ext3"
ARCH="amd64"
DEBIAN_DISTRO="sid"
MOUNT_POINT="/tmp/${DEBIAN_DISTRO}/"
IMG="/home/stefan/$DEBIAN_DISTRO-$ARCH.img"
IMGSIZE="1G"
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# Check if the disk image is already mounted,
# if that's the case, then unmount it.
mountpoint -q $MOUNT_POINT && umount -f $MOUNT_POINT
echo "Creating disk image $IMG"
fallocate -l $IMGSIZE $IMG
du -h $IMG
echo "Creating filesystem $FS on $IMG"
mkfs.$FS -F $IMG
file $IMG
echo "Creating mountpoint $MOUNT_POINT"
mkdir -p $MOUNT_POINT
echo "Mounting $IMG, mountpoint: $MOUNT_POINT"
mount -o loop $IMG $MOUNT_POINT
echo "Creating the root file system"
debootstrap --arch=$ARCH $DEBIAN_DISTRO $MOUNT_POINT "https://deb.debian.org/debian"
echo "Applying some configuration"
sed -i '/^root/ { s/:x:/::/ }' $MOUNT_POINT/etc/passwd
echo "/dev/sda / $FS errors=remount-ro 0 1" > $MOUNT_POINT/etc/fstab
echo "auto enp0s3" >> $MOUNT_POINT/etc/network/interfaces
echo "iface enp0s3 inet dhcp" >> $MOUNT_POINT/etc/network/interfaces
echo "Cleaning up"
umount $MOUNT_POINT
rm -rf $MOUNT_POINT
echo "image '$IMG' created"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment