Partitioning (简体中文)

From ArchWiki
Jump to: navigation, search
Summary help replacing me
An overview of disk partitioning tools, best practices, and additional considerations.
Related
fstab
LVM
Swap
Format a device
File Systems
翻译状态: 本文是英文页面 Partitioning翻译,最后翻译时间:2012-10-26,点击这里可以查看翻译后英文页面的改动。

Tango-preferences-desktop-locale.png本页面需要更新翻译,内容可能已经与英文脱节。要贡献翻译,请访问简体中文翻译组Tango-preferences-desktop-locale.png

附注: please use the first argument of the template to provide more detailed indications.

Partitioning a hard drive allows one to logically divide the available space into sections that can be accessed independently of one another. Partition information is stored within a hard drive's GUID Partition Table or Master Boot Record.

An entire hard drive may be allocated to a single partition, or one may divide the available storage space across multiple partitions. A number of scenarios require creation multiple partitions: dual- or multi-booting, for example, or maintaining a swap partition. In other cases, partitioning is used as a means of logically separating data, such as creating separate partitions for audio and video files. Common partitioning schemes are discussed in detail below.

Each partition should be formatted to a file system type before being used.

MBR 情况下,用户可以创建最多四个主分区。如果需要更多分区,要把其中一个设置成扩展分区,扩展分区可以进而划分为多个逻辑分区。

分区类型

磁盘分区是指在磁盘上定义若干区域(分区),每个都象是一个独立的磁盘,可以在其上创建一个文件系统(格式化)。

MBR 情况下

分区的类型有三种:

  • 主分区
  • 扩展分区
    • 逻辑分区

主分区可以被启动,一个物理磁盘或RAID卷最多能有4个主分区。如果需要三个以上分区,需要使用包含逻辑分区扩展分区。扩展分区可以看作存放逻辑分区的容器。一个硬盘最多只能有一个扩展分区。而且,扩展分区也算主分区,所以有扩展分区的话,就只能有另外三个主分区。扩展分区可以划分为无限多个逻辑分区。如果使用多系统,Windows必须装在主分区。

进行磁盘分区时,可以观察到命名规则为:主分区为 sda1sda3,跟着是扩展分区sda4,在扩展分区中划分出来的逻辑分区分别是sda5sda6,以此类推。

GPT 情况下

只有主分区一种类型,每个硬盘或者 RAID 的分区数是无限的。

交换分区

交换分区(swap)是在硬盘上划出的一段虚拟内存区域,当物理内存已经放不下数据时,用来临时应急充当内存。

以前,一般规则是交换分区为物理内存的两倍,在内存越来越大的今天,这个规则已经逐步过时。如果你的内存较小,只有512M,那么作为日常应用,需要 2 倍就足够了。如果系统有大量内存(超过 1024M),则可以完全不用交换分区,因为可以在以后使用 交换文件 替代。本例中使用 1G 交换分区。

注意: 如果使用休眠到磁盘,必须要等于物理内存的交换分区。这种情况下,推荐使用超过物理内存容量10%-15%。

分区结构

如何对磁盘进行分区全靠个人喜好,下面提供一些指导。设计分区结构一般要考虑扩展性、速度、安全性以及磁盘空间。两种简单而常见的结构是:一,一个根分区,一个交换分区;二,只有一个根分区。请继续阅读,以做出最佳选择。如果同时安装了Windows,请阅读Windows和Arch双系统

Single root partition

This scheme is the simplest and should be enough for most use cases. A swapfile can be created and easily resized as needed. It usually makes sense to start by considering a single / partition and then separate out others based on specific use cases like raid, encryption, a shared media partition, etc.

Discrete partitions

Separating out a path as a partition allows for the choice of a different filesystem and mount options. In some cases like a media partition, they can also be shared between operating systems.

Mount points

下面的挂载点可以放在单独的分区中:

/ (root)

根文件系统是目录等级的最顶部,所有其它文件系统都通过它访问,即使位于不同的物理设备,所有目录和文件也都显示在根目录/下面。根文件系统必须能够启动、恢复、还原或者修复系统。所以 / 下面的一些目录不应该位于不同分区。(参见下面的警告)。

The / partition or root partition is necessary and it is the most important. The other partitions can be replaced by it。

Warning: Directories essential for booting must be on the same partition as / or mounted in early userspace by the initramfs. These essential directories are: /bin, /etc, /lib, /sbin and /usr[1].

/boot

此目录包含内核、ramdisk 镜像和启动加载程序的配置文件和不同阶段使用的文件。/boot 还可以保存内核开始执行用户空间程序前使用的数据,包括保存的主启动扇区和扇区映射文件。/boot 是启动必须的,但是特别的地方是它可以位于单独分区。

A separate /boot partition is needed if installing a software RAID0 (stripe) system.

/home

The /home directory contains user-specific configuration files, caches, application data and media files.

