LFCS - Partitioning Disks

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
372
Reaction score
432
Credits
13,798
We are nearly done with the LFCS certification articles. All that is left deals with disks. Of course, this is a new area of the LFCS, so if you are using VirtualBox to work through these, you can get rid of the machines you used and start over again. Just create a machine and name it 'Server1'. We will add more as we need them. This can be good practice to go through installing the Operating System, either CentOS 7.2 or Ubuntu 18.04.

Dealing with partitioning disks can be easier using VirtualBox hard disks instead of adding a physical disk to a physical machine.

Setting up

Using VirtualBox, I added a second hard disk under 'Storage' for the machine. I set the size to 16 GB.

You do not need any special settings for this drive. We can manage partition settings as we go along.

Partition Limitations

Each drive will have a partition table to manage the partitions on the disk. There are two main types of partition tables we can use:
  1. Master Boot Record (MBR)
  2. GUID Partition Table (GPT)
The MBR has a limit of 2 TB and allows only 4 primary partitions, which is 3 primary and 1 extended partitions. Primary partitions will contain a file system, while an extended partition is a partition that has no file system and can be broken into logical partitions, where each logical partition can have a file system.

For MBR, there is a limit of 15 partitions per drive. The 15 partitions comprise 4 partitions, which are 3 primary and 1 extended. The extended drive can be split into 11 partitions.

The GPT has a limitation of 128 partitions and each partition having a maximum size of 18 Exabytes (EB).

Before creating partitions, we must discuss the idea of partitions and what limitations we have.

A partition is a portion of a disk that is an actual drive with a file system. The partition can be a part of a whole disk or even the whole disk. For example, let's say we have a hard drive that is 2 TB in size. We can create two 1 GB partitions or four 500 GB drives.

If we need a partition larger than 2 TB or over 15 partitions, we need to use GPT.

Table Size and Placement

For the MBR, it is written in the first sector of the drive. The size of the MBR is usually 512 bytes.

If you use GPT, it is stored at the beginning of the drive and comprises 32 sectors at 512 bytes each, or 16KB. The very first sector, if the sectors are 512 bytes, is a legacy MBR boot partition to allow for backward compatibility. There is also a backup boot record that is stored in the last 32 sectors of the disk. If the main partition table becomes corrupted, it can restore it from the backup table. We can get into that more later.

Determine Partition Count

We can see the partition count of any drive by using the command:

Code:
lsblk /dev/<disk>

Let me run that on my drive at '/dev/sda' as shown in Figure 1.

Figure 1.JPG

FIGURE 1

There is a listing of the drive SDA that shows one partition, SDA1. there is also the Major and Minor device number. The device driver being used for SDA is driver 8. The Minor device number is the partition number.

We can look into examples of this soon.

I have not partitioned SDB at all, so if I run 'lsblk /dev/sdb', I should only see the disk itself with zero partitions.

Partitioning Software

There are three different partitioning softwares we will cover. These are all Command-Line Interface (CLI) based. For the certification exam, you will be required to know these three and not GParted, which is Graphical User Interface (GUI) based.

The three types of software are:
  1. fdisk - MBR based
  2. gdisk - GPT based
  3. parted - MBR/GPT based and allows scripting
So, now let's cover these three types.

FDISK

If you plan on using MBR partitioning, then you will use 'fdisk'.

Keep in mind that anything you do in 'fdisk' remains as a virtual table until you give the command to write to the disk. If you exit out of 'fdisk' without writing, the disk remains intact as it was before running 'fdisk'.

NOTE: Be sure to use 'sudo su' to have root privileges or you will need to run the commands with 'sudo'.

If we wanted, we can also list the partition table with the parameter '-l'. The command would be:

Code:
fdisk -l /dev/sdb

So, in our case, we will run the following command:

Code:
fdisk /dev/sdb

Since we have not created a partition table on the drive yet, the command will generate a new MBR for the disk (it is not written yet). You can press 'q' to quit without saving and then rerun the last command to start 'fdisk' again. It will recreate the MBR again in memory, but not write it.

Now we are inside the 'fdisk' prompt. We can enter an 'm' for the menu of options.

If you enter a 'p', it will print out information on the partition. This shows the sector size, but it doesn’t list any partitions since none exists.

Enter an 'n' for a new partition. We can now enter 'p' for a primary partition or 'e' for an extended. After the choice to enter 'p', it shows how many primary and extended partitions exist and also how many remainto be made, as shown in Figure 2.

