Linux中的fdisk命令(管理分区)
fdisk是一种用于Linux的基于文本的实用程序,用于管理磁盘分区。
使用fdisk我们可以查看所有磁盘分区,创建新分区,删除现有的硬盘分区并查看分区的大小。
使用FDISK,我们可以根据磁盘的大小创建最多四个主要分区和任意数量的逻辑分区。
请记住,任何单个分区都需要最小尺寸为40MB。
在本教程中,让我们了解如何使用一些示例在Linux中使用FDISK命令。
1)查看所有现有的磁盘分区
在创建新分区或者修改现有分区之前,我们可能希望在系统中查看所有可用分区。
该命令将从所有连接的硬盘列出分区。
系统上有多个磁盘时,分区列表由设备的/dev名称排序。
例如,/dev/sda,/dev/sdb,/dev/sdc等。
使用fdisk -l
查看所有可用分区,如下所示:
# fdisk -l Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 * 1960 2661 5638752 83 Linux /dev/sda6 2662 2904 1951866 83 Linux /dev/sda7 2905 3147 1951866 83 Linux /dev/sda8 3148 3264 939771 82 Linux swap/Solaris /dev/sda9 3265 5283 16217586 b W95 FAT32
2)查看特定硬盘的分区
要查看"/dev/sda"硬盘的所有分区,请执行以下操作。
# fdisk -l /dev/sda
使用fdisk命令m查看所有fdisk命令
使用fdisk命令m
,查看所有可用的fdisk命令,如下所示:
# fdisk /dev/sda The number of cylinders for this disk is set to 9729. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): m Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)
3)删除硬盘分区
让我们假设我们希望将多个分区(例如,/dev/sda6,/dev/sda7和/dev/sda8)组合成一个磁盘分区。
为此,我们应该首先删除所有这些单个分区,如下所示。
# fdisk /dev/sda The number of cylinders for this disk is set to 9729. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 * 1960 2661 5638752 83 Linux /dev/sda6 2662 2904 1951866 83 Linux /dev/sda7 2905 3147 1951866 83 Linux /dev/sda8 3148 3264 939771 82 Linux swap/Solaris /dev/sda9 3265 5283 16217586 b W95 FAT32 Command (m for help): d Partition number (1-9): 8 Command (m for help): d Partition number (1-8): 7 Command (m for help): d Partition number (1-7): 6 Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
4)创建一个新的磁盘分区
删除所有现有分区后,我们可以使用所有可用空间创建新分区,如下所示。
# fdisk /dev/sda The number of cylinders for this disk is set to 9729. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): n First cylinder (2662-5283, default 2662): Using default value 2662 Last cylinder, +cylinders or +size{K,M,G} (2662-3264, default 3264): Using default value 3264 In the above example, fdisk n command is used to create new partition with the specific size. While creating a new partition, it expects following two inputs. Starting cylinder number of the partition to be create (First cylinder). Size of the partition (or) the last cylinder number (Last cylinder, +cylinders or +size ). Please keep in mind that you should issue the fdisk write command (w) after any modifications. Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
创建分区后,使用mkfs命令格式化,如下所示。
# mkfs.ext3 /dev/sda7
5)查看现有分区的大小
如下所示,fdisk -s
将显示块中分区的大小。
在以下示例中,它显示尺寸'/dev/sda7'为4900mb。
# fdisk -s /dev/sda7 4843566
6)切换分区的引导标志
fdisk命令显示每个分区的引导标志。
如果要在相应的分区上禁用或者启用引导标志,请执行以下操作。
# fdisk /dev/sda The number of cylinders for this disk is set to 9729. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 * 1960 2661 5638752 83 Linux /dev/sda6 3265 5283 16217586 b W95 FAT32 /dev/sda7 2662 3264 4843566 83 Linux Partition table entries are not in disk order Command (m for help): a Partition number (1-7): 5 Command (m for help): p Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 1960 2661 5638752 83 Linux /dev/sda6 3265 5283 16217586 b W95 FAT32 /dev/sda7 2662 3264 4843566 83 Linux Partition table entries are not in disk order Command (m for help):
如上所述,分区'/dev/sda5'上禁用引导标志。
7)修复分区表订单
当我们删除逻辑分区并再次重新创建它时,我们可能会看到"out Offord"问题的"分区"。
例如:'分区表条目不是磁盘顺序的错误消息。
例如,删除三个逻辑分区(SDA6,SDA7和SDA8)并创建新分区时,我们可能希望新分区名称为SDA6.
但是,系统可能会将新分区创建为SDA7.
这是因为,在删除分区之后,"SDA9"分区已被称为SDA6,并且可自由空间移动到末尾。
要修复此分区订单问题,并将SDA6分配给新创建的分区,请执行专家命令f
,如下所示。
$fdisk /dev/sda The number of cylinders for this disk is set to 9729. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 * 1960 2661 5638752 83 Linux /dev/sda6 3265 5283 16217586 b W95 FAT32 /dev/sda7 2662 3264 4843566 83 Linux Partition table entries are not in disk order Command (m for help): x Expert command (m for help): f Done. Expert command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks.
修复分区订单后,我们将无法使用"分区表条目不在磁盘顺序"错误消息中。
# fdisk -l Disk /dev/sda: 80.0 GB, 80026361856 bytes 255 heads, 63 sectors/track, 9729 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0xf6edf6ed Device Boot Start End Blocks Id System /dev/sda1 1 1959 15735636 c W95 FAT32 (LBA) /dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA) /dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS /dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA) /dev/sda5 * 1960 2661 5638752 83 Linux /dev/sda6 2662 3264 4843566 83 Linux /dev/sda7 3265 5283 16217586 b W95 FAT32