查看主机磁盘
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part
├─centos-root 253:0 0 47G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 30G 0 disk
sr0 11:0 1 4.4G 0 rom
可以看出,主机有sda和adb两个硬盘,sda在装系统时使用了,sdb还没被使用
二:通过上面可以看出sdb硬盘一共30G,使用sdb给根扩容10G,在/opt目录下给创建个software,给/opt/software文件夹单独挂载一个逻辑卷20G
通过下面的方式去实现
(1)通过fdisk命令新增一个sdb1分区10G和sdb2分区分别用来给根扩容和创建20G的/opt/software逻辑卷
显示磁盘分区信息: fdisk -l 该命令将显示系统中所有磁盘的分区信息。
[root@localhost ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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 label type: dos
Disk identifier: 0x000c6d73
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 104857599 51379200 8e Linux LVM
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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/centos-swap: 2147 MB, 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
#选择要操作的磁盘 /dev/sdb
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0x899187de.
Command (m for help): p #查看分区信息
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x899187de
Device Boot Start End Blocks Id System
Command (m for help): n #新增分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-62914559, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x899187de
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (20973568-62914559, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559):
Using default value 62914559
Partition 2 of type Linux and of size 20 GiB is set
Command (m for help): p
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x899187de
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 62914559 20970496 83 Linux
Command (m for help): t #修改分区类型
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e #这里的8e是LVM的类型,不同的主机操作系统LVM的类型对应的只有点区别
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x899187de
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 62914559 20970496 83 Linux
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
Disk identifier: 0x899187de
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 8e Linux LVM
/dev/sdb2 20973568 62914559 20970496 8e Linux LVM
Command (m for help): w #保存并退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
(2)在这咱们就能看出咱们将sdb硬盘划分成sdb1和sdb2了
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 49G 0 part
├─centos-root 253:0 0 47G 0 lvm /
└─centos-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 30G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 20G 0 part
sr0 11:0 1 4.4G 0 rom
(2)咱们先进行根文件系统
的扩容
通过下面的命令咱们可以看到当前根目录的大小及文件系统的类型,根文件系统的类型是xfs,常见的文件系统是xfs和ext4
[root@localhost ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 498M 0 498M 0% /dev
tmpfs tmpfs 510M 0 510M 0% /dev/shm
tmpfs tmpfs 510M 8.1M 502M 2% /run
tmpfs tmpfs 510M 0 510M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 51G 6.9G 44G 14% /
/dev/sda1 xfs 1.1G 143M 921M 14% /boot
tmpfs tmpfs 102M 0 102M 0% /run/user/0
```bash
对根目录扩容的思路如下:
先通过sdb1创建PV,然后扩容VG,在扩容LV,然后刷新文件系统生效
#通过lvdisplay能看出来根文件系统在centos的VG中,/dev/centos/root的LV中
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID i8UBfM-m1Dj-32ch-oe2Z-XdBH-QESS-M6Dz22
LV Write Access read/write
LV Creation host, time localhost, 2023-07-02 07:42:18 -0400
LV Status available
# open 1
LV Size 46.99 GiB
Current LE 12030
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
#创建PV,这里要使用/dev/sdb1,可以通过fdisk -l命令查看
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <49.00g 4.00m
#扩容VG
[root@localhost ~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 58.99g 10.00g
#查看和扩容LV
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 46.99g
swap centos -wi-ao---- 2.00g
[root@localhost ~]# lvextend -l 100%VG /dev/centos/root
Reducing 100%VG to remaining free space 56.99 GiB in VG.
Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# lvs #可以看出LV由原来的46G扩容到了56G
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 56.99g
swap centos -wi-ao---- 2.00g
#但是咱们现在看主机为根目录师妹变化的,需要扩展 XFS 文件系统
[root@localhost ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 498M 0 498M 0% /dev
tmpfs tmpfs 510M 0 510M 0% /dev/shm
tmpfs tmpfs 510M 8.1M 502M 2% /run
tmpfs tmpfs 510M 0 510M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 51G 6.9G 44G 14% /
/dev/sda1 xfs 1.1G 143M 921M 14% /boot
tmpfs tmpfs 102M 0 102M 0% /run/user/0
#扩展 XFS 文件系统
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3079680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=12318720, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6015, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 12318720 to 14940160
[root@localhost ~]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 498M 0 498M 0% /dev
tmpfs tmpfs 510M 0 510M 0% /dev/shm
tmpfs tmpfs 510M 8.1M 502M 2% /run
tmpfs tmpfs 510M 0 510M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 62G 6.9G 55G 12% /
/dev/sda1 xfs 1.1G 143M 921M 14% /boot
tmpfs tmpfs 102M 0 102M 0% /run/user/0
(2)对/opt/software挂载单独的逻辑卷,设置开机自动挂载
#先创建对应的文件夹
[root@localhost ~]# cd /opt/
[root@localhost opt]# ll
total 0
[root@localhost opt]# mkdir software
[root@localhost opt]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug 6 10:11 software
单独挂载一个逻辑卷的操作和扩容区别不大
#创建PV
[root@localhost opt]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
#创建VG
[root@localhost opt]# vgcreate software /dev/sdb2
Volume group "software" successfully created
[root@localhost opt]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- 58.99g 0
software 1 0 0 wz--n- <20.00g <20.00g
#在software的VG里创建一个名叫lv_soft的LV
[root@localhost opt]# lvcreate -l 100%VG -n lv_soft software
Logical volume "lv_soft" created.
#查看LV
[root@localhost opt]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 56.99g
swap centos -wi-ao---- 2.00g
lv_soft software -wi-a----- <20.00g
#格式化/dev/software/lv_soft新建的这个逻辑卷
[root@localhost opt]# mkfs.xfs /dev/software/lv_soft #如果想要EXT4文件格式的话使用mkfs.ext4 /dev/software/lv_soft
meta-data=/dev/software/lv_soft isize=512 agcount=4, agsize=1310464 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5241856, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
#在开机自启的配置文件中加上/opt/software逻辑卷得挂载配置,这里的配置添加一定不要出现格式问题,出现格式问题的话,主机重启的情况下无法正常进入系统
[root@localhost opt]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Jul 2 07:42:19 2023
#
# 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=6556b199-ca6e-42c3-b50a-bb45b4d53850 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/software/lv_soft /opt/software xfs defaults 0 0
[root@localhost opt]# mount -a #这里使用mount -a 挂载是检验/etc/fstab配置文件中新增的内容有没有问题
[root@localhost opt]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 498M 0 498M 0% /dev
tmpfs tmpfs 510M 0 510M 0% /dev/shm
tmpfs tmpfs 510M 8.1M 502M 2% /run
tmpfs tmpfs 510M 0 510M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 62G 6.9G 55G 12% /
/dev/sda1 xfs 1.1G 143M 921M 14% /boot
tmpfs tmpfs 102M 0 102M 0% /run/user/0
/dev/mapper/software-lv_soft xfs 22G 34M 22G 1% /opt/software