编程知识 cdmana.com

Linux disk partition management

 3.5.1  Disk partition concept

  linux There are two ways to partition system disk , Primary partition (Primary Partion) And extended partitions (extended partion). Up to four partitions per disk device , in other words , The sum of the number of primary partitions plus extended partitions cannot exceed 4; The primary partition is the least 1 The most 4 individual ; The extended partition may not have , most 1 individual ; For large applications ,4 Partitions may not meet the requirements , So there's a logical partition (logical partion), It's a partition under an extended partition , There is no limit to the number of logical partitions .

   The partition order of the disk , There is often sda1/sda2/sda3/sda4, Each represents the first 、 Two 、 3、 ... and 、 Four divisions ; When a disk is partitioned, it cannot be used directly , The partition must be formatted and mounted above the system directory for use . therefore , In the management of disk, there are some requirements for disk query , Partition 、 format 、 mount 、 Capacity expansion 、 Repair and other command tools , These tools are important skills in disk management , Be familiar with .

3.5.2  Disk management commands

  1) File system disk Statistics ----df

   effect : View system disk usage information

   Parameters :-h  Automatically select the appropriate unit to display the disk size ;-i  Display disk iNode Information ; -T Show file system type .

  [root@centos7 ~]# df       # Default KB Unit display information
  [root@centos7 ~]# df -h      # Adaptive units display disk information
  [root@centos7 ~]# df -i       # Check the... Of the disk inode Information
  [root@centos7 ~]# df -T      # Displays the disk file system type ( Format )

  2) Directory file capacity statistics ----du

   effect : Look at the size of a directory or file

   Parameters :-h  Automatically select the appropriate unit to display the disk size ;-s  Show only the total statistics .

  [root@centos7 ~]# du -h u.txt       # View the specified file size
  [root@centos7 ~]# du -h /home/     # List and view home Directory file directory size
  [root@centos7 ~]# du -sh /home     # see home The total size of the catalog
  [root@centos7 tmp]# du -sh *     # see tmp Directory size of all files in the directory

  3) View block devices ----lsblk

   effect : View block device details

  [root@centos7 ~]# lsblk        # Default view device information
  [root@centos7 ~]# lsblk -m      # Display disk permission attribute information
  [root@centos7 ~]# lsblk -f      # Displays the file system type of the partition

  4) Edit partition table ----fdisk

   effect : Edit partition table , Partition the disk

  [root@centos7 ~]# fdisk -l       # List partition table
  [root@centos7 ~]# fdisk /dev/sdb   # Edit partition table
   Edit common parameters of partition table :
    m : Display menu and help information
    d : Delete partition
    l : Show partition type
    n : New partition
    p : display partition information
    q : Exit without saving
    t : Set partition number
    v : Check the area
    w : Save changes

  5) format partition ----mkfs

   effect : Format a disk to a specified file system

  [root@centos7 ~]# mkfs -t xfs /dev/sda2  # Put the device sda2 Format as xfs file system
  [root@centos7 ~]# mkfs -t ext4 /dev/sdb2  # Put the device sdb2 Format as ext4 file system

  6) Mount and unload ----mount / umount

   effect : Mount dismount file system

   Parameters :-t  Execution file system type ;-o  Appoint ro( read-only )/rw( Reading and writing );-a  Load partition configuration .

  [root@centos7 ~]# mount /dev/sdb3 /data     # take sdb3 The partition disk is mounted to data Catalog
  [root@centos7 ~]# mount -o ro /dev/sdb2 /test   # Mount the read-only device to test Catalog
  [root@centos7 ~]# mount -a         # Load partition table /etc/fstab All configurations
  [root@centos7 ~]# mount /dev/cdrom /mnt    # Mount the optical drive to mnt
  [root@centos7 ~]# mount centos7.iso /mnt   # Mount the image to mnt
  [root@centos7 ~]# umount /dev/sdb2     # Uninstall disk sdb2

   Tips : When the disk is unloaded, the device is busy , Indicates that the disk is being used by the program , Stop the application process to unload , Use yum install -y psmisc install fuser command ,fuser -m /dev/sdb1  You can view the process of using the disk , Stop the service according to the process number ,kill - 9 pid.

  7) Capacity expansion ----resizefs/xfs_growfs

   effect : Expand the capacity of the disk

  [root@centos7 ~]# xfs_growfs /dev/sdb1       #xfs The format is not LVM Capacity expansion sdb1
  [root@centos7 ~]# xfs_growfs /dev/centos/root    #xfs Format LVM Root partition expansion
  [root@centos7 ~]# resize2fs /dev/sdb2       #ext4 The format is not LVM Capacity expansion sdb2

   Tips : Capacity expansion is a complex operation process , Above is the last expansion command for expansion .

  8) Disk repair ----fsck

   effect : Check and repair the file system

  [root@centos7 ~]# fsck -p /dev/sdb1     # Automatically detect and repair file system errors
  [root@centos7 ~]# fsck -f /dev/sdb1     # Force file system checks
  [root@centos7 ~]# fsck -rv -t xfs /dev/sdb1   # Appoint xfs Interactive checking of file systems

3.5.3 fdisk Disk partition management instance

    Use fdisk Managing partition tables , First add a new disk device to the machine sdb, size 20G, Here is an example of partition management operations .

  1) Create partitions

   Use command fdisk /dev/sdb Yes sdb The execution partition is shown below ,m Is to view the interactive parameter command

  1  Create the first partition sdb1, The creation process is n-->p-->1-- Default -->+5G-->w.

image.png

   2  Create a second partition sdb2, The creation process is p--n-->p-->2--> Default -->+6G-->w.

image.png

  3  Create an extended partition sdb3, The creation process is n-->e-->3--> Default -->+3G-->w.

image.png

   4 Create a logical partition sdb5, The creation process is n-->l-->5--> Default -->+1G-->w.

image.png

  2) format partition

    When the partition is created , The partition needs to be formatted

  [root@centos7 ~]# mkfs -t xfs -f /dev/sdb1     # take sdb1 Formatted as xfs file system
  [root@centos7 ~]# mkfs -t ext4 /dev/sdb2      # take sdb2 Formatted as ext4 file system
  [root@centos7 ~]# mkfs -t xfs -f /dev/sdb5      # take sdb5 Formatted as xfs file system

image.png

  3) Mount partition

  1 Perform a temporary mount , After the machine restarts, the mount will fail

  [root@centos7 ~]# mkdir /data /test /prod  # Create mount directory
  [root@centos7 ~]# mount /dev/sdb5 /test  # take sdb5 Mount to test Catalog
  [root@centos7 ~]# mount /dev/sdb2 /prod  # take sdb2 Mount to prod Catalog
  [root@centos7 ~]# mount /dev/sdb1 /data  # take sdb1 Mount to data Catalog

image.png

   2  Permanently mount , Write the mount configuration to fstab file

   Write the configuration that needs to be mounted to /etc/fstab in , perform mount -a  Load the configuration .

  [root@centos7 ~]# vi /etc/fstab
   Add download mount configuration
  /dev/sdb5 /test xfs defaults 0 0
  /dev/sdb2 /prod ext4 defaults 0 0
  /dev/sdb1 /data xfs defaults 0 0
   After the save
  [root@centos7 ~]# mount -a

image.png

  4) Uninstall partition

    Unload partition can be used directly umount uninstall , When a process is using the disk , Unable to successfully uninstall ; You must stop the process that occupies the disk to unload properly , among fuser The command can view the process that occupies the disk , Then stop the process to perform the unload .

  [root@centos7 ~]# umount /dev/sdb5       # uninstall sdb5
  [root@centos7 ~]# yum install -y psmisc     # install fuser command
  [root@centos7 ~]# fuser -m /dev/sdb5     # View the processes that are using the disk
  [root@centos7 ~]# kill -9 12356       # Force to stop pid process
  [root@centos7 ~]# umount /dev/sdb2    # uninstall sdb2

3.5.4 LVM Logical volume concept

  1)LVM Concept

  LVM(Logical volume Manager) Logical volume management is short for logical volume management , It's a management mechanism for disk partitions , Unified management of multiple storage devices into a logical disk device , Partition logical volume partitions , And allows dynamic sizing of logical volume partitions .LVM The following concepts are mainly used in the creation .

  PV(Physical Volume): Physical volumes are logical block devices , It has a logical correspondence with the underlying disk device or partition .
  VG(Volume Group): A volume group is a logical concept of disks , Composed of one or more physical volume groups .
  PE(Physical Extent): The physical domain is the smallest unit of storage divided after the physical volumes are combined into volume groups , The default is 4MB.
  LV(Logical Volume): A logical volume is a logical partition .

  2)LVM Command tool

  1 pvcreate / pvdisplay / pvremove

   effect : establish / see / Delete physical volume ,pvcreate The essence is to initialize a disk or partition

  [root@centos7 ~]# pvcreate /dev/sdb{1,2,3}     # Create a physical volume
  [root@centos7 ~]# pvscan              # List view physical volumes
  [root@centos7 ~]# pvdisplay           # View detailed physical volume information
  [root@centos7 ~]# pvremove /dev/sdb{1,2,3}   # Delete physical volume

  2 vgcreate / vgdisplay / vgextend / vgremove

   effect : establish / see / Expand / Remove the volume group

  [root@centos7 ~]# vgcreate lidao_vg1 /dev/sdb1 /dev/sdb2   # Create a volume group lidao_vg1
  [root@centos7 ~]# vgscan             # List view volume groups
  [root@centos7 ~]# vgdisplay            # View volume group details
  [root@centos7 ~]# vgextend lidao_vg1 /dev/sdb3  # Give the paper group lidao_vg1 Increase physical volume expansion
  [root@centos7 ~]# vgremove lidao_vg1      # Remove the volume group lidao_vg1

  3 lvcreate / lvdisplay / lvextend / lvremove

   effect : establish / see / Expand / Delete logical volume

   Parameters :-L  Specify logical volume size ;-n  Specify the logical volume name

  [root@centos7 ~]# lvcreate -L 3G -n data_lv1 lidao_vg1    # establish 3G The logical volume data_lv1
  [root@centos7 ~]# lvscan                    # List view logical volumes
  [root@centos7 ~]# lvdisplay                 # View logical volume details
  [root@centos7 ~]# lvextend -L +5G /dev/lidao_vg1/data_lv1  # To the logic volume data_lv1 Capacity expansion 5G
  [root@centos7 ~]# lvremove /dev/lidao_vg1/data_lv1     # Delete logical volume data_lv1

3.5.5 LVM Partition management instance

  lvm The creation process : establish pv---> establish vg---> establish lv---> format lv---> mount lv

  lvm Expansion process : increase pv---- Join in vg--- Capacity expansion lv---> Update the file system

  lvm Delete process : uninstall lv---> Delete lv---> Delete vg---> Delete pv

  1) establish lvm Partition instance

   Limited to the size of the computer disk on this computer , Here we use a 20G Size disk sdb As disk instances , First create four partitions sdb1/sdb2/sdb3/sdb4, Create the first two logical disk groups using vg_ops, Then create two logical volumes from the volume group ops_app、ops_web, And perform formatting and mounting .

   1 Create partitions

  [root@centos7 ~]# fdisk /dev/sdb
  n-->p-->1--> Default -->+5G;n-->p-->2--> Default -->+4G;n-->p-->3--> Default -->+5G;n-->p-->4-> Default -->+4G--->w.
   The process of creating four partitions is shown below :

image.png

   2 Create a volume group

   Use here sdb1 and sdb2 Two disks to create a volume group vg_ops, The specific steps are as follows :
  [root@centos7 ~]# pvcreate /dev/sdb1 /dev/sdb2
  [root@centos7 ~]# vgcreate vg_ops /dev/sdb1 /dev/sdb2

image.png

   3  Create logical volumes

   Use vg_ops Volume group creates two logical volumes ops_app、ops_web Steps are as follows :
  [root@centos7 ~]# lvcreate -n ops_app -L 3G vg_ops
  [root@centos7 ~]# lvcreate -n ops_web -L 3G vg_ops

