How to partition a disk in Linux


Table of Contents

about walls
number one, prolonged and logical
partition naming
discovering walls
fdisk -l
/proc/partitions
parted and others
partitioning new disks
recognising the disk
opening the disk with fdisk
empty partition table
create a new partition
display the new partition
approximately the partition desk
master boot file
partprobe
logical drives
GUID partition table
labeling with parted
partitioning with parted
exercise: partitions
answer: walls
This bankruptcy continues at the difficult disk devices from the preceding one. Here we can placed walls on those devices.

This chapter prepares you for the following bankruptcy, wherein we put document systems on our walls.

Approximately walls
primary, prolonged and logical
Linux calls for you to create one or greater walls. The next paragraphs will provide an explanation for a way to create and use partitions.

A partition's geometry and length is typically described by way of a starting and finishing cylinder (once in a while by sector). Partitions can be of type primary (maximum 4), extended (most one) or logical (contained in the extended partition). Each partition has a kind area that carries a code. This determines the computer systems running device or the partitions document gadget.

Table 6.1. Primary, extended and logical walls

Partition Type naming
Primary (max four) 1-4
Extended (max 1) 1-four
Logical 5-

partition naming
We noticed before that tough disk gadgets are named /dev/hdx or /dev/sdx with x relying on the hardware configuration. Next is the partition wide variety, starting the count at 1. Hence the four (feasible) number one partitions are numbered 1 to 4. Logical partition counting usually starts at 5. Thus /dev/hda2 is the second one partition on the first ATA tough disk tool, and /dev/hdb5 is the primary logical partition on the second one ATA tough disk device. Same for SCSI, /dev/sdb3 is the third partition on the second one SCSI disk.

Table 6.2. Partition naming

partition device
/dev/hda1 first primary partition on /dev/hda
/dev/hda2 2nd number one or prolonged partition on /dev/hda
/dev/sda5 first logical power on /dev/sda
/dev/sdb6 2d logical on /dev/sdb

The picture underneath suggests  (spindle) disks with partitions. Note that an prolonged partition is a box holding logical drives.


Discovering partitions
fdisk -l
In the fdisk -l instance under you can see that  walls exist on /dev/sdb. The first partition spans 31 cylinders and contains a Linux change partition. The second partition is tons bigger.

Root@laika:~# fdisk -l /dev/sdb

Disk /dev/sdb: 100.Zero GB, 100030242816 bytes
255 heads, 63 sectors/music, 12161 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start      End     Blocks   Id  System
/dev/sdb1             1       31     248976   eighty two  Linux swap / Solaris
/dev/sdb2            32    12161   97434225   83  Linux
root@laika:~#

/proc/partitions
The /proc/walls file contains a table with foremost and minor range of partitioned gadgets, their number of blocks and the device name in /dev. Verify with /proc/devices to hyperlink the major variety to the proper tool.

Paul@RHELv4u4:~$ cat /proc/partitions
predominant minor  #blocks  call

   3     0     524288 hda
   3    64     734003 hdb
   eight     zero    8388608 sda
   8     1     104391 sda1
   8     2    8281507 sda2
   eight    16    1048576 sdb
   8    32    1048576 sdc
   8    forty eight    1048576 sdd
 253     0    7176192 dm-zero
 253     1    1048576 dm-1

The fundamental range corresponds to the tool kind (or driver) and may be observed in /proc/devices. In this example three corresponds to ide and 8 to sd. The principal number determines the tool driving force to be used with this device.

The minor range is a completely unique identification of an instance of this tool kind. The devices.Txt file in the kernel tree contains a complete listing of most important and minor numbers.

Parted and others
You can be inquisitive about alternatives to fdisk like parted, cfdisk, sfdisk and gparted. This direction particularly uses fdisk to partition hard disks.

Parted is usually recommended by using some Linux distributions for handling garage with gpt rather than mbr.

Below a screenshot of parted on CentOS.

[root@centos65 ~]# rpm -q parted
parted-2.1-21.El6.X86_64
[root@centos65 ~]# parted /dev/sda
GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of instructions.
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: forty two.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File device  Flags
 1      1049kB  525MB   524MB   primary  ext4         boot
 2      525MB   42.9GB  forty two.4GB  number one               lvm

(parted)
partitioning new disks
In the instance underneath, we bought a brand new disk for our machine. After the new hardware is well connected, you may use fdisk and parted to create the vital partition(s). This instance makes use of fdisk, however there is not anything incorrect with the use of parted.

Recognising the disk
First, we check with fdisk -l whether or not Linux can see the new disk. Yes it does, the brand new disk is visible as /dev/sdb, but it does not have any partitions but.

Root@RHELv4u2:~# fdisk -l

Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start      End      Blocks   Id  System
/dev/sda1   *       1        thirteen      104391   eighty three  Linux
/dev/sda2          14      1566    12474472+  8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't include a valid partition table

starting the disk with fdisk
Then we create a partition with fdisk on /dev/sdb. First we begin the fdisk tool with /dev/sdb as argument. Be very very careful not to partition the incorrect disk!!

Root@RHELv4u2:~# fdisk /dev/sdb
Device includes neither a valid DOS partition desk, nor Sun, SGI...
Building a brand new DOS disklabel. Changes will continue to be in memory most effective,
till making a decision to write them. After that, of direction, the previous
content material won't be recoverable.

Warning: invalid flag 0x0000 of partition desk 4 may be corrected...

Empty partition desk
Inside the fdisk device, we can problem the p command to look the modern-day disks partition table.

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, sixty three sectors/song, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
create a brand new partition
No partitions exist yet, so we problem n to create a new partition. We pick out p for primary, 1 for the partition range, 1 for the start cylinder and 14 for the give up cylinder.

Command (m for assist): n
Command action
e   extended
p   primary partition (1-4)
p
Partition wide variety (1-4): 1
First cylinder (1-130, default 1):
Using default price 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default a hundred thirty): 14
We can now problem p again to verify our modifications, however they're now not but written to disk. This way we are able to still cancel this operation! But it appears properly, so we use w to write down the changes to disk, after which cease the fdisk tool.

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/music, one hundred thirty cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  eighty three  Linux

Command (m for assist): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
Root@RHELv4u2:~#
display the new partition
Let's confirm again with fdisk -l to make sure fact fits our desires. Indeed, the screenshot underneath now shows a partition on /dev/sdb.

Root@RHELv4u2:~# fdisk -l

Disk /dev/sda: 12.Eight GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start        End      Blocks   Id  System
/dev/sda1   *        1         thirteen      104391   eighty three  Linux
/dev/sda2           14       1566    12474472+  8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, sixty three sectors/music, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  eighty three  Linux
root@RHELv4u2:~#
approximately the partition desk
master boot document
The partition desk facts (primary and prolonged partitions) is written in the grasp boot report or mbr. You can use dd to duplicate the mbr to a record.

This instance copies the master boot document from the primary SCSI hard disk.

Dd if=/dev/sda of=/SCSIdisk.Mbr bs=512 remember=1
The equal device also can be used to wipe out all information approximately partitions on a disk. This instance writes zeroes over the master boot document.

Dd if=/dev/0 of=/dev/sda bs=512 rely=1
Or to wipe out the entire partition or disk.

Dd if=/dev/zero of=/dev/sda
partprobe
Don't neglect that after restoring a master boot document with dd, which you want to pressure the kernel to reread the partition desk with partprobe. After strolling partprobe, the partitions can be used once more.

[root@RHEL5 ~]# partprobe
[root@RHEL5 ~]#
logical drives
The partition table does not contain facts about logical drives. So the dd backup of the mbr best works for primary and extended partitions. To backup the partition table along with the logical drives, you may use sfdisk.

This example indicates the way to backup all partition and logical force statistics to a file.

Sfdisk -d /dev/sda > parttable.Sda.Sfdisk
The following instance copies the mbr and all logical force data sfdisk /dev/sdb
GUID partition desk
gpt turned into developed due to the constraints of the 1980s mbr partitioning scheme (as an instance only four partitions can be defined, and that they have a most length two terabytes).

Since 2010 gpt is part of the uefi specification, however it is also used on bios structures.

Newer versions of fdisk work satisfactory with gpt, however maximum production servers these days (mid 2015) nonetheless have an older fdisk.. You can use parted as a substitute.

Labeling with parted
parted is an interactive device, much like fdisk. Type help in parted for a listing of instructions and options.

This screenshot shows a way to begin parted to control partitions on /dev/sdb.

[root@rhel71 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a listing of commands.
(parted)
Each command additionally has integrated assist. For instance help mklabel will listing all supported labels. Note that we handiest mentioned mbr(msdos) and gpt on this e-book.

(parted) assist mklabel
 mklabel,mktable LABEL-TYPE             create a brand new disklabel (partition desk)

    LABEL-TYPE is considered one of: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, solar, loop
(parted)
We create an mbr label.

(parted) mklabel msdos>
Warning: The existing disk label on /dev/sdb can be destroyed and all records on
this disk could be misplaced. Do you need to continue?
Yes/No? Yes
(parted) mklabel gpt
Warning: The current disk label on /dev/sdb might be destroyed and all records on
this disk may be lost. Do you want to hold?
Yes/No? Y
(parted)
partitioning with parted
Once classified it is easy to create partitions with parted. This screenshot starts with an unpartitioned (but gpt labeled) disk.

(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

(parted)
This instance suggests a way to create  primary walls of same size.

(parted) mkpart number one zero 50%
Warning: The resulting partition isn't always nicely aligned for great performance.
Ignore/Cancel? I
(parted) mkpart number one 50% a hundred%
(parted)
Verify with print and exit with give up. Since parted works immediately on the disk, there is no need to w(ceremony) like in fdisk.

(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File machine  Name     Flags
 1      17.4kB  4295MB  4295MB               primary
 2      4295MB  8589MB  4294MB               number one

(parted) cease
Information: You may additionally want to update /and many others/fstab.

[root@rhel71 ~]#
practice: partitions
1. Use fdisk -l to display existing partitions and sizes.

2. Use df -h to display present partitions and sizes.

3. Compare the output of fdisk and df.

4. Create a 200MB number one partition on a small disk.

5. Create a 400MB number one partition and two 300MB logical drives on a large disk.

6. Use df -h and fdisk -l to affirm your work.

7. Compare the output again of fdisk and df. Do each commands display the new walls ?

8. Create a backup with dd of the mbr that contains your 200MB primary partition.

Nine. Take a backup of the partition table containing your 400MB number one and 300MB logical drives. Make positive the logical drives are inside the backup.

10. (optionally available) Remove all of your partitions with fdisk. Then repair your backups.

Solution: walls
1. Use fdisk -l to show existing partitions and sizes.

As root: # fdisk -l
2. Use df -h to display existing walls and sizes.

Df -h
three. Compare the output of fdisk and df.

Some walls could be listed in both outputs (maybe /dev/sda1 or /dev/hda1).
Four. Create a 200MB number one partition on a small disk.

Choose one of the disks you delivered (this situation makes use of /dev/sdc).
Root@rhel53 ~# fdisk /dev/sdc
...
Command (m for assist): n
Command motion
   e   extended
   p   number one partition (1-4)
p
Partition quantity (1-four): 1
First cylinder (1-261, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-261, default 261): +200m
Command (m for assist): w
The partition table has been altered!
Calling ioctl() to re-study partition desk.
Syncing disks.
5. Create a 400MB primary partition and two 300MB logical drives on a massive disk.

Choose one of the disks you introduced (this case makes use of /dev/sdb)
fdisk /dev/sdb
interior fdisk : n p 1 +400m input --- n e 2 enter input --- n l +300m (two times)
6. Use df -h and fdisk -l to verify your work.

Fdisk -l ; df -h
7. Compare the output once more of fdisk and df. Do both instructions show the brand new partitions ?

The newly created walls are visible with fdisk.
But they're now not displayed by way of df.
Eight. Create a backup with dd of the mbr that contains your 200MB number one partition.

Dd if=/dev/sdc of=bootsector.Sdc.Dd remember=1 bs=512
9. Take a backup of the partition desk containing your 400MB primary and 300MB logical drives. Make certain the logical drives are inside the backup.

Sfdisk -d /dev/sdb > parttable.Sdb.Sfdisk