Using fdisk to partition a USB thumb drive

Before playing with drives and partitions, you must understand a little of how Linux deals with hard drives. Each hard drive, as well as each other device attached to your computer (mice, keyboard, serial ports) are considered “devices” and have a path within the ‘/dev’ directory assigned by udev. Typical assignments are as follows:

First IDE drive /dev/hda
Second IDE drive /dev/hdb
First Serial ATA drive /dev/sda
USB drive /dev/sdb
SD card (in side slot) /dev/mmc…

Partitions within each drive are numbered beginning at 1 and appended to the device name. For example, the first partition on the first Serial ATA drive is /dev/sda1

WARNING: Be very careful when working with device names to make sure that you have the correct device before starting critical functions such as formatting or repartitioning. Failure to name the correct device could lead to loss of data!

The easiest way to partition a drive in Linux is by using fdisk, from the command line. fdisk must be run as root (either by switching to the root user or by typing “sudo” in front of the command). It must also be passed the device that you would like to use. If you are unsure about which device you want to work with, type:

 ls -l /dev/disk/by-uuid

a sample line from the above command is:

lrwxrwxrwx 1 root root 10 2009-06-20 10:38 0159-6699 -> ../../sda1

Explanation:
lrwxrwxrwx – This displays the read write privileges on the drive. Each file on the computer has privileges for the owner (user), group, and for eveyone. For each class, you can read, write, and execute privileges either on or off. For example, if the owner of a file could read, write, and execute, but no other privileges granted, this line would read lrwx——. The ‘l’ indicates that this is a directory.

1 – root root – Indicates the users, in this case, this file belongs to the user: root and is part of the root group.

10 – indicates the file size in bytes

2009-06-20 10:38 Indicates the creation diate (in this case it was created today as the /dev/ directory is populated on startup)

-> ../../sda1 – Gives the device name (the .. indicates the parent directory and since we are running this from /dev/disk/by-uuid, full filename is ‘/dev/sda1’.

or

fdisk -l

Which shows information on the partitions on drives which have been listed in the /proc/partition directory.

If the drive that you are looking for is already mounted on the system, you can look at:

df -h

Which shows how much space is available, but also shows the mount points.

The easiest way to identify the correct drive is by size (assuming that you do not have two drives the same size). A good rule of thumb would be that your internal harddrive is /dev/sda, although this can vary with system setup.

Partition the drive as a Linux drive using fdisk (ext2)

fdisk has the following things that you can do (accessed by typing ‘m’)

Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

From the command line, type:

sudo fdisk /dev/sdb (assuming that you want to partition /dev/sdb – see discussion above)

Display the partition table by typing ‘p’

If there are partitions already on the disk, then you will need to remove them by using ‘d’ and selecting the partition number that you would like to delete.

Now, add the new partition by selecting ‘n’ selecting the default for starting and ending position.

It should default as a linux ext2 partition. You can check this by printing the partition table again (‘p’). If you do need to change the partition type, use the ‘t’ option.

Mark the partition as bootable

You can mark the partition as bootable by typing ‘a’ and selecting the correct partition.

Write the partition table

You can write the partition table by typing ‘w’
This will also exit fdisk and likely cause Linux to mount the newly partitioned drive.

Unmount the drive

sudo umount /dev/sdb1 

It is possible that other partitions would be mounted. You can see what is mounted using ‘df’

Make the file system using

sudo mke2fs -v /dev/sdb1 (substitute in the appropriate device)

Mount the new drive

sudo mount /dev/sdb1

Change directory to the new drive

Use ‘df’ to determine where /dev/sdb1 has been mounted. If, for example, /dev/sdb1 has been mounted as /media/disk you can change the directory by typing:

cd /media/disk

Leave a Reply

Your email address will not be published. Required fields are marked *