Handbook:X86/Blocks/Disks

From Gentoo Wiki
Jump to: navigation, search
This page is a translated version of the page Handbook:X86/Blocks/Disks and the translation is 100% complete.



Tablice partycji

Mimo, że teoretycznie jest możliwe użycie czystego, niepodzielonego na partycje dysku dla systemu Linuks (na przykład podczas tworzenia btrfs RAID), w praktyce prawie nigdy się tego nie robi. Zamiast tego, dyski są podzielone na mniejsze, łatwiejsze w zarządzaniu części. W architekturze x86 są one nazywane partycjami. Obecnie stosowane są dwie standardowe technologie partycjonowania: MBR i GPT.

MBR

MBR (Master Boot Record) to struktura, która używa 32-bitowych identyfikatorów dla sektora startowego i długości partycji, wspierająca trzy typy partycji: podstawowe, rozszerzone i logiczne. Informacja o partycjach podstawowych jest zapisana w samym Głównym Rekordzie Rozruchowym - bardzo mały rozmiar (zazwyczaj 512 bajtów) na samym początku dysku. Z powodu bardzo małego rozmiaru, są wspierane tylko cztery partycje podstawowe (dla przykładu, /dev/sda1 do /dev/sda4).

Aby posiadać wiecej partycji, jedna z podstawowych musi być oznaczona jako partycja rozszerzona. Taka partycja może zawierać inne partycje logiczne (partycje w partycji).

Important
Chociaż wciąż wspierana przez większość producentów, ta tablica partycji jest uważana za przestarzałą. Jeżeli nie pracujesz z sprzętem z przed 2010 roku, najlepiej partycjonować dysk używając Tablicy Patrtycji GUID. Czytelnicy, którzy nadal muszą korzystać z MBR, powinni zapoznać się z następującymi informacjami:
  • Większość płyt głównych wyprodukowanych po 2010 roku uznaje MBR za przestarzały (wspierany, lecz nie idealny) tryb rozruchowy.
  • Z powodu użycia 32-bitowych identyfikatorów, MBR nie potrafi obsłużyć dysków twardych o pojemności większej od 2TB.
  • Bez użycia partycji rozszerzonej, MBR wspiera utworzenie maksymalnie czterech partycji.
  • System partycjonowania MBR nie zapewnia kopii zapasowej, więc jeżeli aplikacja lub użytkownik nadpiszą MBR, wszystkie informacje o partycjach zostaną utracone.

Autorzy Podręcznika rekomendują użycie GPT ilekroć to możliwe dla instalacji Gentoo.

GPT

GPT (Tablica Partycji GUID) używa 64-bitowych identyfikatorów dla partycji. Obszar pamięci w którym przechowuje informacje o partycjach jest znacznie większy, niż zastosowane 512 bajtów w Głównym Rekordzie Rozruchowym (MBR), co oznacza praktyczny brak limitów ilości partycji dla dysku GPT. Dodatkowo, rozmiar pojedyńczej partycji ma o wiele wiekszy limit (prawie 8 ZiB - tak, zettabajtów).

Gdy interfejs oprogramowania systemowego między systemem operacyjnym a oprogramowaniem układowym to UEFI (zamiast BIOS), GPT jest prawie obowiązkowe, ponieważ pojawią się problemy ze zgodnością z MBR.

GPT wykorzystuje również sumy kontrolne i redundancję. Za pomocą sumy kontrolnej CRC32 sprawdza błędy w tablicach partycji i nagłówka oraz posiada kopię zapasową GPT na końcu dysku. Można ją użyć do odzyskania uszkodzenego podstawowego GPT w pobliżu początku dysku.

GPT czy MBR

Z powyższego opisu można wywnioskować, że używanie GPT powinno zawsze być rekomendowane, jednak istnieje kilka zastrzeżeń.

Korzystanie z GPT na komputerze z systemem BIOS działa, ale wtedy nie można uruchomić systemu operacyjnego Microsoft Windows (dual-boot). Dzieje się tak, ponieważ Microsoft Windows uruchomi się w trybie UEFI, jeżeli wykryje etykietę partycji GPT.

Some buggy motherboard firmware 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 fdisk with the -t dos option to force it to read the partition table using the MBR format.

In this case, launch fdisk and toggle the flag using the a key. Press 1 to select the first partition, then press the w key to write the changes to the disk and exit the fdisk application:

user $fdisk -t dos /dev/sda
Welcome to fdisk (util-linux 2.24.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
  
Command (m for help): a
Partition number (1-4): 1
  
Command (m for help): w

Korzystanie z UEFI

Podczas instalacji Gentoo na systemie wykorzystującym interfejs UEFI (zamiast BIOS) do uruchomienia systemu operacyjnego, bardzo ważne jest utworzenie Partycji Systemu EFI (ESP). Poniższe instrukcje dla parted zawierają niezbędne wskazówki, aby prawidłowo wykonać tą operację.

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/sda2
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!

Advanced storage

RAID Btrfs

As noted above, btrfs 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 in a few commands. Btrfs takes more involvement than other filesystems making it not as friendly to beginners.

ext4 filesytems can be converted into btrfs filesystems, which may be useful for those who'd like to install Gentoo with a stable, well tested filesystem and gradually increase their knowledge about newer filesystems such as btrfs by experimentation.

LVM

The x86 Installation CDs provide support for Logical Volume Manager (LVM). LVM increases the flexibility offered by the partitioning setup. 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.

Domyślny schemat partycjonowania

Throughout the remainder of the handbook, the following partitioning scheme will be used as a simple example layout:

Partition Filesystem Size Description
/dev/sda1 (bootloader) 2M BIOS boot partition
/dev/sda2 ext2 (or fat32 if UEFI is being used) 128M Boot/EFI system partition
/dev/sda3 (swap) 512M or higher Swap partition
/dev/sda4 ext4 Rest of the disk Root partition

If this suffices and the reader going the GPT route they can immediately jump to 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.

Both fdisk and parted are partitioning utilities. fdisk is well known, stable, and recommended for the MBR partition layout while parted was one of the first Linux block device management utilities to support GPT partitions. Those who like the user interface of fdisk can use gdisk (GPT fdisk) as an alternative to parted.

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 number of partitions is highly dependent on the environment. For instance, if there are lots of users, then it is advised to have /home/ separate as it increases security and makes backups easier. If Gentoo is being installed to perform as a mail server, then /var/ should be separate as all mails are stored inside /var/. A good choice of filesystem will then maximize the performance. Game servers will have a separate /opt/ as most gaming servers are installed there. The reason is similar for the /home/ directory: security and backups. In most situations, /usr/ is to be kept big: not only will it contain the majority of applications, it typically also hosts the Gentoo ebuild repository (by default located at /var/db/repos/gentoo) which already takes around 650 MiB. This disk space estimate excludes the binpkgs/ and distfiles/ directories that are stored under /var/cache/ by default.

It very much depends on what the administrator wants 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, nosuid (setuid bits are ignored), noexec (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 /usr/ or /var/ - 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 is necessary, often at least the amount of memory installed in the system.


Czym jest partycja BIOS boot?

Partycja BIOS boot to bardzo mała partycja (o rozmiarze 1 do 2 MB) w której programy rozruchowe jak GRUB2 mogą przechowywać dodatkowe dane, które nie zmieszczą się w zaalokowanej pamięci (w przypadku MBR kilkaset bajtów) i nie mogą być zamieszczone w innym miejscu.

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 a GPT partition layout is used with GRUB2 in PC/BIOS mode. It is not required when booting in EFI/UEFI 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/sda2 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.

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/sda2 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.