Installing Debian in a pendrive formatted with VFAT in a loopback device

Making your own 'Debian live' system on a pendrive formatted with VFAT with debootstrap

Earlier I had some instructions on how to install Debian Squeeze in a pendrive by first formatting it into ext2. This turns not to be a good solution since there are issues with NAND sectors alingnment and such that do not make it a good idea to use ext2 directly in the pendrive.

Debootstrap and a regular linux installation relies on writing a lot of small files. For this, better performance is achieved by using a loopback device formatted with ext2 on top of the vfat partition.

This means switching from grub to SYSLINUX for boot :)

1. Creating the loopback fs

To create a 4 GB image, the maximum filesize that can be stored in a FAT32 system:

 .
 cd /mnt/mount_point_for_the_vfat_pendrive
 dd if=/dev/zero of=wheezy.img bs=1M count=4M
 mkfs.ext2 -m 0 wheezy.img
 mount -o loop wheezy.img /mnt/pendrive

2. Debootstrap over the device

I use apt-cacher-ng, change the url of the debian mirror as needed

 debootstrap wheezy /mnt/pendrive/ http://127.0.0.1:3142/ftp.rediris.es/debian/

3. Setting up root password and create initrd for loop device

 mount -o bind /dev/ /mnt/pendrive/dev/
 mount -t proc proc /mnt/pendrive/proc
 mount -t sysfs sys /mnt/pendrive/sys/
 chroot /mnt/pendrive /bin/bash
 passwd root
 apt-get update
 apt-get install locales
 dpkg-reconfigure locales
 # Select a locale of your convenience
 apt-get install linux-image-amd64
 # Get loop script from http://pigeond.net/blog/2006/06/11/pikkoro-episode-2-loopback-rootfs-with-debian/
 cd /usr/share/initramfs-tools/scripts
 wget http://pigeond.net/misc/loop
 cd /etc/initramfs-tools
 # --- cut into vi modules ---
 usbcore
 ehci-hcd
 ohci-hcd
 uhci-hcd
 usb-storage
 sd_mod
 loop
 ext2
 ext3
 vfat
 jbd
 nls_iso8859_1
 nls_cp437
 nls_utf8
 # ---------------------------
 # if you regenerated the vfat in the drive with mkfs.vfat, probably
 # you need nls_utf8 (it was not in the above tutorial). It does no harm
 # tough
 dpkg-reconfigure initramfs-tools
 # A new initramfs is in /boot/initrd.img-3.2.0-4-amd64
 # /preroot is used by the loop script to mount the vfat partition,
 # create it
 mkdir /preroot
 # exit from the chroot
 exit
 # Out of the chroot now
 umount /mnt/pendrive/sys; umount /mnt/pendrive/proc; umount /mnt/pendrive/dev
 cp /mnt/pendrive/boot/vmlinuz-3.2.0-4-amd64 ./vmlinuz
 cp /mnt/pendrive/boot/initrd.img-3.2.0-4-amd64 ./initrd.img
 # Renaming to comply with 8.3 old MSDOS fs limitations
 umount /mnt/pendrive
 cd ..
 umount /mnt/<mount_point_for_the_vfat_pendrive>

4. Installing syslinux

 apt-get install syslinux
 dd conv=notrunc bs=440 count=1 if=/usr/lib/syslinux/mbr.bin of=/dev/sdb
 parted /dev/sdb set 1 boot on
 syslinux --install /dev/sdb1
 
 mount /mnt/<mount_point_for_the_vfat_pendrive>
 cd /mnt/<mount_point_for_the_vfat_pendrive>
 --- copy contents of syslinux.cfg ---
 DEFAULT  Wheezy
 PROMPT   1
                  
 LABEL    wheezy
 KERNEL   vmlinuz
 APPEND   initrd=initrd.img root=/dev/sdb1 boot=loop rootfstype=vfat loopfile=/wheezy.img loopfstype=ext2
 TIMEOUT  100
 -------------------------------------

5. Testing with kvm

We have the root filesystem in /dev/sdb1, and there is no easy way to tell qemu-kvm that we want to boot from hdb (second hard drive, so we trick it a bit (as root):

 kvm -m 512m -hda /dev/sdb -hdb /dev/sdb

6. Reboot, select your pendrive to boot and enjoy :>