Install a Debian distribution in a pendrive using debootstrap

Last updated 2011-12-28

Motivation

Once upon a time I needed to make a backup of the hardrive of certain laptop that was encrypted using cryptsetup into an external drive encrypted using loop-aes. I tried many live distributions, to no avail, because all of them do not have loop-aes in their stock kernels:

All of this live distributions use squashfs read-only images to... well... squash as many utilities in a reasonable-size image for a regular use to copy into their pendrives.

I was in the way of uncompressing the squashfs partition or even make my own 'debian live' squashfs image including the modules (wich i guess the debian-live developers sanctioned way of doing this kind of things), when i realized that it could be simpler than that.

The partition in my pendrive for live images is 2 GB... more than enough to hold a whole distribution on it, on a regular filesystem, and be able to replace anything, including kernel modules, on it.

Requirements

We will need any linux machine (does not need to have Debian on it) with debootstrap, mkfs.ext2, chroot, and a stock brain. In this example, the pendrive partition is in /dev/sdc1. If you have more partitions in the pendrive, they will not be affected..

1. Make filesystem

# mkfs.ext2 /dev/sdc1
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
[... and al the regular mkfs output ...]

2. Debootstrap debian into it

# mkdir /mnt/pendrive
# mount /dev/sdc1 /mnt/pendrive
# debootstrap --arch i386 squeeze /mnt/pendrive http://ftp.fi.debian.org/debian
I: Retrieving Release
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
I: Found additional required dependencies: insserv libbz2-1.0 libdb4.8 libslang2
I: Found additional base dependencies: libnfnetlink0 libsqlite3-0
I: Checking component main on http://ftp.fi.debian.org/debian...
I: Retrieving libacl1
I: Validating libacl1
[... more retrieving and validating, unpacking, configuring  ...]
I: Configuring tasksel...
I: Base system installed successfully.

Chroot into there and install kernel and grub2

 
# chroot /mnt/pendrive
root@debian:/# apt-get install linux-image-686
root@debian:/# apt-get install grub2
(answer to Continue without installing GRUB? with Yes)

Set root password (quite important)

root@debian:/# passwd root
(put any password you want. If you miss this step, you
 are not logging into your new system ;) 
root@debian:/# exit

Grub-install over the device

Now we are going to mount dev, proc and sys into the chrooted env so we can run grub-install over the device

# mount -o bind /dev/ /mnt/pendrive/dev/
# mount -o bind /proc/ /mnt/pendrive/proc/
# mount -o bind /sys/ /mnt/pendrive/sys/

# chroot /mnt/pendrive
root@debian:/# grub-install /dev/sdc
Installation finished. No error reported.

root@debian:/# exit

# umount /mnt/pendrive/dev/
# umount /mnt/pendrive/proc/
# umount /mnt/pendrive/sys/

# umount /mnt/pendrive

4. Boot

The device has a kernel, grub installed (but not configured) and an entire Debian Squeeze on it, so ... let's boot with it:

GNU GRUB version 1.98 [...]

grub> root (hd0,1)
(hd0, 1): Filesystem is ext2.
grub> linux (hd0,1)/boot/vmlinuz-2.6.32-5-686 root=/dev/sdb1
grub> initrd (hd0,1)/boot/initrd.img-2.6.32-5-686
grub> boot

And we are there. Any change you make to this system (apt-get install anything, or in my case install loop-aes kernel modules) will remain there.

References

Valid HTML 2.0