Figure 2.JPG

FIGURE 2

It says that the default entry is 'p' for primary, so just press ENTER. Now it is asking for a partition number (1-4) with '1' being the default. Press ENTER.

Now, here is something a little different that we haven't discussed yet. It wants to know the first sector to use for the partition. The first available sector is at '2048'. On every hard disk, the first sector used is at 1 MB. To get 1 MB, we use 2048 sectors at 512 bytes each. Technically, we are using sector 0 to sector 2047 for this gap. So, press ENTER to accept sector 2048 as the first one.

Now we can enter an ending sector or a partition size. The size is designated with K, M, G, P, T. The default is to use the whole disk. So we should try '+1G' to add a 1 GB partition.

The result should be:

Code:
Created a new partition 1 of type 'Linux' and of size 1 GiB.

Keep in mind that once we write the partition, it will not be formatted with a file system yet.

Enter a 'w' to write the partition to the disk. Be very sure you are using a disk that can be erased.

Now, if you run 'lsblk /dev/sdb' you should see a partition listed. You may notice that the partition numbers are starting at '16'. This is because '15' are set for 'SDA'. The primary partition we added is number '17'. If you run 'lsblk /dev/sda' you should see that SDA is partition '0' and SDA1 is partition '1'. If we added SDC, it would start at '32'. There are 15 partitions per drive, but one place holder for the drive itself starting at place '0', '16', '32', etc.

We should try something a little different. Let's go back in an add an extended partition to take up 2 GB.

Code:
fdisk /dev/sdb
n
e
2
2099200
+2gb
w

This should create the extended partition we needed.

Let's discuss the use of extended partitions. Once you create an extended partition, you can use it like it was an unpartitioned disk. You can create logical partitions within the extended partition. This lets you get past the 4 partition limit. Now that we have an extended partition made up of 2 GB, we will create two 1 GB logical partitions within the extended one. To do this, perform:

Code:
fdisk /dev/sdb
n
l
ENTER
+1g
n
l
ENTER
ENTER
w

This creates a new partition, then you select 'l' for logical. Since the Extended partition already exists, you cannot create another extended, but add logical partitions to the extended one. Press ENTER to accept the default starting sector, then specify a size of 1 GB. We then create another new partition, also logical. We press ENTER to accept the default start sector and ENTER again for the default ending sector. After it creates the partitions, we use 'w' to write the changes to the Master Boot Record of the disk.

Using the command 'lsblk /dev/sdb' we get the output as shown in Figure 3.

Figure 3.JPG

FIGURE 3

You should see that 'sdb1' is the primary disk we made as partition '17'. The extended partition is number '18'. The Logical partitions are numbered '21' and '22'. Partitions '19' and '20' are left for the other two primary partitions that are allowed. The first four partitions are reserved for the allowed 3 primary and 1 extended partition. The Logical partitions are numbered after the reserved ones.

We stated before that the MBR takes up the first 512 bytes of the disk. So, let's overwrite the MBR and see what happens. Use the command as follows to write zeros to the first 512 bytes of 'sdb':

Code:
dd if=/dev/zero of=/dev/sdb count=1 bs=512

Now if you run the fdisk command on 'sdb', you’ll get the message there is no partition table. You can also use 'lsblk' on 'sdb' and get a listing that shows there are no partitions on the disk. We removed the MBR so we lose everything on the disk.

GDISK

If you want to create or manage partitions on a disk with GPT, then you use 'gdisk'.

I can use the '-l' parameter just like on 'fdisk' and specify the drive. The output is displayed in Figure 4.

Figure 4.JPG

FIGURE 4

There should be no partition table present. So, we can create one:

Code:
gdisk /dev/sdb

As soon as 'gdisk' starts, it will create the GPT records. Of course, this isn't written until you write the changes to the disk.

To get a menu, use the question mark (?). The major commands are just like in fdisk. We can create a new partition (n) and write the table (w).

So, let's create a partition. The first choice after using 'n' for a new partition is the partition number. Just press ENTER to use the default. Then you should be asked about the starting sector or the size. Just press ENTER for the default starting sector. When asked for the ending sector, set the size '+2g' for a 2 GB partition. The next choice is the disk type. The default is a Linux partition (8300) which is the same as with MBR, just with 2 zeros added. You can enter an 'l' for a listing. The listing is quite a bit, but the main ones are:
  • 8200 - Swap
  • 8300 - Linux file system
  • 8302 - Linux /home
  • 8304 - Linux Root
