Contact for queries :
 

Storage Related tasks – Linux Disk Partitioning

Working with disks is an important part of maintaining storage space on your system. Partitions determine how your storage space is carved out for use later by the system and its users.
When adding more storage or creating it for the first time, you should partition your disk space into a logical format for use later.
Many installers come with an “automated” partitioning option for those that don’t need a custom layout; however, most system administrators will probably want to lay out their own partitions.
Before you begin partitioning, you should look at the drives currently available for use as well as the current partitions on any of these drives. You can use two different utilities when partitioning disks:

  • fdisk Disk-partitioning utility
  • parted Another disk-partitioning utility

To view the partition information of the existing disks in your system, you can also check the contents of /proc/partitions

# cat /proc/partitions | grep hd
8 0 20971520 hda
8 1 512000 hda1
8 2 20458496 hda2
8 16 8388608 hdb
8 32 8388608 hdc
8 48 8388608 hdd
If you are using SCSI disks instead of IDE disks, you can just change the command to
# cat /proc/partitions | grep sd

How to Interpret the Partition names:

  • The first two letters represent whether the disk is a SCSI (sd) or IDE (hd) disk.
  • The third letter represents which disk it actually is.
  • If there is a number after the three letters, it is the number of the partition. 
  • There also are four physical drives: a, b, c,and d.
  • Because no partitions are created yet on the last three disks (hdb, hdc, and hdd), there are no numbers.
  • Meanwhile, you can see hda has three partitions.

To list the current partitions on all disks, use this command

# fdisk -l
Disk /dev/hda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00021654
Device Boot Start End Blocks Id System
/dev/hda1 * 1 64 512000 83 Linux
/dev/hda2 64 2611 20458496 8e Linux LVM
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Disk /dev/hdb doesn’t contain a valid partition table
Disk /dev/hdc: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/hdc doesn’t contain a valid partition table
Disk /dev/hdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00079351
Disk /dev/hdd doesn’t contain a valid partition table

To Disk Partition information using parted

# parted -l
Model: VBOX HARDDISK (ide)
Disk /dev/hda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 525MB 525MB primary ext4 boot
2 525MB 21.5GB 20.9GB primary lvm
Error: Unable to open /dev/hdb – unrecognised disk label.
Error: Unable to open /dev/hdc – unrecognised disk label.
Error: Unable to open /dev/hdd – unrecognised disk label.

From the output:

  • The first line specifies the disk and the total amount of storage available (that is, Disk /dev/hda: 21.5 GB).
  • Under the disk info, you can see the current partitions laid out for each disk.
  • Notice the last line, Disk /dev/hdd.
  • It gives information about the disk (it has 8GB of space), but there are currently no partitions on it.
  • Using the fdisk and parted utilities, you can partition the hdb, hdc, and hdd disks.

 

Procedure to Partition a disk using fdisk

First, let’s use fdisk to create a partition. The fdisk utility is driven by commands after you have chosen which disk you’d like to work with.
Step 1. Choose the disk:

# fdisk /dev/hdb

Step 2. View all the options available to you:

Command (m for help): 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)

You can see here that there are many different things you can do with the fdisk utility.
Step 3. Use the p command to print out the current partition table (this shouldn’t exist, but just verify):

Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System

As you can see, there is nothing here. However, you can see that you have 8,589MB of space to work with when dividing up the partitions on this disk. For ease of use, cut this disk directly in half. Create two partitions, each with half of the disk space available; verify they are correct; and then write the changes to the disk.
Step 4. Create a new partition:

Command (m for help): n
Command action
e extended
p primary partition (1-4)  p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1044, default 1044): +4294M

Because you are cutting the disk in half based on the megabytes available, you use the +sizeM syntax (as shown here). You could divide the disk based on kilobytes (+sizeK) or cylinders (+size) if you want, but
working in megabytes is much easier. With the first partition created, you can make the second.
Step 5. Create a second partition:

Command (m for help): n
Command action
e extended
p primary partition (1-4)   p
Partition number (1-4): 2
First cylinder (549-1044, default 549):
Using default value 549
Last cylinder or +size or +sizeM or +sizeK (8322-16644, default 16644):
Using default value 1044

