Practical practice on Linux server disk partitioning in production environment, lossless expansion of root partition and mounting disk to directory

– The following demonstration uses a virtual machine. The operation in the real environment depends on the situation and is generally consistent!

1. Select a virtual machine and add a new disk of 100G to it

Insert image description here

2. Connect to the virtual machine and view the newly added disk

Insert image description here

If you view it directly after adding a new disk, you will not be able to view it because the system has not yet recognized the newly added disk. You need to let the Linux system recognize the new disk again.

两种方法:

1. Restart the server (the server is basically not restarted at will in production)

2.不重启服务器(使用命令来扫描识别新磁盘)

—》不重启服务器识别新磁盘方法如下:

2.1. Check the host bus number

[root@localhost ~]# ls /sys/class/scsi_host/
host0  host1  host2

2.2. Rescan the SCSI bus to add devices (The reason why we scan the SCSI bus is because we are adding a SCSI type disk.)

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host0/scan

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host1/scan

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan

2.3 View the newly added disk recognized

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
└─sdb1            8:17   0   20G  0 part
  └─centos-root 253:0    0   37G  0 lvm  /
sdc               8:32   0  100G  0 disk   --》成功识别到新磁盘
sr0              11:0    1  906M  0 rom

Insert image description here

3. Partition the newly added disk sdc into 2 partitions, and set the disk partition type to 8e, which is the LVM type

[root@localhost ~]# fdisk /dev/sdc
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xc605fa7d 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n                     --添加新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)        --p为主分区,最多只能有4个
   e   extended                             --e为逻辑分区--
Select (default p): p                       --我们需要分主分区,所以输入p
分区号 (1-4,默认 1):1               --这个分区的分区号,sdc1、sdc2这种,默认从1开始计数,默认
起始 扇区 (2048-209715199,默认为 2048):    --起始扇区,默认将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199):+50G         --分多个区时,需要规划每个区分多大的空间,这里为50G,记住前面有+号
分区 1 已设置为 Linux 类型,大小设为 50 GiB
命令(输入 m 获取帮助):t                     --设置sdc1分区的类型
已选择分区 1                                 --选择要设置哪个分区
Hex 代码(输入 L 列出所有代码):8e            --8e类型为LVM类型,设置为8e,方便后期(默认为83类型,8e类型为LVM类型)
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):n              --添加新分区(将一个磁盘分成多个区时,就可以这样分完一个区,再次分下一个)
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):2              --sdc1分区已经分了,所以这里从第2个开始,即为sdc2分区
起始 扇区 (104859648-209715199,默认为 104859648):
将使用默认值 104859648              --因为已经有sdc1分区了,所以起始扇区是从sdc1分区的末尾开始算,sdc1是从2048~104859647,则sdc2分区为104859648~207620095
Last 扇区, +扇区 or +size{K,M,G} (104859648-209715199,默认为 209715199):+49G        --磁盘总大小为100G,sdc1已经分走50G,这里不能再分完整的50G,会提示空间不足,一般需预留一点才行,所以这里分49G
分区 2 已设置为 Linux 类型,大小设为 49 GiB
命令(输入 m 获取帮助):t
分区号 (1,2,默认 2):2
Hex 代码(输入 L 列出所有代码):8e            --设置分区类型为8e,即LVM类型
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):p            --列出分区信息
磁盘 /dev/sdc:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xc605fa7d
   设备 Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048   104859647    52428800   8e  Linux LVM
/dev/sdc2       104859648   207620095    51380224   8e  Linux LVM       —》可以看到分出来的2个磁盘类型都设置为了8e,即LVM类型

命令(输入 m 获取帮助):w          --将分区表写入磁盘保存,如果不想保存,则输入q,可以不保存上面的分区,可以重新分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
└─sdb1            8:17   0   20G  0 part
  └─centos-root 253:0    0   37G  0 lvm  /
sdc               8:32   0  100G  0 disk
├─sdc1            8:33   0   50G  0 part  #可以看到分的2个区
└─sdc2            8:34   0   49G  0 part
sr0              11:0    1  906M  0 rom

a

4.将sdc1分区用于无损扩容根目录空间sdc2分区用于挂载到/home子目录单独使用

--------Losslessly expand the root directory of the sdc1 partition--------

4.1 Create a physical volume from the sdc1 partition

pvcreate /dev/sdc1

pvdisplay #View the created physical volume

Insert image description here

4.2 Expand the physical volume pv to the volume group vg

vgdisplay #First check the name of the volume group vg for expansion

Insert image description here

vgextend centos /dev/sdc1 #Expand volume group

vgdisplay #View the expanded volume group

**[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-AhKpBzQU-1680159952434)(image/ca96168eeb83edb57885c565735f8b1f.png)]**

You can see that the volume group has been expanded from the initial 38.99G to 88.99G.

4.3 Expand the LVM root partition

You can use df -hlthe path to view the root partition / to expand the root partition.

You can also lvdisplaycheck the path of lv to expand the root partition (recommended)

Please add image description

lvextend -L +49.99G /dev/mapper/centos-root

