Linux/Unix – Changing Disk Partitions with fdisk

The fdisk command is a useful Linux tool for listing and changing disk partitions.

Keep in mind that modifying or deleting partitions can cause valuable data to be removed, so be sure of your changes before writing them to disk. To use the fdisk command to list information about the partitions on your hard disk, type the following command as root user:

$ sudo fdisk -l

List disk partitions for every disk

Disk /dev/sda: 82.3 GB, 82348277760 bytes

255 heads, 63 sectors/track, 10011 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

Device      Boot  Start   End   Blocks    Id  System

/dev/sda1     *     1     13    104391    83   Linux

/dev/sda2          14   9881    79264710  83   Linux

/dev/sda3        9882  10011     1044225  82   Linux swap

 

This example is for an 80GB hard disk that is divided into three partitions. The first (/dev/sda1) is a small /boot partition that is configured as a Linux ext3 file system (Id 83). Note the asterisk (*), indicating that the first partition is bootable. The next partition is assigned to the root file system and is also ext3. The final partition is Linux swap.

NOTE Since version 2.6.20 of the Linux kernel, both IDE and SCSI disks use device names /dev/sd?, where the ? is replaced by a letter (a, b, or c, and so on). In older versions of Ubuntu, only SCSI disks and USB flash drives used the /dev/sd? names. IDE hard drives used /dev/hd? instead.

If multiple disks are present, fdisk -l will list them all unless you indicate the specific disk you want:

 

$ sudo fdisk -l /dev/sdb

List disk partitions for a specific disk

 

To work with a specific disk with the fdisk command, simply indicate the disk you want with no other options:

$ sudo fdisk /dev/sda 

Start interactive fdisk session with disk 1

 

Command (m for help): m

Type m to list help text as shown

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):

 

With the prompt displayed, you can use any of the commands shown to work with your hard disk. In particular, you can use p (to print the same listing as fdisk -l), n (to create a new partition), d (to delete an existing partition), l (to list known file system types), or t (to change the file system type for a partition). The following examples show some of those fdisk commands in action:

Command (m for help): d                 — Ask to delete a partition

Partition number (1-4): 4               — Type partition number to delete

Command (m for help): n                 — Create a new disk partition

First cylinder (1-4983, default 1): 1   — Select start (or Enter)

Last cylinder … (default 4983): 4983  – Select end (or Enter)

Command (m for help): a                 — Make a partition bootable

Partition number (1-3): 1               — Type bootable partition number

Command (m for help): t                 — Select a file system type

Partition number (1-3): 3               — Select partition to change

Hex code (type L to list codes): 82     — Assign partition as swap

Unless you tell it otherwise, fdisk assumes the new partition is a Linux ext3 partition (83). You could have typed L to see the same listing of file system types and hex codes produced from the l command. As noted above, 82 can assign the partition as swap. Other Linux partitions that may interest you include Linux extended (85), Linux LVM (8e), Linux software raid (fd), and EFI/GTP (ee). 

For Windows partitions, you can assign a partition as HPFS/NTFS (7), Windows 95 FAT32 (b), FAT 16 (6), or Windows 95 FAT32 LBA (c). Other Unix-type file systems include Minix (be or bf), BSD/OS (e4), FreeBSD (ee), OpenBSD (ef), NeXTSTEP (f0), Darwin UFS (f1), and NetBSD (f4). Any of these file system types might be useful if you have old backup media from those file systems you want to restore. So far, you have not made any permanent changes to your partition table. If you are now very sure that your new settings are correct, type w to write those changes to the partition table. To abandon your changes (or quit after writing your changes), type q to quit your fdisk session.

About these ads

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: