Difference between revisions of "Handbook:AMD64/Blocks/Disks"

From Gentoo Wiki
Jump to:navigation Jump to:search
(move GPT above MBR)
Line 91: Line 91:
  
 
<!--T:135-->
 
<!--T:135-->
If this suffices and the reader going the GPT route they can immediately jump to [[#Default: Using parted to partition the disk|Default: Using parted to partition the disk]]. Those who are still interested in MBR (hey - it happens!) and using the example layout can jump to [[#Alternative: Using fdisk to partition the disk|Alternative: Using fdisk to partition the disk]].
+
If this suffices as information, the advanced reader can directly skip ahead to the actual partitioning.
  
 
<!--T:136-->
 
<!--T:136-->
Both {{c|fdisk}} and {{c|parted}} are partitioning utilities. {{c|fdisk}} is well known, stable, and recommended for the MBR partition layout while {{c|parted}} was one of the first Linux block device management utilities to support GPT partitions. Those who like the user interface of {{c|fdisk}} can use {{c|gdisk}} (GPT fdisk) as an alternative to {{c|parted}}.
+
Both {{c|fdisk}} and {{c|parted}} are partitioning utilities. {{c|fdisk}} is well known, stable, and recommended for the MBR partition layout. {{c|parted}} was one of the first Linux block device management utilities to support GPT partitions, and provides an alternative. Here, we use {{c|fdisk}} since it has a better text-based user interface.
  
 
<!--T:117-->
 
<!--T:117-->

Revision as of 16:47, 13 March 2021


Warning, this page is a work in progress by dilfridge (talk | contribs). Treat its contents with caution.

Partition tables

Although it is theoretically possible to use a raw, unpartitioned disk to house a Linux system (when creating a btrfs RAID for example), this is almost never done in practice. Instead, disk block devices are split up into smaller, more manageable block devices. On amd64 systems, these are called partitions. There are currently two standard partitioning technologies in use: MBR (sometimes also called DOS disklabel) and GPT; these are tied to the two boot process types: legacy BIOS boot and UEFI.

GPT

The GPT (GUID Partition Table) setup uses 64-bit identifiers for the partitions. The location in which it stores the partition information is much bigger than the 512 bytes of the MBR partition table (DOS disklabel), which means there is practically no limit on the amount of partitions for a GPT disk. Also the size of a partition is bounded by a much greater limit (almost 8 ZiB - yes, zebibytes).

When a system's software interface between the operating system and firmware is UEFI (instead of BIOS), GPT is almost mandatory as compatibility issues will arise with DOS disklabel.

GPT also takes advantage of checksumming and redundancy. It carries CRC32 checksums to detect errors in the header and partition tables and has a backup GPT at the end of the disk. This backup table can be used to recover damage of the primary GPT near the beginning of the disk.

Important
There are a few caveats regarding GPT:
  • Using GPT on a BIOS-based computer works, but then one cannot dual-boot with a Microsoft Windows operating system. The reason is that Microsoft Windows will boot in UEFI mode if it detects a GPT partition label.
  • Some buggy (old) motherboard firmware configured to boot in BIOS/CSM/legacy mode might also have problems with booting from GPT labeled disks.

MBR (DOS) boot sector

The Master boot record boot sector was first introduced in 1983 with PC DOS 2.x. MBR uses 32-bit identifiers for the start sector and length of the partitions, and supports three partition types: primary, extended, and logical. Primary partitions have their information stored in the master boot record itself - a very small (usually 512 bytes) location at the very beginning of a disk. Due to this small space, only four primary partitions are supported (for instance, /dev/sda1 to /dev/sda4).

In order to support more partitions, one of the primary partitions in the MBR can be marked as an extended partition. This partition can then contain additional logical partitions (partitions within a partition).

Important
Although still supported by most motherboard manufacturers, MBR boot sectors and their associated partitioning limitations are considered legacy. Unless working with hardware that is pre-2010, it best to partition a disk with GUID Partition Table. Readers who must proceed with setup type should knowingly acknowledge the following information:
  • Most post-2010 motherboards consider using MBR boot sectors a legacy (supported, but not ideal) boot mode.
  • Due to using 32-bit identifiers, partition tables in the MBR cannot address storage space that is larger than 2 TiBs in size.
  • Unless a extended partition is created, MBR supports a maximum of four partitions.
  • This setup does not provide a backup boot sector, so if something overwrites the partition table, all partition information will be lost.
That said, MBR and BIOS boot is still frequently used in virtualized cloud environments such as AWS.

The Handbook authors suggest using GPT whenever possible for Gentoo installations.

Advanced storage

LVM

The amd64 Installation CDs provide support for Logical Volume Manager (LVM). LVM increases the flexibility offered by the partitioning setup. It allows to combine partitions and disks into volume groups and define RAID groups or caches on fast SSDs for slow HDs. The installation instructions below will focus on "regular" partitions, but it is good to know LVM is supported if that route is desired. Visit the LVM article for more details. Newcomers beware: although fully supported, LVM is outside the scope of this guide.

Btrfs RAID

Btrfs also has the ability to create filesystems across multiple devices. Btrfs filesystems generated in this way can act in the following modes: raid0, raid1, raid10, raid5, and raid6. RAID modes 5 and 6 have improved considerably, but are still considered unstable. After a multiple device filesystem has been created, new devices can be added and old devices removed with a few commands. Btrfs takes more involvement than other filesystems making it not as friendly to beginners.

Default partitioning scheme

Throughout the remainder of the handbook, we will discuss and explain two cases: 1) GPT partition table and UEFI boot, and 2) MBR partition table and legacy BIOS boot. While it is possible to mix and match, that goes beyond the scope of this manual. As already stated above, installations on modern hardware should use GPT partition table and UEFI boot; as an exception from this rule, MBR and BIOS boot is still frequently used in virtualized (cloud) environments.

The following partitioning scheme will be used as a simple example layout:

Partition Filesystem Size Description
/dev/sda1 fat32 (UEFI) or ext2 (BIOS) 256M Boot/EFI system partition
/dev/sda2 (swap) RAM size * 2 Swap partition
/dev/sda3 ext4 Rest of the disk Root partition

If this suffices as information, the advanced reader can directly skip ahead to the actual partitioning.

Both fdisk and parted are partitioning utilities. fdisk is well known, stable, and recommended for the MBR partition layout. parted was one of the first Linux block device management utilities to support GPT partitions, and provides an alternative. Here, we use fdisk since it has a better text-based user interface.

Before going to the creation instructions, the first set of sections will describe in more detail how partitioning schemes can be created and mention some common pitfalls.


Designing a partition scheme

How many partitions and how big?

The design of disk partition layout is highly dependent on the demands of the system and the file system(s) applied to the device. If there are lots of users, then it is advised to have /home on a separate partition which will increase security and make backups and other types of maintenance easier. If Gentoo is being installed to perform as a mail server, then /var should be a separate partition as all mails are stored inside the /var directory. Game servers may have a separate /opt partition since most gaming server software is installed therein. The reason for these recommendations is similar to the /home directory: security, backups, and maintenance.

In most situations on Gentoo, /usr and /var should be kept relatively large in size. /usr hosts the majority of applications available on the system and the Linux kernel sources (under /usr/src). By default, /var hosts the Gentoo ebuild repository (located at /var/db/repos/gentoo) which, depending on the file system, generally consumes around 650 MiB of disk space. This space estimate excludes the /var/cache/distfiles and /var/cache/binpkgs directories, which will gradually fill with source files and (optionally) binary packages respectively as they are added to the system.

How many partitions and how big very much depends on considering the trade-offs and choosing the best option for the circumstance. Separate partitions or volumes have the following advantages:

  • Choose the best performing filesystem for each partition or volume.
  • The entire system cannot run out of free space if one defunct tool is continuously writing files to a partition or volume.
  • If necessary, file system checks are reduced in time, as multiple checks can be done in parallel (although this advantage is realized more with multiple disks than it is with multiple partitions).
  • Security can be enhanced by mounting some partitions or volumes read-only, nosuid (setuid bits are ignored), noexec (executable bits are ignored), etc.


However, multiple partitions have certain disadvantages as well:

  • If not configured properly, the system might have lots of free space on one partition and little free space on another.
  • A separate partition for /usr/ may require the administrator to boot with an initramfs to mount the partition before other boot scripts start. Since the generation and maintenance of an initramfs is beyond the scope of this handbook, we recommend that newcomers do not use a separate partition for /usr/.
  • There is also a 15-partition limit for SCSI and SATA unless the disk uses GPT labels.
Note
Installations that intend to use systemd as the service and init system must have the /usr directory available at boot, either as part of the root filesystem or mounted via an initramfs.

What about swap space?

Recommendations for swap space size
RAM size Suspend support? Hibernation support?
2 GB or less 2 * RAM 3 * RAM
2 to 8 GB RAM amount 2 * RAM
8 to 64 GB 8 GB minimum, 16 maximum 1.5 * RAM
64 GB or greater 8 GB minimum Hibernation not recommended! Hibernation is not recommended for systems with very large amounts of memory. While possible, the entire contents of memory must be written to disk in order to successfully hibernate. Writing tens of gigabytes (or worse!) out to disk can can take a considerable amount of time, especially when rotational disks are used. It is best to suspend in this scenario.

There is no perfect value for swap space size. The purpose of the space is to provide disk storage to the kernel when internal dynamic memory (RAM) is under pressure. A swap space allows for the kernel to move memory pages that are not likely to be accessed soon to disk (swap or page-out), which will free memory in RAM for the current task. Of course, if the pages swapped to disk are suddenly needed, they will need to be put back in memory (page-in) which will take considerably longer than reading from RAM (as disks are very slow compared to internal memory).

When a system is not going to run memory intensive applications or has lots of RAM available, then it probably does not need much swap space. However do note in case of hibernation that swap space is used to store the entire contents of memory (likely on desktop and laptop systems rather than on server systems). If the system requires support for hibernation, then swap space larger than or equal to the amount of memory is necessary.

As a general rule for RAM amounts less than 4 GB, the swap space size is recommended to be twice the internal memory (RAM). For systems with multiple hard disks, it is wise to create one swap partition on each disk so that they can be utilized for parallel read/write operations. The faster a disk can swap, the faster the system will run when data in swap space must be accessed. When choosing between rotational and solid state disks, it is better for performance to put swap on the solid state hardware.

It is worth noting that swap files can be used as an alternative to swap partitions; this is mostly helpful for systems with very limited disk space.


What is the BIOS boot partition?

A BIOS boot partition is a very small (1 to 2 MB) partition in which boot loaders like GRUB2 can put additional data that doesn't fit in the allocated storage (a few hundred bytes in case of MBR) and cannot be placed elsewhere.

Such partitions are not always necessary, but considering the low space consumption and the difficulties we have with documenting the plethora of partitioning differences otherwise, it is recommended to create it in either case.

The BIOS boot partition is needed when a GPT partition layout is used with GRUB2 in BIOS/Legacy mode. It is not required when booting in EFI/UEFI mode.

What is the EFI System Partition (ESP)?

When installing Gentoo on a system that uses UEFI to boot the operating system (instead of BIOS), then it is important that an EFI System Partition (ESP) is created. The instructions for parted below contain the necessary pointers to correctly handle this operation.

The ESP must be a FAT variant (sometimes shown as vfat on Linux systems). The official UEFI specification denotes FAT12, 16, or 32 filesystems will be recognized by the UEFI firmware, although FAT32 is recommended for the ESP. Proceed in formatting the ESP as FAT32:

root #mkfs.fat -F 32 /dev/sda1
Important
If a FAT variant is not used for the ESP, the system's UEFI firmware is not guaranteed to find the bootloader (or Linux kernel) and most likely be unable to boot the system!

The EFI system partition is not required when booting in BIOS/Legacy mode.

Default: Using parted to partition the disk

In this chapter, the example partition layout mentioned earlier in the instructions will be used:

Partition Description
/dev/sda1 BIOS boot partition
/dev/sda1 Boot partition
/dev/sda3 Swap partition
/dev/sda4 Root partition

Change the partition layout according to personal preference.

Viewing the current partition layout with parted

The parted application offers a simple interface for partitioning the disks and supports very large partitions (more than 2 TB). Fire up parted against the disk (in our example, we use /dev/sda). It is recommended to ask parted to use optimal partition alignment:

root #parted -a optimal /dev/sda
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.

Alignment means that partitions are started on well-known boundaries within the disk, ensuring that operations on the disk from the operating system level (retrieve pages from the disk) use the least amount of internal disk operations. Misaligned partitions might require the disk to fetch two pages instead of one even if the operating system asked for a single page.

To find out about all options supported by parted, type help and press return.

Setting the GPT label

Most disks on the x86 or amd64 architectures are prepared using an msdos label. Using parted, the command to put a GPT label on the disk is mklabel gpt:

Warning
Changing the partition type will remove all partitions from the disk. All data on the disk will be lost.
(parted)mklabel gpt

To have the disk with MBR layout, use mklabel msdos.

Removing all partitions with parted

If this isn't done yet (for instance through the mklabel operation earlier, or because the disk is a freshly formatted one), first remove all existing partitions from the disk. Type print to view the current partitions, and rm <N> where <N> is the number of the partition to remove.

(parted)rm 2

Do the same for all other partitions that aren't needed. However, make sure to not make any mistakes here - parted executes the changes immediately (unlike fdisk which stages them, allowing a user to "undo" his changes before saving or exiting fdisk).

Creating the partitions

Now parted will be used to create the partitions with the following settings:

  • The partition type to use. This usually is primary. If the msdos partition label is used, keep in mind that there can be no more than 4 primary partitions. If more than 4 partitions are needed, make one of the first four partitions extended and create logical partitions inside it.
  • The start location of a partition (which can be expressed in MB, GB, ...)
  • The end location of the partition (which can be expressed in MB, GB, ...)

First, tell parted that the size unit we work with is megabytes (actually mebibytes, abbreviated as MiB which is the "standard" notation, but we will use MB in the text throughout as it is much more common):

(parted)unit mib

Now create a 2 MB partition that will be used by the GRUB2 boot loader later. Use the mkpart command for this, and inform parted to start from 1 MB and end at 3 MB (creating a partition of 2 MB in size).

(parted)mkpart primary 1 3
(parted)name 1 grub
(parted)set 1 bios_grub on
(parted)print
Model: Virtio Block Device (virtblk)
Disk /dev/sda: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
  
Number   Start      End      Size     File system  Name   Flags
 1       1.00MiB    3.00MiB  2.00MiB               grub   bios_grub

Do the same for the boot partition (128 MB), swap partition (in the example, 512 MB) and the root partition that spans the remaining disk (for which the end location is marked as -1, meaning the end of the disk minus one MB, which is the farthest a partition can go).

(parted)mkpart primary 3 131
(parted)name 2 boot
(parted)mkpart primary 131 643
(parted)name 3 swap
(parted)mkpart primary 643 -1
(parted)name 4 rootfs

When using the UEFI interface to boot the system (instead of BIOS), mark the boot partition as the EFI System Partition. Parted does this automatically when the boot option is set on the partition:

(parted)set 2 boot on

The end result looks like so:

(parted)print
Model: Virtio Block Device (virtblk)
Disk /dev/sda: 20480MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
  
Number   Start      End      Size     File system  Name   Flags
 1       1.00MiB    3.00MiB  2.00MiB               grub   bios_grub
 2       3.00MiB    131MiB   128MiB                boot   boot
 3       131MiB     643MiB   512MiB                swap
 4       643MiB     20479MiB 19836MiB              rootfs
Note
On an UEFI installation, the boot and esp flags will show up on the boot partition.

Use the quit command to exit parted.

With partitioning completed, jump down to the section on creating file systems.

Alternative: Using fdisk to partition the disk

Note
Although recent fdisk should support GPT, it has still shown to have some issues with it. The instructions given below assume that the partition layout being used is MBR.

The following parts explain how to create the example partition layout using fdisk. The example partition layout was mentioned earlier:

Partition Description
/dev/sda1 BIOS boot partition
/dev/sda1 Boot partition
/dev/sda3 Swap partition
/dev/sda4 Root partition

Change the partition layout according to personal preference.

Viewing the current partition layout

fdisk is a popular and powerful tool to split a disk into partitions. Fire up fdisk against the disk (in our example, we use /dev/sda):

root #fdisk /dev/sda
Note
To use GPT support, add -t gpt. It is recommended to closely investigate the fdisk output in case more recent developments in fdisk change its default behavior of defaulting to MBR. The remainder of the instructions assume an MBR layout.

Use the p key to display the disk's current partition configuration:

Command (m for help):p
Disk /dev/sda: 240 heads, 63 sectors, 2184 cylinders
Units = cylinders of 15120 * 512 bytes
  
   Device Boot    Start       End    Blocks   Id  System
