HomeOverviewSign upFAQVarious

 Version Française




Linux : making a bootable USB drive

Making a bootable USB drive with a mini version of Linux and using it to repair our system or restore our MBR (Master Boot Record) is an easy task we tend to forget when everything is working well. But as everything may not always be working well, it is better to be safe than sorry.

Setting up the key

Let's take a small 1Gb USB stick fully formatted. We are going to use only 100Mb and turn them into a Swiss army knife and we'll see in the next article how to turn the 900Mb left into a Fort Knox-like area.
For this example, we'll assume the key is /dev/sdX and the partition to create is /dev/sdX1. Just replace the X letter by the one matching your system (/dev/sdb...). You can find out by typing # dmesg | tail right after plugin your USB stick.

Insert the key and, if needed, unmount the partition :

   # umount /dev/sdX1

We are going to create a 100Mb primary partition with fdisk. Run it and simply type the following commands (shown in red) :

   # fdisk /dev/sdX

   - Type n to create a new partition
   - Type p to make it a primary partition
   - Type 1 to make it the first partition
   - Type 1 so that it statrs a the 1st cylinder
   - Type +100M for its size (100Mb)
   - Type a to activate it
   - Type 1 for the partition number
   - Type p to view your changes
   - Type w to save your changes

Unmount the partition if needed :

   # umount /dev/sdX1

FAT16 formatting :

   # mkfs.vfat -F 16 /dev/sdX1

We are going to install Damn Small Linux, a tiny 50Mb only version of Linux that is perfect for our needs : it is small, fast, efficient and has all the tools required to save or restore our system.

Mount the partition :

   # mkdir /tmp/usbtmp
   # mount /dev/sdX1 /tmp/usbtmp

Go to the directory where you save the Damn Small Linux ZIP file to unpack it right away to our USB brand new parition :

   # unzip -o dsl-3.4.3-embedded.zip -d /tmp/usbtmp
Now we need to make the partition bootable with syslinux. If you don't have it, install it with mtools (needed for mcopy) :

   # apt-get install syslinux
   # apt-get install mtools

   # syslinux -sf /dev/sdX1

Everything should be working now and, before the worst happens, it may be a good idea to backup your MBR and to save it to the USB stick in a directory named /backup.
In that example, the hard disk is shown as /dev/Xda but you'll have once again to replace the X by what matches your system (usually /dev/sda or /dev/hda - if you don't know, just type `fdisk -l`).

Copy MBR to a file named "mbr.bak" with the dd command :

   # mkdir /tmp/usbtmp/backup
   # cd /tmp/usbtmp/backup

   # dd if=/dev/Xda of=mbr.bak bs=512 count=1

Don't forget that if you boot on your USB key and want to restore your MBR, you will have to cd to the /backup directory and type :

   # dd if=mbr.bak of=/dev/Xda bs=512 count=1

If you can't remember such a command line, save it as well to your USB key :

   # echo "-restore MBR: dd if=mbr.bak of=/dev/Xda bs=512 count=1">/tmp/usbtmp/backup/info.txt
   # echo "-backup MBR: dd if=/dev/Xda of=mbr.bak bs=512 count=1">>/tmp/usbtmp/backup/info.txt

You can now restart your computer and boot on the key.

To save some important and confidential documents, check the next article to encrypt the 900Mb left without any third party software.