This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog One Linux command a day (26): fdisk command - Joshua 317 blog
1, Introduction
fdisk (full English Name: "Partition table manipulator for Linux") in Linux is a command to create and maintain disk partition table. It is compatible with DOS type partition table, BSD or SUN type disk list. Partitioning a hard disk is essentially a formatting of the hard disk. Using an image metaphor, partitioning is like drawing a large box on a piece of white paper, and formatting is like marking a grid in the box.
2, Format description
fdisk [options] fdisk [parameter] Usage: fdisk [options] <disk> change partition table fdisk [options] -l <disk> list partition table(s) fdisk -s <partition> give partition size(s) in blocks Options: -b <size> sector size (512, 1024, 2048 or 4096) -c[=<mode>] compatible mode: 'dos' or 'nondos' (default) -h print this help text -u[=<unit>] display units: 'cylinders' or 'sectors' (default) -v print program version -C <number> specify the number of cylinders -H <number> specify the number of heads -S <number> specify the number of sectors per track
3, Option description
-b Specify the size of each hard disk partition,The available value is 512, 1024, 2048 or 4096 -c Compatibility mode:'dos'or'nondos'(default) -h display help information -u Collocation "-l"In the parameter list, the number of partitions will replace the number of cylinders to represent the starting address of each partition -v display version information -l Lists the partition table status for the specified peripheral -s Outputs the specified partition size to standard output in blocks( block)Unit -C [cyls]:Specifies the number of cylinders on the hard disk( number of cylinders); -H [heads]:Specifies the number of hard disk heads( number of heads),Of course, it is not a physical value, but acts on the partition table. The reasonable values are 255 and 16 -S [sects]:Specifies the number of sectors per track. Of course, it is not a physical value, but is used in the partition table. A reasonable value is 63
The difference between a block and a sector. A sector is the smallest storage unit for a hard disk, and a block is the smallest access unit for a file system. Generally speaking, a sector size is 512B, a block size is 4KB, and a block is composed of eight consecutive sectors.
To understand the meaning of the above options, you need to understand the physical composition and related concepts of the disk, such as the specific meaning of the components such as sector, cylinder and head. See the storage principle and internal architecture of the hard disk.
4, Command function
Create and maintain disk partition tables
5, Common usage
5.1 display current zoning
# fdisk -l Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000d64b4 Device Boot Start End Blocks Id System /dev/vda1 * 2048 104857566 52427759+ 83 Linux # fdisk -lu Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000d64b4 Device Boot Start End Blocks Id System /dev/vda1 * 2048 104857566 52427759+ 83 Linux
#Parameter description Device: Partition name; Boot: Whether it is an active partition. An active partition can only be a primary partition, and a hard disk can only have one active primary partition; the sum of the primary partition and the extended partition of a hard disk cannot exceed 4. The hard disk partition follows the "primary partition"→Extended partition→The order principle of "logical partitions", while deleting partitions is the opposite. Primary partition: a hard disk can be divided into multiple primary partitions, but there is no need to divide so many. One is enough. Extended partition: the hard disk space outside the primary partition is the extended partition, Logical partition: it is obtained by re dividing the extended partition. Start: Start subscript of partition cylinder; End: End subscript of partition cylinder; Blocks: Number of blocks for this partition. Id: The file systems of various partitions are different, if any ntfs Partition, fat32 Partition, ext3 Partition, swap Partition, etc. each file system has a code corresponding to the Id. Common file systems ID yes: f: FAT32 Extend,Extended partitions only. 86: NTFS. 7: HPFS/NTFS b: FAT32. 83: Linux Ext2. 82: Linux Exchange area. System: File system name.
5.2 display the partition of the specified equipment
# fdisk -l /dev/vdb Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xeecbd2e1 Device Boot Start End Blocks Id System /dev/vdb1 2048 209715199 104856576 83 Linux [root@iz8vb6yk5a7gzhaw7901zkz ~]#
5.3 create partitions and file systems for the specified devices and mount them to make the disks available
5.3.1 create a partition
1.Run the following command to partition the data disk. fdisk -u /dev/vdb 2.input p View the partition of the data disk. In this example, the data disk has no partitions. 3.input n Create a new partition. 4.input p Select the partition type as the primary partition. Description to create a single partition data disk, you can create only the primary partition. If you want to create more than four partitions, you should select at least one partition e(extended),Create at least one extended partition. 5.Enter the zone number and press enter. In this example, only one partition is created, press enter directly, and the default value is 1. 6.Enter the first available sector number and press enter. In this example, press enter directly and the default value is 2048. 7.Enter the last sector number and press enter. In this example, only one partition is created. Press enter directly and use the default value. 8.input p View the planned partition of the data disk. 9.input w Start partitioning and exit when partitioning is complete.
The results are as follows:
5.3.2 viewing new partition information
fdisk -lu /dev/vdb
The running results are as follows. If the information about / dev/vdb1 appears, it indicates that the new partition has been created.
5.3.3 create file system for partition
Create a file system on the new partition. The following commands describe how to create ext4 and xfs file systems. You can create corresponding file systems according to your actual needs.
-
Run the following command to create an ext4 file system.
mkfs -t ext4 /dev/vdb1
-
Run the following command to create an xfs file system.
mkfs -t xfs /dev/vdb1
In this example, an ext4 file system is created.
5.3.4 configure / etc/fstab file and mount partition
Write the new partition information in / etc/fstab and start the boot to mount the partition automatically.
Note: it is recommended that you use the globally unique identifier UUID in / etc/fstab to refer to the new partition. Releasing the cloud disk may cause the device name of other cloud disks to change. If you use the device name directly in / etc/fstab, your storage data may be affected when the device name changes.
1.Run the following command to back up/etc/fstab File. cp /etc/fstab /etc/fstab.bak 2.stay/etc/fstab Write new partition information in. If it is root You can run the following command to modify it directly/etc/fstab File. If you are an ordinary user, you can modify it manually/etc/fstab file echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab Of which: /dev/vdb1: The data disk partition of the file system has been created. You need to modify the corresponding partition name according to the actual situation. /mnt: Mount( mount)You need to modify the directory node according to the actual situation. ext4: The file system type of the partition. You need to modify it according to the file system type created. be careful: explain Ubuntu 12.04 System does not support barrier,Need to run echo '`blkid /dev/vdb1 | awk '{print $3}' | sed 's/\"//G '` / MNT ext4 barrier = 0' > > / etc / fstab command. 3.see/etc/fstab New partition information in. cat /etc/fstab
The results are as follows. If the message of creating a new file system appears, the file system is mounted successfully
4.mount /etc/fstab Configured file system mount -a 5.Check the mount results. df -h
The results are as follows. If the message of creating a new file system appears, the file system is mounted successfully.
This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog One Linux command a day (26): fdisk command - Joshua 317 blog