For this partition, you don’t need to specify a partition size because, by default, it will grow the partition to the size remaining on the disk.
When you are asked what the size should be, pressing Enter just allocates the remaining disk space available.
Step 6. Verify your newly created partitions:

Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
/dev/hdb1 1 548 4401778+ 83 Linux
/dev/hdb2 549 1044 3984120 83 Linux
You can see here the two partitions just created. Based on their block
sizes, they are almost identical in size (there is some variation because of
reserved space, superblocks, and other factors that we discuss later).

Step 7. Write the changes to disk:

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Now that two new partitions have been created and written to disk, you should verify their existence. Before doing that, however, you want the kernel to reread the partition table to make sure that it recognizes all disks and partitions correctly.
To do this, you use the partprobe  command.

 partprobe [OPTIONS] [DEVICE]

Step 8. Call the partprobe command:

# partprobe /dev/hdb

This command has no output to it.
Step 9. Verify the partition creation one last time:

# fdisk -l
Disk /dev/hda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00021654
Device Boot Start End Blocks Id System
/dev/hda1 * 1 64 512000 83 Linux
/dev/hda2 64 2611 20458496 8e Linux LVM
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
/dev/hdb1 1 548 4401778+ 83 Linux
/dev/hdb2 549 1044 3984120 83 Linux
Disk /dev/hdc: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/hdc doesn’t contain a valid partition table
Disk /dev/hdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00079351
Disk /dev/hdd doesn’t contain a valid partition table

 

Creating Disk Partitions using Parted

Let’s check the instruction to create the same two partitions, but this time use the third disk, /dev/hdc.
Step 1. Start the parted utility the same way you used fdisk:

# parted /dev/hdc

CAUTION: The process of creating a label for a drive using the parted utility initializes whatever disk you run the command on. Because it is being initialized as a new disk, all data is erased from the disk. DO NOT use parted to label a disk that contains data! As always, ensure that you have a backup of your data before making changes to your system.
Step 3. Create your first partition in a similar manner to fdisk:

