Handbook:AMD64/Blocks/Disks

Block devices
Let's take a good look at disk-oriented aspects of Gentoo Linux and Linux in general, including Linux filesystems, partitions and block devices. Once the ins and outs of disks and filesystems are understood, we will set up partitions and filesystems for the Gentoo Linux installation.

To begin, let's look at block devices. The most famous block device is probably the one that represents the first drive in a Linux system, namely. SCSI and Serial ATA drives are both labeled ; even IDE drives are labeled with the new libata framework in the kernel. When using the old device framework, then the first IDE drive is.

The block devices above represent an abstract interface to the disk. User programs can use these block devices to interact with your disk without worrying about whether the drives are IDE, SCSI or something else. The program can simply address the storage on the disk as a bunch of contiguous, randomly-accessible 512-byte blocks.

Partitions
Although it is theoretically possible to use a full disk to house a Linux system, this is almost never done in practice. Instead, full disk block devices are split up in smaller, more manageable block devices. On AMD64 systems, these are called partitions. There are currently two standard partitioning technologies in use: MBR and GPT.

MBR
The MBR (Master Boot Record) setup 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, to ).

To support more partitions, one of the primary partitions can be marked as an extended partition. This partition can then contain logical partitions (partitions within a partition).

Each partition is limited to 2 TB in size (due to the 32-bit identifiers). Also, the MBR setup does not provide any backup-MBR, so if an application or user overwrites the MBR, all partition information is lost.

GPT
The GPT (GUID Partition table) setup uses 64-bit identifiers for the partitions. The location in which it stores the partition information is also much bigger than the 512 bytes of an MBR, and there is no limit on the amount of partitions. Also the size of a partition is bounded by a much greater limit (almost 8 ZB - yes, zettabytes).

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 MBR here.

GPT also has the advantage that it has a backup GPT at the end of the disk, which can be used to recover damage of the primary GPT at the beginning. GPT also carries CRC32 checksums to detect errors in the header and partition tables.

GPT or MBR
From the description above, one might think that using GPT should always be the recommended approach. But there are a few caveats with this.

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 EFI mode if it detects a GPT partition label.

Some buggy BIOSes or EFIs configured to boot in BIOS/CSM/legacy mode might also have problems with booting from GPT labeled disks. If that is the case, it might be possible to work around the problem by adding the boot/active flag on the protective MBR partition which has to be done through  (  understands the GPT tables and would not show the protective MBR partition).

In this case, launch  and toggle the flag using  on the first partition. Then write the changes to the disk and exit the   application:

Advanced storage
The AMD64 Installation CDs provide support for LVM2. LVM2 increases the flexibility offered by the partitioning setup. During the installation instructions, we will focus on "regular" partitions, but it is still good to know LVM2 is supported as well.

Default partitioning scheme
Throughout the remainder of the handbook, the following partitioning scheme is used. If this suffices, then the reader can immediately jump to Default: Using parted to partition the disk or Alternative: Using fdisk to partition the disk. Both are partitioning tools,  is well known and stable,   is a bit more recent but supports partitions larger than 2TB).

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

How many and how big
The number of partitions is highly dependent on the environment. For instance, if there are lots of users, then it is advised to have separate as it increases security and makes backups easier. If Gentoo is being installed to perform as a mail server, then should be separate as all mails are stored inside. A good choice of filesystem will then maximize the performance. Game servers will have a separate as most gaming servers are installed there. The reason is similar for : security and backups. In most situations, is to be kept big: not only will it contain the majority of applications, the Portage tree alone takes around 500 Mbyte excluding the various sources that are stored in it.

As can be seen, it very much depends on what the administrator want to achieve. 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 more with multiple disks than it is with multiple partitions)
 * Security can be enhanced by mounting some partitions or volumes read-only,  (setuid bits are ignored),   (executable bits are ignored) etc.

However, multiple partitions have disadvantages as well. If not configured properly, the system might have lots of free space on one partition and none on another. Another nuisance is that separate partitions - especially for important mount points like or  - often require the administrator to boot with an initramfs to mount the partition before other boot scripts start. This isn't always the case though, so results may vary.

There is also a 15-partition limit for SCSI and SATA unless the disk uses GPT labels.

What about swap space
There is no perfect value for the swap partition. The purpose of swap space is to provide disk storage to the kernel when internal 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), freeing memory. Of course, if that memory is suddenly needed, these pages need to be put back in memory (page-in) which will take a while (as disks are very slow compared to internal memory).

When the system is not going to run memory intensive applications or the system has lots of memory available, then it probably does not need much swap space. However, swap space is also used to store the entire memory in case of hibernation. If the system is going to need hibernation, then a bigger swap space necessary, often at least the amount of memory installed in the system.

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.

For completeness, the BIOS boot partition is needed when GPT partition layout is used with GRUB2, or when the MBR partition layout is used with GRUB2 when the first partition starts earlier than the 1 MB location on the disk.

Default: Using parted to partition the disk
In this chapter, we the example partition layout mentioned earlier in the instructions will be used:

Change the partition layout according to personal preference.

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

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  and press return.

Setting the GPT label
Most disks on x86/amd64 are prepared using an msdos label. Using, the command to put a GPT label on the disk is  :

To have the disk with MBR layout, use.

Removing all partitions with parted
If this isn't done yet (for instance through the  operation earlier, or because the disk is a freshly formatted one), first remove all existing partitions from the disk. Type  to view the current partitions, and   where NUMBER is the partition to remove.

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  which stages them, allowing a user to "undo" his changes before saving or exiting fdisk).

Creating the partitions
Now create the partitions. Creating partitions with  isn't very difficult - all we need to do is inform parted about 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 ae 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):

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

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

The end result looks like so:

Use the  command to exit parted.

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

Change the partition layout according to personal preference.

Viewing the current partition layout
is a popular and powerful tool to split a disk into partitions. Fire up  against the disk (in our example, we use ):

Type to display the disk's current partition configuration:

This particular disk is 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 to delete a partition. For instance, to delete an existing :

The partition has now been scheduled for deletion. It will no longer show up when printing the list of partitions (, 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 immediately and hit enter and the partition will not be deleted.

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

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 to create a new partition, then  to select a primary partition, followed by  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:

Mark the partition for EFI purposes:

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

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

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

Creating the swap partition
To create the swap partition, type to create a new partition, then  to tell fdisk to create a primary partition. Then type to create the third primary partition,. When prompted for the first sector, hit. 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 to set the partition type,  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 to create a new partition, then  to tell   to create a primary partition. Then type to create the fourth primary partition,. When prompted for the first sector, hit. When prompted for the last sector, hit to create a partition that takes up the rest of the remaining space on the disk. After completing these steps, typing should display a partition table that looks similar to this:

Saving the partition layout
To save the partition layout and exit, type.

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