loopback filesystems, grub, and debootstrap

Update May 1, 2006: I now organize this type of info here.

I’ve been playing with VMMs like Xen and VMWare and OSes like Linux. One thing that can consume a lot of time is repartitioning disks all the time to get the desired number and variety of guest operating systems. I don’t usually do things in these guest OSes which are bound by disk performance, so it makes sense for me to use loopback-mounted disk images. I don’t just want a partition-as-image though, I want a disk-as-image, complete with an MBR and potentially containing multiple partitions.

I’ve used others’ instructions on loopback filesystems, creating partitions on disk images, installing grub, and debootstrap to figure this stuff out, but I’m putting a reminder-to-self here.

This command (see partitions on disk images)will create a 2GB file called disk-image, with 4096 cylinders (it also has 16 heads and 63 sectors — not sure where those numbers come from):

dd if=/dev/zero of=/path/to/c.img bs=516096c count=4096

Use fdisk to create a partition (I only created one, things get more complicated w/ additional ones):

losetup /dev/loop0 disk.img
fdisk -u -C4096 -S63 -H16 /dev/loop0

Now we must unmount the whole image and then remount just our partition so we can format it (#blocks is the number of blocks listed in fdisk when it prints partition information:

losetup -d /dev/loop0
losetup -o32256 /dev/loop0 disk.img
mke2fs -b1024 /dev/loop0 #blocks
mount -t ext3 /dev/loop0 /mnt
mkdir /mnt/boot
mkdir /mnt/boot/grub
cp /boot/grub/* /mnt/boot/grub
umount /mnt

Now we want to unmount the loopback device and install the grub bootloader on the MBR of our virtual disk (grub is supposed to be able to handle giving it /dev/loop0 when the disk.img is mounted via the loopback system, but it gives me “error 22: no such partition”).

losetup -d /dev/loop0
grub --no-floppy
grub> device (hd0) disk.img
grub> geometry (hd0) 4096 16 63
grub> root (hd0,0)
grub> setup (hd0)

Mount the formatted partition and install a base Debian system on it:

mount -text3 -oloop=/dev/loop2,offset=32256 disk.img /mnt
/usr/sbin/debootstrap --arch i386 sarge /mnt http://http.us.debian.org/debian

chroot into the new system to complete the install:

LANG= chroot /mnt /bin/bash

vi /etc/fstab
mount -a
mount -t proc proc /proc
dpkg-reconfigure console-data
vi /etc/network/interfaces
vi /etc/resolv.conf
/usr/sbin/base-config new


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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