102.1 Lesson 1
Certificate: |
LPIC-1 |
---|---|
Version: |
5.0 |
Topic: |
102 Linux Installation and Package Management |
Objective: |
102.1 Design hard disk layout |
Lesson: |
1 of 1 |
Introduction
To succeed in this objective, you need to understand the relationship between disks, partitions, filesystems and volumes.
Think of a disk (or storage device, since modern devices do not contain any “disks” at all) as a “physical container” for your data.
Before a disk can be used by a computer it needs to be partitioned. A partition is a logical subset of the physical disk, like a logical “fence”. Partitioning is a way to “compartmentalize” information stored on the disk, separating, for example, operating system data from user data.
Every disk needs at least one partition, but can have multiple partitions if needed, and information about them is stored in a partition table. This table includes information about the first and last sectors of the partition and its type, as well as further details on each partition.
Inside each partition there is a filesystem. The filesystem describes the way the information is actually stored on the disk. This information includes how the directories are organized, what is the relationship between them, where is the data for each file, etc.
Partitions cannot span multiple disks. But using the Logical Volume Manager (LVM) multiple partitions can be combined, even across disks, to form a single logical volume.
Logical volumes abstract the limitations of the physical devices and let your work with “pools” of disk space that can be combined or distributed in a much more flexible way than traditional partitions. LVM is useful in situations where you would need to add more space to a partition without having to migrate the data to a larger device.
In this objective you will learn how to design a disk partitioning scheme for a Linux system, allocating filesystems and swap space to separate partitions or disks when needed.
How to create and manage partitions and filesystems will be discussed in other lessons. We will discuss an overview of LVM in this objective, but a detailed explanation is out of the scope.
Mount Points
Before a filesystem can be accessed on Linux it needs to be mounted. This means attaching the filesystem to a specific point in your system’s directory tree, called a mount point.
When mounted, the contents of the filesystem will be available under the mount point. For example, imagine you have a partition with your users' personal data (their home directories), containing the directories /john
, /jack
and /carol
. When mounted under /home
, the contents of those directories will be available under /home/john
, /home/jack
and /home/carol
.
The mount point must exist before mounting the filesystem. You cannot mount a partition under /mnt/userdata
if this directory does not exist. However if the directory does exist and contains files, those files will be unavailable until you unmount the filesystem. If you list the contents of the directory, you will see the files stored on the mounted filesystem, not the original contents of the directory.
Filesystems can be mounted anywhere you want. However, there are some good practices that should be followed to make system administration easier.
Traditionally, /mnt
was the directory under which all external devices would be mounted and a number of pre-configured anchor points for common devices, like CD-ROM drives (/mnt/cdrom
) and floppy disks (/mnt/floppy
) existed under it.
This has been superseded by /media
, which is now the default mount point for any user-removable media (e.g. external disks, USB flash drives, memory card readers, optical disks, etc.) connected to the system.
On most modern Linux distributions and desktop environments, removable devices are automatically mounted under /media/USER/LABEL
when connected to the system, where USER
is the username and LABEL
is the device label. For example, a USB flash drive with the label FlashDrive
connected by the user john
would be mounted under /media/john/FlashDrive/
. The way this is handled is different depending on the desktop environment.
That being said, whenever you need to manually mount a filesystem, it is good practice to mount it under /mnt
. The specific commands to control the mounting and unmounting of filesystems under Linux will be discussed in another lesson.
Keeping Things Separated
On Linux, there are some directories that you should consider keeping on separate partitions. There are many reasons for this: for example, by keeping bootloader-related files (stored on /boot
) on a boot partition, you ensure your system will still be able to boot in case of a crash on the root filesystem.
Keeping user’s personal directories (under /home
) on a separate partition makes it easier to reinstall the system without the risk of accidentally touching user data. Keeping data related to a web or database server (usually under /var
) on a separate partition (or even a separate disk) makes system administration easier should you need to add more disk space for those use cases.
There may even be performance reasons to keep certain directories on separate partitions. You may want to keep the root filesystem (/
) on a speedy SSD unit, and bigger directories like /home
and /var
on slower hard disks which offer much more space for a fraction of the cost.
The Boot Partition (/boot
)
The boot partition contains files used by the bootloader to load the operating system. On Linux systems the bootloader is usually GRUB2 or, on older systems, GRUB Legacy. The partition is usually mounted under /boot
and its files are stored in /boot/grub
.
Technically a boot partition is not needed, since in most cases GRUB can mount the root partition (/
) and load the files from a separate /boot
directory.
However, a separate boot partition may be desired for safety (ensuring the system will boot even in case of a root filesystem crash), or if you wish to use a filesystem which the bootloader cannot understand in the root partition, or if it uses an unsupported encryption or compression method.
The boot partition is usually the first partition on the disk. This is because the original IBM PC BIOS addressed disks using cylinders, heads and sectors (CHS), with a maximum of 1024 cylinders, 256 heads and 63 sectors, resulting in a maximum disk size of 528 MB (504 MB under MS-DOS). This means that anything past this mark would not be accessible on legacy systems, unless a different disk addressing scheme (like Logical Block Addressing, LBA) was used.
So for maximum compatibility, the boot partition is usually located at the start of the disk and ends before cylinder 1024 (528 MB), ensuring that no matter what, the machine will be always able to load the kernel.
Since the boot partition only stores the files needed by the bootloader, the initial RAM disk and kernel images, it can be quite small by today’s standards. A good size is around 300 MB.
The EFI System Partition (ESP)
The EFI System Partition (ESP) is used by machines based on the Unified Extensible Firmware Interface (UEFI) to store boot loaders and kernel images for the operating systems installed.
This partition is formatted in a FAT-based filesystem. On a disk partitioned with a GUID Partition Table it has a globally unique identifier of C12A7328-F81F-11D2-BA4B-00A0C93EC93B
. If the disk was formatted under the MBR partitioning scheme the partition ID is 0xEF
.
On machines running Microsoft Windows this partition is usually the first one on the disk, although this is not required. The ESP is created (or populated) by the operating system upon installation, and on a Linux system is mounted under /boot/efi
.
The /home
Partition
Each user in the system has a home directory to store personal files and preferences, and most of them are located under /home
. Usually the home directory is the same as the username, so the user John would have his directory under /home/john
.
However there are exceptions. For example the home directory for the root user is /root
and some system services may have associated users with home directories elsewhere.
There is no rule to determine the size of a partition for the /home
directory (the home partition). You should take into account the number of users in the system and how it will be used. A user which only does web browsing and word processing will require less space than one who works with video editing, for example.
Variable Data (/var
)
This directory contains “variable data”, or files and directories the system must be able to write to during operation. This includes system logs (in /var/log
), temporary files (/var/tmp
) and cached application data (in /var/cache
).
/var/www/html
is also the default directory for the data files for the Apache Web Server and /var/lib/mysql
is the default location for database files for the MySQL server. However, both of these can be changed.
One good reason for putting /var
in a separate partition is stability. Many applications and processes write to /var
and subdirectories, like /var/log
or /var/tmp
. A misbehaved process may write data until there is no free space left on the filesystem.
If /var
is under /
this may trigger a kernel panic and filesystem corruption, causing a situation that is difficult to recover from. But if /var
is kept under a separate partition, the root filesystem will be unaffected.
Like in /home
there is no universal rule to determine the size of a partition for /var
, as it will vary with how the system is used. On a home system, it may take only a few gigabytes. But on a database or web server much more space may be needed. In such scenarios, it may be wise to put /var
on a partition on a different disk than the root partition adding an extra layer of protection against physical disk failure.
Swap
The swap partition is used to swap memory pages from RAM to disk as needed. This partition needs to be of a specific type, and set-up with a proper utility called mkswap
before it can be used.
The swap partition cannot be mounted like the others, meaning that you cannot access it like a normal directory and peek at its contents.
A system can have multiple swap partitions (though this is uncommon) and Linux also supports the use of swap files instead of partitions, which can be useful to quickly increase swap space when needed.
The size of the swap partition is a contentious issue. The old rule from the early days of Linux (“twice the amount of RAM”) may not apply anymore depending on how the system is being used and the amount of physical RAM installed.
On the documentation for Red Hat Enterprise Linux 7, Red Hat recommends the following:
Amount of RAM | Recommended Swap Size | Recommended Swap Size with Hibernation |
---|---|---|
< 2 GB of RAM |
2x the amount of RAM |
3x the amount of RAM |
2-8 GB of RAM |
Equal to the amount of RAM |
2x the amount of RAM |
8-64 GB of RAM |
At least 4 GB |
1.5x the amount of RAM |
> 64 GB of RAM |
At least 4 GB |
Not recommended |
Of course the amount of swap can be workload dependent. If the machine is running a critical service, such as a database, web or SAP server, it is wise to check the documentation for these services (or your software vendor) for a recommendation.
Note
|
For more on creating and enabling swap partitions and swap files, see Objective 104.1 of LPIC-1. |
LVM
We have already discussed how disks are organized into one or more partitions, with each partition containing a filesystem which describes how files and associated metadata are stored. One of the downsides of partitioning is that the system administrator has to decide beforehand how the available disk space on a device will be distributed. This can present some challenges later, if a partition requires more space than originally planned. Of course partitions can be resized, but this may not be possible if, for example, there is no free space on the disk.
Logical Volume Management (LVM) is a form of storage virtualization that offers system administrators a more flexible approach to managing disk space than traditional partitioning. The goal of LVM is to facilitate managing the storage needs of your end users. The basic unit is the Physical Volume (PV), which is a block device on your system like a disk partition or a RAID array.
PVs are grouped into Volume Groups (VG) which abstract the underlying devices and are seen as a single logical device, with the combined storage capacity of the component PVs.
Each volume in a Volume Group is subdivided into fixed-sized pieces called extents. Extents on a PV are called Physical Extents (PE), while those on a Logical Volume are Logical Extents (LE). Generally, each Logical Extent is mapped to a Physical Extent, but this can change if features like disk mirroring are used.
Volume Groups can be subdivided into Logical Volumes (LVs), which functionally work in a similar way to partitions but with more flexibility.
The size of a Logical Volume, as specified during its creation, is in fact defined by the size of the physical extents (4 MB by default) multiplied by the number of extents on the volume. From this it is easy to understand that to grow a Logical Volume, for example, all that the system administrator has to do is add more extents from the pool available in the Volume Group. Likewise, extents can be removed to shrink the LV.
After a Logical Volume is created it is seen by the operating system as a normal block device. A device will be created in /dev
, named as /dev/VGNAME/LVNAME
, where VGNAME
is the name of the Volume Group, and LVNAME
is the name of the Logical Volume.
These devices can be formatted with a desired filesystem using standard utilities (like mkfs.ext4
, for example) and mounted using the usual methods, either manually with the mount
command or automatically by adding them to the /etc/fstab
file.
Guided Exercises
-
On Linux systems, where are the files for the GRUB bootloader stored?
-
Where should the boot partition end to ensure that a PC will always be able to load the kernel?
-
Where is the EFI partition usually mounted?
-
When manually mounting a filesystem, under which directory should it usually be mounted?
Explorational Exercises
-
What is the smallest unit inside of a Volume Group?
-
How is the size of a Logical Volume defined?
-
On a disk formatted with the MBR partitioning scheme, which is the ID of the EFI System Partition?
-
Besides swap partitions, how can you quickly increase swap space on a Linux system?
Summary
In this lesson you learned about partitioning, which directories are usually kept in separate partitions and why this is done. Also, we discussed an overview of LVM (Logical Volume Management) and how it can offer a more flexible way to allocate your data and disk space compared to traditional partitioning.
The following files, terms and utilities have been discussed:
/
-
The Linux root filesystem.
/var
-
The standard location for “variable data”, data that can shrink and grow over time.
/home
-
The standard parent directory for home directories of regular users on a system.
/boot
-
The standard location for the boot loader files, Linux kernel and initial RAM disk.
- EFI System Partition (ESP)
-
Used by systems that have UEFI implemented for the storage of the system’s boot files.
- Swap space
-
Used to swap out kernel memory pages when RAM is heavily used.
- Mount points
-
Directory locations where a device (such as a hard disk) will be mounted to.
- Partitions
-
Divisions on a hard disk.
Answers to Guided Exercises
-
On Linux systems, where are the files for the GRUB bootloader stored?
Under
/boot/grub
. -
Where should the boot partition end to ensure that a PC will always be able to load the kernel?
Before cylinder 1024.
-
Where is the EFI partition usually mounted?
Under
/boot/efi
. -
When manually mounting a filesystem, under which directory should it usually be mounted?
Under
/mnt
. However, this is not mandatory. You can mount a partition under any directory you want.
Answers to Explorational Exercises
-
What is the smallest unit inside of a Volume Group?
Volume Groups are subdivided into extents.
-
How is the size of a Logical Volume defined?
By the size of the physical extents multiplied by the number of extents on the volume.
-
On a disk formatted with the MBR partitioning scheme, which is the ID of the EFI System Partition?
The ID is
0xEF
. -
Besides swap partitions, how can you quickly increase swap space on a Linux system?
Swap files can be used.