命令行大全

fdisk

rose1 发表于 2020-09-11 09:07浏览次数:

fdisk是Linux的命令行分区表编辑器。它可用于创建,销毁和修改磁盘分区。

查看英文版

目录:

1 fdisk 运行系统环境

2 fdisk 说明

3 fdisk 语法

4 fdisk 例子

fdisk 运行系统环境

Linux

fdisk 说明

fdisk是一个菜单驱动的程序,用于创建和操作分区表。它了解DOS类型的分区表和BSD类型或SUN类型的磁盘标签。

fdisk无法理解GPT(GUID分区表),并且不适用于大型分区。在这些情况下,请使用更高级的GNU parted。

fdisk不使用DOS兼容模式,默认情况下将柱面作为显示单位。可以使用-c = dos -u = cylinders命令行选项启用旧的DOS行为。

硬盘可以分为一个或多个逻辑磁盘,称为分区。该分区记录在分区表中,该分区表位于磁盘的扇区 0中。在BSD世界中,人们谈论“磁盘片”和“磁盘标签”。

Linux需要至少一个分区,专门用于其根文件系统。它可以使用交换文件和/或交换分区,但是后者效率更高。因此,通常人们会想要第二个Linux分区专门用作交换分区。在与Intel兼容的硬件上,引导系统的BIOS通常只能访问磁盘的前1024个柱面。因此,拥有大量磁盘的人通常会创建第三个分区,该分区通常要挂载在/ boot上,该分区又大几个MB,以存储内核映像和启动时需要的一些辅助文件,以确保BIOS可以访问此文件。使用安全性,简化管理和备份或测试的原因可能要使用超过最小数量的分区。

fdisk is a menu-driven program for creation and manipulation of partition tables. It understands DOS-type partition tables and BSD-type or SUN-type disklabels.

fdisk does not understand GPTs (GUID partition tables) and it is not designed for large partitions. In these cases, use the more advanced GNU parted.

fdisk does not use DOS-compatible mode and cylinders as display units by default. The old deprecated DOS behavior can be enabled with the '-c=dos -u=cylinders' command-line options.

Hard disks can be divided into one or more logical disks called partitions. This division is recorded in the partition table, found in sector 0 of the disk. In the BSD world one talks about `disk slices' and a `disklabel'.

Linux needs at least one partition, specifically for its root file system. It can use swap files and/or swap partitions, but the latter are more efficient. So, usually one will want a second Linux partition dedicated as swap partition. On Intel-compatible hardware, the BIOS that boots the system can often only access the first 1024 cylinders of the disk. For this reason people with large disks often create a third partition, a few MBlarge, often mounted on /boot, to store the kernel image and a few auxiliary files needed at boot time, so as to make sure that this stuff is accessible to the BIOS. There may be reasons of security, ease of administration and backup, or testing, to use more than the minimum number of partitions.

查看英文版

查看中文版

fdisk 语法

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
fdisk -l [-u] [device...]
fdisk -s partition...
fdisk -v
fdisk -h

选件

-b sectorsize

指定磁盘的扇区大小。有效值是512,1024,20484096。最近的内核知道扇区大小。仅在旧内核上使用此功能,或覆盖内核的思想。从util-linux-2.17开始,fdisk区分逻辑扇区大小和物理扇区大小。此选项更改两个扇区大小来sectorsize。

-c[=mode]

指定兼容模式“ dos ”“ nondos ”。默认为非DOS模式。为了向后兼容,可以使用不带mode参数的选项。在这种情况下,将使用默认值。请注意,可选模式参数不能与-c选项用空格分隔,正确的格式例如是' -c = dos '。

-C cyls

指定磁盘的柱面数。这是一件很奇怪的事情,但是如果需要的话,这个选项就可以完成。

-H heads

指定磁盘的磁头数。不是物理编号,而是用于分区表的编号。合理的值为25516。

-S sects

指定磁盘每个磁道的扇区数。不是物理编号,而是用于分区表的编号。合理值为63。

-h

打印帮助,然后退出。

-l

列出指定设备的分区表,然后退出。如果未提供任何设备,则使用/ proc / partitions中提到的设备(如果存在)。

-u[=unit]

列出分区表时,请在“ 扇区 ”或“ 柱面 ”中显示大小。默认设置是显示扇区大小。为了向后兼容,可以使用不带units参数的选项。在这种情况下,将使用默认值。注意,可选的单位参数不能与-u选项用空格分隔;正确的格式例如是' -u = cylinders '。

-v

打印版本信息,然后退出。

设备

设备通常是/ dev / sda,/ dev / sdb等。设备名称指的是整个磁盘。没有libata(Linux内核内部用于支持ATA主机控制器和设备的库)的旧系统会在IDE和SCSI磁盘之间产生差异。在这种情况下,设备名称将为/ dev / hd *(IDE)或/ dev / sd *(SCSI)。

分区是设备名称,后跟分区号。例如,/ dev / sda1是系统中第一个硬盘上的第一个分区。

磁盘标签

一个BSD / SUN类型的磁盘标签可以描述8个分区,其中第三个应该是“整个磁盘”分区。不要在柱面0处启动实际上使用其第一个扇区的分区(例如交换分区),因为这会破坏disklabel。

IRIX / SGI类型的磁盘标签可以描述16个分区,其中的第十一个分区应该是整个“卷”分区,而第九个分区应该标记为“卷标头”。卷标头还将覆盖分区表,即,它从块零开始,默认情况下扩展到五个柱面。卷标头中的剩余空间可由标头目录条目使用。卷头不能有任何分区重叠。另外,请勿更改其类型或在其上创建一些文件系统,因为这样会丢失分区表。仅当在IRIX / SGI计算机上的Linux或Linux下的IRIX / SGI磁盘上使用Linux时,才使用这种类型的标签。

DOS类型的分区表可以描述无限数量的分区。在扇区0中,有描述4个分区(称为“主要”)的空间。其中之一可以是扩展分区。这是一个保存逻辑分区的盒子,在链接的扇区列表中找到描述符,每个描述符位于相应的逻辑分区之前。存在或不存在的四个主分区的编号为1-4。逻辑分区从5开始编号。

在DOS类型的分区表中,起始偏移量和每个分区的大小以两种方式存储:作为扇区的绝对数量(以32位给定),以及作为圆柱/磁头/扇区三重(以10 + 8给定) +6位)。前者还可以;如果使用512字节的扇区,则最多可以工作2 TB。后者有两个问题。首先,仅当已知磁头的数量和每个磁道的扇区数时才能填充这些C / H / S字段。其次,即使我们知道这些数字应该是多少,可用的24位也不足够。DOS仅使用C / H / S,Windows同时使用,Linux从不使用C / H / S。

如果可能,fdisk将自动获取磁盘几何。这不一定是物理磁盘的几何形状(实际上,现代磁盘实际上并没有像物理几何形状那样的东西,当然也不能以简单的Cylinders / Heads / Sectors形式描述),而是MS-DOS的磁盘几何形状用于分区表。

通常,默认情况下一切正常,并且如果Linux是磁盘上的唯一系统,则没有问题。但是,如果必须与其他操作系统共享磁盘,则通常最好让来自另一个操作系统的fdisk至少构成一个分区。当Linux启动时,它会查看分区表,并尝试推断出与其他系统良好协作所需的(伪)几何形状。

每当打印出分区表时,都会对分区表条目执行一致性检查。此检查将验证物理和逻辑起点和终点是否相同,并且每个分区都在圆柱边界上开始和结束(第一个分区除外)。某些版本的MS-DOS创建的第一个分区不是从圆柱边界开始,而是在第一个圆柱的扇区2上开始。从柱面1开始的分区不能从柱面边界开始,但是除非您的计算机上装有OS / 2,否则这不太可能引起困难。

在更新分区表时,在退出之前执行sync()ioctl(BLKRRPART)(从磁盘重新读取分区表)。很久以前,使用fdisk后必须重新启动。不再需要;实际上,应该注意的是内核和磁盘硬件都可以缓冲数据,因此在使用fdisk分区后立即重新引导可能是不可取的。

DOS 6.x警告

DOS 6.x FORMAT命令在分区的数据区域的第一个扇区中查找某些信息,并将此信息视为比分区表中的信息更可靠。DOS FORMAT期望DOS FDISK会在大小发生变化时清除分区数据区域的前512个字节。即使给出了/ U标志,DOS FORMAT也会查看这些额外信息:这被认为是DOS FORMAT和DOS FDISK中的错误。

底线是,如果您使用cfdiskfdisk更改DOS分区表条目的大小,则在使用DOS FORMAT格式化分区之前,还必须使用dd将该分区的前512个字节清零。例如,如果使用cfdisk/ dev / sda1创建DOS分区表条目,则(退出fdisk或cfdisk并重新引导Linux以使分区表信息有效之后),可以使用命令“ dd if = / dev / zero of = / dev / sda1 bs = 512 count = 1 “将分区的前512个字节清零。

警告

如果您使用 dd命令,请格外小心,小的错字可能会使磁盘上的所有数据失效。

为了获得最佳结果,应始终使用特定于操作系统的分区表程序。例如,您应使用DOS FDISK程序创建DOS分区,并使用Linux fdisk或Linux cfdisk程序创建Linux分区。

fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
fdisk -l [-u] [device...]
fdisk -s partition...
fdisk -v
fdisk -h

Options

-b sectorsize

Specify the sector size of the disk. Valid values are 51210242048 or 4096. Recent kernels know the sector size. Use this only on old kernels or to override the kernel's ideas. Since util-linux-2.17, fdisk differentiates between logical and physical sector size. This option changes both sector sizes to sectorsize.

-c[=mode]

Specify the compatibility mode, 'dos' or 'nondos'. The default is non-DOS mode. For backward compatibility, it is possible to use the option without the mode argument; in that case, the default is used. Note that the optional mode argument cannot be separated from the -c option by a space, the correct form is, for example, '-c=dos'.

-C cyls

Specify the number of cylinders of the disk. This would be a very strange thing to want to do, but if so desired, this option will get it done.

-H heads

Specify the number of heads of the disk. Not the physical number, but the number used for partition tables. Reasonable values are 255 and 16.

-S sects

Specify the number of sectors per track of the disk. Not the physical number, but the number used for partition tables. A reasonable value is 63.

-h

Print help and then exit.

-l

List the partition tables for the specified devices and then exit. If no devices are given, those mentioned in /proc/partitions (if that exists) are used.

-u[=unit]

When listing partition tables, show sizes in 'sectors' or in 'cylinders'. The default is to show sizes in sectors. For backward compatibility, it is possible to use the option without the units argument; in this case, the default is used. Note that the optional unit argument cannot be separated from the -u option by a space; the correct form is, for example, '-u=cylinders'.

-v

Print version information, and exit.

Devices

The device is usually /dev/sda/dev/sdb, etc. A device name refers to the entire disk. Old systems without libata (a library used inside the Linux kernel to support ATA host controllers and devices) make a difference between IDE and SCSI disks. In such cases the device name will be /dev/hd* (IDE) or /dev/sd* (SCSI).

The partition is a device name followed by a partition number. For example, /dev/sda1 is the first partition on the first hard disk in the system.

Disk labels

A BSD/SUN-type disklabel can describe 8 partitions, the third of which should be a "whole disk" partition. Do not start a partition that actually uses its first sector (like a swap partition) at cylinder 0, since that will destroy the disklabel.

An IRIX/SGI-type disklabel can describe 16 partitions, the eleventh of which should be an entire `volume' partition, while the ninth should be labeled `volume header'. The volume header will also cover the partition table, i.e., it starts at block zero and extends by default over five cylinders. The remaining space in the volume header may be used by header directory entries. No partitions may overlap with the volume header. Also, do not change its type or make some filesystem on it, since you will lose the partition table. Use this type of label only when working with Linux on IRIX/SGI machines or IRIX/SGI disks under Linux.

A DOS-type partition table can describe an unlimited number of partitions. In sector 0 there is room for the description of 4 partitions (called "primary"). One of these may be an extended partition; this is a box holding logical partitions, with descriptors found in a linked list of sectors, each preceding the corresponding logical partitions. The four primary partitions, present or not, get numbers 1-4. Logical partitions start numbering from 5.

In a DOS-type partition table the starting offset and the size of each partition is stored in two ways: as an absolute number of sectors (given in 32 bits), and as a Cylinders/Heads/Sectors triple (given in 10+8+6 bits). The former is OK; with 512-byte sectors this will work up to 2 TB. The latter has two problems. First, these C/H/S fields can be filled only when the number of heads and the number of sectors per track are known. And second, even if we know what these numbers should be, the 24 bits that are available do not suffice. DOS uses C/H/S only, Windows uses both, Linux never uses C/H/S.

If possible, fdisk will obtain the disk geometry automatically. This is not necessarily the physical disk geometry (indeed, modern disks do not really have anything like a physical geometry, certainly not something that can be described in simplistic Cylinders/Heads/Sectors form), but it is the disk geometry that MS-DOS uses for the partition table.

Usually, all goes well by default, and there are no problems if Linux is the only system on the disk. However, if the disk has to be shared with other operating systems, it is often a good idea to let an fdisk from another operating system make at least one partition. When Linux boots it looks at the partition table, and tries to deduce what (fake) geometry is required for good cooperation with other systems.

Whenever a partition table is printed out, a consistency check is performed on the partition table entries. This check verifies that the physical and logical start and end points are identical, and that each partition starts and ends on a cylinder boundary (except for the first partition). Some versions of MS-DOS create a first partition which does not begin on a cylinder boundary, but on sector 2 of the first cylinder. Partitions beginning in cylinder 1 cannot begin on a cylinder boundary, but this is unlikely to cause difficulty unless you have OS/2 on your machine.

sync() and an ioctl(BLKRRPART) (reread partition table from disk) are performed before exiting when the partition table is updated. Long ago it used to be necessary to reboot after the use of fdisk. This is no longer required; in fact one should note that both the kernel and the disk hardware may buffer data, so rebooting immediately after partitioning with fdisk may be inadvisable.

DOS 6.x warning

The DOS 6.x FORMAT command looks for some information in the first sector of the data area of the partition, and treats this information as more reliable than the information in the partition table. DOS FORMAT expects DOS FDISK to clear the first 512 bytes of the data area of a partition whenever a size change occurs. DOS FORMAT will look at this extra information even if the /U flag is given: this is considered to be a bug in DOS FORMAT and DOS FDISK.

The bottom line is that if you use cfdisk or fdisk to change the size of a DOS partition table entry, then you must also use dd to zero the first 512 bytes of that partition before using DOS FORMAT to format the partition. For example, if you were using cfdisk to make a DOS partition table entry for /dev/sda1, then (after exiting fdisk or cfdisk and rebooting Linux so that the partition table information is valid) you would use the command "dd if=/dev/zero of=/dev/sda1 bs=512 count=1" to zero the first 512 bytes of the partition.

Warning

BE EXTREMELY CAREFUL if you use the dd command, a small typo can make all of the data on your disk useless.

For best results, you should always use an OS-specific partition table program. For example, you should make DOS partitions with the DOS FDISK program and Linux partitions with the Linux fdisk or Linux cfdisk program.

查看英文版

查看中文版

fdisk 例子

Tip

您必须具有root用户访问权限,此命令才能起作用。

fdisk -l

列出您登录的计算机的分区信息。以下是此输出的示例:

Disk /dev/sda: 250.0 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device     Boot  Start  End    Blocks      Id  System
/dev/sda1  *     1      191    1534176     83  Linux
/dev/sda2        192    2231   16386300    83  Linux
/dev/sda3        2232   3506   10241437+   83  Linux
/dev/sda4        3507   30401  216034087+  5   Extended
/dev/sda5        3507   3767   2096451     82  Linux swap / Solaris
/dev/sda6        3768   3832   522081      83  Linux
/dev/sda7        3833   30401  213415461   83  Linux Disk
/dev/sdb: 250.0 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device     Boot  Start  End    Blocks      Id  System
/dev/sdb1  *     1      30401  244196001   83  Linux
Tip

You must have root access for this command to work.

fdisk -l

List the partition information of the computer you're logged into. Below is an example of what this output may look like:

Disk /dev/sda: 250.0 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device     Boot  Start  End    Blocks      Id  System
/dev/sda1  *     1      191    1534176     83  Linux
/dev/sda2        192    2231   16386300    83  Linux
/dev/sda3        2232   3506   10241437+   83  Linux
/dev/sda4        3507   30401  216034087+  5   Extended
/dev/sda5        3507   3767   2096451     82  Linux swap / Solaris
/dev/sda6        3768   3832   522081      83  Linux
/dev/sda7        3833   30401  213415461   83  Linux Disk
/dev/sdb: 250.0 GB, 250059350016 bytes 255 heads, 63 sectors/track, 30401 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device     Boot  Start  End    Blocks      Id  System
/dev/sdb1  *     1      30401  244196001   83  Linux

查看英文版

查看中文版