Linux disk partition management

osc_l8yszczz 2020-11-11 16:41:39
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 .

[[email protected] ~]# df  # Default KB Unit display information
[[email protected] ~]# df -h   # Adaptive units display disk information
[[email protected] ~]# df -i    # Check the... Of the disk inode Information
[[email protected] ~]# 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 .

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

3) View block devices ----lsblk

effect : View block device details

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

4) Edit partition table ----fdisk

effect : Edit partition table , Partition the disk

[[email protected] ~]# fdisk -l  # List partition table
[[email protected] ~]# 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

[[email protected] ~]# mkfs -t xfs /dev/sda2 # Put the device sda2 Format as xfs file system
[[email protected] ~]# 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 .

[[email protected] ~]# mount /dev/sdb3 /data  # take sdb3 The partition disk is mounted to data Catalog
[[email protected] ~]# mount -o ro /dev/sdb2 /test  # Mount the read-only device to test Catalog
[[email protected] ~]# mount -a  # Load partition table /etc/fstab All configurations
[[email protected] ~]# mount /dev/cdrom /mnt # Mount the optical drive to mnt
[[email protected] ~]# mount centos7.iso /mnt  # Mount the image to mnt
[[email protected] ~]# 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

[[email protected] ~]# xfs_growfs /dev/sdb1  #xfs The format is not LVM Capacity expansion sdb1
[[email protected] ~]# xfs_growfs /dev/centos/root  #xfs Format LVM Root partition expansion
[[email protected] ~]# 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

[[email protected] ~]# fsck -p /dev/sdb1  # Automatically detect and repair file system errors
[[email protected] ~]# fsck -f /dev/sdb1  # Force file system checks
[[email protected] ~]# 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.


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


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


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


2) format partition

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

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


3) Mount partition

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

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


  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 .

[[email protected] ~]# 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
[[email protected] ~]# mount -a


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 .

[[email protected] ~]# umount /dev/sdb5  # uninstall sdb5
[[email protected] ~]# yum install -y psmisc  # install fuser command
[[email protected] ~]# fuser -m /dev/sdb5  # View the processes that are using the disk
[[email protected] ~]# kill -9 12356 # Force to stop pid process
[[email protected] ~]# 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

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

2 vgcreate / vgdisplay / vgextend / vgremove

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

[[email protected] ~]# vgcreate lidao_vg1 /dev/sdb1 /dev/sdb2  # Create a volume group lidao_vg1
[[email protected] ~]# vgscan  # List view volume groups
[[email protected] ~]# vgdisplay   # View volume group details
[[email protected] ~]# vgextend lidao_vg1 /dev/sdb3  # Give the paper group lidao_vg1 Increase physical volume expansion
[[email protected] ~]# 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

[[email protected] ~]# lvcreate -L 3G -n data_lv1 lidao_vg1  # establish 3G The logical volume data_lv1
[[email protected] ~]# lvscan     # List view logical volumes
[[email protected] ~]# lvdisplay  # View logical volume details
[[email protected] ~]# lvextend -L +5G /dev/lidao_vg1/data_lv1  # To the logic volume data_lv1 Capacity expansion 5G
[[email protected] ~]# 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

[[email protected] ~]# 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 :


  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 :
[[email protected] ~]# pvcreate /dev/sdb1 /dev/sdb2
[[email protected] ~]# vgcreate vg_ops /dev/sdb1 /dev/sdb2


  3  Create logical volumes

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


  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
[[email protected] ~]# mkfs -t ext4 /dev/vg_ops/ops_web
[[email protected] ~]# mkfs -t xfs -f /dev/vg_ops/ops_app
[[email protected] ~]# mkdir /web /app
[[email protected] ~]# mount /dev/vg_ops/ops_web /web
[[email protected] ~]# mount /dev/vg_ops/ops_app /app
[[email protected] ~]# df -Th


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 .
[[email protected] ~]# pvcreate /dev/sdb3
[[email protected] ~]# vgextend vg_ops /dev/sdb3
[[email protected] ~]# vgdisplay vg_ops


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.
[[email protected] ~]# lvextend -L +2G /dev/vg_ops/ops_web
[[email protected] ~]# lvextend -L +2G /dev/vg_ops/ops_app
[[email protected] ~]# resize2fs /dev/vg_ops/ops_web
[[email protected] ~]# xfs_growfs /dev/vg_ops/ops_app
[[email protected] ~]# df -Th


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 .
[[email protected] ~]# umount /dev/vg_ops/ops_web 
[[email protected] ~]# umount /dev/vg_ops/ops_app
[[email protected] ~]# lvremove /dev/vg_ops/ops_web
[[email protected] ~]# lvremove /dev/vg_ops/ops_app 
[[email protected] ~]# vgremove vg_ops
[[email protected] ~]# pvremove /dev/sdb{1,2,3}


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 .

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


  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 .



  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云