OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Manipulating Partitions With fdisk

The fdisk program allows us to interact directly with disk-like devices (such as hard disk drives and flash drives) at a very low level. With this tool we can edit, delete, and create partitions on the device. To work with our flash drive, we must first unmount it (if needed) and then invoke the fdisk program as follows:


[me@linuxbox ~]$ sudo umount /dev/sdb1

[me@linuxbox ~]$ sudo fdisk /dev/sdb

[me@linuxbox ~]$ sudo umount /dev/sdb1

[me@linuxbox ~]$ sudo fdisk /dev/sdb


Notice that we must specify the device in terms of the entire device, not by partition num- ber. After the program starts up, we will see the following prompt:



Command (m for help):

Command (m for help):


Entering an “m” will display the program menu:



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)


Command (m for help):

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)


Command (m for help):


The first thing we want to do is examine the existing partition layout. We do this by en- tering “p” to print the partition table for the device:



Command (m for help): p


Disk /dev/sdb: 16 MB, 16006656 bytes

1 heads, 31 sectors/track, 1008 cylinders Units = cylinders of 31 * 512 = 15872 bytes

Command (m for help): p


Disk /dev/sdb: 16 MB, 16006656 bytes

1 heads, 31 sectors/track, 1008 cylinders Units = cylinders of 31 * 512 = 15872 bytes


Device Boot

Start

End

Blocks Id System

/dev/sdb1

2

1008

15608+ b W95 FAT32


In this example, we see a 16 MB device with a single partition (1) that uses 1006 of the available 1008 cylinders on the device. The partition is identified as a Windows 95 FAT32 partition. Some programs will use this identifier to limit the kinds of operation that can be done to the disk, but most of the time it is not critical to change it. However, in the interest of demonstration, we will change it to indicate a Linux partition. To do this, we must first find out what ID is used to identify a Linux partition. In the listing above, we see that the ID “b” is used to specify the existing partition. To see a list of the avail - able partition types, we refer back to the program menu. There we can see the following choice:



l list known partition types

l list known partition types


If we enter “l” at the prompt, a large list of possible types is displayed. Among them we see “b” for our existing partition type and “83” for Linux.

Going back to the menu, we see this choice to change a partition ID:



t change a partition's system id

t change a partition's system id


We enter “t” at the prompt enter the new ID:



Command (m for help): t

Selected partition 1

Hex code (type L to list codes): 83

Changed system type of partition 1 to 83 (Linux)

Command (m for help): t

Selected partition 1

Hex code (type L to list codes): 83

Changed system type of partition 1 to 83 (Linux)


This completes all the changes that we need to make. Up to this point, the device has been untouched (all the changes have been stored in memory, not on the physical device), so we will write the modified partition table to the device and exit. To do this, we enter “w” at the prompt:



Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.

Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.



WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information.

Syncing disks. [me@linuxbox ~]$


WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information.

Syncing disks. [me@linuxbox ~]$


If we had decided to leave the device unaltered, we could have entered “q” at the prompt, which would have exited the program without writing the changes. We can safely ignore the ominous sounding warning message.


Top OS Cloud Computing at OnWorks: