[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to create qemu-bootable image using debootstrap?

Matej Kosik wrote:

Previously, I had the same problem but I did not know how to deal with
the partions (btw. I still have too figure out how did you got those
numbers to be able to create smaller disks). The `parted' stuff you
posted before was a missing piece of information. Here is a complete
sequence of commands that
- creates a harddisk image
- install multiboot specification compliant kernel to it
- install grub to the harddisk image

Aha, excellent -- with your grub commands I was able to complete my
script. (Though I'm still not sure where the stage1/2/e2fs_stage1 should
come from -- currently it just copies off the host, which isn't that
clean but works for now.) I've posted it on my blog and will keep
updating it here:


As for the magic numbers in the script to make it work for 1GB, here's
how I came up with them for the "dd" command:

bs = blocksize = 1024
count = 1GB/bs = (1024*1024*1024)/1024 = 1048576

For the "parted" command, I got the numbers by installing with the
netinst ISO onto a 1GB raw file, and then doing "parted test.raw print".
  I don't know how the netinst installer came up with those numbers,
but I just copied them.  Regardless, it seems like you can pretty much
choose whatever you want; I'm sure there's a whole science behind it.
(Where the "1069" came from I have no idea, as there are only 1024 MB on

For the "loop,offset=16384" that took some guesswork: "parted print"
spit out 16.4K as the start of the first partition, and I guessed that
it was just rounding off 16384 and it worked.

Regardless, here it is for those of you following along.  Thanks for all
your help!



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 ""

Reply to: