Quinthar

Building a 1GB bootable qemu image using debootstrap

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 http://ftp.us.debian.org/debian

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/id_rsa.pub $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 ""

2 comments:

Brandon said...

When I run this, I'm able to build a raw VM image and run it, but I get lots of input/output errors. The VM is unusable, since I can't get it resolve DNS names:
ls: /etc/resolv.conf: Input/output error

During boot, I see similar errors:
chown: failed to get attributes of /etc/resolv/conf input/output error
chmod: failed to get attributes of /etc/resolv.conf: input/output error
chown/chmod: failed to get attributes of /var/log/dmesg: input/output error
chown: failed to get attributes of /etc/resolv.conf

Do you see these?

Anonymous said...

See also https://github.com/tuxofil/linsygen (based on this script, heavily modified to support Debian and openSUSE)

- Jan 2014 (1) - Mar 2012 (1) - Nov 2011 (1) - Oct 2011 (1) - Apr 2011 (1) - Mar 2011 (3) - Feb 2011 (2) - Jan 2011 (9) - Nov 2010 (1) - May 2010 (1) - Mar 2010 (1) - Feb 2010 (1) - Jan 2010 (1) - Dec 2009 (1) - Nov 2009 (1) - Oct 2009 (1) - Sep 2009 (1) - Aug 2009 (2) - Jul 2009 (1) - Jun 2009 (4) - May 2009 (3) - Apr 2009 (3) - Mar 2009 (10) - Feb 2009 (5) - Jan 2009 (3) - Dec 2008 (5) - Nov 2008 (5) - Oct 2008 (5) - Sep 2008 (4) - Aug 2008 (5) - Jul 2008 (11) - Jun 2008 (8) - Feb 2008 (1) - Aug 2007 (1) -