Linux - basic file system management disk partition

File system management

Disk structure

  1. Logical structure of disk

The size of each sector is fixed, 512Byte. A sector is also the smallest storage unit of a disk

  • The size of the hard disk is the number of Heads used × Cylinders × sector number × The size of each sector is calculated according to the formula "size of each sector". The number of Heads indicates the total number of Heads of the hard disk, which can also be understood as the number of disk surfaces of the hard disk, and then multiplied by two; Cylinders indicates how many tracks each disk of the hard disk has; Sectors indicates how many sectors there are on each track; The size of each sector is generally 512Byte.
  1. Disk interface

    • IDE hard disk interface (Integrated Drive Electronics, parallel port, i.e. electronic integrated drive), also known as "ATA hard disk" or "PATA hard disk", is the main interface of early mechanical hard disk. The theoretical speed of ATA133 hard disk can reach 133MB/s (this speed is the theoretical average), and IDE hard disk interface
    • SATA interface (Serial ATA, serial port) is a higher speed hard disk standard, with higher transmission speed and stronger error correction ability. At present, it is the third generation of SATA, and the theoretical transmission speed reaches 600MB/s (this speed is the theoretical average)
    • SCSI interface (Small Computer System Interface) is widely used in servers. It has the advantages of wide application range, multitasking, large bandwidth, low CPU occupancy and supporting hot plug. The theoretical transmission speed reaches 320MB/s

file system

  1. Features of Linux file system:

    • super block: records the information of the entire file system, including the total number of blocks and inodes, the number of inodes and blocks used, the number of unused inodes and blocks, the number of unused inodes and blocks, the size of blocks and inodes, the mounting time of the file system, the last write time, the last disk inspection time, etc.
    • date block (data block, also known as block): used to actually save data (partition of the cabinet). The size (1KB, 2KB or 4KB) and quantity of blocks have been determined after formatting and cannot be changed unless reformatted (when making the cabinet, the partition size has been determined and cannot be changed unless the cabinet is made again). Each blcok can only save the data of one file. If the file data is less than a block, the remaining space of this block cannot be used by other files; If the file data is larger than one block, it will occupy multiple block blocks. The principle of disk defragmentation tool in Windows is to sort multiple block blocks occupied by a file together as much as possible, so as to speed up the reading and writing speed.
    • Inode (i node, label on the cabinet door): used to record the permission (r, w, x) of the file, the owner and group of the file, the size of the file, the time when the state of the file changes (ctime), the time when the file was last read (atime), the time when the file was last modified (mtime), and the block number of the file's data really saved. Each file needs to occupy an inode.
  2. Linux common file system

File system describe
ext The earliest file system in Linux has been rarely used because of many defects in performance and compatibility
ext2 It is an upgraded version of ext file system. Systems before Red Hat Linux 7.2 are ext2 file system by default. Released in 1993, it supports up to 16TB partitions and up to 2TB files (1TB=1024GB=1024) × 1024KB)
ext3 It is an upgraded version of ext2 file system. The biggest difference is the logging function, so as to improve the reliability of the file system when the system suddenly stops. Support up to 16TB partitions and up to 2TB files
ext4 Is an upgraded version of ext3 file system. Ext4 has made a lot of improvements in performance, scalability and reliability. The changes of ext4 can be said to be earth shaking, such as downward compatibility with ext3, maximum 1EB file system and 16TB files, unlimited subdirectories, extensions continuous data block concept, multi block allocation, delayed allocation, persistent pre allocation, fast FSCK, log verification, no log mode, online defragmentation, inode enhancement, default enable barrier, etc. It is CentOS 6 Default file system for X
xfs XFS was first developed for IRIX operating system. It is a high-performance log file system, which can ensure the consistency of file system data in case of power failure and operating system crash. It is a 64 bit file system, which was later open-source and transplanted to the Linux operating system. At present, CentOS 7 X takes XFS+LVM as the default file system. According to the official statement, XFS has good reading and writing performance for large files.
swap Swap is a file system used to swap partitions in Linux (similar to virtual memory in Windows). When memory is insufficient, swap partitions are used to temporarily replace memory. The general size is twice that of memory, but not more than 2GB. It is a required partition for Linux
NFS NFS is the abbreviation of Network File System. It is a network service used to realize file sharing between different hosts. Local hosts can use remotely shared resources by mounting
iso9600 Standard file system for optical discs. If Linux wants to use CD, it must support iso9660 file system
fat It is the fat16 file system under Windows, which is recognized as fat in Linux
vfat Under Linux, fat32 is the file system of fat32. Supports up to 32GB partitions and up to 4GB files
NTFS It is the NTFS file system under Windows, but Linux cannot recognize the NTFS file system by default. If you need to recognize it, you need to recompile the kernel to support it. It is more secure and faster than fat32 file system, and supports up to 2TB partitions and up to 64GB files
ufs The file system adopted by Sun's operating system Solaris and SunOS
proc The memory based virtual file system in Linux is used to manage the memory storage directory / proc
sysfs Like proc, it is also a memory based virtual file system, which is used to manage the memory storage directory / sysfs
tmpfs It is also a memory based virtual file system, but swap can also be used to swap partitions