image.png

   4  Format and mount

   take ops_web The volume is formatted as ext4 Format file system , take ops_app Format as xfs Format file system , And mount them to web、app Catalog
  [root@centos7 ~]# mkfs -t ext4 /dev/vg_ops/ops_web
  [root@centos7 ~]# mkfs -t xfs -f /dev/vg_ops/ops_app
  [root@centos7 ~]# mkdir /web /app
  [root@centos7 ~]# mount /dev/vg_ops/ops_web /web
  [root@centos7 ~]# mount /dev/vg_ops/ops_app /app
  [root@centos7 ~]# df -Th

image.png

  2)lvm Instance of partition expansion

  lvm There are two cases of partition expansion , One is vg Insufficient volume group capacity , One is lv Insufficient logical volume capacity , The following unified expansion . Add a new disk or partition to a volume group , Execute the volume group expansion command ; Logical volume sizing , You need to execute the logical volume expansion command , And the update to the file system will take effect . The following is an example of the expansion operation .

  1  Volume group expansion

   take sdb3 Partition added to vg_ops In the volume group , use first pvcreate initialization , And then use vgextend super-popular .
  [root@centos7 ~]# pvcreate /dev/sdb3
  [root@centos7 ~]# vgextend vg_ops /dev/sdb3
  [root@centos7 ~]# vgdisplay vg_ops

image.png

  2  Logical volume expansion

   Logical volume expansion , First, check whether the capacity of the volume group can meet the demand , If the volume group capacity is insufficient, expand the volume group first ( Above ); When the volume group can meet the requirement of logical volume expansion , Follow the steps below lv Capacity expansion , Try it first lvextend Expand logical volumes ops_web and ops_app, Then select the appropriate update command according to the file system format , This is equivalent to formatting the new space ,ext4 Format use resize2fs,xfs Format use xfs_growfs.
  [root@centos7 ~]# lvextend -L +2G /dev/vg_ops/ops_web
  [root@centos7 ~]# lvextend -L +2G /dev/vg_ops/ops_app
  [root@centos7 ~]# resize2fs /dev/vg_ops/ops_web
  [root@centos7 ~]# xfs_growfs /dev/vg_ops/ops_app
  [root@centos7 ~]# df -Th

image.png

  3) Delete lvm example

   When logical volume partitions are no longer in use , Can be removed to free disk devices . Remove the file system first , Then delete the logical volume , Then delete the volume group , Finally, clean up the physical volume , This is the reverse of the order in which it was created .
  [root@centos7 ~]# umount /dev/vg_ops/ops_web 
  [root@centos7 ~]# umount /dev/vg_ops/ops_app
  [root@centos7 ~]# lvremove /dev/vg_ops/ops_web
  [root@centos7 ~]# lvremove /dev/vg_ops/ops_app 
  [root@centos7 ~]# vgremove vg_ops
  [root@centos7 ~]# pvremove /dev/sdb{1,2,3}

image.png

3.5.6 Not LVM Partition expansion

    When a partition is not in use lvm When the partition is mounted , The expansion depends on the size of the disk device space , If there is a surplus of capacity , Then we can expand the area . Now there are disk devices sdb size 20G, The current partition has a xfs Format sdb1 size 5G, Mounted on /test Catalog , Due to the increase in business volume , Current partition sdb1 Unable to meet demand , Need to give sdb1 Online expansion . Here's right and wrong lvm Partition sdb1 How to expand the capacity of .

  [root@centos7 ~]# fdisk /dev/sdb
  sdb Partition operation   p--->d--->1--->n--->p--->1--->2048--->+10G--->w
  [root@centos7 ~]# partprobe /dev/sdb
  [root@centos7 ~]# xfs_growfs /dev/sdb1
  [root@centos7 ~]# df -Th

image.png

    This section is about disk partitioning , Is an important skill in daily disk management . In addition to learning the examples above , We also need to study disk management in different environments , For example, change the traditional partition to lvm Partition 、 Multiple disk devices build together lvm、 Expand the root partition, etc , Practice more in the test environment , Explore multiple possibilities . It is said that , Test how sweaty , Produce less tears , When faced with uncertainty , It must be fully tested and verified before it can be put into production .

 

版权声明
本文为[osc_l8yszczz]所创,转载请带上原文链接,感谢

Scroll to Top