(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 0
End? 4294m

CAUTION: You may get an error message about the result partitions not being properly aligned for best performance. You can choose to ignore this message for now because you are just working in a lab and performance isn’t a big concern.
Step 4. Make your second partition:

(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? ext4
Start? 4294m
End? 8590m

Step 5. Before writing changes to disk, you should verify that they have been created the way you want them:

(parted) print
Model: VBOX HARDDISK (ide)
Disk /dev/hdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 4294MB 4294MB primary
2 4295MB 8590MB 4295MB primary

Step 6. Exit the program to save your changes:

(parted) quit

There are a few things you should notice here. First, you need to specify exactly where you want the start and end of the partition to be. If you don’t plan this out ahead of time, you will end up with incorrect partition sizes. You should also take note of the fact that you don’t have to write the changes to disk manually; this is done for you when you quit the parted program.
Step 7. Again, you need to force the kernel to reread the partition table:

# partprobe

Step 8. Once again, verify that your partitions have been created successfully:

# parted -l
Model: VBOX HARDDISK (ide)
Disk /dev/hda: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 525MB 524MB primary ext4 boot
2 525MB 21.5GB 20.9GB primary lvm
Model: VBOX HARDDISK (ide)
Disk /dev/hdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 32.3kB 4507MB 4507MB primary
2 4507MB 8587MB 4080MB primary
Model: VBOX HARDDISK (ide)
Disk /dev/hdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 4294MB 4294MB primary
2 4295MB 8590MB 4295MB primary
Error: Unable to open /dev/hdd – unrecognised disk label.

 

Creating a Swap Partition

The system uses swap space as a type of “virtual memory” for when your physical memory begins to run low. It does this by using a piece of disk storage to swap files in and out of memory (hence, the term swap).
This capability can be useful on systems that don’t have large amounts of physical memory or when your system is running something that becomes memory intensive.
Before you can create a swap, however (which is done in the next chapter), you need to partition space on your disk for it.
This is also a little different from the partitions you have previously been creating because the partition type is different. Let’s make a swap partition on /dev/hdd for the system.
 
Step 1. Like before, use the fdisk utility to start:

# fdisk /dev/hdd

Step 2. Create a new partition:

Command (m for help): n
Command action
e extended
p primary partition (1-4)   p
Partition number (1-4): 1
First cylinder (1-16644, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-16644, default 16644): +2048M

When you are creating partitions, by default, partition type 83 is chosen (Linux). For a swap partition, however, you need to change the type to 82 (Linux Swap). If you want a list of all the available partition types,  you can use the l option.
Step 3. Change the type of the partition with the t option:

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)

Step 4. Verify that the partition is set up correctly:

Command (m for help): p
Disk /dev/hdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00079351
Device Boot Start End Blocks Id System
/dev/hdd1 1 262 2104483+ 82 Linux swap / Solaris

You can see that the swap partition is set up properly, but do not write the changes to disk! You can have only one swap partition per physical disk, and we still need to cover how to make a swap with the parted
command, so don’t save your changes here.
Step 5. Quit without saving changes:

Command (m for help): q

As you can see, there isn’t too much of a difference when creating a swap partition. Let’s look again at how to create a swap partition, but this time you can use the parted utility instead.
Step 6. Start by selecting the drive you’d like to use:

# parted /dev/hdd
Step 7. Create the partition:
(parted) mkpart
Partition type? primary/extended? primary
File system type? [ext2]? linux-swap
Start? 0
End? 2048m

Notice here that you can specify a different partition type from the default while creating the new partition, unlike with the fdisk utility, where you need to change it after the partition is created.
Step 8. Verify that the partition is set up correctly:

(parted) print
Model: VBOX HARDDISK (ide)
Disk /dev/hdd: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 2048MB 2048MB primary

Step 9. Finally, you can exit the parted utility and reread the partition table:

# partprobe /dev/hdd

 

Deleting a Partition

In the next section, we discuss Logical Volume Manager (LVM), in which you use multiple drives and partitions. Because you have created a bunch of different partitions in this section, the last thing to discuss is deleting partitions so that you can clean off all your drives before beginning the section on LVM. Deleting a partition is much easier than creating one because you need to specify only the partition number that you want to delete.
Step 1. Start the fdisk utility:
# fdisk /dev/hdb
Step 2. Print out the current partition table:
Command (m for help): p
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
/dev/hdb1 1 548 4401778+ 83 Linux
/dev/hdb2 549 1044 3984120 83 Linux
You can see the two partitions created earlier. Delete them both using the d option and specifying the partition number you want to delete.
Step 3. Delete the first partition:

Command (m for help): d
Partition number (1-4): 1

Step 4. Delete the second partition:

Command (m for help): d
Selected partition 2

Step 5. Write the changes to disk:

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Step 6. Don’t forget to reread the partition table:

# partprobe /dev/hdb
You can also delete partitions with the parted utility.

Step 7. Start the parted utility:

# parted /dev/hdc

Step 8. Print out the current partition table:

(parted) print
Model: VBOX HARDDISK (ide)
Disk /dev/hdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 512B 4294MB 4294MB primary
2 4295MB 8590MB 4295MB primary
Step 9. Again, you can see the two partitions on this drive. Remove them both
with the rm option:
(parted) rm 1
(parted) rm 2
Step 10. Exit the parted program and reread the partition table:
(parted) quit
# partprobe /dev/hdc

Step 11. Also remove the swap partition that you created as well:

# parted /dev/hdd

Step 12. Remove the first partition:

(parted) rm 1
Step 13. Exit the parted program and reread the partition table:
(parted) quit
# partprobe /dev/hdd

Step 14. You can verify all the deletions to the different disks using fdisk or parted:

# fdisk -l
Disk /dev/hda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00021654
/dev/hda1 * 1 64 512000 83 Linux
/dev/hda2 64 2611 20458496 8e Linux LVM
Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004b72d
Device Boot Start End Blocks Id System
Disk /dev/hdc: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000661e0
Device Boot Start End Blocks Id System
Disk /dev/hdd: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00079351
Device Boot Start End Blocks Id System

November 14, 2015

0 responses on "Storage Related tasks - Linux Disk Partitioning"

Leave a Message

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

About iGURKUL

IGURKUL I.T. Training Hub offering various Career Certification courses in Computer Networking, Unix, Linux, Cloud Computing and DevOps Technologies. With its rich experience in IT training service sector, iGURKUL has been able to set Industry best practices in IT Training for the past five years.

In Past five years, more than 5000 professionals have been trained by iGURKUL for System administration, Cloud Computing and DevOps Skill set through our Online Training portal www.unixadminschool.com. And , each day , more than 10000 working professionals from all over the globe visiting our knowledge base www.unixadminschool.com/blog for the best practices and Knowledge learning.

Popular Tags

Who’s Online

Profile picture of Mark Joe
Profile picture of Arun S Vijayan
top
Template Design © igurkul I.T. solutions. All rights reserved.
X