Common disk management commands

df count the remaining disk space

[root@iZbp10cdilkxcswm4l893lZ ~]# df [options] [directory or file name]
option
-a  Show special file systems
-h  Humanized display KB,MB
-T  There is an extra column of file system type

du statistics file size

[root@iZbp10cdilkxcswm4l893lZ ~]# du [options] [directory or file name]
option
-a  Displays the disk usage of each subfile
-h  Humanized display KB,MB
-s  Statistics of the usage. The usage of subdirectories and sub files is not displayed

fsck file system repair command

[root@iZbp10cdilkxcswm4l893lZ ~]# fsck -y /dev/vda1 / / auto repair 
fsck from util-linux 2.23.2
e2fsck 1.42.9 (28-Dec-2013)
/dev/vda1 is mounted.
e2fsck: Cannot continue, aborting.

dumpe2fs displays the disk status

[root@iZbp10cdilkxcswm4l893lZ ~]# dumpe2fs -h /dev/vda1 / / displays the disk status
dumpe2fs 1.42.9 (28-Dec-2013)
Filesystem volume name:   <none>                           //Volume label
Last mounted on:          /                                //Mount point
Filesystem UUID:          1114fe9e-2309-4580-b183-d778e6d97397
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 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                    //Mount parameters
Filesystem state:         clean                             //File system status, normal
Errors behavior:          Continue               
Filesystem OS type:       Linux
Inode count:              2621440                           //Total inode s
Block count:              10484164                          //Total number of blocks   
Reserved block count:     471777
Free blocks:              9287867
Free inodes:              2495670
First block:              0
Block size:               4096                              //Block size
Fragment size:            4096

stat view file details

[root@iZbp10cdilkxcswm4l893lZ ~]# stat test.sh / / view test SH file details
  File: 'test.sh'   //file name                             
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
 //Block occupied by file size 			 System partition block size     
Device: fd01h/64769d	Inode: 917519      Links: 1
//Number of hard links of inode number of the device where the file is stored
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
//Permission belongs to main array
Access: 2020-09-22 09:31:48.312915609 +0800         //Visit time
Modify: 2020-09-22 09:31:48.312915609 +0800         //Data modification time 
Change: 2020-09-22 09:31:48.312915609 +0800         //Status modification time
 Birth: -

type file determines the file type

[root@iZbp10cdilkxcswm4l893lZ ~]# File name / / judge the file type
[root@iZbp10cdilkxcswm4l893lZ ~]# Type command name / / judge the command type

fdisk command manual partition

Only MBR partition tables can be partitioned, not GPT partitions

New hard disk partition steps

  1. Shutdown add hard disk
  2. Partition (primary or logical partition, partition number, size)
  3. format
  4. mount

partition

  • n-p main ----- 1 partition code ----- 1 starting cylinder ----- partition size + 100M-----w

  • n-e expansion-2 partition number-124 starting cylinder-1024 cylinder (all remaining space is allocated to expansion partition)

  • n - l logic - do not specify partition code - 124 starting cylinder - + 100m (specified size) ---- w

[root@localhost ~]# fdisk -l / / view all hard disks and partitions of the system
Disk /dev/sda: 20 GiB, 21474836480 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
Disklabel type: dos
Disk identifier: 0xb565d87c

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 41943039 39843840  19G 8e Linux LVM

Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors   //This is the hard disk I added manually
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 /dev/mapper/cl-root: 17 GiB, 18249416704 bytes, 35643392 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 /dev/mapper/cl-swap: 2 GiB, 2147483648 bytes, 4194304 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
[root@localhost ~]# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 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
Disklabel type: dos
Disk identifier: 0xb565d87c

[root@localhost ~]# fdisk /dev/sdb / / start partitioning into the hard disk

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xdfb50382.

Command (m for help): n             //New partition
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p               //Above, p represents the selection of primary partition and e represents the selection of extended partition    
Partition number (1-4, default 1): 1       //The drive number of the partition is 1
First sector (2048-20971519, default 2048):    //The number of sectors is 2048 by default. Press enter
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +1G  //What is the score for the main partition + 1G represents the partition 1G

Created a new partition 1 of type 'Linux' and of size 1 GiB.

Command (m for help): w    //Save exit
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
command explain
a Set bootable flag
b Edit bsd disk label
c Set DOS operating system compatibility flag
d Delete a partition
l Shows that the known file system type 82 is Linux swap partition 83 is Linux partition
m Show help menu
n New partition
o Create a blank DOS partition table
p Show partition list
q Exit without saving
s New blank SUN disk label
t Change the system ID of a partition
u Change display record unit
v Validate partition table
w Save exit

format

//mkfs cannot adjust the default parameters of the partition
[root@localhost ~]# mkfs -t ext4 /dev/sdb1
                 // Format -t create file system disk location
mke2fs 1.44.6 (5-Mar-2019)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 62259879-3c50-4095-9ebe-ee7741aa531e
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

// ----------------mke2fs can adjust the default parameters of partitions--------------------
[root@localhost ~]# mke2fs [options] partition device file name
    -t file system      //Specify which file system to format, ext2 ext3
    -b byte          //Specifies the size of the block
    -i byte          //Specifies the ratio of bytes / inode
    -j               //Establish a file system with ext3 logging function
    -L Volume label        //If you set the volume label to the file system, you cannot use the e21abel command to set it

mount

[root@localhost ~]# mkdir /disk1              // 1. Create mount directory 
[root@localhost ~]# ls /
bin   dev   disk1  home  lib64  mnt  proc  run   srv  test  usr
boot  disk  etc    lib   media  opt  root  sbin  sys  tmp   var
[root@localhost ~]# mount /dev/sdb1 /disk1    // 2. Mount to directory
Auto Mount
  • Modify the partition auto mount file, which can be mounted with UUID or detailed path of disk
[root@localhost ~]# VI / etc / fstab / / modify the partition auto mount file
UUID=b03826e6-79c2-4f0b-8b03-e714094e26af /boot               ext4    defaults        1 2
//Device file name mount point file system mount options      
//The fifth column 1 can be backed up 0 no backup 1 daily backup 2 irregular backup
//Whether the sixth column 2 detects disk fsck 0, does not detect 1 startup, but detects 2 after startup
    
//View system UUID
[root@localhost ~]# dumpe2fs /dev/sdb1
    [root@localhost ~]# dumpe2fs /dev/sdb
sdb   sdb1  
[root@localhost ~]# dumpe2fs /dev/sdb
sdb   sdb1  
[root@localhost ~]# dumpe2fs /dev/sdb1
dumpe2fs 1.44.6 (5-Mar-2019)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          62259879-3c50-4095-9ebe-ee7741aa531e    //Disk UUID
//........................ Omitted here
 perhaps
[root@localhost ~]# LS - L / dev / disk / by UUID / / view the UUIDs of all disks in the system
total 0
lrwxrwxrwx. 1 root root  9 Sep 21 22:01 2020-01-03-21-42-40-00 -> ../../sr0
lrwxrwxrwx. 1 root root 10 Sep 21 22:01 61c13acc-51d5-47a0-9bcc-0d00792ce99a -> ../../dm-0
lrwxrwxrwx. 1 root root 10 Sep 21 23:43 62259879-3c50-4095-9ebe-ee7741aa531e -> ../../sdb1
lrwxrwxrwx. 1 root root 10 Sep 21 22:01 b03826e6-79c2-4f0b-8b03-e714094e26af -> ../../sda1
lrwxrwxrwx. 1 root root 10 Sep 21 22:01 e9fc89a9-476c-4264-b825-bb1ce544ddf3 -> ../../dm-1