When using the above command to expand, because our sdc1 partition space has 50G, when we expand lvm, we can only enter +49.99G. We need to reserve some space. If we directly enter +50G, it will prompt that there is insufficient space. Just fill in 49.99 G can!

lvextend -L +49.99G /dev/centos/root /dev/sdc1

Insert image description here

4.4 Synchronizing the file system means re-reading the root partition disk information

xfs\_growfs /dev/mapper/centos\-root #Synchronize the file system to the root/directory disk path

xfs\_growfs /dev/centos/root(The path here can be the path of df -hl or the path of lvdisplay) --centos7 generally uses this

or

resize2fs /dev/centos/root --centos6 generally uses this
Insert image description here
Insert image description here
Insert image description here

You can see that the root directory has been successfully and losslessly expanded through the above operations!

--------The sdc2 partition is used to mount to the /home subdirectory for separate use---------

Insert image description here

注意:

After adding a new disk to the Linux server, when expanding the capacity of the root partition, follow the sdc1 operation above step by step; if you want to use the partial partition sdc2 of the disk as a mounting directory alone, follow the following methods. These two The first few steps of usage are the same, that is, partition the new disk /dev/vdc first (sdc1 and sdc2 have been divided), and then format the partitioned disk sdc2 to be mounted separately, and then mount it. Loaded and used!

Sdc2分区相关操作如下:

1. Partition the newly added disk sdc into 2 partitions, and set the disk partition type to 8e, which is the LVM type (sdc1 has been demonstrated for root expansion, here we mainly look at the part of sdc2)

[root@localhost ~]# fdisk /dev/sdc
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0xc605fa7d 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n   #添加新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)  #p为主分区,最多只能有4个
   e   extended     #e为逻辑分区
Select (default p): p  #我们需要分主分区,所以输入p
分区号 (1-4,默认 1):1   #这个分区的分区号,sdc1、sdc2这种,默认从1开始计数,默认
起始 扇区 (2048-209715199,默认为 2048):  #起始扇区,默认
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199):+50G   #分多个区时,需要规划每个区分多大的空间,这里为50G,记住前面有+号
分区 1 已设置为 Linux 类型,大小设为 50 GiB
命令(输入 m 获取帮助):t    #设置sdc1分区的类型
已选择分区 1     #选择要设置哪个分区
Hex 代码(输入 L 列出所有代码):8e    #8e类型为LVM类型,设置为8e,方便后期
已将分区“Linux”的类型更改为“Linux LVM”

下面是sdc2分区的分区过程:

命令(输入 m 获取帮助):n   #添加新分区(将一个磁盘分成多个区时,就可以这样分完一个区,再次分下一个)
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
分区号 (2-4,默认 2):2     #sdc1分区已经分了,所以这里从第2个开始,即为sdc2分区
起始 扇区 (104859648-209715199,默认为 104859648):
将使用默认值 104859648    #因为已经有sdc1分区了,所以起始扇区是从sdc1分区的末尾开始算,sdc1是从2048~104859647,则sdc2分区为104859648~207620095
Last 扇区, +扇区 or +size{K,M,G} (104859648-209715199,默认为 209715199):+49G   #磁盘总大小为100G,sdc1已经分走50G,这里不能再分完整的50G,会提示空间不足,一般需预留一点才行,所以这里分49G
分区 2 已设置为 Linux 类型,大小设为 49 GiB
命令(输入 m 获取帮助):t
分区号 (1,2,默认 2):2
Hex 代码(输入 L 列出所有代码):8e  #设置分区类型为8e,即LVM类型
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):p   #列出分区信息
磁盘 /dev/sdc:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xc605fa7d
   设备 Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048   104859647    52428800   8e  Linux LVM
/dev/sdc2       104859648   207620095    51380224   8e  Linux LVM    —》可以看到分出来的2个磁盘类型都设置为了8e,即LVM类型

命令(输入 m 获取帮助):w    #将分区表写入磁盘保存,如果不想保存,则输入q,可以不保存上面的分区,可以重新分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   37G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
└─sdb1            8:17   0   20G  0 part
  └─centos-root 253:0    0   37G  0 lvm  /
sdc               8:32   0  100G  0 disk
├─sdc1            8:33   0   50G  0 part  
└─sdc2            8:34   0   49G  0 part  #可以看到本次要用的sdc2分区
sr0              11:0    1  906M  0 rom

Insert image description here

2. Format the partition file system (the most common types are xfs and ext4)

xfs和ext4的区别:
1.RHEL/Centos7默认文件系统是XFS,Centos6为Ext4,Centos5为Ext3。
2.XFS可扩展性和Scalability比较强,Ext4受限于磁盘结构和兼容问题。
3.EXT4可支持单个文件的大小:16GB到16TB。性能更高!
4.XFS可支持单个文件的大小:16TB到16EB。
5.XFS是64位文件系统,理论上最大支持8EB减1字节的单个文件系统。
6.Ext4是32位文件系统,理论上最大支持1EB减1字节的单个文件系统。

2.1 Format the partition sdc2 file system to xfs file system type

Insert image description here

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   87G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk
└─sdb1            8:17   0   20G  0 part
  └─centos-root 253:0    0   87G  0 lvm  /
