Create a Linux bootable USB pen drive[27 Sept 2011]

What you need

  • the .iso image of your favorite GNU/Linux distribution
  • a Linux shell
  • a USB stick, of course!! whose size should be at least equal to the .iso file size

My situation

  • grml linux .iso file
  • debian testing (wheezy)
  • a 16GB Sony USB stick

Do it!

Firstly plug the USB stick into your linux box. Now type:

# dmesg

to find out the usb device identifier. Mine is:

[ 2210.706676] sd 7:0:0:0: [sdb] Attached SCSI removable disk

so my USB stick will be /dev/sdb.
Step two concerns of copying the iso file in the usb stick. After this operation every data contained in the USB pen drive before will be lost. Hence be sure to backup your data before starting this:

# dd if=/path/to/iso/file.iso of=/dev/sdb bs=1M

Note: output file ("of" parameter) must be the device name (something like /dev/sdb) and not a partition name (something like /dev/sdb1)
Ok, you are done! It was pretty easy but... you will see that your USB pen drive size is exactly as large as the iso file size, no free space has been preserved!

How to recover the lost space

This is just my solution; others are available (you can invent your own).
Use fdisk utility to create a new partiton:

# fdisk /dev/sdb

It will output:

Command (m for help):

Type p to print the partition table. It will appear something like:

Disk /dev/sdb: 16.0 GB, 16030629888 bytes
16 heads, 32 sectors/track, 61152 cylinders, total 31309824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd04ce516

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb4   *           0     1406975      703488   96  Unknown

Command (m for help): 

This /dev/sdb4 is your bootable partition (in fact the boot flag is setted).
Create a new partition with n, and type "return" to each next step to accept the defaults (fdisk will create a single partition with all the remaining free space). Using the p command you will get an output similar to:

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1         1406976    31309823    14951424   83  Linux
/dev/sdb4   *           0     1406975      703488   96  Unknown

Now, if you want, you can shift the System type of your new partition to something like FAT32, to be able to read the USB pen drive data on every computer. In this case, use command t followed by the partition number (1 in our example) and by the new System code (b for a FAT32 partition; you can get a list of all the System codes using the command l).
Save your changes and exit with command w.

Now the last step is to format the new partition:

# mkfs.vfat /deb/sdb1

Note: this time you must specify the partition, not the whole device''

Ok! That's it!