Managing file systems on Linux
Link: https://www.cnblogs.com/f-ck-need-u/p/7048971.html
Mechanical hard disk
Mechanical hard disks are composed of multiple platters, all of which rotate around the spindle. There are read-write heads suspended above and below each platter, and the distance between them and the platter is extremely small. Every time data is read or written, the platter rotates, and the read-write head is controlled by the magnetic arm to move continuously to read the data.
All the platters rotate synchronously at the same time, and all the heads move synchronously.
The disk is physically divided into sectors, tracks, and cylinders. If partitions are used, the partitions are logically separated by cylinder boundaries.
When the read/write head is stopped, the track swept by the head when the disk rotates is called a track. All tracks are concentric circles. Starting from the outer circle of the disk and counting inward, the track number starts from 0 and increases gradually.
Each track is divided into multiple arc segments of 512 bytes, and each arc segment is a sector. However, it should be noted that the size of a sector is not necessarily 512 bytes. Therefore, the number of sectors in the outer circle tracks is larger, and the number of sectors in the inner circle tracks is smaller. The number of track sectors written on some hard disk parameters is usually marked with a range, such as 373-768 , which means that the outermost circle track has 768 sectors and the innermost circle has 373 sectors. This can calculate the number of bytes of each track.
The sector records the physical data, sector number, head number ( or platter number ) and track number.
The structure of the old disk is without zones . Each track has the same number of sectors, but each sector is still 512 bytes, which means that the number of 0s and 1s recorded by the magnetic material is the same. The disadvantage of this structure is that the outer track area is large, the stored data is loosely distributed, and the inner track area is small, the stored data is densely distributed, which leads to a waste of the outer area of the disk.
The new disk structure is partitioned, dividing each track into 512- byte spaces as a sector, so different tracks have different number of sectors.
Today's disks all use a new sector division structure.
All the tracks with the same number of tracks on the disk are divided into cylinders. The number of tracks is the same as the marking method, starting from 0 and increasing from the outside to the inside.
The reason for dividing the cylinders is that all disks rotate synchronously, all heads move synchronously, and all heads are always on the same track and the same sector at any time. When reading and writing data, any piece of data is always read and written by cylinder. Therefore, the more platters there are, the fewer sectors are scanned for reading and writing, the less time is required, and the better the performance is.
Data is written to the disk from the outer cylinder to the inner cylinder, and data is written to the next cylinder only after one cylinder is completed.
5.2 Disk or partition capacity calculation
Although today's disks all have a new structure (with a different number of sectors per track), the disk information is still calculated based on the old structure, that is, the number of sectors per track is the same, and the "sector/track" value indicates how many sectors there are on each track, which can also be considered as the average value under the new structure.
Disk related English:
disk |
disk |
heads |
Heads. The heads you see in the Linux system generally include many virtual heads. The actual physical disk has two heads, one on each side of the upper and lower sides. |
sectors |
Sector: A track is divided into multiple sector-shaped areas, and generally one sector is 512 bytes by default. |
track |
Track. One circle on the platter is considered a track. |
cylinders |
Cylinder. All tracks of the same radius on the disk form a cylinder. Number of cylinders = number of disks * number of tracks on the disk. |
units |
Unit block. The size is equal to the size of a cylinder. |
Disk or partition size calculation method:
Disk size = units × number of cylinders
Disk size = number of heads (heads) × number of sectors per track (sectors) × 512 × number of cylinders (cylinders)
For example: view /dev/sda3.
[root@xuexi tmp]# fdisk -l /dev/ sda3
Disk /dev/sda3: 19.1 GB, 19116589056 bytes # Total size 19G
255 heads, 63 sectors/track, 2324 cylinders # 255 heads, 2324 cylinders, 63 sectors/track (this is the average)
Units = cylinders of 16065 * 512 = 8225280 bytes # Unit block size
Sector size (logical /physical): 512 bytes / 512 bytes
I /O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Units = 255 × 63 × 512 = 16065 × 512 = 8225280
Disk size = 255*63*2324*512=19115550720= 19.11555072GB, don't use 1024, use 1000.
5.3 Partitioning
Partitions are used to logically separate certain cylinders to form boundaries. They are divided by cylinders, but starting with CentOS 7, they are divided by sectors.
When the amount of disk data is very large, the advantage of partitioning is that scanning block bitmaps is faster: there is no need to scan the block bitmap of the entire disk, only the block bitmap of the corresponding partition needs to be scanned.
5.3.1 Partitioning Methods (MBR and GPT)
In an MBR formatted disk, the first sector of the disk - the MBR sector - is maintained. In this sector, the 64 bytes after the 446th byte are the partition table. Each partition occupies 16 bytes, so a disk is limited to a maximum of 4 primary partitions (Primary, P). If there are more than 4 partitions, the primary partition can only be reduced to 4 by creating an extended partition (Extend, E), and then creating a logical partition (Logical, L) in the extended partition to break through the 4-partition limit. There is no limit on the number of logical partitions.
In Linux, the primary partition numbers of MBR formatted disks range from 1 to 4, the extended partition numbers range from 2 to 4, and the logical partition numbers start from 5.
For example, if you want to divide a disk into 6 partitions, you can:
1P+5L:sda1+sda5+sda6+sda7+sda8+sda9
2P+4L: sda1+sda2+sda5+sda6+sda7+sda8
3P+3L: sda1+sda2+sda3+sda5+sda6+sda7
The GPT format breaks through the limitations of MBR. It is no longer limited to storing only 4 partition table entries. Instead, it uses a format similar to the MBR extended partition table entry, which allows 128 primary partitions, making it possible to partition disks larger than 2TB.
5.3.2 MBR and GPT partition table information
In the MBR format partition table, the MBR sector occupies 512 bytes, the first 446 bytes are the master boot record, i.e. the boot loader. The middle 64 bytes record the partition table information, each primary partition information occupies 16 bytes, so there can be at most 4 primary partitions, and the last 2 bytes are valid identification bits. If an extended partition is used, the 16 bytes corresponding to the extended partition record the pointer to the extended partition table in the extended partition.
On an MBR disk, partition and boot information are stored together. If this part of the data is overwritten or damaged, the only way is to rebuild the MBR. GPT saves multiple copies of this information on the entire disk, so it is more robust and can recover the damaged information. GPT also saves cyclic redundancy check codes (CRC) for this information to ensure its integrity and correctness. If the data is damaged, GPT will detect the damage and recover it from other places on the disk.
The following is the partition table information in GPT format, which occupies approximately 17 bytes.
The EFI part can be divided into 4 areas: EFI information area (GPT header), partition table, GPT partition area and backup area.
-
EFI information area (GPT header): starts at LBA1 of the disk and usually only occupies this single sector. Its function is to define the location and size of the partition table. The GPT header also contains the checksum of the header and partition table, so that errors can be discovered in time.
-
Partition table: The partition table area contains partition table entries. This area is defined by the GPT header and generally occupies the disk LBA2 to LBA33 sectors. Each sector can store the partition information of 4 primary partitions, so a total of 128 primary partitions can be divided. Each partition entry in the partition table consists of a start address, an end address, a type value, a name, an attribute flag, and a GUID value. After the partition table is created, the 128-bit GUID is unique to the system.
-
GPT partition: The largest area, consisting of sectors allocated to the partition. The start and end addresses of this area are defined by the GPT header.
-
Backup area: The backup area is located at the end of the disk and contains the backup of the GPT header and partition table. It occupies 33 sectors between the GPT end sector and the EFI end sector. The last sector is used to back up the EFI information of sector 1, and the remaining 32 sectors are used to back up the partition table of sectors LBA2 to LBA33.
5.3.3 Adding a disk
Normally, after adding a disk, you need to restart the system so that it can be recognized by the kernel, and the corresponding device number will appear under /dev/, and it will be displayed using fdisk -l. However, sometimes it is inconvenient to restart, so the following is a disk hot-swap method. For more hot-swap and hot-unplug methods, see my other article: Disk Hot-Swap on Linux .
[root@node1 ~]# ls /sys/class/scsi_host/ # View the host scsi bus number
host0 host1 host2
Rescan the SCSI bus to add new devices in hot-plug mode.
[root@node1 ~]# echo " - - - " > /sys/class/scsi_host/host0/ scan [root@node1 ~]# echo " - - - "
> /sys/class/scsi_host/host1/ scan [root@node1 ~]# echo " - - - " > / sys / class / scsi_host/host2/ scan [root@node1 ~]# fdisk -l # Check again and you will find it
If the scsi_host directory contains many hostN directories, a loop is used to complete it.
1 host16 host19 host21 host24 host27 host3 host32 host6 host9 [ root@xuexi scsi_host]# ls /sys/class/scsi_host/
host0 host11 host14 host17 host2 host22 host25 host28 host30 host4 host7
host1 host12 host15 host18 host20 host23 host26 host29 host31 host5 host8 host10
host13 host16 host19 host21 host24 host27 host3 host32 host6 host9
[root@xuexi scsi_host]# ls /sys/class/scsi_host/ host0 host11 host14 host17 host2 host22 host25 host28 host30 host4 host7 host1 host12 host15 host18 host20 host23 host26 host29 host31 host5 host8 host10 host13 host16 host19 host21 host24 host27 host3 host32 host6 host9 [root@xuexi scsi_host] #
5.3.4 Using fdisk partition tool
The fdisk tool is used to partition MBR disks. To partition GPT disks, you can use gdisk. The parted tool supports both formats of disk partitions.
If a storage device has been partitioned, it may be in mbr format or gpt format. If it is already in mbr format, you can only use fdisk to partition it. If it is already in gpt format, you can only use gdisk to partition it. Of course, no matter what format it is, you can use parted to partition it, but you can only create partitions with the same format as the existing partitions, because no matter what format the partition is, its partition table and partition identifier are fixed.
Use fdisk partition, it can only achieve MBR format partition.
[root@xuexi ~]# fdisk /dev/ sdb # There is no number after sdb
Command (m for help): m # Enter m to view available command help
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition # Delete a partition. If you delete an extended partition, the logical partitions inside will also be deleted
l list known partition types # List partition types
m print this menu # Display help information
n add a new partition # Create a new partition
o create a new empty DOS partition table
p print the partition table # Output partition information
q quit without saving changes # Exit without saving changes
s create a new empty Sun disklabel
t change a partition 's system id # Change the partition type
u change display/ entry units
v verify the partition table
w write table to disk and exit # Save partition information and exit
x extra functionality (experts only)
Create the first primary partition:
Command (m for help): n # add a partitionCommand
action
e extended # add an extended partitionp
primary partition ( 1 - 4 ) # add a primary partitionp
# Enter p to create the first primary partitionPartition
number ( 1 - 4 ): 1 # Enter the partition number, starting from 1First
cylinder ( 1 - 1305 , default 1 ): # Enter the cylinder number. If not entered, the default is 1.
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} ( 1 - 1305 , default 1305 ): +2G # Give the first primary partition /dev/ sdb1 2G. You can also use the cylinder number to specify the size.
sdb1 1 262 2104483 + 83 Linux WARNING : Restart the hard disk drive of / dev / sdb1. Start of the new hard disk drive . Start of the new hard disk drive . Start of the new hard disk drive . Start of the new hard disk drive . Start of the new hard disk drive .
Create a new extended partition:
sdb : 10.7 GB , 10737418240 bytes 255 heads , 63 sectors / track , 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size ( logical / physical ) : 512 bytes / 512 bytes I /O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x2d8d64eb Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483 + 83 Linux /dev/sdb2 263 1305 8377897 + 5 Extended
Create a new extended partition:
Command (m for help): n # Create a new logical partition
Command action
l logical ( 5 or over) # There is no longer an extended partition identifier e here, only l.
sdb : 10.7 GB , 10737418240 bytes 255 heads , 63 sectors / track , 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (
logical / physical ) : 512 bytes / 512 bytes I / O size ( minimum / optimal ) : 512 bytes / 512 bytes Disk identifier : 0x2d8d64eb Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483 + 83 Linux /dev/sdb2 263 1305 8377897 + 5 Extended /dev/sdb5 263 655 3156741 83 Linux
The partition is complete, save. If you do not want to save, press q.
Command (m for help): w The partition table has been altered ! Calling ioctl() to re - read partition table. Syncing disks.
The partitioning process is essentially dividing the cylinders and modifying the partition table.
All the fdisk operations above are executed in memory and must be saved to take effect. After saving, the kernel has not yet recognized the partition. You can view the files in the /proc/partition directory. These files are partitions that can be recognized by the kernel. Run the partprobe or partx command to re-read the partition table to let the kernel recognize the new partition. Only after the kernel recognizes it can it be formatted. In addition, when the partition is completed, pressing w to save the partition table may sometimes fail and prompt a reboot. At this time, running the partprobe command can take effect instead of rebooting.
[root@xuexi ~ ]# partprobe # Execute partprobe. There are a lot of information below. Ignore it.
Warning: WARNING: the kernel failed to re -read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
Warning: Unable to open /dev/sr0 read- write (Read-only file system). /dev/sr0 has been opened read- only.
Warning: Unable to open /dev/sr0 read- write (Read-only file system). /dev/sr0 has been opened read- only.
Error: Invalid partition table - recursive partition on /dev/sr0.
You can also specify to reload the partition table on /dev/sdb to avoid being unable to read the busy /dev/sda disk, giving the above information.
[root@xuexi ~]# partprobe /dev/sdb
After partitioning, use fdisk -l to view the new partition status.
The commands needed for the above partitioning are summarized below to facilitate the use of script partitioning in the future.
-
s fdisk /dev/sdb # Select the device to be partitioned
-
n # Create partitions
-
p/e/l # Select the partition type
If there are three primary partitions and an extended partition has been created, you can only create logical partitions when you continue partitioning. In this case, option l will skip directly to the next stage.
-
s N #Specify the partition number
-
\n # Specify the starting cylinder number. If you use the default value, just press Enter to enter the new line.
-
+N # Specify the partition size as N
-
w # Partition completed, save and exit
-
partprobe /dev/sdb &>/dev/null # Reread the partition table
-
fdisk -l | grep "^/dev/sdb" &>/dev/null # Check partition status
5.3.5 Using the gdisk partitioning tool
gdisk is used to divide gpt partitions, and this tool package needs to be installed separately.
shell> yum -y install gdisk
When partitioning, just bring the device. The following is the process of dividing the new hard disk into gpt partitions.
[root@xuexi ~]# gdisk /dev/ sdb
GPT fdisk (gdisk) version 0.8 . 10
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Command ( ? for help): ?
b back up GPT data to a file
c change a partition ' s name
d delete a partition # Delete partitioni
show detailed information on a partition # List partition detailsl
list known partition types # List all known partition typesn
add a new partition # Add a new partitiono
create a new empty GUID partition table (GPT) # Create a new empty GUID partition tablep
print the partition table # Output partition table informationq
quit without saving changes # Exit the gdisk toolr
recovery and transformation options (experts only)
s sort partitions
t change a partition ' s type codev
verify disk
w write table to disk and exit # Write partition information to diskx
extra functionality (experts only)
? print this menu
Add a new partition.
000 ) or { +- } size {KMGTP}: + 10G Current type is ' Linux filesystem ' Hex code or GUID (L to show codes, Enter = 8300 ) : Changed type of partition to ' Linux filesystem ' Command ( ? for help
) : p Disk /dev/sdb: 41943040 sectors, 10.0 GiB Logical sector size: 512 bytes Disk identifier ( GUID ) : F8AE925F -515F- 4807 -92ED- 4109D0827191 Partition table holds up to 128 entries First usable sector is 34 , last usable sector is 41943006 Partitions will be aligned on 2048 - sector boundaries Total free space is 20971453 sectors ( 10.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 20973567 10.0 GiB 8300 Linux filesystem Command ( ? for help): i # View partition detailsUsing 1 Partition GUID code: 0FC63DAF - 8483 - 4772 -8E79- 3D69D8477DE4 (Linux filesystem) Partition unique GUID: B2452103 -4F32-4B60-AEF7- 4BA42B7BF089 First sector: 2048 (at 1024.0 KiB) Last sector: 20973567 (at 10.0 GiB) Partition size: 20971520 sectors ( 10.0 GiB) Attribute flags: 000000000000000 Partition name: ' Linux filesystem '
Save the partition table to disk.
Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS !! Do you want to proceed ? (Y/ N): Y OK; writing new GUID partition table (GPT) to /dev/ sdb. The operation has completed successfully.
Run partprobe to re-read the partition table information.
[root@server2 ~]# partprobe /dev/sdb
gdisk also has several expert-only commands, which are actually not useful to both experts and non-experts. What we need to know is when the commands can be used and what their functions are.
In the gdisk interactive process command line, press x to enter the extended function mode. Most of the functions in this mode are related to the gpt partition table. It is not recommended to modify the gpt partition table structure if you are not very familiar with it, but it is okay to view the information. The following are the commands in the extended function mode.
Command (? for help): x
Expert command ( ? for help): ?
a set attributes
c change partition GUID
d display the sector alignment value
e relocate backup data structures to the end of the disk
g change disk GUID
h recompute CHS values in protective/ hybrid MBR
i show detailed information on a partition
l set the sector alignment value
m return to main menu
n create a new protective MBR
o print protective MBR data
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s resize partition table
t transpose two partition table entries
u Replicate partition table on new device
v verify disk
w write table to disk and exit
z zap (destroy) GPT data structures and exit
? print this menu
5.3.6 Using the parted partition tool
Parted supports disk partitions in mbr and gpt formats. Its power lies in that it can be done in one step without constant interactive input (it can also be interactive).
The parted partition tool is real-time, so each step of the operation is written directly to the disk instead of into the memory. It is not like fdisk/gdisk which requires the w command to save the results in the memory to the disk.
[root@xuexi ~]# parted /dev/ sdc
GNU Parted 2.1
Using /dev/ sdc
Welcome to GNU Parted ! Type ' help ' to view a list of commands.
(parted) help
align -check TYPE N check partition N for TYPE(min| opt) alignment
check NUMBER do a simple check on the file system(This function has been deleted on centos 7)
cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER copy file system to another partition(This function has been deleted on centos 7)
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL - TYPE create a new disklabel (partition table)
mkfs NUMBER FS -TYPE make a FS-TYPE file system on partition NUMBER (This function has been deleted on centos 7)
mkpart PART -TYPE [FS-TYPE] START END make a partition
mkpartfs PART -TYPE FS-TYPE START END make a partition with a file system(This function has been deleted on CentOS 7)
move NUMBER START END move partition NUMBER(This function has been deleted on CentOS 7)
name NUMBER NAME name partition NUMBER as NAME
print [devices |free|list,all|NUMBER] display the partition table,available devices,free space, all found partitions,or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resize NUMBER START END resize partition NUMBER and its file system(Modify the partition size(This function has been deleted on CentOS 7))
rm NUMBER delete partition NUMBER (Delete partition)
select DEVICE choose the device to edit (Reselect the disk to enter parted state)
set NUMBER FLAG STATE change the FLAG on partition NUMBER(Set the partition state, such as turning it off or on)
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER (modify the file system type, such as swap, lvm)
unit UNIT set the default unit to UNIT (modify the default unit, kB /MB/ GB, etc.)
version display the version number and copyright information of GNU Parted
Commonly used commands are mklabel/rm/print/mkpart/help/quit. Some seemingly good functions in parted, such as mkfs/mkpartfs/resize, may damage current data and are not safe enough, so just use its 5 commonly used commands.
The premise of parted partitioning is that the disk already has a partition table (partition table) or disk label (disk label), otherwise it will display "unrecognised disk label", which is different from fdisk/gdisk, so you need to use mklabel to create a label or partition table first. The most common labels (partition tables) are "msdos" and "gpt", among which msdos partition is a partition table in MBR format, that is, there will be concepts and restrictions on primary partitions, extended partitions and logical partitions.
Next, use parted to create a new msdos partition for /dev/sdc.
[root@xuexi ~]# parted /dev/ sdc
GNU Parted 2.1
Using /dev/ sdb
Welcome to GNU Parted ! Type ' help ' to view a list of commands.
(parted) mklabel # Create a disk partition label (partition table type)
New disk label type ? msdos # Select msdos, i.e., MBR type
# The above two steps can also be performed in one step: (parted) mklabel msdos
(parted) mkpart # Start partitioning
Partition type ? primary/extended? p # Create a primary partition
File system type ? [ext2]? ext4 # Create an ext4 file system
# (Note that although the file system is specified here, it does not make any sense. You still need to manually format and select the file system type later)
Start ? 1 # The starting position of the partition. The default unit is M, which means starting from 1M. You can also directly specify 1G.
End ? 1024 # The end position of the partition, 1024 - 1 = 1023M
(parted) p sdc
: 21.5GB Sector size (logical /physical): 512B/ 512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary # You can complete multiple actions in one command in one step (parted) mkpart p ext4 1026M 4096M # You can complete it in one step
, or you can complete it in one step to any position, and then continue to the next step interactively # You may be prompted that the partition is not properly aligned " Warning: The resulting partition is not properly aligned for best performance. " , ignore it (parted) mkpart e 4098 -1 # Create an extended partition. Note that the file system type is not specified when creating an extended partition; - 1 means that all the remaining partitions are allocated to the partition (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 21.5GB Sector size (logical /physical): 512B/ 512B /physical): 512B/ 512B Partition Table: msdos Number Start End Size Type File system Flags 1 1049kB 1024MB 1023MB primary 2 1026MB 4096MB 3070MB primary 3 4098MB 21.5GB 17.4GB extended lba (parted) mkpart l ext4 4099 8194 # Create a logical partition, specify ext4 (parted)
.5GB 17 .4GB logical (parted) rm 5 # Delete partition 5
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21 .5GB
Sector size (logical /physical): 512B/ 512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1024MB 1023MB primary
2 1026MB 4096MB 3070MB primary
3 4098MB 21 .5GB 17 .4GB extended lba
5 4099MB 8194MB 4095MB logical
6 8195MB 21 .5GB 13 .3GB logical
(parted) rm 5 # Delete partition 5
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 21 .5GB
Sector size (logical /physical): 512B/ 512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 1049kB 1024MB 1023MB primary
2 1026MB 4096MB 3070MB primary
3 4098MB 21 .5GB 17 .4GB extended lba
5 8195MB 21 .5GB 13 .3GB logical
(parted) quit # Exit parted tool
Information: You may need to update /etc/fstab. # The prompt that you need to update the configuration in /etc/fstab indicates that the tool can perform online partitioning
The mkfs and mkpartfs commands are not perfect, and the following warning message has given a hint.
(parted) mkfs 1 WARNING: you are attempting to use parted to operate on (mkfs) a file system. parted ' s file system manipulation code is not as robust as what you ' ll find in dedicated, file -system- specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Warning: The existing file system will be destroyed and all data on the partition will be lost. Do you want to continue ? parted: invalid token: 1 Yes /No? n
Partitioning with the parted tool does not require running partprobe to re-read the partition table. The kernel will immediately recognize the partition information that has been partitioned. As shown below.
[root@xuexi tmp]# cat /proc/partitions | grep " sdc "
8 32 20971520 sdc
8 33 999424 sdc1
8 34 2998272 sdc2
8 35 1 sdc3
8 37 12967936 sdc5
It is important to note that although the file system is specified in the parted tool, it does not make sense, it still needs to be formatted manually and the partition type specified. In fact, the file system does not need to be specified in parted, even in non-interactive mode.
5.3.7 Non-interactive partition operations with fdisk/gdisk and parted
When using non-interactive partitioning, the most important thing is that the starting point of the partition to be partitioned cannot be in use. You can use lsblk or fdisk -l or parted DEV print to determine where the partition will start. In fact, parted is the best tool for non-interactive partitioning, not only because of its concise writing style, but also because it will automatically prompt whether to adjust the starting point of the partition to be partitioned if it is unreasonable.
5.3.7.1 Parted implements non-interaction
The parted command can only perform all actions in one command at a time, non-interactively. As shown below:
parted /dev/ sdb mklabel msdos # Set the hard disk flag
parted /dev/sdb mkpart primary ext4 1 1000 # Mbr format partitions, respectively partition type/fstype/start/ end
parted /dev/sdb mkpart 1 ext4 1M 10240M # GPT format partitions, respectively name/fstype/start/ end
parted /dev/sdb mkpart 1 10G 15G # Omit the interactive partition of fstype
parted /dev/sdb rm 1 # Delete the partition
parted /dev/sdb p # Output information
If you are not sure about the starting size of the partition, you can add the -s option to use script mode. In this mode, parted will answer all default values, such as yes or no.
shell> parted -s /dev/sdb mkpart 3 14G 16G
Warning: You requested a partition from 14.0GB to 16.0GB .
The closest location we can manage is 15.0GB to 16.0GB .
Is this still acceptable to you ?
Information: You may need to update /etc/fstab.
5.3.7.2 fdisk implements non-interaction
The principle of fdisk's non-interactive implementation is to read from standard input and pass an operation for each line read.
So there are two ways: use echo and pipe passing; write operations to files and read from files.
For example, the following command creates two partitions. To use the default value, just pass a blank line.
echo -e " n\np\n1\n\n+5G\nn\np\n2\n\n+1G\nw\n " | fdisk /dev/sdb
If you have many operations to pass, you can write them to a file and read them from it.
echo -e " n\np\n1\n\n+5G\nn\np\n2\n\n+1G\nw\n " >/tmp/ a.txt
fdisk /dev/sdb </tmp/a.txt
5.3.7.3 Non-interactive gdisk
The principle is the same as fdisk. For example:
echo -e " n\n1\n\n+3G\n\nw\nY\n " | gdisk /dev/sdb
The meanings of the parameters passed above are: create a new partition, partition number is 1, use the default start sector position, partition size +3G, use the default partition type, save, and confirm.
5.4 Formatting Partitions
After partitioning, you need to format and create a file system. The process of formatting a partition is the process of creating a file system. You can use the mkfs (make filesystem) tool to format, or you can use other tools in the tool family such as mkfs.ext4/mkfs.xfs and other tools specifically for file systems.
To view the supported file system types, simply enter mkfs and press the tab key twice to list the formatting commands corresponding to each file system. These are the supported file system types.
Supported on CentOS 6:
[root@xuexi ~ ]# mkfs
mkfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev mkfs.msdos mkfs.vfat
Supported on CentOS 7:
[root@server2 ~ ]# mkfs
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.fat mkfs.minix mkfs.msdos mkfs.vfat mkfs.xfs
5.4.1 mkfs tool
mkfs [-t fstype] partition
The tool is very simple. It only requires an optional "-t" option to specify the type of file system to be created. If omitted, an ext2 file system is created by default. The "-t" option specified by the tool actually calls the formatting tool dedicated to the corresponding file system.
5.4.2 mke2fs tool
mkfs.ext2/mkfs.ext3/mkfs.ext4 or mkfs -t extX are actually calling the mke2fs tool.
When the tool creates a file system, it reads the default configuration items from the /etc/mke2fs.conf configuration.
mke2fs [ -c ] [ -b block-size ] [ -f fragment-size ] [ -g blocks-per-group ] [ -G number-of- groups ]
[ -i bytes-per-inode ] [ -I inode-size ] [ -j ] [ -N number-of-inodes ] [ -m reserved-blocks-percentage ]
[ -q ] [ -r fs-revision-level ] [ -v ] [ -L volume-label ] [ -S ] [ -t fs-type ] device [ blocks- count ]
Option description:
-t fs-type: Specifies the type of file system to be created (ext2, ext3 ext4). If not specified, the default file system type is obtained from /etc/mke2fs.conf.
-b block-size: Specifies the size of each block. Valid values are 1024, 2048, and 4096. The unit is bytes.
-I inode-size : Specifies the inode size in bytes. It must be a power of 2 and greater than or equal to 128 bytes. The larger the value, the more space the inode table, which is the aggregate of inodes, takes up.
This not only occupies the available space in the file system, but also reduces performance because it takes more time to scan the inode table. However, after Linux kernel 2.6.10
, 128 bytes are no longer enough because inodes are used to store many extended additional attributes. Therefore, the default inode size of ext4 has become 256. Although
the inode size has increased, the performance improvement brought by using inodes to store extended attributes is much higher than the negative impact caused by the increase in inode size. Therefore, it is still recommended to use 256-byte
inodes.
-i bytes-per-inode : Specifies how many bytes to allocate an inode number for. The larger the value, the fewer inode numbers are allocated in a file system, which is more suitable for storing a large number of large files. The smaller the value
, the more inode numbers are allocated, which is more suitable for storing a large number of small files. This value cannot be less than the size of a block, because this will cause redundant inodes.
Note that this value cannot be changed after the file system is created.
- c : Check whether the device has bad blocks before creating the file system.
-f fragment- size : Specify the size of fragments in bytes.
-g blocks-per- group : Specify the number of blocks in each block group. It is not recommended to modify this option.
-G number-of- groups : This option is used for the ext4 file system (strictly speaking, the flex_bg feature is enabled). It specifies the number of block groups contained in a virtual block group (i.e., an extent). It must be a power of 2. For the ext4 file
system, using the extent function can greatly improve its performance.
-j : Create a file system with a log function, i.e., ext3. If you want to specify the settings for the log, use -J on the basis of -j , but the default is generally sufficient. For
specific options that can be specified, see the man document.
-L new-volume- label : Specify the volume label name. The name must not exceed 16 bytes.
-m reserved-blocks-percentage : Specify the ratio of the number of blocks reserved by the file system. Reserving a portion of blocks can reduce physical fragmentation. The default ratio is 5% .
-N number-of- inodes: Force the specification of how many inode numbers the file system should allocate. It will overwrite the result of the calculation of the number of inodes (
the number of Inodes is obtained based on the block size, number and number of bytes allocated for one inode), but it is not recommended.
- q: Quiet mode, which can be used in scripts
- S: Rebuild superblock and group descriptions. It is useful when all superblocks and backup superblocks are damaged. It will reinitialize the superblock and
group descriptions, but will not change the inode table, bmap and imap (if it is really changed, the partition data will be lost, so it is better to reformat it). After rebuilding
the superblock, e2fsck should be executed to ensure the consistency of the file system. But please note that the block size should be specified completely correctly, and this option cannot completely guarantee that
data will not be lost.
-v: Output detailed execution process
Therefore, the options that may be used are "-t" to specify the file system type, "-b" to specify the block size, "-I" to specify the inode size, and "-i" to specify the ratio of allocated inodes.
For example:
4096 (log= 2 ) Fragment size = 4096 ( log = 2 ) Stride = 0 blocks
, Stripe width = 0 blocks 655360 inodes , 2621440 blocks 131072 blocks ( 5.00 % ) reserved for the super user First data block = 0 Maximum filesystem blocks = 2684354560 80 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768 , 98304 , 163840 , 229376 , 294912 , 819200 , 884736 , 1605632 Writing inode tables: done Creating journal ( 32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Tip: Use tune2fs to modify the frequency of automatic file system detection. See below.
5.4.3 tune2fs modifies ext file system attributes
This tool is actually not very useful. After the file system is created, many attributes are fixed and cannot be modified. The attributes that can be modified are very limited and are all irrelevant.
But sometimes you can still use it to do something, for example, you will be prompted to modify the self-test time right after creating an ext file system.
tune2fs [ -c max- mount -counts ] [ -i interval-between-checks ] [ - j ] device
- j: upgrade ext2 file system to ext3;
-c: modify the maximum number of times the file system is mounted before self-checking, set to 0 or - 1 to never self-check;
-i: modify the time after which self-checking is performed. The time unit can be specified as day (default)/month/week [d|m| w ], set to 0 to never self-check.
For example: tune2fs -i 0 /dev/sdb1
5.4.4 Born for Experimentation: Quickly Create a File System
Sometimes it is quite troublesome to insert a new disk, scan SCSI devices, repartition and format it just for experimentation.
Fortunately, there is a more convenient way:
dd if =/dev/zero of=sdx bs=1M count= 32
mke2fs sdx
Now the sdx file is an ext family file system, equivalent to the formatted /dev/sdxN, which can be mounted directly.
mount sdx /mnt/sdx
You can also use the mkisofs command tool to quickly create a directory into a mountable image file:
mkdir -p foo/bar/ baz
mkisofs -o test.iso foo # Package foo into an iso image
Now, the test.iso image file can also be mounted directly:
mount test.iso /mnt/test
5.5 View file system status information
5.5.1 lsblk
lsblk (list block devices) is used to list devices and their status, mainly non-empty storage devices. In fact, it only lists the major and minor device number files in /sys/dev/block, and by default only non-empty devices are listed.
[root@server2 ~]# lsblk
The meanings of the above columns are as follows:
NAME: device name;
MAJ:MIN: major device number and this device number;
RM: Whether it is a removable device, 1 means a removable device. Removable devices include CDs, USBs, etc. Not all devices that can be umounted are removable;
SIZE: total device space size;
RO: whether it is read-only;
TYPE: whether it is a disk, a partition, a rom, or a loop device;
mountpoint: mount point.
[root@server2 ~]# lsblk /dev/ sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8 : 16 0 20G 0 disk
├─sdb1 8 : 17 0 9 .5G 0 part /mydata/ data
└─sdb2 8 : 18 0 3G 0 part
[root@server2 ~]# lsblk /dev/ sdb1
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb1 8 : 17 0 9 .5G 0 part /mydata/data
Another commonly used option is "-f", which can view the file system type, the file system's uuid and mount point.
[root@xuexi ~]# lsblk - f
NAME FSTYPE LABEL UUID MOUNTPOINT
sda
├─sda1 ext4 77b5f0da -b0f9- 4054 - 9902 -c6cdacf29f5e / boot
├─sda2 ext4 f199fcb4 -fb06-4bf5-a1b7-a15af0f7cb47 /
└─sda3 swap 6ae3975c -1a2a-46e3-87f3- d5bd3f1eff48 [SWAP]
sr0
sdb
├─sdb1 ext4 95e5b9d5 - be78-43ed-a06a- 97fd1de9a3fe
├─sdb2 ext2 45da2d94-190a - 4548-85bb - b3c46ae6d9a7
└─sdb3
Each formatted file system has its type and uuid, while an unformatted device (such as /dev/sdb3) will only display a Name result, indicating that the device has not been formatted.
5.5.2 blkid
Although it has many powerful functions, generally only one of them is used, which is to view the file system type and uuid.
= [root@xuexi ~ ]# blkid
/dev/sda1: UUID= " 77b5f0da-b0f9-4054-9902-c6cdacf29f5e " TYPE= " ext4 "
/dev/sda2: UUID= " f199fcb4-fb06-4bf5-a1b7-a15af0f7cb47 " TYPE= " ext4 "
/dev/sda3: UUID= " 6ae3975c-1a2a-46e3-87f3-d5bd3f1eff48 " TYPE= " swap "
/dev/sdb1: UUID= " 95e5b9d5-be78-43ed-a06a-97fd1de9a3fe " TYPE=" ext4 " /dev/sdb2: UUID="f199fcb4-fb06-4bf5-a1b7-a15af0f7cb47" TYPE= " ext4" " 45da2d94-190a-4548-85bb-b3c46ae6d9a7 " TYPE= " ext2 " [root@xuexi ~]# blkid /dev/ sdb1 /dev/sdb1: UUID= " 95e5b9d5-be78-43ed-a06a-97fd1de9a3fe " TYPE= " ext4 "
5.5.3 parted /dev/sda print and fdisk -l
21.5 GB, 1474836480 bytes, 41943040 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 WARNING : The manufacturer has not yet completed the process of resolving this issue . The manufacturer has not yet completed the process of resolving this issue . WARNING : The manufacturer has not yet completed the process of resolving this issue . label type: dos Disk identifier: 0x000cb657 Device Boot Start End Blocks Id System /dev/sda1 * 2048 514047 256000 83 Linux /dev/sda2 514048 37847039 18666496 83 Linux /dev/sda3 37847040 41943039 2048000 82 Linux swap / Solaris
Although fdisk and gdisk are dedicated tools for mbr and gpt formats respectively, they can still be used to view information. parted is compatible with both, so it can also be used.
5.5.4 file -s
[root@xuexi ~]# file -s /dev/ sdb2
/dev/sdb2: Linux rev 1.0 ext2 filesystem data (large files)
5.5.5 du
The du command is used to evaluate the space usage of files. It will count the size of each file and recursively count the files in the directory. That is, it will traverse the entire directory to be counted, so the statistical speed may not be ideal.
du [OPTION]... [FILE]...
Option description:
-a, --all : Lists the statistics of all files in the directory. By default, only the statistics of subdirectories in the directory are listed, but not the statistics of files.
-h, --human-readable: Human-readable display of size
-0 , --null : Ends with a null character, that is, " \0 " instead of " \n " for newlines -S, --separate- dirs : Does not include the size of subdirectories -s, --summarize: Make a total statistics of the directory, and do not list the size information of files in the directory -c, --total: Make a total of the given files or directories. It is very useful when counting the size of files in different directories. See the example below. -d, --max-depth: Specify the directory depth when displaying. By default, all levels will be recursively displayed. --max-depth=N: Only list directory statistics at a given level. If N=0, it is equivalent to "-s" -x, --one- file - system: Ignore files on different file systems and do not count them. -X, --exclude-from= FILE: Read files to be excluded from the file. --exclude=PATTERN: Specify files to be ignored and not counted.
Notice:
(1) Among the options above, some do not list certain items, and some do not count certain items. They are different.
(2) If a file system is mounted under the directory to be counted, the size of the file system will also be included in the size of the directory.
[root@xuexi ~]# du - sh / etc
29M /etc
[root@xuexi ~]# du -ah / tmp
4.0K /tmp/ b.txt
4.0K /tmp/ a
4.0K /tmp/.ICE- unix
4.0K /tmp/testdir/ subdir
0 /tmp/testdir/ a.log
8.0K /tmp/ testdir
24K /tmp
[root@xuexi ~]# du -h --max-depth= 1 / usr
15M /usr/ include
383M /usr/ lib64
132K /usr/ local
391M /usr/ share
4.0K /usr/ etc
118M /usr/ lib
44M /usr/ libexec
49M /usr/ src
32M /usr/ sbin
4.0K /usr/ games
75M /usr/ bin
1.1G /usr
[root@xuexi ~]# du -h --max-depth= 1 --exclude=/usr/lib64 / usr
15M /usr/ include
132K /usr/ local
391M /usr/ share
4.0K /usr/ etc
118M /usr/ lib
44M /usr/ libexec
49M /usr/ src
32M /usr/ sbin
4.0K /usr/ games
75M /usr/ bin
721M /usr
# The size of all files and directories in the statistician directory except hidden files
[root@xuexi ~]# du -h --max-depth= 1 --exclude= " .* " ~
# Files larger than 1M in the statistician directory
[root@xuexi ~]# du -h --max-depth= 1 -t 1M ~
Search for files that match the criteria and then count their total size. Used in conjunction with find, the effect is excellent.
0 - 327 .el7.x86_64kdump.img 76M total [root@xuexi ~]# find /boot/ -type f -name " *.img " -print0 | xargs - 0 du - ch
28K /boot/grub2/i386-pc/ core.img
4.0K /boot/grub2/i386-pc/ boot.img
592K /boot/initrd- plymouth.img
44M /boot/initramfs- 0 -rescue- d13bce5e247540a5b5886f2bf8aabb35.img
17M /boot/initramfs- 3.10 . 0 - 327 .el7.x86_64.img
16M / boot/initramfs- 3.10 . 0 - 327 .el7.x86_64kdump.img
76M total
Please note the difference between "-c" and "-s" statistics.
0 - 327 .el7.x86_64kdump.img 16M /boot/initramfs- 3.10 . 0 - 327 .el7.x86_64kdump.img [root@xuexi ~]# find /boot/ -type f -name " *.img " -print0 | xargs - 0 du - sh
28K /boot/grub2/i386-pc/ core.img
4.0K / boot / grub2/i386-pc/ boot.img 592K /
boot / initrd- plymouth.img 44M
/ boot / initramfs- 0 -rescue- d13bce5e247540a5b5886f2bf8aabb35.img
17M /boot/ initramfs- 3.10 . 0 - 327 .el7.x86_64kdump.img
5.5.6 df
df is used to report disk space usage. The default displayed size is the number of 1K blocks, that is, in units of K.
Unlike du, df reads the superblock information of each file system, so the evaluation speed is very fast. Since it reads the superblock, if another file system is mounted under the directory, this mounted file system will not be counted in the directory size. Note that the statistical results of du and df are different. If you are interested in the difference between their results, you can refer to my other article: Detailed analysis of why the statistical results of du and df are different .
If you use df to count the space usage of a file, it will instead count the space usage of the file system where the file is located.
df [OPTION]... [FILE]...
Option description:
- h: Humanized conversion size display unit
- i: Statistics on inode usage rather than space usage
- l, --local : Only list the usage of the local file system, not the network file system information
- T, --print- type: Output the file system type at the same time
- t, --type= TYPE: Only list the statistics of the given file system
- x, --exclude-type=TYPE: Specify the statistical information of the file system type not to be displayed
Example:
shm tmpfs tmpfs 913M 8.6M 904M 1 % / run tmpfs tmpfs 913M 0 913M 0 % / sys / fs / cgroup / dev / sda1 xfs 247M 110M 137M 45
% / boot tmpfs tmpfs 183M 0 183M 0 % / run / user / 0 /dev/sdb1 ext4 9.3G 37M 8.8G 1 % / mydata/data
shm tmpfs 233586 1 233585 1 % / run tmpfs 233586 13 233573 1 % /sys/fs/ cgroup /dev/ sda1 256000 330 255674 1 % / boot tmpfs 233586 1 233585 1 % / dev / shm tmpfs 233586 1 233585 1 % / run tmpfs 233586 13 233573 1 % / sys/fs / cgroup / dev / sda2 256000 330 255674 1 % / dev /run/user/ 0 /dev/sdb1 625856 14 625842 1 % /mydata/data
5.5.7 dumpe2fs
Used to view the superblock and block group information of ext file systems. Use the -h option to only display superblock information.
The following is the information of the ext4 file system superblock.
11 ( 16 -May- 2010 ) [root@xuexi ~]# dumpe2fs -h /dev/sda2 dumpe2fs 1.41 . 12 ( 17 -May- 2010 ) Filesystem volume
name : <none> Last mounted on : / Filesystem UUID: f199fcb4 -fb06-4bf5-a1b7- a15af0f7cb47 Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem featurs: has_journal ext_attr resize_inode dir_index filetype needs_recovery exent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: user_xattr acl Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 1166880 Block 47 2017 2017 Mount count : 6 Maximum mount count: - 1 Last checked: Sat Feb 25 11 : 48 : 47 2017 2017 Last write time : Sat Feb 25 11 : 53 : 49 2017 2017 Mount count : 6 Maximum mount count : - 1 Last checked : Sat Feb 25 11 : 48 : 47 2017 2017 Mount count : 6 Maximum mount count : - 1 Last checked : Sat Feb 25 11 : 48 : 47 2017 Check interval: 0 (<none> ) Lifetime writes: 2657 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: d4e6493a -09ef-41a1-9d66-4020922f1aa9 Journal
backup: inode blocks
Journal features: journal_incompat_revoke
Journal size: 128M
Journal length: 32768
Journal sequence: 0x00001bd9
Journal start: 23358
One of the block group information.
8160 free inodes, 0 directories, 8160 unused inodes [root@xuexi ~]# dumpe2fs /dev/sda2 | tail - 7
dumpe2fs 1.41 . 12 ( 17 -May- 2010 )
Group 142 : (Blocks 4653056 - 4666623 ) [INODE_UNINIT, ITABLE_ZEROED]
Checksum 0x64ce , unused inodes 8160
Block bitmap at 4194318 (+ 4294508558 ), Inode bitmap at 4194334 (+ 4294508574 )
Inode table at 4201476 - 4201985 (+ 4294515716 )
13568 free blocks, 8160 free inodes, 0 directories, 8160 unused inodes
Free blocks: 4653056 - 4666623
Free inodes: 1158721 - 1166880
5.6 Mounting and unmounting file systems
Here, we will only briefly introduce the usage of mount and umount. For the details of the mechanism and principle of mounting and unmounting, please refer to the details of mounting file systems .
5.6.1 mount
Mount is used to display mount information or mount file systems. It is extremely powerful (ridiculously powerful). It not only supports mounting a wide variety of file systems, such as ext/xfs/nfs/smbfs/cifs (shared directories on Windows), but also supports shared mount points, inherited mount points (parent-child relationship), bound mount points, mobile mount points, etc. This article only introduces the simplest mount function.
Different file systems have different mount options. If an error occurs during the mount process, you should run man mount and check the mount options for the corresponding file system.
Mount does not only mount file systems, it can also mount a directory to another directory. In fact, it implements a "hard link" to the directory. By default, it is impossible to create a hard link to the directory, but it can be bound through mount. After binding, the inode numbers of the two directories are exactly the same. Although a "hard link" to the directory is created, it is actually only used as a soft link.
The following are options for ext file systems. Some options may not be supported by other file systems.
mount # will display the currently mounted information
mount [-t the type of file system to be mounted] [-o special options] device name mount directory
option description: -a
re-mount the mount options specified in the
/etc/ fstab file. -t supports ext2/ext3/ext4/vfat/fat/iso9660 (default format for CDs). Without -t, blkid will be called by default to obtain the file system type.
-n does not write the mount record in the /etc/mtab file. Generally, the mount will record the mount information in /proc/mounts and then synchronize it to /etc/mtab. Specifying -n means not synchronizing the mount information.
-o specifies special mount options. The following are two commonly used ones:
loop mounts image files, such as iso files
ro mounts read-only
rw mounts read-write
auto is equivalent to mount - a
dev If there is a device access entry in the mounted file system, enable it so that it can be used as a device access entry
default rw,suid,dev,exec,auto,nouser,async,and relatime
async asynchronous mount, only writes to memory
sync synchronous mount, writes to the other party's hard disk through the mount location
atime modifies the access time. Modifying atime for each access will reduce performance, so the default is noatime
noatime does not modify the access time. Using this option in high concurrency can reduce disk IO
nodiratime does not modify the folder access time. Using this option in high concurrency can reduce disk IO
exec / noexec Whether the executable program in the mounted file system is executable. The default is exec, which has a higher priority than the permission limit
remount remounts. At this time, you do not need to specify a mount point.
suid / nosuid Enable or disable suid for the mounted file system. It is best to disable suid for external devices.
_netdev When network mounting is required, the default will stay in the mounting interface until the network is loaded. Use _netdev to ignore the network and mount normally. For example, NFS boot mounting.
user allows ordinary users to mount the directory, but only the mounter is allowed to unmount the directory
users allows all users to mount and unmount the directory
nouser prohibits ordinary users from mounting and unmounting the directory. This is the default. By default, when a directory does not specify user /users, only root can mount it.
Generally, user/users/nouser are used in /etc/fstab. It is not very meaningful to use these options directly in the command line.
For example:
(1). Mount the CentOS installation image to /mnt.
mount /dev/cdrom /mnt
In fact, /dev/cdrom is a soft link of /dev/sr0. /dev/sr0 is the CD-ROM device, so it can also be mounted using /dev/sr0.
mount /dev/sr0 /mnt
(2) Remount.
[root@xuexi ~]# mount -t ext4 -o remount /dev/sdb1 /data1
(3) Remount the file system to make it readable and writable.
mount -t ext4 -o rw remount /dev/sdb1 /data1
(4). Mount the Windows shared directory.
The file system for shared files on Windows is of cifs type. To mount it on Linux, you must have the mount.cifs command. If not, install the cifs-utils package.
Assume that the UNC path of the shared directory on Windows is \\192.168.100.8\test, the username and password shared are long3:123 respectively, and it is to be mounted on the /mydata directory on Linux.
shell> mount .cifs -o username= " long3 " ,password= " 123 " // 192.168.100.8/test/mydata
Note that if it is a newer version of win10 (versions updated after 2017) or a newer version of win server, directly mount.cifs will report an error:
[root@xuexi ~]# mount .cifs -o username= " long3 " ,password= " 123 " // 192.168.100.8/test /mnt
mount error( 112 ): Host is down
Refer to the mount .cifs( 8 ) manual page (eg man mount .cifs)
This is because a Microsoft patch in 2017 disabled the SMBv1 protocol, as reported by smbclient:
[root@xuexi ~]# yum -y install samba- client
[root@xuexi ~]# smbclient -L // 192.168.100.8
Enter root ' s password:
protocol negotiation failed: NT_STATUS_CONNECTION_RESET
Therefore, when mounting, just specify the cifs (SMB) version number as 2.0.
[root@xuexi ~]# mount .cifs -o username= " long3 " ,password= " 123 " ,vers= 2.0 // 192.168.100.8/test /mnt
However, please note that the (lower) version of the module cifs.ko under CentOS 4, 5, and 6 can only use the SMBv1 protocol, so even if the version number is specified, it will be invalid. SMBv2 or SMBv3 can only be used on CentOS 7.
(5). Mount the remote directory based on ssh.
How to mount a directory on a remote host based on ssh like NFS? You can use the sshfs tool, which is in the fuse-sshfs package, which is provided in the epel source.
yum -y install fuse-sshfs
For example, mount the root directory on 192.168.100.8 to the local /mnt.
sshfs 192.168.100.8:/ / mnt
When uninstalling, just umount it.
For details about sshfs, see: https://www.cnblogs.com/f-ck-need-u/p/9104950.html
( 6) Mount a directory to another directory. When mounting a directory, the inode of the mount directory and the mount point are the same, and their contents are exactly the same.
mount --bind /mydata /mnt
(7) To check whether a directory is a mount point, use the mountpoint command.
[root@xuexi ~]# mountpoint /mydata/
/mydata/ is a mountpoint
[root@xuexi ~]# echo $?
0
[root@xuexi ~]# mountpoint / mnt
/ mnt is not a mountpoint
[root@xuexi ~]# echo $?
1
The mount parameter information is stored in /proc/mounts (a soft link to /proc/self/mounts), and more detailed mount information is recorded in /proc/self/mountstats and /proc/mountinfo.
,relatime,size=491000k,nr_inodes= 122750 ,mode= 755 0 0 devtmpfs /dev devtmpfs rw,relatime,gid= 5 ,mode= 620 , ptmxmode = 000 0 tmpfs / dev / shm tmpfs rw , relatime
0 0 / dev / sda2 / ext4 rw , relatime , barrier = 1 , data = ordered 0 0 / proc / bus/usb /proc/bus/usb usbfs rw, relatime 0 0 /dev/sda1 /boot ext4 rw,relatime,barrier= 1 ,data=ordered 0 0 none /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0
The file system needs driver support. A file system without a driver cannot be mounted. The file system drivers supported in Linux are under /lib/modules/$(uname -r)/kernel/fs.
[root@xuexi ~]# ls /lib/modules/$(uname -r)/kernel/fs/
autofs4 cachefiles configfs dlm exportfs ext3 fat fuse jbd jffs2 mbcache.ko nfs_common nls ubifs xfs
btrfs cifs cramfs ecryptfs ext2 ext4 fscache gfs2 jbd2 lockd nfs nfsd squashfs udf
5.6.2 Directly mount the image file
Sometimes you need to mount a CentOS image file. In a virtual machine, you usually put the image into the virtual CD/DVD drive of the virtual machine, and then mount /dev/cdrom on Linux. In fact, /dev/cdrom is a soft link of /dev/sr0, and /dev/sr0 is the optical drive in Linux. So the above process is equivalent to connecting the image file through the virtual drive of the virtual software and the optical drive in Linux, so you only need to mount the optical drive in Linux. However, there is no virtual optical drive in a non-virtual environment, and does an image file in Linux have to be copied to the host and connected through the virtual optical drive?
Mount is an extremely powerful mounting tool that supports mounting many types of files, including image files. In fact, it even supports mounting directories.
─sda3 8 : 3 0 2G 0 part [ SWAP ] sr0 11 : 0 1 1024M 0 rom ├─sda1 8 : 1 0 250M 0 part / boot ├─sda2 8 :
2 0 17.8G 0 part / └─sda3 8 : 3 0 2G 0 part [ SWAP ] sr0 11 : 0 1 1024M 0 rom
5.6.3 umount
umount device name or mount directory
umount -lf force unmount
When uninstalling, you can use either the device name or the mount point. Sometimes when mounting a network system (such as NFS), the device name is very long, so it is much more convenient to use the mount point to uninstall.
If a user is accessing a directory or file, causing the uninstallation to display Busy, use fuser -v DIR to find out who is accessing the directory or file.
[root@xuexi ~]# fuser -v / root
USER PID ACCESS COMMAND
/root:root 37453 ..c.. bash
Use the -k option to kill the process that is using the directory or file, use the -km option to kill all processes on the file system, and then umount.
[root@xuexi ~]# fuser -km /mnt/cdrom; umount /mnt/cdrom
5.6.4 Automatically mount /etc/fstab at boot
By writing the mount options into /etc/fstab, the system will automatically mount the configuration items in the file. However, please note that this file is read during the first few boot processes, so a configuration error is likely to cause a boot failure.
The last two columns represent the backup file system and power-on self-test, which can generally be set to 0.
Since there are so many backup tools available, no one will set up a backup here, so the backup column is set to 0.
The last column is the power-on self-test setting column. The power-on self-test calls the fsck program, so some ext-type file systems may be set to 1 when used as "/", but fsck does not support the xfs file system, so for the xfs file system, this item must be set to 0.
In fact, there is no need to think so much, just set these two columns to 0.
5.6.5 Fixing the incorrect /etc/fstab
In case /etc/fstab is incorrectly configured, resulting in a failure to load at boot. At this time, you will be prompted to enter the root password to enter single-user maintenance mode, but the root file system is read-only in the maintenance mode, and even root cannot directly modify /etc/fstab, so the "/" file system should be remounted.
Execute the following command to remount the root partition and grant read and write permissions, then modify the incorrect fstab file record and reboot.
[root@xuexi ~]# mount -n -o remount,rw /
5.6.7 Automatically mount on demand (autofs)
Use autofs to mount when needed, and automatically unmount after 5 minutes when not needed. However, on-demand mounting is rarely used in actual environments.
Autofs is a service program that needs to run in the background. It can be used to mount NFS or local file systems.
Autofs is not installed by default, you need to install it yourself.
[root@xuexi ~]# yum install -y autofs
The way autofs implements on-demand mounting is to specify a monitoring directory, which can be specified in its configuration file /etc/auto.master.
There are only two columns in /etc/auto.master: the first column is the monitoring directory; the second column is the file that records the mount options, which can be named anything.
[root@xuexi ~]# cat /etc/ auto.master
/share /etc/auto. mount # Monitor the /share directory and use /etc/auto.nfs to record mount options
The /share directory monitored above is actually the parent directory of the monitoring. Directories under this directory, such as the /share/data directory, can be used as mount points. When /share/data is accessed, it will be monitored, and then the mount device will be mounted to /share/data according to the mount options.
The mount option file configured in the above configuration is /etc/auto.mount, so create this file and write the mount options.
[root@xuexi ~]# cat /etc/auto. mount
#cd -fstype=iso9660,ro,nosuid,nodev :/dev/ cdrom
# the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp .example.org:/pub/ linux
#boot -fstype=ext2 :/dev/ hda1
#floppy -fstype=auto :/dev/ fd0
#floppy -fstype=ext2 :/dev/ fd0
#e2floppy -fstype=ext2 :/dev/ fd0
#jaz -fstype=ext2 :/dev/ sdc1
#removable -fstype=ext2 :/dev/hdd
The file has 3 columns:
The first column specifies the directory /share/data under /share specified in /etc/auto.master. It is the actual monitored path and also the mount point. The relative path data can be used to represent /share/data.
The second column is the mount option, which is indicated by a "-" in front of it. This column is optional.
The third column is the device to be mounted, which can be a shared directory on the NFS server or a local device.
[root@xuexi ~]# vim /etc/auto. mount
data -rw,bg,soft,rsize= 32768 ,wsize= 32768 192.168 . 100.61 :/data
The above configuration means that when /share/data is accessed, the /data directory of the remote 192.168.100.61 is automatically mounted to /share/data using the parameters (rw,bg,soft,rsize=32768,wsize=32768).
The remaining step is to start the autofs service.
[root@xuexi data]# /etc/init.d/autofs restart
5.7 Swap Partition
Although there is basically no need to set up a swap partition on a personal computer, you should still prepare a swap partition on the server to be prepared and prevent many "metaphysical" problems.
5.7.1 Check swap usage
[root@xuexi ~]# free
total used free shared buffers cached
Mem: 1906488 349376 1557112 200 16920 200200
-/+ buffers/cache: 132256 1774232
Swap: 2097148 0 2097148
[root@xuexi ~]# free - m # Display
total used free shared buffers cached
Mem: 1861 341 1520 0 16 195
-/+ buffers/cache: 129 1732 # This is the actual available memory space
Swap: 2047 0 2047 # This is the swap space, and it is found that it is not used at all
You can use mount/lsblk to check which partition is acting as a swap partition. You can also use swapon -s to check it directly.
[root@server2 ~]# swapon - s
Filename Type Size Used Priority
/dev/sda3 partition 2047996 37064 - 1
5.7.2 Adding a swap partition
(1) You can create a new partition and specify the partition ID as SWAP type.
This ID may be different on disks in mbr and gpt formats, but generally the format in gpt is to add a two-digit value after the mbr format ID. For example, the swap type ID in mbr is 82, while it is 8200 in gpt. The linux filesystem type ID in mbr is 83, while it is 8300 in gpt. The lvm ID in mbr is 8e, while it is 8e00 in gpt.
(2) Format as swap partition: mkswap
[root@xuexi ~]# mkswap /dev/ sdb5
Setting up swapspace version 1 , size = 1951096 KiB
no label, UUID =02e5af44-2a16-479d-b689-4e100af6adf5
(3) Add swap partition space (swapon):
[root@xuexi ~]# swapon /dev/ sdb5
[root@xuexi ~]# free - m
total used free shared buffers cached
Mem: 1861 343 1517 0 16 196
-/+ buffers/cache: 131 1730
Swap: 3953 0 3953
(4) Cancel the swap partition space (swapoff):
[root@xuexi ~]# swapoff /dev/ sdb5
[root@xuexi ~]# free - m
total used free shared buffers cached
Mem: 1861 343 1518 0 16 196
-/+ buffers/cache: 130 1731
Swap: 0 0 0
(5). Automatically load the swap partition at startup:
Modify /etc/fstab and add a line.
/dev/sda3 swap swap defaults 0 0
The spring recruitment has begun. If you are not adequately prepared, it will be difficult to find a good job during the spring recruitment.
Here is a big employment gift package for everyone. You can prepare for the spring recruitment and find a good job!