sdc               8:32   0  100G  0 disk
├─sdc1            8:33   0   50G  0 part
│ └─centos-root 253:0    0   87G  0 lvm  /
└─sdc2            8:34   0   49G  0 part
sr0              11:0    1  906M  0 rom
[root@localhost ~]# mkfs.xfs /dev/sdc2
meta-data=/dev/sdc2              isize=512    agcount=4, agsize=3211264 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=12845056, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=6272, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

2.2 View the file system of the formatted sdc2 partition

[root@localhost ~]# blkid
/dev/sda1: UUID="53742718-1099-483a-8eb5-bd343b82d709" TYPE="xfs"
/dev/sda2: UUID="h8HIlP-RzLz-Qabd-zU9m-TekA-CDDx-wwbVrR" TYPE="LVM2_member"
/dev/sdb1: UUID="o97VVU-AdR2-Fm61-oRUA-CWZx-FF86-GKNaJN" TYPE="LVM2_member"
/dev/sr0: UUID="2018-05-03-21-07-04-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="4d85ff81-ed74-449a-8d72-0c91a9b5b5e2" TYPE="xfs"
/dev/mapper/centos-swap: UUID="1b727d3c-ccf7-4828-911f-78bf59691809" TYPE="swap"
/dev/sdc1: UUID="n4PDMJ-8Pmy-4G5E-q9qv-zbGs-ESQc-6Vd0Vd" TYPE="LVM2_member"
/dev/sdc2: UUID="bd0941e7-102c-4dbd-9eec-7a75cdbe98d2" TYPE=“xfs"

Insert image description here

3. Create a mount directory

[root@localhost ~]# mkdir /home/file

Insert image description here

4. Write the automatic mounting configuration file /etc/fstab and add new mounting configuration

格式:
设备名称  挂载点  分区的类型  挂载选项  dump选项  fsck选项

-设备名称:
  可以是实际设备名称/dev/sda1(这类名称不稳定,在配置文件中不要使用);也可以是卷标LABEL;最好是用UUID;(不会变化)可以通过blkid命令获取设备uuid信息

-挂载点:
/、/usr、swap等都是系统安装是分区的默认挂载点

-文件系统类型
ext2、ext3、ext4、xfs、brtfs、jfs、reiserfs、reiser4、swap

-挂载选项:
auto/noauto:默认auto支持;是否支持系统自动挂载,是否支持-a选项
user/nouser:默认nouser不支持;是否支持普通用户挂载此设备
ro:按制度权限挂载
rw:按可读可写权限挂载
pri:swap设备需要指定优先级
defaults:默认支持rw,nouid,dev,auto,nosuer,async(若不写入则默认支持)   —》基本都是默认用这个

-dump选项:
设置是否让备份程序备份文件系统,0为不备份,1为每天次,如果上次是用dump备份,将显示备份至今的天数

-fsck选项:
告诉fsck程序以什么顺序检查文件系统,0为不检查,根分区为1表示永远检查,其他从2开始,数字相同就同时检查,但是不能同时为1;注意,最好将这个选项设置为0,有故障的时候机器还可以重启,如果不设置成为0,出故障还需要去机房排查故障

Original /etc/fstab configuration file:

Insert image description here

View the details (UUID) of the sdc2 partition

[root@localhost ~]# blkid
/dev/sda1: UUID="53742718-1099-483a-8eb5-bd343b82d709" TYPE="xfs"
/dev/sda2: UUID="h8HIlP-RzLz-Qabd-zU9m-TekA-CDDx-wwbVrR" TYPE="LVM2_member"
/dev/sdb1: UUID="o97VVU-AdR2-Fm61-oRUA-CWZx-FF86-GKNaJN" TYPE="LVM2_member"
/dev/sr0: UUID="2018-05-03-21-07-04-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/mapper/centos-root: UUID="4d85ff81-ed74-449a-8d72-0c91a9b5b5e2" TYPE="xfs"
/dev/mapper/centos-swap: UUID="1b727d3c-ccf7-4828-911f-78bf59691809" TYPE="swap"
/dev/sdc1: UUID="n4PDMJ-8Pmy-4G5E-q9qv-zbGs-ESQc-6Vd0Vd" TYPE="LVM2_member"
/dev/sdc2: UUID="bd0941e7-102c-4dbd-9eec-7a75cdbe98d2" TYPE="xfs"

Add new configuration to /etc/fstab file

[root@localhost ~]# vi /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Dec  3 19:51:41 2021
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=53742718-1099-483a-8eb5-bd343b82d709 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=bd0941e7-102c-4dbd-9eec-7a75cdbe98d2 /home/file xfs defaults 0 0

5. Use mount -a to reload the configuration file at boot and check the mounting status.

Insert image description here

You can see that the sdc2 partition disk has been successfully mounted to the /home/file directory, and the file has also been configured to automatically mount at boot!

[Personal public account]

【非著名运维】 --》 公众号回复 “资料” 送运维自学资料大礼包哦!I will also share some operation and maintenance knowledge above. Please pay attention to it. Thank you all.

Guess you like

Origin blog.csdn.net/qq_44895681/article/details/129858125