← Index

Install a Debian distribution in a pendrive using debootstrap

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 (which 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

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

Chroot into there and install kernel and grub2

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

Set root password (quite important)

passwd

Configure the network

apt-get install net-tools iputils-ping wireless-tools wpasupplicant

Write /etc/fstab

/dev/sda1       /       ext2    defaults,errors=remount-ro       0       1

Write /etc/network/interfaces

auto lo
iface lo inet loopback
allow-hotplug eth0
iface eth0 inet dhcp

Write /etc/hostname

pendrive

Write /etc/hosts

127.0.0.1       pendrive

3. Grub-install over the device

grub-install /dev/sdc

Write /boot/grub/grub.cfg

menuentry 'Debian on pendrive' {
        insmod ext2
        linux /boot/vmlinuz-3.2.0-4-686-pae root=/dev/sda1 ro quiet
        initrd /boot/initrd.img-3.2.0-4-686-pae
}

4. Boot

Reboot and select the pendrive as boot device in the BIOS. If everything went ok, you should be in a working Debian system on a pendrive.


References

  1. http://www.debian.org
  2. http://ftp.fi.debian.org
  3. http://www.gnu.org/software/grub