Separating out /home allows / to be re-partitioned separately, but note that you can still reinstall Arch with /home untouched even if it isn't separate - the other top-level directories just need to be removed, and then pacstrap can be run.

You should not share home directories between users on different distributions, because they use incompatible software versions and patches. Instead, consider sharing a media partition or at least using different home directories on the same /home partition.

/var

/var
存放可变数据,比如管理和登录数据、pacman缓存、ABS目录树等。该分区的存在使得挂载/usr为只读成为可能,以前在/usr读写的数据现在都改在/var下进行。
注意: /var下存放着很多零散的小文件。如果使用独立分区,需要斟酌一下文件系统类型(见下)。

It exists to make it possible to mount /usr as read-only. Everything that historically went into /usr that is written to during system operation (as opposed to installation and software maintenance) must reside under /var.

Note: /var contains many small files. The choice of filesystem type (see below) should consider this fact if a separate partition is used.

/tmp

This is already a separate partition by default, by virtue of being mounted as tmpfs by systemd.

Swap

A swap partition provides memory that can be used as virtual RAM. A swapfile should be considered too, as they have almost no performance overhead compared to a partition but are much easier to resize as needed. A swap partition can potentially be shared between operating systems, but not if hibernation is used.

Note: The old rule of matching the swap partition size with the available RAM when using suspend-to-disk no longer applies. The default suspend method uses an image the size of 40% of the currently available RAM by default. Even with TuxOnIce the atomic copy generally only takes about 70% after compression.[2]
我的分区应该设多大?

这个问题最好根据个人需要回答,请参考下面这些原则:

  • 根文件系统(/)包含/usr目录,这样如果安装很多程序,会变得很大。15-20GB应该能够满足大部分人的要求。
  • /var文件系统除包含软件数据,还有ABS树和pacman缓存。缓存软件包快捷方便;可以按照要求降级软件包。/var的大小会逐步增长;pacman缓存在运行中逐步增大,但是可以很方便的清空。8-12GB对桌面系统来说应该足够了,服务器需要更大的/var文件系统。
  • /home文件系统一般存放用户数据、下载和多媒体文件。在桌面电脑中,/home 一般是远超过其它的最大的文件系统。
  • /boot一般只需要大概100MB。
  • swap — [varies] : Historically, the general rule for swap partition size was to allocate twice the amount of physical RAM. As computers have gained ever larger memory capacities, this rule has become deprecated. On machines with up to 512MB RAM, the 2x rule is usually adequate. If a sufficient amount of RAM (more than 1024MB) is available, it may be possible to have a smaller swap partition or even eliminate it. With more than 2 GB of physical RAM, one can generally expect good performance without a swap partition.
  • 如果空间充足,可以为每个文件系统多留25%空间,以应对以后的变化,还可以避免碎片。

Partitioning tools

  • fdisk — Terminal partitioning tools included in Linux.
https://www.kernel.org/ || util-linux
  • cfdisk — Terminal partitioning tool written with ncurses libraries.
https://www.kernel.org/ || util-linux
Note: The first partition created by cfdisk starts at sector 63, instead of the usual 2048. This will cause problems with GRUB2. grub-legacy and syslinux should work fine.
  • gdiskGPT version of fdisk.
http://www.rodsbooks.com/gdisk/ || gptfdisk
  • cgdiskGPT version of cfdisk.
http://www.rodsbooks.com/gdisk/ || gptfdisk
  • GNU Parted — Terminal partitioning tool.
http://www.gnu.org/software/parted/parted.html || parted
  • GParted — Graphical tool written in GTK.
http://gparted.sourceforge.net/ || gparted
  • Partitionmanager — Graphical tool written in QT.
http://sourceforge.net/projects/partitionman/ || partitionmanagerAUR
  • QtParted — Similar to Partitionmanager, available in AUR.
http://qtparted.sourceforge.net/ || qtpartedAUR

Partition Alignment

High-level Overview

Proper partition alignment is essential for optimal performance and longevity. The key to alignment is partitioning to (at least) the EBS (erase block size) of the SSD.

Note: The EBS is largely vendor specific; a Google search on the model of interest would be a good idea! The Intel X25-M for example is thought to have an EBS of 512 KiB, but Intel has yet to publish anything officially to this end.
Note: If one does not know the EBS of one's SSD, use a size of 512 KiB. Those numbers are greater or equal than almost all of the current EBS. Aligning partitions for such an EBS will result in partitions also aligned for all lesser sizes. This is how Windows 7 and Ubuntu "optimize" partitions to work with SSD.

If the partitions are not aligned to begin at multiples of the EBS (512 KiB for example), aligning the file system is a pointless exercise because everything is skewed by the start offset of the partition. Traditionally, hard drives were addressed by indicating the cylinder, the head, and the sector at which data was to be read or written. These represented the radial position, the drive head (= platter and side) and the axial position of the data respectively. With LBA (logical block addressing), this is no longer the case. Instead, the entire hard drive is addressed as one continuous stream of data.

Using GPT - Modern Method

GPT is an alternative, contemporary partitioning style. It is intended to replace the old Master Boot Record (MBR) system. GPT has several advantages over MBR, which has quirks dating back to MS-DOS times. With recent developments to the formatting tools fdisk (MBR) and gdisk (GPT), it is equally easy to use GPT or MBR and get maximum performance.

Choosing between GPT and MBR

The choice basically boils down to this:

  • If using GRUB Legacy as the bootloader, one must use MBR. See #Using MBR - Legacy Method.
  • To dual-boot with Windows, one must use MBR. See #Using MBR - Legacy Method.
    • A special exception to this rule: dual-booting Windows Vista/7 64 bit, and using UEFI instead of BIOS, one must use GPT.
  • If none of the above apply, choose freely between GPT and MBR. Since GPT is more modern, it is recommended in this case.

Gdisk Usage Summary

The GPT-able tool equivalent to fdisk, gdisk, can perform partition alignment automatically on a 2048 sector (or 1024KiB) block size base which should be compatible with the vast majority of SSDs if not all. GNU parted also supports GPT, but is less user-friendly for aligning partitions. A summary of the typical usage of gdisk:

  • Install gdisk through the gptfdisk package from the extra repository.
  • Start gdisk against your SSD.
  • If the SSD is brand new or if wanting to start over, create a new empty GUID partition table (aka GPT) with the o command.
  • Create a new partition with the n command (primary type/1st partition).
  • Assuming the partition is new, gdisk will pick the highest possible alignment. Otherwise, it will pick the largest power of two that divides all partition offsets.
  • If choosing to start on a sector before the 2048th gdisk will automatically shift the partition start to the 2048th disk sector. This is to ensure a 2048-sectors alignment (as a sector is 512B, this is a 1024KiB alignment which should fit any SSD NAND erase block).
  • Use the +x{M,G} format to extend the partition x megabytes or gigabytes, if choosing a size that is not a multiple of the alignment size (1024kiB), gdisk will shrink the partition to the nearest inferior multiple.
  • Select the partition's type id, the default, Linux/Windows data (code 0700), should be fine for most use. Press L to show the codes list.
  • Assign other partitions in a like fashion.
  • Write the table to disk and exit via the w command.
  • Create the filesystems as usual.
Warning: If planning to use the GPT partitioned SSD as a boot-disk on a BIOS based system (most systems except Apple computers and some very rare motherboard models with Intel chipset) one may have to create, preferably at the disk's beginning, a 2 MiB partition with no filesystem and with the partition type as BIOS boot or bios_grub partition (gdisk type code EF02) for booting from the disk using GRUB. For Syslinux, one does not need to create a separate 2 MiB bios_grub partition, but one needs to have separate /boot partition and enable Legacy BIOS Bootable partition attribute for that partition (using gdisk). See GPT for more information.
Warning: GRUB legacy does not support GUID partitioning scheme, users must use burg, GRUB or Syslinux.
Warning: If planning to dual boot with Windows (XP, Vista or 7) do NOT use GPT since they do NOT support booting from a GPT disk in BIOS systems! Users need to use the legacy MBR method described below for dual-boot in BIOS systems! This limitation does not apply if booting in UEFI mode and using Windows Vista (64bits) or 7 (64bits). For 32-bit Windows Vista and 7, and 32 and 64-bit Windows XP, users need to use MBR partitioning and boot in BIOS mode only.

Using MBR - Legacy Method

Using MBR, the utility for editing the partition table is called fdisk. Recent versions of fdisk have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. The latest fdisk automatically aligns all partitions to 2048 sectors, or 1024 KiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.

Note that in the olden days, fdisk used cylinders as the default display unit, and retained an MS-DOS compatibility quirk that messed with SSD alignment. Therefore one will find many guides around the internet from around 2008-2009 making a big deal out of getting everything correct. With the latest fdisk, things are much simpler, as reflected in this guide.

Fdisk Usage Summary

  • Start fdisk.
  • If the SSD is brand new, create a new empty DOS partition table with the o command.
  • Create a new partition with the n command (primary type/1st partition).
  • Use the +xG format to extend the partition x gigabytes.
  • Change the partition's system id from the default type of Linux (type 83) to the desired type via the t command. This is an optional step should the user wish to create another type of partition for example, swap, NTFS, LVM, etc. Note that a complete listing of all valid partition types is available via the l command.
  • Assign other partitions in a like fashion.
  • Write the table to disk and exit via the w command.

When finished, users may format their newly created partitions with mkfs.x /dev/sdXN where x is the filesystem, X is the drive letter, and N is the partition number. The following example will format the first partition on the first disk to ext4 using the defaults specified in /etc/mke2fs.conf:

# mkfs.ext4 /dev/sda1
Warning: Using the mkfs command can be dangerous as a simple mistake can result in formatting the WRONG partition and in data loss! TRIPLE check the target of this command before hitting the Enter key!

See also