Building a 1GB bootable qemu image using debootstrap

July 8, 2008

I’ve been a long qemu fan, and a new debootstrap groupie. The following script was built with the help of the debian-user list and combines these two affections — with some fancy footwork — to build a “from scratch” bootable qemu image. There are still some kinks to be worked out (specifically, I don’t know where to get a “real” stage1, stage2, and e2fs_stage1_5 file, so it copies it from the host system), and right now it is fixed at 1GB (though that could be easily corrected), but otherwise it seems to work surprisingly well. Behold:

echo “Creating 1GB file of zeros in $1.raw”
dd if=/dev/zero of=$1.raw bs=1024 count=1048576

echo “Formating $1.raw with ext2 filesystem”
/sbin/parted $1.raw mklabel msdos
/sbin/parted $1.raw mkpart primary ext2 0 954
/sbin/parted $1.raw mkpart extended 954 1069
/sbin/parted $1.raw mkpart logical linux-swap 954 1069
/sbin/parted $1.raw set 1 boot on
/sbin/parted $1.raw mkfs 1 ext2

echo “Mounting $1.raw on $1.mount”
mkdir -p $1.mount
sudo mount -o loop,offset=16384 -t ext2 $1.raw $1.mount

echo “Installing Etch into $1.mount”
sudo debootstrap –arch i386 etch $1.mount

echo “Setting up host networking in $1.mount for apt”
sudo cp /etc/resolv.conf $1.mount/etc
sudo cp /etc/hosts $1.mount/etc

echo “Installing kernel into $1.mount”
sudo chroot $1.mount apt-get update
sudo chroot $1.mount apt-get -y install gnupg
sudo chroot $1.mount apt-get update
echo “do_symlinks = yes
relative_links = yes
do_bootloader = yes
do_bootfloppy = no
do_initrd = yes
link_in_boot = no” > /tmp/kernel-img.conf
sudo mv /tmp/kernel-img.conf $1.mount/etc
sudo chroot $1.mount apt-get -y install linux-image-2.6-686

echo “Manually installing grub into $1.mount”
sudo mkdir -p $1.mount/boot/grub
sudo cp /boot/grub/stage1 $1.mount/boot/grub
sudo cp /boot/grub/stage2 $1.mount/boot/grub
sudo cp /boot/grub/e2fs_stage1_5 $1.mount/boot/grub
echo “default 0
timeout 0
title Linux
root (hd0,0)
kernel /boot/vmlinuz-2.6.18-6-686 root=/dev/hda1 ro
initrd /boot/initrd.img-2.6.18-6-686″ > /tmp/menu.lst
sudo mv /tmp/menu.lst $1.mount/boot/grub
sudo echo “device (hd0) $1.raw
root (hd0,0)
setup (hd0)
quit” > /tmp/grub.input
sudo grub –device-map=/dev/null < /tmp/grub.input

echo “Configuring qemu networking”
echo “auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp
” > /tmp/interfaces
sudo mv /tmp/interfaces $1.mount/etc/network

echo “Starting sshd and granting $USER a root key”
sudo chroot $1.mount apt-get -y install ssh
sudo chroot $1.mount /etc/init.d/ssh stop
sudo mkdir -p $1.mount/root/.ssh
sudo cp ~/.ssh/ $1.mount/root/.ssh/authorized_keys
sudo chmod -R 755 $1.mount/root/.ssh

echo “Dismounting $1.mount”
sudo umount $1.mount

echo “Done. To start, run:”
echo “”
echo ” sudo qemu -kernel-kqemu -redir tcp:2222::22 $1.raw”
echo “”
echo “To SSH in, run:”
echo “”
echo ” ssh -p 2222 root@localhost”
echo “”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: