CompTIA Linux+ Certification Guide
上QQ阅读APP看书,第一时间看更新

Using the fdisk utility

In Linux, whenever we're working with hard disks, chances are we will  have to partition a hard disk at some point in time. Partitioning simply means separating the hard disk. This enables us to have varying sizes of partition and gives us the ability to satisfy various software installation requirements. Furthermore, when we partition a hard disk, each partition is treated as an entirely separate hard disk by the operating system. The fdisk (fixed disk or format disk) is a command-line based utility that can be used for manipulating a hard disk. With fdisk, you can view, create, delete, and change, among other things.

To start with, let's expose the hard disk within our Ubuntu distribution:

philip@ubuntu:~$ ls /dev/ | grep sd
sda
sda1
sda2
sda5
philip@ubuntu:~$

From the preceding output, the hard disk in the system is represented by /dev/sda. The first partition is /dev/sda1, the second partition is /dev/sda2, and so on. For us to see the partition information, we will run the following command:

philip@ubuntu:~$ fdisk -l /dev/sda
fdisk: cannot open /dev/sda: Permission denied
philip@ubuntu:~$

From the preceding output, we get Permission denied. This is because we require a root privilege to view and change the partition of a hard disk. Let's retry as the root user:

philip@ubuntu:~$ sudo su
[sudo] password for philip:
root@ubuntu:/home/philip#
root@ubuntu:/home/philip# fdisk -l /dev/sda
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf54f42a0

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 39845887 39843840 19G 83 Linux
/dev/sda2 39847934 41940991 2093058 1022M 5 Extended
/dev/sda5 39847936 41940991 2093056 1022M 82 Linux swap / Solaris
root@ubuntu:/home/philip#

From the preceding output, the way to read this would be as follows:

Disk /dev/sda: 20 GiB, 21,474,836,480 bytes, 41,943,040 sectors. This is the actual physical hard disk:

Device           Boot            Start             End            Sectors           Size            Id           Type           Comment

/dev/sda1                       2048            39,845,887              39,843,840            19 G            83             Linux           First partition is 19 GB

/dev/sda2                      39,847,934            41,940,991           2,093,058            1,022 M              5           Extended           Second partition is 1,022 MB

/dev/sda5                          39,847,936              41,940,991              2,093,056            1,022 M            82            Linux swap / Solaris           Fifth partition is 1,022 MB

Now, for us to be able to make any changes, we will use the fdisk command once again. This time we will leave out the -l option:

root@ubuntu:/home/philip# fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

From the previous code, we are now inside the fdisk utility and we are presented with a nice little message.

Do not commit any changes before first making sure you understand the dangers surrounding removing partition; the system could become unstable if you remove a partition that stores system files such as  /boot/ and  /, to name a few.

For us to view the available options, we can press the m key:

Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition

Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file

Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table

Command (m for help):

From the preceding output, we can see a variety of options to choose from. We can even use l to see the known partition types:

From the preceding screenshot, we can see a range of different partition types available for usage. The common types are 5 Extended, 7 NTFS Windows, 82 Linux swap, 83 (Linux), a5 FreeBSD, ee GPT, and  ef EFI, to name a few.

Now, to view the partitions created, we can use p:

I've added a second hard disk to this system, so let's verify this:

root@ubuntu:/home/philip# ls /dev/ | grep sd
sda
sda1
sda2
sda5
sdb
root@ubuntu:/home/philip#

Awesome! We can now see /dev/sdb. We will use fdisk with this new hard disk:

root@ubuntu:/home/philip# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x0079e169.
Command (m for help):

Now let's press p, which will print the current partition(s) on the /dev/sdb:

Command (m for help): p
Disk /dev/sdb: 15 GiB, 16106127360 bytes, 31457280 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0079e169

Command (m for help):

As you can see, there is no partition on the /dev/sdb. For us to create a partition, we will use the n key:

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):

This will ask us to declare the type of partition. There are primary and extended partition types available with the fdisk utility. There is also a logical partition type. For us to install an operating system, we will choose p, which stands for primary partition type.

You would not install an operating system on a logical partition type.

As you can see, we use the n to create new partitions. An important point to note is the fact that the partitions that we have created thus far are all Linux-type partitions. If for some reason we want to change the partition type, we can use t to change it. Let's change the /dev/sdb2 to an HPFS/NTFS/exFAT partition. We will use type 7 in the fdisk utility:

Command (m for help): t
Partition number (1-3, default 3): 2
Partition type (type L to list all types): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT

Awesome! Now we can see the partition type as type 7:

Partition type (type L to list all types): 7
Changed type of partition 'Empty' to 'HPFS/NTFS/exFAT'.
Command (m for help): p
Disk /dev/sdb: 15 GiB, 16106127360 bytes, 31457280 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x2584b986
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 18876415 8388608 4G 7 HPFS/NTFS/exFAT
/dev/sdb3 18876416 31457279 12580864 6G 0 Empty

We will also change the /dev/sdb3 partition to type ef:

Now when we rerun the p command, we can see that our newly created partition type is set to ef:

Device     Boot      Start     End     Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 18876415 8388608 4G 7 HPFS/NTFS/exFAT
/dev/sdb3 18876416 31457279 12580864 6G ef EFI (FAT-12/16/32)

Now if we decide to install an operating system, we will have to make one of these partitions bootable. We will make the third partition /dev/sdb3 bootable:

Command (m for help): a  
Partition number (1-3, default 3): 3
The bootable flag on partition 3 is enabled now.
Command (m for help): p
Disk /dev/sdb: 15 GiB, 16106127360 bytes, 31457280 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x2584b986
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 10487807 10485760 5G 83 Linux
/dev/sdb2 10487808 18876415 8388608 4G 7 HPFS/NTFS/exFAT
/dev/sdb3 * 18876416 31457279 12580864 6G ef EFI (FAT-12/16/32)

From the previous output, the /dev/sdb3 is now marked as bootable.

Finally, to change or write our changes, we will press w to save and exit:

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

The output of these commands can be seen in the following screenshot: