Disk Management - Linux Administration (2016)

Linux Administration (2016)

Disk Management

When a disk or storage device is “partitioned” it is divided into parts. Each one of these parts is called a partition. Partitioning a disk allows you to allocate different sections of the disk for different purposes.

As a system administrator, you can decide what partitioning scheme to use. For example, you could use four partitions, allocating one partition for the operating system data, another partition for the application data, yet another one for the user data, and finally a partition dedicated to swap space. Another example scheme would simply be to use two partitions and separate the user’s home directories from the rest of the system.

Having separate partitions is one way to prevent one part of the system from adversely affecting another part of the system. Having a dedicated partition for user home directories, for example, prevents a user from filling up the entire disk and interfering with the normal operation of the operating system. Even though a user, or group of users, may fill up the storage allocated for home directories and prevent another user from using more space, the operating system and any applications that are running will still be able to function normally. If this system hosts a website, the operating system will continue to run as well as the web server and a service outage will have been avoided.

The MBR Partitioning Scheme

The MBR, or master boot record, is a boot sector at the beginning of a storage device. The partition table that resides in the MBR contains information on how the logical partitions are organized on the disk. Because the partition table in the MBR can only address storage space up to 2 TB, it is being replaced by the GUID Partition Table (GPT).

The MBR partitioning scheme allows for up to four primary partitions. If you want to create more than four partitions, you’ll need to use an extended partition. An extended partition is a special kind of primary partition that is used as a container for other partitions. This allows you to create an unlimited number of logical partitions.

The GUID Partition Table (GPT)

The GUID (global unique identifier) Partition Table, or GPT for short, is replacing the older MBR partitioning system. It is part of the Unified Extensible Firmware Interface (UEFI) standard. The UEFI is replacing the traditional BIOS; however, GPT has been used on some BIOS systems primarily due to the disk size limitations of MBR partition tables.

There are no primary and extended partitions with GPT. Using the default configuration, GPT supports up to 128 partitions. Also, GPT supports storage devices up to 9.4 ZB. The primary downside of GPT is is that it is not supported on older operating systems. Also, you’ll need to use newer partitioning utilities that support GPT.

Mount Points

A mount point is simply a directory that is used to access the data on a partition. At minimum, there will be one partition mounted on the / mount point. Any additional partitions will be mounted on mount points below / in the directory tree. For example, if you allocated a partition for user home directories, that partition would be mounted at /home. The files and directories that are at or below the /home mount point will reside on that partition. For example, the files in my home directory, /home/jason, will be on the partition mounted at /home.

If you were to disconnect, or unmount, that partition and mount it to another directory (mount point), all the data would be available at that new mount point. If /home were unmounted and the partition was then mounted at /export/home, the files in my home directory would become available at /export/home/jason.

It’s important to point out that you can mount partitions over existing data. For example, if files are created in /home before /home is mounted, those files will not be accessible. Let’s say you only have the / partition mounted and you create a home directory for Sarah at /home/sarah. The directory /home/sarah resides on the partition that is mounted on /. If you were to then mount another partition on /home, you would no longer be able to access the /home/sarah directory. The data for /home/sarah still exists, but it is on the partition associated with /. Once you unmounted/home you would then see that /home/sarah still exists.

You can mount partitions anywhere in the Linux directory tree. You can even have mount points that reside on other mounted partitions. Let’s say you have a partition mounted on /home. You could then mount yet another partition on /home/jason. It’s important that /home be mounted before /home/jason. You’ll learn how to associate partitions with mount points as well as control the order that partitions are mounted later in this chapter.

Creating Partitions

When performing interactive Linux installations you will most likely end up using a partitioning tool provided by the Linux distribution. However, if you want to manipulate disks after the initial installation you will most likely need to use a standard Linux tool. The fdisk utility has been traditionally used to create and modify partitions on a disk, but there are other viable alternatives including gdisk and parted. Earlier version of fdisk lacked support for GPT, but, as of this writing, fdisk supports GPT. To manage the partitions on a disk with fdisk, simply provide the path to the device you wish to manage as an argument to the command.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

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

Building a new DOS disklabel with disk identifier 0x1c0ae930.

Command (m for help): q

#

If you are unsure what disks are available, run fdisk -l to display a list of devices. This server has two disks. The first disk, /dev/sda, has already been partitioned and the second disk, /dev/sdb, has not.

# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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

Disk label type: dos

Disk identifier: 0x000ad6ec

Device Boot Start End Blocks Id System

/dev/sda1 2048 4098047 2048000 82 Linux swap / Solaris

/dev/sda2 * 4098048 417794047 206848000 83 Linux

Disk /dev/sdb: 549.8 GB, 549755813888 bytes, 1073741824 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

#

Let's choose to work with /dev/sdb and ask fdisk for some help by typing in m.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

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

Building a new DOS disklabel with disk identifier 0xde069e1a.

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

g create a new empty GPT partition table

G create an IRIX (SGI) partition table

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

The following example demonstrates how to create a new MBR partition table. We will create three primary partitions. The first one will be a 1GB swap partition, the second will be a 20GB partition, and the final partition will use the remaining space on the disk.

To create a new MBR partition, type n. You will then be guided through a series of prompts which are fairly self-explanatory. The default partition type created by fdisk is Linux represented by 0x83. To change the partition type, type in c. The swap partition is represented by 0x82. To get a list of partition types, type l. To view the current partition table, use p and to write your changes to disk use w.

# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

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

Building a new DOS disklabel with disk identifier 0x6a290d93.

Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p): p

Partition number (1-4, default 1):

First sector (2048-1073741823, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-1073741823, default 1073741823): +1G

Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): t

Selected partition 1

Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): n

Partition type:

p primary (1 primary, 0 extended, 3 free)

e extended

Select (default p):

Using default response p

Partition number (2-4, default 2):

First sector (2099200-1073741823, default 2099200):

Using default value 2099200

Last sector, +sectors or +size{K,M,G} (2099200-1073741823, default 1073741823): +20G

Partition 2 of type Linux and of size 20 GiB is set

Command (m for help): n

Partition type:

p primary (2 primary, 0 extended, 2 free)

e extended

Select (default p):

Using default response p

Partition number (3,4, default 3):

First sector (44042240-1073741823, default 44042240):

Using default value 44042240

Last sector, +sectors or +size{K,M,G} (44042240-1073741823, default 1073741823):

Using default value 1073741823

Partition 3 of type Linux and of size 491 GiB is set

Command (m for help): p

Disk /dev/sdb: 549.8 GB, 549755813888 bytes, 1073741824 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

Disk label type: dos

Disk identifier: 0xf22b19b6

Device Boot Start End Blocks Id System

/dev/sdb1 2048 2099199 1048576 82 Linux swap / Solaris

/dev/sdb2 2099200 44042239 20971520 83 Linux

/dev/sdb3 44042240 1073741823 514849792 83 Linux

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

#

To create a GPT partition table with fdisk, use the g option in fdisk. Next, type n to create a new partition. This example simply creates one large partition.

# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.23.2).

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

Building a new DOS disklabel with disk identifier 0x774d7c12.

Command (m for help): g

Building a new GPT disklabel (GUID: B55798FB-5F51-42D1-9E97-AAFA9074C0E4)

Command (m for help): n

Partition number (1-128, default 1):

First sector (2048-83886046, default 2048):

Last sector, +sectors or +size{K,M,G,T,P} (2048-83886046, default 83886046):

Created partition 1

Command (m for help): p

Disk /dev/sdc: 42.9 GB, 42949672960 bytes, 83886080 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

Disk label type: gpt

# Start End Size Type Name

1 2048 83886046 40G Linux filesyste

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

Creating Filesystems

Before a partition can be used by a Linux system, it will need a filesystem. The extended file system, or ext for short, was created specifically for Linux. It was soon replaced with ext2, the second extended file system. Since then, ext3 (the third extended file system) and ext4 (the fourth extended file system) have been released. These are the most commonly used file systems on Linux systems and are often found as the default file systems on Linux distributions. If you have special needs, you should research some of the other popular filesystems available on Linux. These include ReiserFS, JFS, XFS, ZFS, and Btrfs.

To create a filesystem, use the mkfs command. The format is mkfs -t TYPE DEVICE. The TYPE will be a file system type such as ext3, btrfs, etc. The DEVICE is the path to the partition on which you want the file system to reside. Here is how to create an ext3 file system on the second partition of the sdb disk (/dev/sdb2).

# mkfs -t ext3 /dev/sdb2

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

1310720 inodes, 5242880 blocks

262144 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=4294967296

160 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000

Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

#

Here is how to create an ext4 file system.

# mkfs -t ext4 /dev/sdb3

mke2fs 1.42.9 (28-Dec-2013)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

32178176 inodes, 128712448 blocks

6435622 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2277507072

3928 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks:

32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,

4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,

102400000

Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done

#

The mkfs utility is actually a front-end for the various file system builders. Running mkfs -t ext4 /dev/sdb3 is actually the same as running mkfs.ext4 /dev/sdb3. If you want to see the options available for a specific file system, consult the man page for the given command. For example, you can run man mkfs.xfs to find more information about creating XFS file systems.

# ls -1 /sbin/mkfs*

/sbin/mkfs

/sbin/mkfs.btrfs

/sbin/mkfs.cramfs

/sbin/mkfs.ext2

/sbin/mkfs.ext3

/sbin/mkfs.ext4

/sbin/mkfs.minix

/sbin/mkfs.xfs

#

Mounting and Unmounting Partitions

To mount a partition, use the mount command followed by the path to the device and then the directory where you want to mount that device. For example, to mount /dev/sdb3 on /opt, run mount /dev/sdb3 /opt.

# mount /dev/sdb3 /opt

#

You can use the mount command without any arguments to see what is currently mounted. Be aware that mount will not only show physical filesystems but also virtual filesystems. These virtual filesystems are often RAM based filesystems that provide ways to interact with other parts of the system. If you want a shorter list of mount points that contain storage devices, use the df command. You can think of the df command as the “disk-free” command, as it reports file system usage.

# mount

proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)

devtmpfs on /dev type devtmpfs (rw,nosuid,size=500196k,nr_inodes=125049,mode=755)

securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)

tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)

devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)

tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)

tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,mode=755)

cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)

pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)

cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)

cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)

cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)

cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)

cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)

cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)

cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)

cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)

cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)

configfs on /sys/kernel/config type configfs (rw,relatime)

/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,noquota)

systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=32,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)

debugfs on /sys/kernel/debug type debugfs (rw,relatime)

mqueue on /dev/mqueue type mqueue (rw,relatime)

hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)

/dev/sdb3 on /opt type ext4 (rw,relatime,data=ordered)

# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda2 198G 1.7G 196G 1% /

devtmpfs 489M 0 489M 0% /dev

tmpfs 497M 0 497M 0% /dev/shm

tmpfs 497M 6.5M 491M 2% /run

tmpfs 497M 0 497M 0% /sys/fs/cgroup

/dev/sdb3 484G 73M 459G 1% /opt

#

Note that manually mounting a file system from the command line will not cause it to persist between reboots. In order to make the mount permanent, you will need to add an entry in the /etc/fstab file. This will be covered in a later section of this book.

To unmount a partition, use the umount command followed by either the device path or the mount point. To unmount /opt, you could run umount /opt or umount /dev/sdb3.

# umount /opt

# df

Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda2 206747000 1766844 204980156 1% /

devtmpfs 500196 0 500196 0% /dev

tmpfs 508740 0 508740 0% /dev/shm

tmpfs 508740 6612 502128 2% /run

tmpfs 508740 0 508740 0% /sys/fs/cgroup

#

Creating Swap Space

Instead of creating a file system and mounting it, with swap you create a swap area and enable it. To prepare the swap partition for use, use the mkswap command followed by the path to the partition. To enable the swap partition, use the swapon command followed by the path to the device. To see the swap devices in use, run swapon -s.

# mkswap /dev/sdb1

Setting up swapspace version 1, size = 1048572 KiB

no label, UUID=619dc6d9-1b0b-4a9a-9df5-bfc343fb8d6e

# swapon /dev/sdb1

# swapon -s

Filename Type Size Used Priority

/dev/sda1 partition 2047996 0 -1

/dev/sdb1 partition 1048572 0 -2

The File System Table

The /etc/fstab file controls where devices are mounted on a Linux system and what options to use when mounting those devices. Each entry consists of one line with six columns. If a line starts with #, it's a comment and is ignored. The six columns are the device, mount point, filesystem type, options, dump, and file system check order. Each of these columns is separated by a space or a tab. Here is an example /etc/fstab file.

# device mount point filesystem options dump fsck

/dev/sda2 / xfs defaults 0 1

/dev/sda1 swap swap defaults 0 0

Device

This column contains a path to a device, a label for a device, or a UUID (universally unique identifier) of a device.

Mount Point

This column determines where the device will be mounted.

Filesystem Type

The third column contains the filesystem type. Use the same type here are as you did when you created the filesystem with the mkfs command.

Mount Options

Use this column to specify which options to mount the filesystem with. The keyword defaults represents the defaults for the filesystem which are typically the rw, suid, dev, exec, auto, nouser, and async options. Consult the man page for fstab, mount, and the filesystem for a full list of options. To specify multiple options separate them with a comma. Do not use spaces before or after the commas.

Dump

This column is used by dump utility. If it contains a 0, dump will ignore this filesystem. If it contains a 1, dump will backup this filesystem. You can safely ignore this column if you do not use the dump utility to perform backups. Today, dump is rarely used for backups.

Fsck

This column is used by the fsck program at boot time to determine if a filesystem is to be checked and in what order to check the filesystems. Valid values are 0, 1, and 2. If this column contains a 0, fsck will skip checking this filesystem. File systems with a value of 1 will be checked first then the filesystems with a value of 2 will be checked next. For filesystems that you want to have checked, it's a best practice to set the / filesystem to 1 with the remaining filesystems set to 2.

Let's look at the example /etc/fstab file again.

# device mount point filesystem options dump fsck

/dev/sda2 / xfs defaults 0 1

/dev/sda1 swap swap defaults 0 0

The first entry ensures that /dev/sda2 will get mounted on /. Its filesytem type is xfs and it uses the default options. Since the dump column contains a 0, the dump utility will not backup this filesystem. The value in the fsck column is 1, so this filesystem will get checked first during the boot up.

The second entry ensures that /dev/sda1 will be used as a swap device. For swap devices, use the swap keyword as the mount point and filesystem type. The dump and fsck columns are set to 0 as swap space does not need to be backed up or checked.

This fstab uses UUIDs ands labels in the device column. The device with the UUID of dbae4fe7-b06f-4319-85dc-b93ba4a16b17 will be mounted on /. The device with the label opt will be mounted on /opt and the /dev/sda1 device will be used as swap space.

UUID=dbae4fe7-b06f-4319-85dc-b93ba4a16b17 / xfs defaults 0 1

LABEL=opt /opt ext4 defaults 1 1

/dev/sda1 swap swap defaults 0 0

To view labels and UUIDs, use the lsblk –f command. If you are only interested in the UUIDs, you can use the blkid command.

# lsblk –f

NAME FSTYPE LABEL UUID MOUNTPOINT

sda

├─sda1 swap 1cb76bec-a1fa-4ac6-8296-c508e936b744 [SWAP]

└─sda2 xfs dbae4fe7-b06f-4319-85dc-b93ba4a16b17 /

sdb

├─sdb1 swap 619dc6d9-1b0b-4a9a-9df5-bfc343fb8d6e [SWAP]

├─sdb2 ext3 6517c68b-3671-42e7-9f37-e2fb9a549322

└─sdb3 ext4 opt 8b885f83-0d2c-4fe5-a4f1-dc678a9dec5a

sdc

└─sdc1

sr0

# blkid

/dev/sda1: UUID="1cb76bec-a1fa-4ac6-8296-c508e936b744" TYPE="swap"

/dev/sda2: UUID="dbae4fe7-b06f-4319-85dc-b93ba4a16b17" TYPE="xfs"

/dev/sdb1: UUID="619dc6d9-1b0b-4a9a-9df5-bfc343fb8d6e" TYPE="swap"

/dev/sdb2: UUID="6517c68b-3671-42e7-9f37-e2fb9a549322" SEC_TYPE="ext2" TYPE="ext3"

/dev/sdb3: LABEL="opt" UUID="8b885f83-0d2c-4fe5-a4f1-dc678a9dec5a" TYPE="ext4"

Each filesystem type will have a utility that you can use to create or modify the label for the filesystem. For ext filesystems, you can use the e2label command. Simply pass the path to the device as the first argument and the label as the second argument. Here is how to add the label "opt" to the /dev/sdb3 device.

# e2label /dev/sdb3 opt

Summary

In this chapter, you learned what partition tables are and some of the reasons to use partitions. You also learned about the GPT and MBR partition tables. This chapter covered mount points and how they are simply directories that are used to access the data on a partition. You also learned how to create partitions using the fdisk utility.

Creating file systems with the mkfs command was covered as well as how to mount those file systems with the mount command. Once a file system is mounted you learned how to view the disk usage with the df command. Next, we talked about using the umount command to unmount file systems. You also learned how to prepare swap space using the mkswp command and how to enable it with swapon.

Quiz

1. Partitioning a disk allows you to allocate different sections of the disk for different purposes.

1. True

2. False

2. How many primary partitions does the MBR partitioning scheme allow?

1. 2

2. 3

3. 4

4. Unlimited

3. A mount point is simply a directory that is used to access data on a partition.

1. True

2. False

4. These two commands perform the same task:

mkfs -t ext4 /dev/sdb3

mkfs.ext4 /dev/sdb3

1. True

2. False

5. Which command is used to unmount a filesystem.

1. unmount

2. umount

3. dismount

6. The /etc/filetab file controls where devices are mounted on a Linux system and what options to use when mounting those devices.

1. True

2. False

7. Which command or commands can be used to view a UUID?

1. Only the uuid command.

2. Only the lsblk command.

3. Only the blkid command.

4. Both the lsblk and blkid commands.

8. What command prepares a swap partition for use. Afterwards, you can enable the swap partition with the swapon command.

1. mkswap

2. mkswp

3. makeswap

Quiz Answers

1. A

2. C

3. A

4. A

5. B

6. B

7. D

8. A