If you want, add another few partitions for practice and maybe change the disk type.

When you enter a 'w' to write the table, you will be prompted to verify that you want to perform the writing of the table. Answer 'Y' to write the table on the disk.

To see the partition listing, use the command:

Code:
lsblk /dev/sdb

Since GPT has no partition number reservations, you’ll see the partition numbers in order. The only way you get partitions skipped is if you use a different partition number when creating the partition besides the default. It also happens if you remove a partition that is in the middle of others. If you have a third disk, it keeps its disk number even if you remove it, boot the system,shutdown, add it back and boot again.

Now, what if we delete the first 16KB sections of the drive? This will delete the main GPT table, but not the backup GPT table. So perform the following to remove the main table:

Code:
dd if=/dev/zero of=/dev/sdb bs=16K count=1

If you run 'lsblk /dev/sdb' it should show the drive as empty of all partitions. Run 'gdisk' and it will report that the GPT table has been corrupted and it will restore it from the backup table. You should be asked if you want to restore it or create a blank table. This is shown in Figure 5.

Figure 5.JPG

FIGURE 5

If you select the choice to recover the table, you need to write the recovered table. If you run 'lsblk' on the drive, you'll see the partitions recovered.

To delete both partition tables, you can write zeros to the while disk:

Code:
dd if=/dev/zero of=/dev/sdb bs=16k

Since we do not define a 'count', it writes the whole disk with zeros. Now, if you run 'gdisk /dev/sdb' it will respond as if the drive has no table at all and you are starting fresh.

PARTED

With the 'parted' utility, we can set a disk as either MBR or GPT. We can also use 'parted' in a script.

To check a drive to see what is on it, you can issue the following 'parted' command:

Code:
parted /dev/sdb print

Sample output is shown in Figure 6.

Figure 6.JPG

FIGURE 6

My drive has no partition table on it at this point.

To get into a shell prompt for 'parted', just enter the command:

Code:
parted /dev/sdb

You can enter 'help' to get a list of commands.

To make a new table, you can use either the 'mklabel' or 'mktable' command with either the parameter of 'msdos' or 'gpt' to create which table type you prefer.

For MBR, you can use one of the following three commands to create a partition:
  1. mkpart primary <start-sector> <end-sector>
  2. mkpart extended <start-sector> <end-sector>
  3. mkpart logical <start-sector> <end-sector>
To get to the end of the disk, you can specify the ending sector of '-1'.

You can just as easily simply use the command 'mkpart' with no parameters and the program will prompt you for the needed information. You may be asked the file-type, such as 'ext2', but the partition will not be formatted.

Use the command 'quit' to save the table to the disk and exit the 'parted' prompt.

To use 'parted' in a script, you start each line with the command 'parted' and a parameter of '-s' for a script command. An example of setting up multiple partitions and setting flags on a partition, see the following script:

Code:
#!/bin/bash
parted -s /dev/sdb -- mklabel msdos mkpart extended 1m -1m
parted -s /dev/sdb mkpart logical 2m 1000m #Partition 5
parted -s /dev/sdb mkpart logical 1001m 2000m #Partition 6
parted -s /dev/sdb mkpart logical 2001m 3000m #Partition 7
parted -s /dev/sdb mkpart logical 3001m 4000m #Partition 8
parted -s /dev/sdb mkpart logical 4001m 5000m #Partition 9
parted -s /dev/sdb mkpart logical 5001m 6000m #Partition 10
parted -s /dev/sdb mkpart logical 6001m 7000m #Partition 11
parted -s /dev/sdb mkpart logical 7001m 8000m #Partition 12
parted -s /dev/sdb mkpart logical 8001m 9000m #Partition 13
parted -s /dev/sdb mkpart logical 9001m 10000m #Partition 12
parted -s /dev/sdb mkpart logical 10001m 11000m #Partition 13

parted -s /dev/sdb set 10 lvm on
parted -s /dev/sdb set 11 lvm on
parted -s /dev/sdb set 12 lvm on

parted -s /dev/sdb set 13 raid on
parted -s /dev/sdb set 14 raid on

If you run the script and open 'gparted' and then drive 'sdb', you should see a little easier what happened to the disk.

Conclusion

Partitioning new disks can be a very important task. Most drives come pre-formatted, but you may want to delete the existing table and create a new one.

Knowing how to use the various commands as well as understanding the partition numbering can be very important in certain situations. Definitely try to perform the various commands.
 

Members online


Top