Merge pull request #12274 from zhs852/patch-2

Translated
This commit is contained in:
Xingyu.Wang 2019-02-02 20:28:51 +08:00 committed by GitHub
commit 22c96284d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,70 +7,49 @@
[#]: via: (https://www.2daygeek.com/linux-fdisk-command-to-manage-disk-partitions/)
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
fdisk Easy Way To Manage Disk Partitions In Linux
fdisk Linux 下管理磁盘分区的利器
======
Hard disks can be divided into one or more logical disks called partitions.
一块硬盘可以被划分成一个或多个逻辑磁盘,我们将其称作分区。我们对硬盘进行的划分信息被储存于建立在扇区 0 的分区表MBR 或 GPT中。
This division is described in the partition table (MBR or GPT) found in sector 0 of the disk.
Linux 需要至少一个分区来当作 root 文件系统,所以我们不能在没有分区的情况下安装 Linux 系统。当我们创建一个分区时,我们必须将它格式化为一个适合的文件系统,否则我们就没办法往里面储存文件了。
Linux needs at least one partition, namely for its root file system and we cant install Linux OS without partitions.
要在 Linux 中完成分区的相关工作我们需要一些工具。Linux 下有很多可用的相关工具,我们曾介绍过 **[Parted 命令][1]**。不过,今天我们的主角是 fdisk。
Once created, a partition must be formatted with an appropriate file system before files can be written to it.
人人都喜欢用 fdisk它是 Linux 下管理磁盘分区的最佳利器之一。它可以操作最大 `2TB` 的分区。大量 Linux 管理员都喜欢使用这个工具,因为当下 LVM 和 SAN 的原因,并没有多少人会用到 2TB 以上的分区。并且这个工具被世界上许多的基础设施所使用。如果你还是想创建比 2TB 更大的分区,请使用 **Parted 命令****cfdisk 命令**
To do so, we need some utility to perform this in Linux.
对磁盘进行分区和创建文件系统是 Linux 管理员的日常。如果你在许多不同的环境中工作,你一定每天都会重复几次这项操作。
There are many utilities are available for that in Linux. We had written about **[Parted Command][1]** in the past and today we are going to discuss about fdisk.
### Linux 内核是如何理解硬盘的?
fdisk command is one of the the best tool to manage disk partitions in Linux.
作为人类,我们可以很轻松地理解一些事情;但是电脑就不是这样了,它们需要合适的命名交流才能理解事情。
It supports maximum `2 TB`, and everyone prefer to go with fdisk.
在 Linux 中,外围设备都位于 `/dev` 挂载点,内核以一下的方式理解硬盘:
This tool is used by vast of Linux admin because we dont use more than 2TB now a days due to LVM and SAN. Its used in most of the infra structure around the world.
* **`/dev/hdX[a-z]:`** IDE 硬盘被命名为 hdX
* **`/dev/sdX[a-z]:`** SCSI 硬盘被命名为 sdX
* **`/dev/xdX[a-z]:`** XT 硬盘被命名为 xdX
* **`/dev/vdX[a-z]:`** 虚拟硬盘被命名为 vdX
* **`/dev/fdN:`** 软盘被命名为 fdN
* **`/dev/scdN or /dev/srN:`** CD-ROM 被命名为 /dev/scdN 或 /dev/srN
Still if you want to create a large partitions, like more than 2TB then you have to go either **Parted Command** or **cfdisk Command**.
### 什么是 fdisk 命令?
Disk partition and file system creations is one of the routine task for Linux admin.
fdisk 的意思是 <ruby>固定的磁盘<rt>Fixed Disk</rt></ruby><ruby>格式化磁盘<rt>Format Disk</rt></ruby>,它是命令行下允许用户对分区进行查看、创建、调整大小、删除、移动和复制的工具。它支持 MBR、Sun、SGI、BSD 分区表,但是它不支持 GUID 分区表GPT。它不是为操作大分区设计的。
If you are working on vast environment then you have to perform this task multiple times in a day.
fdisk 允许我们在每块硬盘上创建最多四个主分区。它们中的其中一个可以作为扩展分区并下设多个逻辑分区。1-4 扇区作为主分区被保留,逻辑分区从扇区 5 开始。
### How Linux Kernel Understand Hard Disks?
![磁盘分区结构图][3]
As a human we can easily understand things but computer needs the proper naming conversion to understand each and everything.
### 如何在 Linux 下安装 fdisk
In Linux, devices are located on `/dev` partition and Kernel understand the hard disk in the following format.
fdisk 作为核心组件内置于 Linux 中,所以你不必手动安装它。
* **`/dev/hdX[a-z]:`** IDE Disk is named hdX in Linux
* **`/dev/sdX[a-z]:`** SCSI Disk is named sdX in Linux
* **`/dev/xdX[a-z]:`** XT Disk is named sdX in Linux
* **`/dev/vdX[a-z]:`** Virtual Hard Disk is named vdX in Linux
* **`/dev/fdN:`** Floppy Drive is named fdN in Linux
* **`/dev/scdN or /dev/srN:`** CD-ROM is named /dev/scdN or /dev/srN in Linux
### 如何用 fdisk 列出可用磁盘?
在执行操作之前,我们必须知道的是哪些磁盘被加入了系统。要想列出所有可用的磁盘,请执行下文的命令。这个命令将会列出磁盘名称、分区数量、分区表类型、磁盘识别代号、分区 ID 和分区类型。
### What Is fdisk Command?
fdisk stands for fixed disk or format disk is a cli utility that allow users to perform following actions on disks. It allows us to view, create, resize, delete, move and copy the partitions.
It understands MBR, Sun, SGI and BSD partition tables and it doesnt understand GUID Partition Table (GPT) and it is not designed for large partitions.
fdisk allows us to create a maximum of four primary partitions per disk. One of these may be an extended partition and it holds multiple logical partitions.
1-4 is reserved for four primary partitions and Logical partitions start numbering from 5.
![][3]
### How To Install fdisk On Linux
You dont need to install fdisk in Linux system because it has installed by default as part of core utility.
### How To List Available Disks Using fdisk Command
First we have to know what are the disks were added in the system before performing any action. To list all available disks on your system run the following command.
It lists possible information about the disks such as disk name, how many partitions are created in it, Disk Size, Disklabel type, Disk Identifier, Partition ID and Partition Type.
```
```shell
$ sudo fdisk -l
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
@ -107,11 +86,11 @@ Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
```
### How To List A Specific Disk Partitions Using fdisk Command
### 如何使用 fdisk 列出特定分区信息?
If you would like to see a specific disk and its partitions, use the following format.
如果你希望查看指定分区的信息,请使用以下命令:
```
```shell
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 sectors
Units: sectors of 1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 512 = 512 bytes
@ -124,11 +103,11 @@ Device Boot Start End Sectors Size Id Type
/dev/sda1 core.md Dict.md lctt2014.md lctt2016.md lctt2018.md LICENSE published README.md scripts sources translated 20973568 62914559 41940992 20G 83 Linux
```
### How To List Available Actions For fdisk Command
### 如何列出 fdisk 所有的可用操作?
When you hit `m` in the fdisk command that will show you available actions for fdisk command.
在 fdisk 中敲击 `m`,它便会列出所有可用操作:
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -177,11 +156,11 @@ Help:
s create a new empty Sun partition table
```
### How To List Partitions Types Using fdisk Command
### 如何使用 fdisk 列出分区类型?
When you hit `l` in the fdisk command that will show you an available partitions type for fdisk command.
在 fdisk 中敲击 `l`,它便会列出所有可用分区的类型:
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -219,16 +198,13 @@ Command (m for help): l
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
```
### 如何使用 fdisk 创建一个磁盘分区?
### How To Create A Disk Partition Using fdisk Command
如果你希望新建磁盘分区,请参考下面的步骤。比如我希望在 `/dev/sdc` 中新建四个分区(三个主分区和一个扩展分区),只需要执行下文的命令。
If you would like to create a new partition use the following steps. In my case, i'm going to create 4 partitions (3 Primary and 1 Extended) on `/dev/sdc` disk. To the same for other partitions too.
首先,请在操作 “First sector” 的时候先按下 `Enter`,然后在 “Last sector” 中输入你希望创建分区的大小(可以在数字后面加 KB、MB、G 和 TB。例如你希望为这个分区扩容 1GB就应该在 “Last sector” 中输入 `+1G`。当你创建三个分区之后fdisk 默认会将分区类型设为扩展分区,如果你希望创建第四个主分区,请输入 `p` 来替代它的默认值 `e`
As this takes value from partition table so, hit `Enter` for first sector. Enter the size which you want to set for the partition (We can add a partition size using KB,MB,G and TB) for last sector.
For example, if you would like to add 1GB partition then the last sector value should be `+1G`. Once you have created 3 partitions, it will automatically change the partition type to extended as a default. If you still want to create a fourth primary partitions then hit `p` instead of default value `e`.
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -266,11 +242,11 @@ Calling ioctl() to re-read partition table.
Syncing disks.
```
### How To Create A Extended Disk Partition Using fdisk Command
### 如何使用 fdisk 创建扩展分区?
Make a note, you have to use remaining all space when you create a extended partition because again you can able to create multiple logical partition in that.
请注意,创建扩展分区时,你应该使用剩下的所有空间,以便之后在扩展分区下创建逻辑分区。
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -311,13 +287,13 @@ Calling ioctl() to re-read partition table.
Syncing disks.
```
### How To View Unpartitioned Disk Space Using fdisk Command
### 如何用 fdisk 查看未分配空间?
As described in the above section, we have totally created 4 partitions (3 Primary and 1 Extended). Extended partition disk space will show unpartitioned until you create a logical partitions in that.
上文中,我们总共创建了四个分区(三个主分区和一个扩展分区)。在创建逻辑分区之前,扩展分区的容量将会以未分配空间显示。
Use the following command to view the unpartitioned space for a disk. As per the below output we have `7GB` unpartitioned disk.
使用以下命令来显示磁盘上的未分配空间,下面的示例中显示的是 7GB
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -336,12 +312,11 @@ Sector size (logical/physical): 512 bytes / 512 bytes
Command (m for help): q
```
### How To Create A Logical Partition Using fdisk Command
### 如何使用 fdisk 创建逻辑分区?
Follow the same above procedure to create a logical partition once you have created the extended partition.
Here, i have created `1GB` of logical partition called `/dev/sdc5`, you can double confirm this by checking the partition table value.
创建扩展分区后,请按照之前的步骤创建逻辑分区。在这里,我创建了位于 `/dev/sdc5``1GB` 逻辑分区。你可以查看分区表值来确认这点。
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -377,13 +352,13 @@ Calling ioctl() to re-read partition table.
Syncing disks.
```
### How To Delete A Partition Using fdisk Command
### 如何使用 fdisk 命令删除分区?
If the partition is no more used in the system than we can remove it by using the below steps.
如果我们不再使用某个分区,请按照下面的步骤删除它。
Make sure you have to enter the correct partition number to delete it. In this case, i'm going to remove `/dev/sdc2` partition.
请确保你输入了正确的分区号。在这里,我准备删除 `/dev/sdc2` 分区:
```
```shell
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.30.1).
@ -416,13 +391,13 @@ Calling ioctl() to re-read partition table.
Syncing disks.
```
### How To Format A Partition Or Create A FileSystem On The Partition
### 如何在 Linux 下格式化分区或建立文件系统?
In computing, a file system or filesystem controls how data is stored and retrieved through inode tables.
在计算时,文件系统控制了数据的储存方式,并通过 <ruby>索引节点<rt>Inode Tables</rt></ruby> 来检索数据。如果没有文件系统,操作系统是无法找到信息储存的位置的。
Without a file system, the system can't find where the information is stored on the partition. Filesystem can be created in three ways. Here, i'm going to create a filesystem on `/dev/sdc1` partition.
在此,我准备在 `/dev/sdc1` 上创建分区。有三种方式创建文件系统:
```
```shell
$ sudo mkfs.ext4 /dev/sdc1
or
$ sudo mkfs -t ext4 /dev/sdc1
@ -441,34 +416,32 @@ Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
```
When you creating a filesystem on tha partition that will create the following important things on it.
当你在分区上建立文件系统时,以下重要信息会同时被创建:
* **`Filesystem UUID:`** UUID stands for Universally Unique Identifier, UUIDs are used to identify block devices in Linux. It's 128 bit long numbers represented by 32 hexadecimal digits.
* **`Superblock:`** Superblock stores metadata of the file system. If the superblock of a file system is corrupted, then the filesystem cannot be mounted and thus files cannot be accessed.
* **`Inode:`** An inode is a data structure on a filesystem on a Unix-like operating system that stores all the information about a file except its name and its actual data.
* **`Journal:`** A journaling filesystem is a filesystem that maintains a special file called a journal that is used to repair any inconsistencies that occur as the result of an improper shutdown of a computer.
* **`Filesystem UUID:`** UUID 代表了通用且独一无二的识别符UUID 在 Linux 中通常用来识别设备。它 128 位长的数字代表了 32 个十六进制数。
* **`Superblock:`** Superblock 储存了文件系统的元数据。如果某个文件系统的 Superblock 被破坏,我们就无法挂载它了(也就是说无法访问其中的文件了)。
* **`Inode:`** Inode 是类 Unix 系统中文件系统的数据结构,它储存了所有除名称以外的文件信息和数据。
* **`Journal:`** 日志式文件系统包含了用来修复电脑意外关机产生下错误信息的日志Journal
### How To Mount A Partition In Linux
### 如何在 Linux 中挂载分区?
Once you have created the partition and filesystem then we need to mount the partition to use.
在你创建完分区和文件系统之后,我们需要挂载它们以便使用。我们需要创建一个挂载点来挂载分区,使用 mkdir 来创建一个挂载点。
To do so, we need to create a mountpoint to mount the partition. Use mkdir command to create a mountpoint.
```
```shell
$ sudo mkdir -p /mnt/2g-new
```
For temporary mount, use the following command. You will be lose this mountpoint after rebooting your system.
如果你希望进行临时挂载,请使用下面的命令。在计算机重启之后,你会丢失这个挂载点。
```
```shell
$ sudo mount /dev/sdc1 /mnt/2g-new
```
For permanent mount, add the partition details in the fstab file. It can be done in two ways either adding device name or UUID value.
如果你希望永久挂载某个分区,请将分区详情加入 fstab 文件。我们既可以输入设备名称,也可以输入 UUID。
Permanent mount using Device Name:
使用设备名称来进行永久挂载:
```
# vi /etc/fstab
@ -477,6 +450,7 @@ Permanent mount using Device Name:
```
Permanent mount using UUID Value. To get a UUID of the partition use blkid command.
使用 UUID 来进行永久挂载(请使用 blkid 来获取 UUID
```
$ sudo blkid
@ -490,7 +464,7 @@ $ sudo blkid
UUID=d17e3c31-e2c9-4f11-809c-94a549bc43b7 /mnt/2g-new ext4 defaults 0 0
```
The same has been verified using df Command.
使用 df 命令亦可:
```
$ df -h
@ -512,7 +486,7 @@ via: https://www.2daygeek.com/linux-fdisk-command-to-manage-disk-partitions/
作者:[Magesh Maruthamuthu][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
译者:[zhs852](https://github.com/zhs852)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出