LVM配置
1.新建LVM
1.1.添加2个磁盘
1.2.识别磁盘
[03:11:18 root@ubuntu apt]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 49.5G 0 lvm /
sr0 11:0 1 945M 0 rom /media
刷新后识别到2个新增磁盘,或者通过重启让系统识别
[03:12:24 root@ubuntu apt]#for i in `ls /sys/class/scsi_host/` ;do echo "- - -" >/sys/class/scsi_host/$i/scan ;done
[03:12:45 root@ubuntu apt]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 49.5G 0 lvm /
sdb 8:16 0 5G 0 disk
sdc 8:32 0 4G 0 disk
sr0 11:0 1 945M 0 rom /media
1.3.创建分区
[03:12:53 root@ubuntu apt]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.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 0x5fce5cb4.
创建磁盘分区
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 #序号为1
First sector (2048-10485759, default 2048): #起始位置
Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +1.5G #大小为1.5G
Created a new partition 1 of type 'Linux' and of size 1.5 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2 #第二个主分区
First sector (3123200-10485759, default 3123200):
Last sector, +sectors or +size{K,M,G,T,P} (3123200-10485759, default 10485759): +500M
Created a new partition 2 of type 'Linux' and of size 500 MiB.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 83 Linux
/dev/sdb2 3123200 4147199 1024000 500M 83 Linux
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 83 Linux
/dev/sdb2 3123200 4147199 1024000 500M 83 Linux
Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e #修改分区格式为lvm
Changed type of partition 'Linux' to 'Linux LVM'.
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: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 8e Linux LVM
/dev/sdb2 3123200 4147199 1024000 500M 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
此时磁盘使用情况
[04:41:43 root@ubuntu apt]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 49.5G 0 lvm /
sdb 8:16 0 5G 0 disk
├─sdb1 8:17 0 1.5G 0 part <------------新增P1
└─sdb2 8:18 0 500M 0 part <------------新增P2
sdc 8:32 0 4G 0 disk <------------未分区的磁盘
sr0 11:0 1 945M 0 rom /media
[04:41:55 root@ubuntu apt]#
1.4.创建PV
[04:43:10 root@ubuntu apt]#pvcreate /dev/sd{b1,b2,c} #将3个分区都创建为PV
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdc" successfully created.
[04:46:05 root@ubuntu apt]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <99.00g <49.50g
/dev/sdb1 lvm2 --- <1.49g <1.49g
/dev/sdb2 lvm2 --- 500.00m 500.00m
/dev/sdc lvm2 --- 4.00g 4.00g
1.5.创建VG
[04:46:53 root@ubuntu apt]#vgcreate vg_mysql /dev/sd{b1,b2,c}
#vg名为vg_mysql 将3个PV都加入到改VG中
Volume group "vg_mysql" successfully created
[04:47:14 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 3 0 0 wz--n- 5.96g 5.96g
[04:49:04 root@ubuntu apt]#vgdisplay vg_mysql
--- Volume group ---
VG Name vg_mysql
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 5.96 GiB
PE Size 4.00 MiB
Total PE 1527
Alloc PE / Size 0 / 0
Free PE / Size 1527 / 5.96 GiB
VG UUID 3NITHK-udgY-0cTk-0fcI-GjUn-UJwm-KS0a5B
[04:49:11 root@ubuntu apt]#
1.6.LV创建
1.6.1.创建数据卷
[04:49:11 root@ubuntu apt]#lvcreate -n mysql -L 1G vg_mysql
#lv名字为mysql,大小1G从vg_mysql中划分
Logical volume "mysql" created.
[04:52:45 root@ubuntu apt]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-a----- 1.00g
[04:52:59 root@ubuntu apt]#lvdisplay /dev/vg_mysql/mysql
--- Logical volume ---
LV Path /dev/vg_mysql/mysql
LV Name mysql
VG Name vg_mysql
LV UUID WMAvN2-gTq6-4W3U-sZPE-2x42-BMj7-9LoxRM
LV Write Access read/write
LV Creation host, time ubuntu, 2021-07-15 04:52:07 +0000
LV Status available
# open 0
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
[04:53:02 root@ubuntu apt]#vgdisplay vg_mysql
--- Volume group ---
VG Name vg_mysql
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 5.96 GiB
PE Size 4.00 MiB
Total PE 1527
Alloc PE / Size 256 / 1.00 GiB <---------1G已被划分
Free PE / Size 1271 / 4.96 GiB
VG UUID 3NITHK-udgY-0cTk-0fcI-GjUn-UJwm-KS0a5B
[04:54:39 root@ubuntu apt]#ll /dev/vg_mysql/mysql
lrwxrwxrwx 1 root root 7 Jul 15 04:52 /dev/vg_mysql/mysql -> ../dm-1
一共有3中方式可以挂载这个卷
1./dev/vg_mysql/mysql
2./dev/dm-1
3./dev/mapper/vg_mysql-mysql
1.6.2.再创建个日志卷
[04:56:52 root@ubuntu apt]#lvcreate -n mysql_log -l 50%free vg_mysql
Logical volume "mysql_log" created.
[04:57:53 root@ubuntu apt]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-a----- 1.00g
mysql_log vg_mysql -wi-a----- 2.48g
[04:57:58 root@ubuntu apt]#vgdisplay vg_mysql
--- Volume group ---
VG Name vg_mysql
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 5.96 GiB
PE Size 4.00 MiB
Total PE 1527
Alloc PE / Size 891 / 3.48 GiB
Free PE / Size 636 / 2.48 GiB
VG UUID 3NITHK-udgY-0cTk-0fcI-GjUn-UJwm-KS0a5B
[04:57:59 root@ubuntu apt]#ll /dev/vg_mysql/mysql_log
lrwxrwxrwx 1 root root 7 Jul 15 04:56 /dev/vg_mysql/mysql_log -> ../dm-2
1.7.格式化LV
[04:58:53 root@ubuntu apt]#mkfs.xfs /dev/vg_mysql/mysql
mysql mysql_log
[04:58:53 root@ubuntu apt]#mkfs.xfs /dev/vg_mysql/mysql
meta-data=/dev/vg_mysql/mysql isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0, rmapbt=0, reflink=0
data = bsize=4096 blocks=262144, 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
[04:59:43 root@ubuntu apt]#mkfs.xfs /dev/vg_mysql/mysql_log
meta-data=/dev/vg_mysql/mysql_log isize=512 agcount=4, agsize=162560 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=0, rmapbt=0, reflink=0
data = bsize=4096 blocks=650240, 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
1.8.LV开机挂载
1.8.1.创建挂载点目录
[05:20:55 root@ubuntu apt]#mkdir /mysql /mysql_log
1.8.2.编辑/etc/fstab
/dev/disk/by-id/dm-uuid-LVM-y0iT0XqxFqWd00Dz5aeCghGvJfggzFhdVexLz1sWqfbBOo65ksehl6U3Vtk1bLcu / ext4 defaults 0 0
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/cd0dca1b-70be-4254-8d94-f117bac194c1 /boot ext4 defaults 0 0
/swap.img none swap sw 0 0
~
~
~
~
~
~
:r!blkid /dev/vg_mysql/mysql
通过vim 命令得到LV的UUID
/dev/vg_mysql/mysql: UUID="29a9e829-5327-4216-b212-27091b4bc99c" TYPE="xfs"
整理后得到
UUID=29a9e829-5327-4216-b212-27091b4bc99c /mysql xfs defaults 0 0
UUID=ed512ac1-1898-407b-81ae-604b0d1198b4 /mysql_log xfs defaults 0 0
:x 保存退出
[05:11:14 root@ubuntu apt]#mount -a
[05:11:18 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/sr0 945M 945M 0 100% /media
/dev/mapper/vg_mysql-mysql 1014M 34M 981M 4% /mysql
/dev/mapper/vg_mysql-mysql_log 2.5G 35M 2.5G 2% /mysql_log
2.扩容LVM
2.1.扩容方法1
保证卷组空间是够用
[05:20:55 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 3 2 0 wz--n- 5.96g 2.48g
[05:36:12 root@ubuntu apt]#lvextend -l +50%free /dev/vg_mysql/mysql
Size of logical volume vg_mysql/mysql changed from 1.00 GiB (256 extents) to 2.24 GiB (574 extents).
Logical volume vg_mysql/mysql successfully resized.
[05:37:15 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 3 2 0 wz--n- 5.96g 1.24g
[05:37:43 root@ubuntu apt]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-ao---- 2.24g
mysql_log vg_mysql -wi-ao---- 2.48g
[05:37:45 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/sr0 945M 945M 0 100% /media
/dev/mapper/vg_mysql-mysql 1014M 34M 981M 4% /mysql
/dev/mapper/vg_mysql-mysql_log 2.5G 35M 2.5G 2% /mysql_log
[05:38:50 root@ubuntu apt]#xfs_growfs /dev/vg_mysql/mysql
meta-data=/dev/mapper/vg_mysql-mysql isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1 spinodes=0 rmapbt=0
= reflink=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 262144 to 587776
[05:39:05 root@ubuntu apt]#df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 966M 0 966M 0% /dev
tmpfs tmpfs 200M 1.1M 199M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 53G 6.3G 44G 13% /
tmpfs tmpfs 1.0G 0 1.0G 0% /dev/shm
tmpfs tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs tmpfs 1.0G 0 1.0G 0% /sys/fs/cgroup
/dev/sda2 ext4 1.1G 157M 797M 17% /boot
tmpfs tmpfs 200M 0 200M 0% /run/user/1000
/dev/sr0 iso9660 991M 991M 0 100% /media
/dev/mapper/vg_mysql-mysql xfs 2.4G 37M 2.4G 2% /mysql
/dev/mapper/vg_mysql-mysql_log xfs 2.7G 37M 2.7G 2% /mysql_lo
2.2.扩容方法2
通过-r参数实现一步到位,不用再判断是xfs还是ext4格式分区
[05:53:52 root@ubuntu apt]#lvextend -r -l +100%free /dev/vg_mysql/mysql
Size of logical volume vg_mysql/mysql changed from 2.24 GiB (574 extents) to 3.48 GiB (892 extents).
Logical volume vg_mysql/mysql successfully resized.
meta-data=/dev/mapper/vg_mysql-mysql isize=512 agcount=9, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1 spinodes=0 rmapbt=0
= reflink=0
data = bsize=4096 blocks=587776, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 587776 to 913408
[05:53:59 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 3 2 0 wz--n- 5.96g 0
[05:54:02 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/sr0 945M 945M 0 100% /media
/dev/mapper/vg_mysql-mysql 3.5G 37M 3.5G 2% /mysql
/dev/mapper/vg_mysql-mysql_log 2.5G 35M 2.5G 2% /mysql_log
3.卷组扩容
3.1.添加分区
[05:57:02 root@ubuntu apt]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 8e Linux LVM
/dev/sdb2 3123200 4147199 1024000 500M 8e Linux LVM
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (4147200-10485759, default 4147200):
Last sector, +sectors or +size{K,M,G,T,P} (4147200-10485759, default 10485759): +600M
Created a new partition 3 of type 'Linux' and of size 600 MiB.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 8e Linux LVM
/dev/sdb2 3123200 4147199 1024000 500M 8e Linux LVM
/dev/sdb3 4147200 5375999 1228800 600M 83 Linux
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Syncing disks.
3.2.扩容vg
[05:58:50 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 3 2 0 wz--n- 5.96g 0
[05:59:22 root@ubuntu apt]#partprobe
[06:01:12 root@ubuntu apt]#vgextend vg_mysql /dev/sdb3
Volume group "vg_mysql" successfully extended
[06:01:23 root@ubuntu apt]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 4 2 0 wz--n- <6.55g 596.00m
4.缩容vg
1.缩容需要停应用,umount
2.ext支持,xfs不支持缩减
步骤:
1.umount /dev/vg_mysql/mysql
2.e2fsck -f /dev/vg_mysql/mysql
3.resize2fs /dev/vg_mysql/mysql
4.lvreduce -L -xx /dev/vg_mysql/mysql
5.mount /dev/vg_mysql/mysql /mysql
4.1.由于xfs不支持缩减,先将文件系统改为ext4格式
[06:09:38 root@ubuntu apt]#umount /mysql_log
[06:09:57 root@ubuntu apt]#mkfs.ext4 /dev/vg_mysql/mysql_log
mke2fs 1.44.1 (24-Mar-2018)
/dev/vg_mysql/mysql_log contains a xfs file system
Proceed anyway? (y,N) y
Creating filesystem with 650240 4k blocks and 162560 inodes
Filesystem UUID: 7b4fa28e-1944-424e-a65e-3c89a4778f69
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
[06:13:03 root@ubuntu apt]#tail -1 /etc/fstab
UUID=7b4fa28e-1944-424e-a65e-3c89a4778f69 /mysql_log ext4 defaults 0 0
[06:13:06 root@ubuntu apt]#mount -a
[06:12:11 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/mapper/vg_mysql-mysql 3.5G 37M 3.5G 2% /mysql
/dev/mapper/vg_mysql-mysql_log 2.4G 7.5M 2.3G 1% /mysql_log
创建一些文件
[06:13:08 root@ubuntu apt]#cp /etc/*.conf /mysql_log/
[06:14:27 root@ubuntu apt]#ls /mysql_log/
adduser.conf deluser.conf hdparm.conf ld.so.conf lost+found nsswitch.conf popularity-contest.conf sos.conf updatedb.conf
ca-certificates.conf fuse.conf host.conf libaudit.conf ltrace.conf overlayroot.conf resolv.conf sysctl.conf
debconf.conf gai.conf kernel-img.conf logrotate.conf mke2fs.conf pam.conf rsyslog.conf ucf.conf
[06:14:41 root@ubuntu apt]#dd if=/dev/zero of=/mysql_log/mysql.log bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 2.91382 s, 180 MB/s
[06:15:44 root@ubuntu apt]#du -sh /mysql_log/
501M /mysql_log/
[06:15:48 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/mapper/vg_mysql-mysql 3.5G 37M 3.5G 2% /mysql
/dev/mapper/vg_mysql-mysql_log 2.4G 508M 1.8G 23% /mysql_log
4.2.卸载挂载
可以看到,如果不检测卷会报错
[06:21:09 root@ubuntu apt]#umount /mysql_log
[06:21:46 root@ubuntu apt]#resize2fs /dev/vg_mysql/mysql_log 700M
resize2fs 1.44.1 (24-Mar-2018)
Please run 'e2fsck -f /dev/vg_mysql/mysql_log' first.
4.3.检查卷
[06:22:15 root@ubuntu apt]#e2fsck -f /dev/vg_mysql/mysql_log
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg_mysql/mysql_log: 36/162560 files (0.0% non-contiguous), 156535/650240 blocks
4.4.缩小分区
[06:23:44 root@ubuntu apt]#resize2fs /dev/vg_mysql/mysql_log 700M
resize2fs 1.44.1 (24-Mar-2018)
resize2fs: New size smaller than minimum (254053)
4.5.缩减逻辑卷
[06:24:15 root@ubuntu apt]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-ao---- 3.48g
mysql_log vg_mysql -wi-a----- 2.48g
[06:24:52 root@ubuntu apt]#
[06:24:52 root@ubuntu apt]#lvreduce -L 700M /dev/vg_mysql/mysql_log
WARNING: Reducing active logical volume to 700.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_mysql/mysql_log? [y/n]: y <----输入y后按回车
Size of logical volume vg_mysql/mysql_log changed from 2.48 GiB (635 extents) to 700.00 MiB (175 extents).
Logical volume vg_mysql/mysql_log successfully resized.
[06:26:00 root@ubuntu apt]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-ao---- 3.48g
mysql_log vg_mysql -wi-a----- 700.00m
4.6.重新挂载
至此,缩减失败!所以缩减磁盘还是存在很大风险的
[06:28:23 root@ubuntu apt]#mount -a
mount: /mysql_log: wrong fs type, bad option, bad superblock on /dev/mapper/vg_mysql-mysql_log, missing codepage or helper program, or other error.
4.7.重来一次
这次成功了,得到的教训是:
1.缩减风险比较大,可能会失败.
2.缩减尽量预留出比较大空间.
3.操作是正确的,成不成功就…
[06:28:27 root@ubuntu apt]#mkfs.ext4 /dev/mapper/vg_mysql-mysql_log
mke2fs 1.44.1 (24-Mar-2018)
/dev/mapper/vg_mysql-mysql_log contains a ext4 file system
last mounted on /mysql_log on Thu Jul 15 06:12:11 2021
Proceed anyway? (y,N) y
Creating filesystem with 179200 4k blocks and 44832 inodes
Filesystem UUID: 1769212e-3354-4235-a0aa-3d8735a3168b
Superblock backups stored on blocks:
32768, 98304, 163840
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[06:29:07 root@ubuntu apt]#mount /dev/mapper/vg_mysql-mysql_log /mysql_log/
[06:31:29 root@ubuntu apt]#lvextend -L +1G /dev/vg_mysql/mysql_log
Size of logical volume vg_mysql/mysql_log changed from 700.00 MiB (175 extents) to 1.68 GiB (431 extents).
Logical volume vg_mysql/mysql_log successfully resized.
[06:32:05 root@ubuntu apt]#resize2fs /dev/mapper/vg_mysql-mysql_log
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/mapper/vg_mysql-mysql_log is mounted on /mysql_log; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/vg_mysql-mysql_log is now 441344 (4k) blocks long.
[06:32:16 root@ubuntu apt]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1.1M 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/mapper/vg_mysql-mysql 3.5G 37M 3.5G 2% /mysql
/dev/mapper/vg_mysql-mysql_log 1.7G 2.1M 1.6G 1% /mysql_log
[06:35:28 root@ubuntu ~]#dd if=/dev/zero of=/mysql_log/mysql.log bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 0.801787 s, 654 MB/s
[06:35:57 root@ubuntu ~]#cp /etc/*.conf /mysql_log/
[06:36:11 root@ubuntu ~]#umount /mysql_log
[06:36:30 root@ubuntu ~]#e2fsck -f /dev/vg_mysql/mysql_log
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg_mysql/mysql_log: 36/104608 files (0.0% non-contiguous), 139233/441344 blocks
[06:36:47 root@ubuntu ~]#resize2fs /dev/vg_mysql/mysql_log 1G
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/vg_mysql/mysql_log to 262144 (4k) blocks.
The filesystem on /dev/vg_mysql/mysql_log is now 262144 (4k) blocks long.
[06:37:04 root@ubuntu ~]#lvreduce -L 1G /dev/vg_mysql/mysql_log
WARNING: Reducing active logical volume to 1.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_mysql/mysql_log? [y/n]: y
Size of logical volume vg_mysql/mysql_log changed from 1.68 GiB (431 extents) to 1.00 GiB (256 extents).
Logical volume vg_mysql/mysql_log successfully resized.
[06:37:32 root@ubuntu ~]#mount -a
mount: /mysql_log: can't find UUID=7b4fa28e-1944-424e-a65e-3c89a4778f69.
[06:37:47 root@ubuntu ~]#blkid /dev/vg_mysql/mysql_log
/dev/vg_mysql/mysql_log: UUID="1769212e-3354-4235-a0aa-3d8735a3168b" TYPE="ext4"
[06:38:04 root@ubuntu ~]#mount /dev/vg_mysql/mysql_log /mysql_log/
[06:38:11 root@ubuntu ~]#df -h
Filesystem Size Used Avail Use% Mounted on
udev 921M 0 921M 0% /dev
tmpfs 191M 1020K 190M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 49G 5.9G 41G 13% /
tmpfs 953M 0 953M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 953M 0 953M 0% /sys/fs/cgroup
/dev/sda2 976M 150M 760M 17% /boot
tmpfs 191M 0 191M 0% /run/user/1000
/dev/mapper/vg_mysql-mysql 3.5G 37M 3.5G 2% /mysql
/dev/mapper/vg_mysql-mysql_log 994M 502M 432M 54% /mysql_log
[06:38:13 root@ubuntu ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
ubuntu-lv ubuntu-vg -wi-ao---- 49.50g
mysql vg_mysql -wi-ao---- 3.48g
mysql_log vg_mysql -wi-ao---- 1.00g
5.移除磁盘
[06:38:24 root@ubuntu ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <99.00g <49.50g
/dev/sdb1 vg_mysql lvm2 a-- 1.48g 0
/dev/sdb2 vg_mysql lvm2 a-- 496.00m 0
/dev/sdb3 vg_mysql lvm2 a-- 596.00m 596.00m
/dev/sdc vg_mysql lvm2 a-- <4.00g 1.48g
[06:43:37 root@ubuntu ~]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 4 2 0 wz--n- <6.55g 2.06g
[06:44:15 root@ubuntu ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <99.00g <49.50g
/dev/sdb1 vg_mysql lvm2 a-- 1.48g 0
/dev/sdb2 vg_mysql lvm2 a-- 496.00m 0
/dev/sdb3 vg_mysql lvm2 a-- 596.00m 596.00m
/dev/sdc vg_mysql lvm2 a-- <4.00g 1.48g
想要拆走/dev/sdc,通过vgdisplay可以看到sdc上有644个PE已经被使用了
需要先将他挪到其他分区是上.但是其他几个磁盘上空余的PE不足644.
那么需要先对vg扩容
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg_mysql
PV Size <1.49 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 380
Free PE 0
Allocated PE 380
PV UUID mASTrv-YJj3-2jUr-s6fD-TGVr-ZYs3-VAQoO0
--- Physical volume ---
PV Name /dev/sdb2
VG Name vg_mysql
PV Size 500.00 MiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 124
Free PE 0
Allocated PE 124
PV UUID 4emYBK-Kuk1-6GKr-SUTJ-djDM-xb7K-gpIl1m
--- Physical volume ---
PV Name /dev/sdc
VG Name vg_mysql
PV Size 4.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1023
Free PE 379
Allocated PE 644
PV UUID 7iRNH0-UwfP-eU7u-iH7R-67DL-c2vw-qQovie
--- Physical volume ---
PV Name /dev/sdb3
VG Name vg_mysql
PV Size 600.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 149
Free PE 149
Allocated PE 0
PV UUID fvaBe0-GM18-uVnD-Aebe-TC0G-MVhj-WxfBF3
通过扩容使得剩余PE大于644
[06:47:36 root@ubuntu ~]#fdisk /dev/sdb
Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): p
Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 8e Linux LVM
/dev/sdb2 3123200 4147199 1024000 500M 8e Linux LVM
/dev/sdb3 4147200 5375999 1228800 600M 8e Linux LVM
Command (m for help): n
Partition type
p primary (3 primary, 0 extended, 1 free)
e extended (container for logical partitions)
Select (default e): p
Selected partition 4
First sector (5376000-10485759, default 5376000):
Last sector, +sectors or +size{K,M,G,T,P} (5376000-10485759, default 10485759):
Created a new partition 4 of type 'Linux' and of size 2.4 GiB.
Command (m for help):
Command (m for help): t
Partition number (1-4, default 4): 4
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: 5 GiB, 5368709120 bytes, 10485760 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: 0x5fce5cb4
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 3123199 3121152 1.5G 8e Linux LVM
/dev/sdb2 3123200 4147199 1024000 500M 8e Linux LVM
/dev/sdb3 4147200 5375999 1228800 600M 8e Linux LVM
/dev/sdb4 5376000 10485759 5109760 2.4G 8e Linux LVM
Command (m for help): w
The partition table has been altered.
Syncing disks.
[06:52:41 root@ubuntu ~]#partprobe
[06:53:52 root@ubuntu ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 1G 0 part /boot
└─sda3 8:3 0 99G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 49.5G 0 lvm /
sdb 8:16 0 5G 0 disk
├─sdb1 8:17 0 1.5G 0 part
│ └─vg_mysql-mysql 253:1 0 3.5G 0 lvm /mysql
├─sdb2 8:18 0 500M 0 part
│ └─vg_mysql-mysql 253:1 0 3.5G 0 lvm /mysql
├─sdb3 8:19 0 600M 0 part
└─sdb4 8:20 0 2.4G 0 part
sdc 8:32 0 4G 0 disk
├─vg_mysql-mysql 253:1 0 3.5G 0 lvm /mysql
└─vg_mysql-mysql_log 253:2 0 1G 0 lvm /mysql_log
sr0 11:0 1 945M 0 rom
[06:55:34 root@ubuntu ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vg_mysql
PV Size <1.49 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 380
Free PE 0
Allocated PE 380
PV UUID mASTrv-YJj3-2jUr-s6fD-TGVr-ZYs3-VAQoO0
--- Physical volume ---
PV Name /dev/sdb2
VG Name vg_mysql
PV Size 500.00 MiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 124
Free PE 0
Allocated PE 124
PV UUID 4emYBK-Kuk1-6GKr-SUTJ-djDM-xb7K-gpIl1m
--- Physical volume ---
PV Name /dev/sdc
VG Name vg_mysql
PV Size 4.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1023
Free PE 379
Allocated PE 644
PV UUID 7iRNH0-UwfP-eU7u-iH7R-67DL-c2vw-qQovie
--- Physical volume ---
PV Name /dev/sdb3
VG Name vg_mysql
PV Size 600.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 149
Free PE 149
Allocated PE 0
PV UUID fvaBe0-GM18-uVnD-Aebe-TC0G-MVhj-WxfBF3
--- Physical volume ---
PV Name /dev/sdb4
VG Name vg_mysql
PV Size <2.44 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 623
Free PE 623
Allocated PE 0
PV UUID kmU6zq-diYn-2aq1-Rfna-7qFH-G4C8-AGpf4c
--- Physical volume ---
PV Name /dev/sda3
VG Name ubuntu-vg
PV Size <99.00 GiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 25343
Free PE 12671
Allocated PE 12672
PV UUID ju6L1M-NnBw-6sgl-QU1w-o9eV-TR31-TiycGT
把sdc上占用的PE移动到其他分区上.
[06:55:38 root@ubuntu ~]#pvmove /dev/sdc
/dev/sdc: Moved: 3.88%
/dev/sdc: Moved: 60.25%
/dev/sdc: Moved: 96.74%
/dev/sdc: Moved: 100.00%
[06:58:53 root@ubuntu ~]#pvdisplay /dev/sdc
--- Physical volume ---
PV Name /dev/sdc
VG Name vg_mysql
PV Size 4.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1023
Free PE 1023
Allocated PE 0
PV UUID 7iRNH0-UwfP-eU7u-iH7R-67DL-c2vw-qQovie
把sdc从vg_msyql中移走
[06:59:15 root@ubuntu ~]#vgreduce vg_mysql /dev/sdc
Removed "/dev/sdc" from volume group "vg_mysql"
[07:00:23 root@ubuntu ~]#vgs
VG #PV #LV #SN Attr VSize VFree
ubuntu-vg 1 1 0 wz--n- <99.00g <49.50g
vg_mysql 4 2 0 wz--n- 4.98g 512.00m
[07:00:29 root@ubuntu ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <99.00g <49.50g
/dev/sdb1 vg_mysql lvm2 a-- 1.48g 0
/dev/sdb2 vg_mysql lvm2 a-- 496.00m 0
/dev/sdb3 vg_mysql lvm2 a-- 596.00m 512.00m
/dev/sdb4 vg_mysql lvm2 a-- 2.43g 0
/dev/sdc lvm2 --- 4.00g 4.00g
[07:00:36 root@ubuntu ~]#pvremove /dev/sdc
Labels on physical volume "/dev/sdc" successfully wiped.
[07:01:00 root@ubuntu ~]#pvs
PV VG Fmt Attr PSize PFree
/dev/sda3 ubuntu-vg lvm2 a-- <99.00g <49.50g
/dev/sdb1 vg_mysql lvm2 a-- 1.48g 0
/dev/sdb2 vg_mysql lvm2 a-- 496.00m 0
/dev/sdb3 vg_mysql lvm2 a-- 596.00m 512.00m
/dev/sdb4 vg_mysql lvm2 a-- 2.43g 0