Linux disk and file system management

Linux server development 2021-09-15 10:41:53

1 Check and confirm the new hard disk

Attach the new hard disk device and start the host ,Linux The system will automatically detect and load the hard disk , No need to install additional drivers . perform “fdisk -l” Command to view , Confirm the device name and location of the new hard disk . effect : Lists information about all hard disk devices and their partitions in the current system .

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux
[root@localhost ~]#

The above output information includes the overall situation and partition of each hard disk , among “/dev/sda” For the original hard disk device , For existing partitions , The following information will be output as a list ;

Device: Device file name of partition .

Boot: Is it a bootstrap partition . yes , Then there are “*” identification .

Start: The starting position of the partition in the hard disk .

End: The end of the partition on the hard disk .

Blocks: Size of partition , With Blocks( block ) In units of , The default block size is 1024 byte .

Id: The system corresponding to the partition ID Number .83 Express Linux Medium EXT4 Partition ,8e Express LVM Logic volume .

System: Partition type .

2 Add a new hard disk

image.png

Right click a disk in the lower right corner of the figure above ,“ Set up ” Button . The following dialog box pops up ,“ add to ” Button .
image.png

Select the appropriate hardware type . Click on “ next step ” Button .
image.png

Choose the disk type , The default is SCSI.
image.png
image.png

Select the maximum disk size , And choose to store the virtual disk as a single file , Click on “ next step ” Button . When I'm done , Restart the device .
image.png

Execute... After restart “fdisk -l” command , To see if the new hard drive is recognized .

[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x00000000

[root@localhost ~]#

3 Plan the partition in the hard disk

Create... In the hard disk device 、 Delete 、 Operations such as changing partitions can also be done through fdisk Command to , Just use the device file of the hard disk as the parameter . for example , perform “fdisk /dev/sdb” command , You can enter the interactive partition management interface .

In the operation interface “Command (m for help):” After prompt , Enter specific partition operation instructions , It can complete various partition management tasks . Input “m” After the instruction , You can view the help information of various operation instructions .

fdisk Interactive operation interface of partition tool

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x97fe0724.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

 switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help):

【 Article Welfare 】: Xiaobian has sorted out some learning books that I think are better 、 Video data is shared in group files , If necessary, you can add it by yourself !~ Click to add (832218493 You need to take it yourself )
image.png

About fdisk Help information for interactive operation instructions

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)

Command (m for help):

p Instructions —— List the partitions in the hard disk

Use “p” Instructions can list detailed partition conditions , Format and execution of information display “fdisk -l” The same command , When a partition has not been created in the hard disk , The output list information is empty .

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x97fe0724

Device Boot Start End Blocks Id System

Command (m for help):

n Instructions —— New partition

Use “n” Instructions can be used to create partitions , Including primary partition and extended partition . Follow the prompts to continue typing “p” Select Create primary partition , Input “e” Select Create extended partition . Then select the partition serial number , The starting position , The end position or partition size completes the creation of a new partition .

When selecting a partition , The sequence number of primary partition and extended partition can only be 1-4. The starting position of the partition is generally determined by fdisk Default recognition , The end position or size can be used "+sizeM" or "+sizeG" In the form of .

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +5G
Value out of range.

When finished, you can enter “p” Command to view the partition ;

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x97fe0724

Device Boot Start End Blocks Id System
/dev/sdb1 1 652 5237158+ 83 Linux

d Instructions —— Delete partition

Use “d” Directive can delete the specified partition , Continue to enter the partition serial number to be deleted according to the prompt , Be careful when deleting partitions before execution , Use... First p Command to view the sequence number of the partition , Delete after confirmation .

It should be noted that , If the extended partition is deleted , The logical partition under the extended partition will also be deleted . Therefore, it is recommended to delete from the last partition , lest fdisk The recognized partition serial number is confused .

t Instructions —— Change the type of partition

stay fdisk Partition tool , The default file system type used for the new partition is EXT4, Generally, there is no need to change , However, if the newly created partition needs to be used as swap When swapping partitions or other types of file systems , You need to change the partition type to maintain consistency , This avoids confusion when managing partitions .

Use “t” Directive can change the of a partition ID Number , During operation, you need to specify the serial number of the target partition in turn , New system ID Number . Different types of file systems correspond to different types of files ID Number , With 16 A decimal number means , stay fdisk Enter... In the interactive environment “l” Command to view the list , Most commonly used EXT4、swap File system ID The numbers are 83、82, And for windows Medium NTFS File system ID The number is usually 86.

w and q Instructions —— sign out fdisk Partitioning tool

After partitioning the hard disk , It can be executed w or q Command exit fdisk Partitioning tool . among “w” The partition operation will be saved , and “q” The command will not save the partition operation on the hard disk .

When partitioning a hard disk that already contains data , Be sure to back up your data , Make sure the operation is correct before saving , To avoid data corruption , If you are unable to determine whether the partition operation is correct , It is recommended to use “q” Exit without saving the command .

After changing the partition settings of the disk , It is recommended to restart the system once , Or perform “partprobe” Command enables the operating system to detect new partition table conditions . In some Linux Operating system , If these operations are not carried out , It may cause damage to the existing data in the hard disk when formatting the partition , In severe cases, it even causes system crash . perform partprobe Command detection /dev/sdb The partition in the disk changes .

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]#

4 Manage file systems

mkfs Use of commands

mkfs Command is a front-end tool , Different programs can be loaded automatically to create various types of partitions , The back end includes multiple and mkfs Command related tools and programs , be located /sbin Directory .

[root@localhost ~]# ls /sbin/mkfs*
/sbin/mkfs /sbin/mkfs.cramfs /sbin/mkfs.ext2 /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.ext4dev /sbin/mkfs.msdos /sbin/mkfs.vfat
[root@localhost ~]#

The basic command format

mkfs -t File system type Zone devices

mkswap Use of commands

Use mkswap Command can create a swap file system on a specified partition , The target partition passes first fdisk Tools will ID Number set to 82.

For the newly added switching partition , Need to use swapon Command to enable , Instead, use swapoff The command deactivates the specified swap partition .

[root@localhost ~]# fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x000b2b5d

Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 102400 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 13 279 2134016 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 279 2611 18734080 83 Linux

5 mount 、 Uninstall file system

Mount file system

mount The basic usage format of the command

mount [-t File system type ] The storage device Mount point

The device file corresponding to the optical disc usually uses "/dev/cdrom", In fact, this is a link file , Link to the actual optical device "/dev/sr0". Both names can be used to represent the optical device . Because the optical disc is a read-only storage medium , Therefore, when mounting, the system will appear "mounting read-only" A reminder of .

Use... Without any parameters or options mount On command , Information about each partition mounted in the current system will be displayed , The most recently mounted file system will be displayed at the end .

[root@localhost ~]# mount
/dev/sda3 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/CentOS_6.5_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
[root@localhost ~]#

Uninstall file system

When the file system needs to be unmounted , The command used is umount, Use the mount point directory or the file name of the corresponding device as the uninstall parameter ,Linux In the system , Because the same device can be mounted to multiple directories , Therefore, it is generally recommended to uninstall through the directory location of the mount point .

[root@localhost ~]# umount /dev/cdrom
[root@localhost ~]#

Set up the automatic mount of the file system

In the system /etc/fstab The document can be regarded as mount The configuration file for the command , It stores the static mount data of the file system .Linux Every time the system is powered on , Will automatically read the contents of this file , Automatically mount the specified file system . default fatab The file includes the root partition 、/boot Partition 、 Exchange partition and proc、tmpfs Mount configuration of pseudo file system .

[root@localhost ~]# cat /etc/fstab

/etc/fstab
Created by anaconda on Sat Jun 4 06:06:05 2016

Accessible filesystems, by reference, are maintained under '/dev/disk'
See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

UUID=793c943d-a96e-453d-8249-a8eb9ad1ec59 / ext4 defaults 1 1
UUID=758a54e1-de64-4e02-a4a9-f09f4c28fdfa /boot ext4 defaults 1 2
UUID=0535a0f7-88f2-490c-b26f-94a27e45804d swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@localhost ~]#

stay "/etc/fstab" In file , Each line records the mounting configuration information corresponding to a partition or device , The meaning of each part is as follows .

The first 1 Field : Device name or device volume label .

The first 2 Field : The location of the mount point directory of the file system .

The first 3 Field : File system type , Such as ext4、swap etc. .

The first 4 Field : Mount parameters ,mount command "-o" The parameters available after the option .

(defaults、rw、ro、noexec Each represents the default parameter 、 Can write 、 read-only 、 Disable executors )

The first 5 Field : Indicates whether the file system needs dump Backup , by 1 It is necessary to , by 0 Will be dump Neglected .

The first 6 Field : This number is used to determine the order of disk checks at system startup .0 It means no inspection ,1 It means priority inspection ,2 Next, check , For the root partition, it should be set to 1, Other partitions are set to 2.

Check disk usage

Without options and parameters mount The command can display the mounting status of the partition , Understand the disk usage of each mounted file system in the system. You can use df command .

df The command uses files or devices as command parameters , The common options are "-h","-T"."-h" Option to display more readable capacity units , and "-T" Option is used to display the type of the corresponding file system .

[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 ext4 18G 5.0G 12G 30% /
tmpfs tmpfs 495M 224K 495M 1% /dev/shm
/dev/sda1 ext4 97M 30M 63M 32% /boot
[root@localhost ~]#

Reprinted address :Linux Disk and file system management

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15