/dev/sda1   *         1        14    105808+  83  Linux
/dev/sda2            15        49    264600   82  Linux swap
/dev/sda3            50        70    158760   83  Linux
/dev/sda4            71      2184  15981840    5  Extended
/dev/sda5            71       209   1050808+  83  Linux
/dev/sda6           210       348   1050808+  83  Linux
/dev/sda7           349       626   2101648+  83  Linux
/dev/sda8           627       904   2101648+  83  Linux
/dev/sda9           905      2184   9676768+  83  Linux

This particular disk was configured to house seven Linux filesystems (each with a corresponding partition listed as "Linux") as well as a swap partition (listed as "Linux swap").

Removing all partitions with fdisk

First remove all existing partitions from the disk. Type d to delete a partition. For instance, to delete an existing /dev/sda1:

Command (m for help):d
Partition number (1-4): 1

The partition has now been scheduled for deletion. It will no longer show up when printing the list of partitions (p, but it will not be erased until the changes have been saved. This allows users to abort the operation if a mistake was made - in that case, type q immediately and hit Enter and the partition will not be deleted.

Repeatedly type p to print out a partition listing and then type d and the number of the partition to delete it. Eventually, the partition table will be empty:

Command (m for help):p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
  
Device Boot    Start       End    Blocks   Id  System

Now that the in-memory partition table is empty, we're ready to create the partitions.

Creating the BIOS boot partition

First create a very small BIOS boot partition. Type n to create a new partition, then p to select a primary partition, followed by 1 to select the first primary partition. When prompted for the first sector, make sure it starts from 2048 (which is needed for the boot loader) and hit Enter. When prompted for the last sector, type +2M to create a partition 2 Mbyte in size:

Note
The start from sector 2048 is a fail-safe in case the boot loader does not detect this partition as being available for its use.
Command (m for help):n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (64-10486533532, default 64): 2048
Last sector, +sectors +size{M,K,G} (4096-10486533532, default 10486533532): +2M

Mark the partition for UEFI purposes:

Command (m for help):t
Selected partition 1
Hex code (type L to list codes): 4
Changed system type of partition 1 to 4 (BIOS boot)
Note
Using UEFI with MBR partition layout is discouraged. If an UEFI capable system is used, please use GPT layout.

Creating the boot partition

Now create a small boot partition. Type n to create a new partition, then p to select a primary partition, followed by 2 to select the second primary partition. When prompted for the first sector, accept the default by hitting Enter. When prompted for the last sector, type +128M to create a partition 128 Mbyte in size:

Command (m for help):n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (5198-10486533532, default 5198): (Hit enter)
Last sector, +sectors +size{M,K,G} (4096-10486533532, default 10486533532): +128M

Now, when pressing p, the following partition printout is displayed:

Command (m for help):p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
  
   Device Boot    Start       End    Blocks   Id  System
/dev/sda1             1         3      5198+  ef  EFI (FAT-12/16/32)
/dev/sda2             3        14    105808+  83  Linux

Type a to toggle the bootable flag on a partition and select 2. After pressing p again, notice that an * is placed in the "Boot" column.

Creating the swap partition

To create the swap partition, type n to create a new partition, then p to tell fdisk to create a primary partition. Then type 3 to create the third primary partition, /dev/sda3. When prompted for the first sector, hit Enter. When prompted for the last sector, type +512M (or any other size needed for the swap space) to create a partition 512MB in size.

After all this is done, type t to set the partition type, 3 to select the partition just created and then type in 82 to set the partition type to "Linux Swap".

Creating the root partition

Finally, to create the root partition, type n to create a new partition, then p to tell fdisk to create a primary partition. Then type 4 to create the fourth primary partition, /dev/sda4. When prompted for the first sector, hit Enter. When prompted for the last sector, hit Enter to create a partition that takes up the rest of the remaining space on the disk. After completing these steps, typing p should display a partition table that looks similar to this:

Command (m for help):p
Disk /dev/sda: 30.0 GB, 30005821440 bytes
240 heads, 63 sectors/track, 3876 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
  
   Device Boot    Start       End    Blocks   Id  System
/dev/sda1             1         3      5198+  ef  EFI (FAT-12/16/32)
/dev/sda2   *         3        14    105808+  83  Linux
/dev/sda3            15        81    506520   82  Linux swap
/dev/sda4            82      3876  28690200   83  Linux

Saving the partition layout

To save the partition layout and exit fdisk, type w.

Command (m for help):w

With the partitions created, it is now time to put filesystems on them.