netarky.com

Install Linux from a USB drive using dd

Updated 28 Feb 2014

Don't use UNetbootin! * dd * Using dd on Linux * Using dd on Mac OSX * Restore the USB drive * More dd commands that might come in handy

 

Don't use UNetbootin!

UNetbootin can be used on any Linux distribution or Windows to copy an ISO image to a USB device. However, Unetbootin overwrites syslinux.cfg, creating a USB device that does not boot properly. For this reason, Unetbootin is not the recommended way to create a bootable image.

 

dd

dd is a Unix command whose primary purpose is to convert and copy a file. The GNU/Linux implementation of dd seems unfriendly, but it is a very powerful command able to do many different things.

On Unix-based systems, everything is a file, as the saying goes. Device drivers for hardware (such as hard disks) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files; dd can read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive. dd can duplicate data across files, devices, partitions and volumes and also perform conversions on the data as it is copied.

Using dd is a fairly simple way to create a bootable USB drive. But be careful when using it. A small mistake such as reversing the input file and output file parameters or specifying the wrong path could result in the loss of some or all data on a disk.

Before proceeding, make sure the USB drive you will be using has no data on it that you need. Any data will be irrevocably destroyed.

Once the USB drive has completed its purpose it can be restored to full capacity and repartitioned.

 

Using dd on Linux

Check the name of your USB drive using lsblk. If necessary, figure it out using dmesg | tail after mounting it. Then unmount it using umount. Run dd as follows, but replace sdx with the name of the device e.g. sdb. Make sure you get the name right!

dd bs=4M if=/path/to/linux.iso of=/dev/sdx && sync

Also, don't use sdx1, rather sdx. (An integer refers to an existing partition on the drive and not the drive itself.)

It may take a few minutes to complete depending on the size of the iso. And that's it!

 

Using dd on Mac OSX

When you insert your USB drive, OSX will automount it. Find its name using the diskutil command:

diskutil list

Be careful identifying the USB drive. You don't want to end up dd-ing the wrong device. One way to be sure is to mount the drive using mount and then dmesg | tail to check kernel info regarding what just happened. When you're ready, unmount the drive. Here we'll assume the USB drive is called disk2:

diskutil unmountDisk /dev/disk2

Here's the dd command:

dd if=/path/to/linux.iso of=/dev/disk2 bs=8192

Wait for the operation to complete and you're good to go. Eject the drive just to be sure before you physically remove it:

diskutil eject /dev/disk2

 

Restore the USB drive

After you install Linux and you are done with the USB drive, you should zero out its first 512 bytes (meaning the boot code from the MBR and, in this case, the non-standard partition table) if you want to restore it to full capacity:

dd count=1 bs=512 if=/dev/zero of=/dev/sdx && sync

(Where sdx is a substitute for the device's real name.) Then use cfdisk or parted to create a new partition table and filesystem.

 

More dd commands that might come in handy

Backup the boot code and partition table record:

dd if=/dev/sdX of=/tmp/sda-mbr.bin bs=512 count=1

Restore just the partition table:

dd if= sda-mbr.bin of=/dev/sdX bs=1 count=64 skip=446 seek=446

Zero the boot code, but not the partition table:

dd if=/dev/zero of=/dev/hda bs=446 count=1

 

Sources:
dd
Putting installation media on a USB key