Partitioned command partition

  • MBR partition table: the maximum partition supported is 2TB (1TB=1024GB); It supports up to 4 primary partitions, or 3 primary partitions and 1 extended partition
  • GPT partition table: supports partitions with a maximum of 18EB (1EB=1024PB=1024*1024TB); It supports 128 partitions at most, 1 system reserved partition in the others and 127 user-defined partitions

parted interaction mode

parted interactive command explain
help [COMMAND] Show all command help
mklabel,mktable LABEL-TYPE Create a new disk volume label (partition table)
mkpart PART-TYPE [FS-TYPE] START END Create a partition
name NUMBER NAME Name the partition
print Display partition table and other information
quit sign out
rescue START END Repair missing partitions
resizepart NUMBER END Modify partition size
rm NUMBER delete a partition
select DEVICE Select the device you want to edit
disk_set FLAG STATE Change flags on selected devices
disk_toggle [FLAG] Toggles the status of the flag when selected
set NUMBER FLAG STATE Change the flag on the partition number
toggle [NUMBER [FLAG]] Toggles the status of the flag on the partition
unit UNIT Set default units
version Display version

parted create partition

  • Modify partition table
  • create a partition
[root@localhost ~]# parted /dev/sdb / / enter the parted partition
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) print                             //Print partition 
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos                     //The partition table type is MBR partition  
Disk Flags: 
//Partition number partition start location partition end location size type file system type flag
 Number     Start       End        Size     Type       File system     Flags
   1        1049kB     1075MB      1074MB   primary        ext4
//--------------------Modify partition-----------------------------------
(parted) mklabel gpt                       //The modified partition table is now a GPT partition
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? yes                                // Enter yes                              
//---------------------Create partition----------------------------------
(parted) mkpart                            //Create a new disk volume label (partition table)
Partition name?  []? disk2                 //Partition name
File system type?  [ext2]?                 //Partition type                                
Start? 1                                   //Partition starts from 1MB                             
End? 2GB                                   //Partition ends at 2GB                               

(parted) print                             //Partition printing                                 
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  2000MB  1999MB  ext2         disk2

resize partition

delete a partition

swap partition

  1. Add hard disk, new partition
  2. format
  3. Use swap partition
//------------------New partition--------------------------
[root@localhost ~]# fdisk /dev/sdb 
Command (m for help): n                  
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048):    
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +5G
Do you want to remove the signature? [Y]es/[N]o: Y

Command (m for help): p                         //Print

Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xe669ff7c

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 10487807 10485760   5G 83 Linux         //Divided into the first area

Filesystem/RAID signature on partition 1 will be wiped.
//-----------------Create swap partition---------------------------------
Command (m for help): t                            //Modify the system ID of the partition
Selected partition 1                               //There is only one partition, so you don't have to choose a partition
Hex code (type L to list all codes): 82            //The ID of the swap partition is 82
Changed type of partition 'FAT12' to 'Linux swap / Solaris'.

Command (m for help): w                            //Save and exit
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
//---------------------------Format as swap partition---------------------------
[root@localhost ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 5 GiB (5368705024 bytes)
no label, UUID=12fd3a49-11f9-4647-b8cd-50ce5844b453
//----------------------Check---------------------------------
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          1.8Gi       209Mi       1.3Gi       8.0Mi       217Mi       1.4Gi
Swap:         2.0Gi          0B       2.0Gi
//------------------Mount swap partition------------------------------
[root@localhost ~]# Swap on / dev / sdb1 / / similar to mount mounting CD
[root@localhost ~]# vim /etc/fstab / / set the boot auto mount swap partition
/dev/sdb1               swap                    swap    defaults        0 0 

summary

The above is what I want to talk about today. This article only briefly introduces how to partition Linux disks, steps and commands.

Tags: Linux Operating System

Posted by mdl on Wed, 11 May 2022 08:22:59 +0300