1. Disk Management
Disk is our daily life of the most important storage medium, in the industry also has a lot of use, for a hard disk, we can not directly read the above data, because the disk is binary data, magnetic represents 1, no magnetism represents 0, read data is the computer operating system to help us do, So how does the computer read the data on the disk?
The computer through the magnetic head to check the disk has no magnetism to read data, the disk has 3 sets of important data, namely the cylinder number, the disc number (also known as the number of magnets), the sector area code, through the 3 sets of data we can locate the sector on the disk, the system through other operations can find the specific presence of the data.
The smallest module of a disk is called a block, that is, blank, so we call everything a block-based device called a block device.
1. Partitioned table structure
There will be at least one partition on a hard drive, and we can view the current partition table structure with the following command:
hexdump -C -n 512 -v /dev/sda
-C means using 16 binary display,-N to show how many bytes before, because the partition table structure is stored in the first 512 bytes, all we only need to view the first 512 bytes,-v do not ignore 0, if not plus-V, the partition table in the structure of the 0 ignore unrealistic. My current partition table structure is as follows:
In the first 512 bytes, it can be divided into three parts, where 0-446 bytes represents the boot startup program at system startup, No. 447 to No. 510 byte is the real partition table structure, NO. 511 and No. 512 byte 55AA represents the end flag.
In the No. 447 to No. 510 byte, a total of 64 bytes, for the MBR partition table structure, the 64 bytes are divided into 4 parts, each part is a partition table, each part is 16 bytes, the first byte is 80 or 00, 80 represents the active partition, 00 represents the inactive partition, The MBR partition table structure uses the CHS addressing method, so the second to the four bytes represents the beginning of the partition of the magnetic number, the sector code, the cylinder number, where the second byte 8 bits represents the magnetic number one, the third byte low 6 bits represents the sector area code, the third byte of the high two bits with the fourth byte 8 bits altogether 10 bits represents the The five bytes represent the partition type, and if 0 means that it is not occupied, we can view all the partition table types with the following command
fdisk /dev/sdaL/l
L or L can view all the partition table types, and then the next three bytes represent the end of the partition table of the magnetic number, the sector area code, the number of cylinders, the 6th byte represents the end of the magnetic number, the seventh byte low six indicates the sector area code, the high two bits and the eighth byte represents the end of the cylinder number, the The maximum addressable space is only 8G, with 24-bit addressing, that is, there are 2^24 sectors, each sector 512B, that is, 2^9b, to obtain a total of 2^33b, that is 8G, but most of the hard disk size is greater than 8G, this time can only use LBA addressing mode, LBA using 48-bit addressing, The maximum addressable space is 128PB, which is 2^48*2^9b, and the result is 128PB. Whereas the latter 8 bytes of the partitioned table is the LBA addressing method, the first 4 bytes represent the starting bit, the last 4 bytes represent the end bit, the 32-bit addressing method, the addressing range is 2T, the maximum partition size of all MBR partitioned table structure is 2T, if more than 2T, can only adopt the GPT partition table structure.
However, 64 bits can only have 4 partitions, which is not enough in actual use, it is necessary to extend the partition, the extended partition needs to sacrifice a primary partition to identify the extended partition, point to the first extended partition, the extended partition also has a partition table structure, also 512 bytes, but the first 446 bytes are all 0, The last two bytes are still 55AA for the end of the partition table, the middle 64 bytes, also divided into 4 16 bytes, the first 16 bytes is exactly the same as the primary partition 16 bytes, the second 16 bytes point to the next Extended partition table, the latter two 16 bytes are temporarily unused, so that we can create any number of partitions.
As for the GPT partitioned table structure, temporarily do not learn.
2. Partition Management
We can use LSBLK to list all current block devices, for example:
lsblk
You can use three commands to create a partition, namely Fdisk,parted,gdisk.
Fdisk
You can use the FDISK command to view partitions.
fdisk -l
Lists all the partition table information on the current hard disk, such as
fdisk -l /dev/sda
Lists the partition tables for/DEV/SDA, such as
You can use the FDISK command to manage partitions, such as
fdisk /dev/sda
With the above command, you will be left out of the current shell and into the admin partition Table interface, here are some subcommands:
p, view a list of current partitions, such as:
o, create a new partition table structure, the partition table structure for, msdos, that is, the MBR partition table structure, before we add a hard disk and make it effective, the following command takes effect:
echo ‘- - -‘>/sys/class/scsi_host/host0/scan 或
Then use O to create a new partitioned table structure, as follows
T, change the partition type, such as:
n, create a new partition, such as:
P represents the creation of the primary partition, E for the extended partition, and then enter the partition number, and the partition start cylinder and partition size, can be the end cylinder number, or can be + partition size, Unit is K,M,G
Can be viewed with P, for example:
d, delete the partition, such as:
W, Save the partition and exit, as
When we create a partitioned table, we can use P to view the information of the partition table on the admin page, but if we leave the current page it will not take effect, we can use W to make it effective, for example:
W Front
Save
W Rear
Q, do not save exit
After the partition is created, you can use the following command to see if the kernel has identified the new partition table, for example:
cat /etc/partations
You can see that the new partition SDB1 has been recognized by the kernel, but when we add a new partition to the system disk, the situation will be different, for example:
Create a new partition Sda6:
Use the command above to see if it is recognized by the kernel:
We can see that there is no sda6, because the OS in the system disk has been read, not re-read by the kernel again, and the next reboot takes effect, so that it takes effect immediately as follows:
partprobe /dev/sda
This command is available in Centos7 and before 6, but 6 is unavailable, and the following command can be used in CENTOS6:
partx -a /dev/sda
Then use the previous command to view:
Gdisk
The Gdisk command is similar to the FDISK command, except that the GPT partition is created and is still using the MSDOS partitioned table structure, for example:
Parted
parted -l
List partition information:
parted /dev/sda print
Lists information for the specified partition, for example:
You can create a partition with the following command
parted /dev/sdb mkpart primary 1 2G
1 represents starting from the first byte, 2G for size 2G
The parted is in real time and is used with care during use.
Disk Management under Linux