Making a bootable USB stick

Introduction

This procedure is to guide me through making a basic Debian+XFCE bootable USB stick.

It is heavily based on David Bremner’s procedure at http://www.cs.unb.ca/~bremner/blog/posts/bootable-usb/, but tweaked for less manual work and to add the software I want.

Procedure

  1. For next time:
    • prepare the image offline and then dd it over the USB stick; the stick is too slow to do installs onto
    • configure a caching-only name server so we get access to root domain servers, rather than having to reconfigure /etc/resolv.conf for each location; don’t use bind for this
  2. Attach the USB stick, identify the device and wipe it by running:
    dmesg
    

    and identify the stick, e.g. /dev/sdc.

  3. Set some environment variables needed by the rest of this procedure:
    DEV=/dev/sdc
    MNT=/mnt
  4. Blank the USB stick:
    dd if=/dev/zero of=$DEV bs=1M

    and wait dd until complains there is no space left on the device.

  5. Partition the stick, create the filesystem and mount it by running:
    parted $DEV
    mklabel msdos
    mkpart primary ext2 1 -1
    set 1 boot on
    quit
    mkfs -t ext3 ${DEV}1
    mount ${DEV}1 $MNT
  6. Install the base system by running:
    debootstrap --variant=minbase jessie $MNT http://httpredir.debian.org/debian/
    grub-install --boot-directory $MNT/boot ${DEV}1
  7. Fix up some small things:
    cp /etc/fstab $MNT/etc/fstab
    blkid -p ${DEV}1 | cut -f2 -d' ' >> $MNT/etc/fstab
    vi /mnt/etc/fstab
    #  I like to set a blank password on a USB stick
    perl -pi -e 's/root:[^:]*:/root::/' $MNT/etc/shadow
    
  8. Chroot into the stick by running:
    mount -o bind /dev $MNT/dev
    mount -o bind /sys $MNT/sys
    mount -o bind /proc $MNT/proc
    chroot $MNT
  9. Install software:
    apt-get install linux-base linux-image-amd64 grub-pc xfce4 net-tools \
        vim-gtk man-db iceweasel console-data telnet iputils-ping \
        xfwm4-themes nmap tcpdump xfce4-goodies screen ethtool nmap \
        lvm2 lshw bind9-host bind9
  10. Tailor root’s environment a bit:
    touch $MNT/root/.hushlogin
    echo 'PS1="\\h\\$ "' >> $MNT/root/.bashrc
  11. Prevent NIC renaming:
    rm $MNT/etc/udev/rules.d/70-persistent-net.rules
    ln -s /dev/null $MNT/etc/udev/rules.d/70-persistent-net.rules
  12. Clean up by running:
    exit
    umount $MNT/proc
    umount $MNT/sys
    #  I found a dbus-daemon process was holding /mnt/dev open
    fuser -c $MNT/dev
    kill <pids-holding-$MNT/dev/-open>
    umount $MNT/dev
    umount $MNT
    sync

    and remove the stick.

See also