25.Linux逻辑卷管理
25.Linux逻辑卷管理
文章目录
- 25.Linux逻辑卷管理
- 环境准备
- 逻辑卷基本管理
- 硬盘分区管理方案缺点
- 逻辑卷管理硬盘方案优点
- 逻辑卷基本概念
- 创建物理卷
- 创建卷组
- 创建逻辑卷
- 清理
- 扩展和缩减卷组
- 环境准备
- 扩展卷组
- 缩减卷组
- 扩展和缩减逻辑卷
- 扩展逻辑卷
- 缩减逻辑卷
- 扩展和缩减文件系统
- 扩展 XFS 文件系统
- 扩展 EXT4 文件系统
- 缩减 EXT4 文件系统
- 逻辑卷快照
- raid 逻辑卷
- 实验-扩容root分区
- 实验目的
- 实验要求
- 实验步骤
- 实验总结
环境准备
虚拟机添加3块20G硬盘,sdb sdc sdd
[root@server ~ 13:35:31]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sde 8:64 0 20G 0 disk
sdf 8:80 0 20G 0 disk
sdg 8:96 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom
逻辑卷基本管理
硬盘分区管理方案缺点
- 硬盘分区空间必须使用连续空间,所以硬盘分区扩容很麻烦,特别是要扩容的分区后没有空间。
- 使用硬盘分区创建的文件系统不同跨多个硬盘,受单个硬盘容量限制。
- 如果硬盘损坏,则文件系统中数据也会丢失。
逻辑卷管理硬盘方案优点
- 使用逻辑卷可以更加灵活地扩展和缩减文件系统空间。
- 使用逻辑卷创建的文件系统可以跨多个硬盘,适合创建超大容量文件系统。
- 使用逻辑卷可以创建镜像卷,保证数据的冗余性。即使单个硬盘损坏,文件系统中数据也会丢失。
- 使用逻辑卷还可以创建快照,保留某一时刻数据,就像虚拟机快照一样。
总结
硬盘管理:
-
磁盘分区
单个盘空间太大,大分小
缺点:分区的空间大小不好调整。 -
raid
文件系统需要的空间超过单个硬盘。
缺点:分区的空间大小不好调整。 -
LVM logical volume manage
除了支持以上存储管理优点,还支持更多的优点,例如快照功能。
raid,由多个硬盘组成磁盘组 disk array,raid可以继续分割为分区使用,
lvm, 由多个硬盘(phycical volume)组成卷组(volume group),vg可以继续分割为lv(logical volume)使用
逻辑卷基本概念
LVM是 Logical Volume Manager(逻辑卷管理)的简写,LVM将若干个磁盘或者磁盘分区连接为一个整块的卷组,形成一个存储池。管理员可以在卷组上任意创建逻辑卷,并进一步在逻辑卷上创建文件系统。

- 物理卷(PV, Physical Volume),是LVM的基本存储逻辑块,通过块设备创建。块设备是指磁盘、磁盘分区或从逻辑上和磁盘分区具有同样功能的设备(如RAID)。与基本的物理存储介质(如分区、磁盘等)相比,物理卷包含有和LVM相关的管理参数。
- 卷组(VG, Volume Group),是一个逻辑的存储空间,由一个或多个物理卷组成。
- 逻辑卷(LV, Logical Volume),是从卷组中创建出来的一个逻辑空间。如果把一个卷组类比为一个逻辑硬盘,那么逻辑卷则可以类比为一个硬盘分区,所以也可以在逻辑卷之上创建文件系统。
简单地说:卷组由多个物理卷组成,可以在卷组中创建多个逻辑卷,对逻辑卷创建文件系统给用户使用。
LVM管理基本流程:
- 创建物理卷
- 创建卷组
- 创建逻辑卷

创建物理卷
# 创建单个PV
[root@server ~ 13:35:39]# pvcreate /dev/sdbPhysical volume "/dev/sdb" successfully created.#创建多个PV
[root@server ~ 13:36:04]# pvcreate /dev/sd{c,d}Physical volume "/dev/sdc" successfully created.Physical volume "/dev/sdd" successfully created.# 查看PV列表
[root@server ~ 13:36:15]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb lvm2 --- 20.00g 20.00g/dev/sdc lvm2 --- 20.00g 20.00g/dev/sdd lvm2 --- 20.00g 20.00g# 查看单个PV详细信息[root@server ~ 13:42:04]# pvdisplay /dev/sdb--- Physical volume ---PV Name /dev/sdbVG Name webappPV Size 20.00 GiB / not usable 4.00 MiBAllocatable yes PE Size 4.00 MiBTotal PE 5119Free PE 3839Allocated PE 1280PV UUID 5dg3QA-l5Tp-UnPF-omNt-K4c9-nBPP-GqczBN
创建卷组
#创建包含PV单个VG
[root@server ~ 13:36:23]# vgcreate webapp /dev/sdbVolume group "webapp" successfully created#创建多个VG
[root@server ~ 13:36:56]# vgcreate dbapp /dev/sd{c,d}Volume group "dbapp" successfully created
[root@server ~ 13:37:08]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb webapp lvm2 a-- <20.00g <20.00g/dev/sdc dbapp lvm2 a-- <20.00g <20.00g/dev/sdd dbapp lvm2 a-- <20.00g <20.00g
[root@server ~ 13:37:15]# vgsVG #PV #LV #SN Attr VSize VFree centos_centos7 1 3 0 wz--n- <199.00g 4.00mdbapp 2 0 0 wz--n- 39.99g 39.99gwebapp 1 0 0 wz--n- <20.00g <20.00g
创建逻辑卷
[root@server ~ 13:37:24]# lvcreate -n webapp01 -L 5G webapp
WARNING: xfs signature detected on /dev/webapp/webapp01 at offset 0. Wipe it? [y/n]: yWiping xfs signature on /dev/webapp/webapp01.Logical volume "webapp01" created.
[root@server ~ 13:38:00]# lvcreate -n data01 -L 25G dbappLogical volume "data01" created.
[root@server ~ 13:38:34]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g data01 dbapp -wi-a----- 25.00g webapp01 webapp -wi-a----- 5.00g
[root@server ~ 13:38:37]# mkfs.xfs /dev/webapp/webapp01
meta-data=/dev/webapp/webapp01 isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=1310720, 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[root@server ~ 13:40:56]# mkdir -p /webapp/webapp01
[root@server ~ 13:41:20]# mount /dev/webapp/webapp01 /webapp/webapp01
[root@server ~ 13:41:27]# df -h /webapp/webapp01/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 5.0G 33M 5.0G 1% /webapp/webapp01
[root@server ~ 13:41:35]# cp /etc/ho* /webapp/webapp01/
[root@server ~ 13:41:52]# ls /webapp/webapp01/
host.conf hostname hosts hosts.allow hosts.deny
清理
#先卸载
[root@server ~ 13:46:48]# umount /webapp/webapp01 #删除卷组
[root@server ~ 13:53:24]# vgremove webapp dbapp #删除物理卷
[root@server ~ 13:54:37]# pvremove /dev/sd{b,c,d}Labels on physical volume "/dev/sdb" successfully wiped.Labels on physical volume "/dev/sdc" successfully wiped.Labels on physical volume "/dev/sdd" successfully wiped.
[root@server ~ 13:55:00]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
sdc 8:32 0 20G 0 disk
sdd 8:48 0 20G 0 disk
sde 8:64 0 20G 0 disk
sdf 8:80 0 20G 0 disk
sdg 8:96 0 20G 0 disk
sr0 11:0 1 4.4G 0 rom
[root@server ~ 13:55:04]# pvsPV VG Fmt Attr PSize PFree/dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m
[root@server ~ 13:58:23]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g
扩展和缩减卷组
环境准备
[root@server ~ 14:05:01]# pvcreate /dev/sdbPhysical volume "/dev/sdb" successfully created.
[root@server ~ 14:05:29]# vgcreate webapp /dev/sdbVolume group "webapp" successfully created
[root@server ~ 14:05:44]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb webapp lvm2 a-- <20.00g <20.00g
[root@server ~ 14:05:53]# lvcreate -n webapp01 -L 10G webapp
WARNING: xfs signature detected on /dev/webapp/webapp01 at offset 0. Wipe it? [y/n]: yWiping xfs signature on /dev/webapp/webapp01.Logical volume "webapp01" created.
扩展卷组
[root@server ~ 14:06:36]# vgextend webapp /dev/sd{c,d}Physical volume "/dev/sdc" successfully created.Physical volume "/dev/sdd" successfully created.Volume group "webapp" successfully extended
[root@server ~ 14:08:58]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb webapp lvm2 a-- <20.00g <10.00g/dev/sdc webapp lvm2 a-- <20.00g <20.00g/dev/sdd webapp lvm2 a-- <20.00g <20.00g
[root@server ~ 14:09:10]# vgsVG #PV #LV #SN Attr VSize VFree centos_centos7 1 3 0 wz--n- <199.00g 4.00mwebapp 3 1 0 wz--n- <59.99g <49.99g
缩减卷组
[root@server ~ 14:11:15]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb webapp lvm2 a-- <20.00g <10.00g/dev/sdc webapp lvm2 a-- <20.00g <20.00g/dev/sdd webapp lvm2 a-- <20.00g <20.00g
[root@server ~ 14:11:21]# vgreduce webapp /dev/sdbPhysical volume "/dev/sdb" still in use
[root@server ~ 14:11:51]# pvmove /dev/sdb/dev/sdb: Moved: 0.90%/dev/sdb: Moved: 100.00%
[root@server ~ 14:12:32]# vgreduce webapp /dev/sdbRemoved "/dev/sdb" from volume group "webapp"
[root@server ~ 14:12:35]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 centos_centos7 lvm2 a-- <199.00g 4.00m/dev/sdb lvm2 --- 20.00g 20.00g/dev/sdc webapp lvm2 a-- <20.00g <10.00g/dev/sdd webapp lvm2 a-- <20.00g <20.00g
扩展和缩减逻辑卷
扩展逻辑卷
如果逻辑卷空间不足,在卷组空间充足的情况下,也可以扩展逻辑卷空间。
[root@server ~ 14:12:39]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-a----- 10.00g
[root@server ~ 14:12:55]# lvextend -L +20G /dev/webapp/webapp01 Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 30.00 GiB (7680 extents).Logical volume webapp/webapp01 successfully resized.
[root@server ~ 14:13:17]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-a----- 30.00g
缩减逻辑卷
[root@server ~ 14:13:21]# lvreduce -L -10G /dev/webapp/webapp01 WARNING: Reducing active logical volume to 20.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce webapp/webapp01? [y/n]: ySize of logical volume webapp/webapp01 changed from 30.00 GiB (7680 extents) to 20.00 GiB (5120 extents).Logical volume webapp/webapp01 successfully resized.
[root@server ~ 14:13:46]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-a----- 20.00g
扩展和缩减文件系统
扩展 XFS 文件系统
XFS文件系统只支持扩展,不支持缩减。
#环境准备:格式化文件系统,并创建数据
[root@server ~ 14:49:59]# mkfs.xfs /dev/webapp/webapp01
meta-data=/dev/webapp/webapp01 isize=512 agcount=4, agsize=1310720 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242880, 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
[root@server ~ 14:50:17]# mkdir /var/www/html
mkdir: cannot create directory ‘/var/www/html’: File exists
[root@server ~ 14:50:26]# mkdir -p /var/www/html
[root@server ~ 14:50:34]# mount /dev/webapp/webapp01 /var/www/html
[root@server ~ 14:50:42]# cp /etc/host* /var/www/html
[root@server ~ 14:50:49]# ls /var/www/html/
host.conf hostname hosts hosts.allow hosts.deny#扩容逻辑卷
[root@server ~ 14:51:03]# lvextend -L 15G /dev/webapp/webapp01New size given (3840 extents) not larger than existing size (5120 extents)
[root@server ~ 14:51:16]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-ao---- 20.00g
#扩容文件系统
[root@server ~ 14:51:23]# xfs_growfs /var/www/html
meta-data=/dev/mapper/webapp-webapp01 isize=512 agcount=4, agsize=1310720 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, 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
[root@server ~ 14:51:38]# df -h /var/www/html
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 20G 33M 20G 1% /var/www/html
[root@server ~ 14:51:43]# ls /var/www/html
host.conf hostname hosts hosts.allow hosts.deny#文件系统和逻辑卷同时扩容
[root@server ~ 14:51:57]# lvextend -rL 20G /dev/webapp/webapp01Size of logical volume webapp/webapp01 unchanged from 20.00 GiB (5120 extents).Logical volume webapp/webapp01 successfully resized.
[root@server ~ 14:52:09]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-ao---- 20.00g
[root@server ~ 14:52:16]# df -h /var/www/html
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 20G 33M 20G 1% /var/www/html
扩展 EXT4 文件系统
EXT4文件系统即支持扩展,也支持缩减。
#环境准备:格式化文件系统,并创建数据 [root@server ~ 14:52:24]# umount /var/www/html [root@server ~ 14:53:16]# mkfs.ext4 /dev/webapp/webapp01 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5242880 blocks 262144 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2153775104 160 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done#挂载 [root@server ~ 14:53:34]# mount /dev/webapp/webapp01 /var/www/html/ [root@server ~ 14:53:54]# df -h /var/www/html/ Filesystem Size Used Avail Use% Mounted on /dev/mapper/webapp-webapp01 20G 45M 19G 1% /var/www/html [root@server ~ 14:54:17]# cp /etc/ho* /var/www/html/ [root@server ~ 14:54:41]# ls /var/www/html host.conf hostname hosts hosts.allow hosts.deny lost+found#扩展逻辑卷 [root@server ~ 14:54:48]# lvextend -L 25G /dev/webapp/webapp01Size of logical volume webapp/webapp01 changed from 20.00 GiB (5120 extents) to 25.00 GiB (6400 extents).Logical volume webapp/webapp01 successfully resized. [root@server ~ 14:54:58]# lvs /dev/webapp/webapp01LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertwebapp01 webapp -wi-ao---- 25.00g #将文件系统扩展到所在逻辑卷(或分区)的最大可用空间,即自动填充整个设备的容量。 [root@server ~ 14:55:04]# resize2fs /dev/webapp/webapp01 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/webapp/webapp01 is mounted on /var/www/html; on-line resizing required old_desc_blocks = 3, new_desc_blocks = 4 The filesystem on /dev/webapp/webapp01 is now 6553600 blocks long.[root@server ~ 14:55:34]# df -h /var/www/html Filesystem Size Used Avail Use% Mounted on /dev/mapper/webapp-webapp01 25G 44M 24G 1% /var/www/html#-rL直接扩容逻辑卷和文件系统 [root@server ~ 14:55:48]# lvextend -rL 30G /dev/webapp/webapp01Size of logical volume webapp/webapp01 changed from 25.00 GiB (6400 extents) to 30.00 GiB (7680 extents).Logical volume webapp/webapp01 successfully resized. resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/mapper/webapp-webapp01 is mounted on /var/www/html; on-line resizing required old_desc_blocks = 4, new_desc_blocks = 4 The filesystem on /dev/mapper/webapp-webapp01 is now 7864320 blocks long.[root@server ~ 14:55:59]# lvs /dev/webapp/webapp01LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertwebapp01 webapp -wi-ao---- 30.00g [root@server ~ 14:56:07]# df -h /var/www/html Filesystem Size Used Avail Use% Mounted on /dev/mapper/webapp-webapp01 30G 44M 28G 1% /var/www/html
缩减 EXT4 文件系统
缩减EXT4文件系统注意事项:
- 不支持在线缩减,必须卸载后缩减。
- 缩减后的容量,不能小于当前使用的容量。
ext4 减容流程:
- 卸载文件系统
- e2fsck 文件系统
- 缩减文件系统
- 缩减lv
- 挂载测试
具体操作如下:
#卸载文件系统
[root@server ~ 14:56:14]# umount /var/www/html #检查文件系统
[root@server ~ 14:57:12]# e2fsck -f /dev/webapp/webapp01
e2fsck 1.42.9 (28-Dec-2013)
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/webapp/webapp01: 16/1966080 files (0.0% non-contiguous), 167447/7864320 blocks#缩减文件系统
[root@server ~ 14:57:30]# resize2fs /dev/webapp/webapp01 10G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/webapp/webapp01 to 2621440 (4k) blocks.
The filesystem on /dev/webapp/webapp01 is now 2621440 blocks long.#缩减逻辑卷,需要与文件系统缩小大小一致
[root@server ~ 14:57:58]# lvreduce -L 10G /dev/webapp/webapp01 WARNING: Reducing active logical volume to 10.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce webapp/webapp01? [y/n]: ySize of logical volume webapp/webapp01 changed from 30.00 GiB (7680 extents) to 10.00 GiB (2560 extents).Logical volume webapp/webapp01 successfully resized.#挂载
[root@server ~ 14:58:24]# mount /dev/webapp/webapp01 /var/www/html/
[root@server ~ 14:58:46]# df -h /var/www/html/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 9.8G 37M 9.2G 1% /var/www/html
[root@server ~ 14:58:57]# ls /var/www//html/
host.conf hostname hosts hosts.allow hosts.deny lost+found
补充说明: 如果文件系统是构建在磁盘分区上,扩展文件系统容量和缩减文件系统容量操作相同。
逻辑卷快照
我们可以使用快照记录了lv中数据,后续可以用来恢复数据。
[root@server ~ 16:28:20]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Converthome centos_centos7 -wi-ao---- <145.12g root centos_centos7 -wi-ao---- 50.00g swap centos_centos7 -wi-ao---- <3.88g webapp01 webapp -wi-ao---- 10.00g
# 创建快照:快照的容量不能小于lv容量[root@server ~ 16:28:23]# lvcreate -s -n webapp01-snap1 -L 10G /dev/webapp/webapp01Logical volume "webapp01-snap1" created.# 挂载快照
[root@server ~ 16:28:44]# mount /dev/webapp/webapp01-snap1 /webapp/webapp01/#查看数据
[root@server ~ 16:28:59]# ls /webapp/webapp01/
host.conf hostname hosts hosts.allow hosts.deny lost+found
raid 逻辑卷
准备环境
[root@server ~ 16:29:05]# umount /webapp/webapp0
umount: /webapp/webapp0: mountpoint not found
[root@server ~ 16:33:53]# umount /var/www/html
[root@server ~ 16:34:01]# lvremove /dev/webapp/webapp01*
Do you really want to remove active origin logical volume webapp/webapp01 with 1 snapshot(s)? [y/n]: yLogical volume webapp/webapp01-snap1 contains a filesystem in use.LV webapp/webapp01 has open 1 snapshot(s), not deactivating.Logical volume webapp/webapp01 not removed.Logical volume webapp/webapp01-snap1 contains a filesystem in use.
创建 raid1 逻辑卷
[root@server ~ 16:34:10]# lvcreate --type raid1 -n webapp01 -L 15G webapp[root@server ~ 16:34:37]# mkfs.xfs /dev/webapp/webapp01
模拟故障
[root@server ~ 16:35:17]# dd if=/dev/zero of=/dev/sdd bs=1M count=256
256+0 records in
256+0 records out
268435456 bytes (268 MB) copied, 0.174861 s, 1.5 GB/s
此时文件系统仍可以正方访问
[root@server ~ 16:35:51]# ls /var/www/html/
host.conf hostname hosts hosts.allow hosts.deny lost+found
[root@server ~ 16:35:59]# umount /var/www/html
[root@server ~ 16:36:34]# mount /dev/webapp/webapp01 /var/www/html/
修复raid
# 删除丢失的物理卷
[root@server ~ 16:36:41]# vgreduce --removemissing webapp --force WARNING: Device for #扩容
[root@server ~ 16:37:12]# vgextend webapp /dev/sdd# 修复raid1 逻辑卷
[root@server ~ 16:37:22]# lvconvert --repair /dev/webapp/webapp01# 扫描物理卷变化
[root@server ~ 16:37:29]# pvscan.....PV /dev/sdc VG webapp lvm2 [<20.00 GiB / <10.00 GiB free]PV [unknown] VG webapp lvm2 [<20.00 GiB / <10.00 GiB free]PV /dev/sda2 VG centos_centos7 lvm2 [<199.00 GiB / 4.00 MiB free]Total: 3 [<238.99 GiB] / in use: 3 [<238.99 GiB] / in no VG: 0 [0 ]# 验证结果
[root@server ~ 16:37:35]# pvs|grep webap........./dev/sdc webapp lvm2 a-- <20.00g <10.00g[unknown] webapp lvm2 a-m <20.00g <10.00g
实验-扩容root分区
实验目的
-
sda盘怎么实现这样的存储规划?
sda分两个分区sda1和sda2
sda2作为pv,属于CentOS卷组,卷组中创建3跟lv,格式FS,给/ swap /home -
为什么使用了分区后又使用lvm?
lvm 灵活: 调整空间。 -
缩减/home空给100G给/使用
实验要求
要求实验过程中不能丢失文件数据,不改变原有的文件系统格式。
[root@server ~ 13:35:49]# lsblk /dev/sda
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 145.1G 0 lvm /home
实验步骤
- 备份 home
- 卸载
- 缩减 /dev/mapper/centos-home 100G
- 格式化 /dev/mapper/centos-home xfs
- 恢复home的数据
- /dev/mapper/centos-root 扩容100
- 查看需要备份的home目录下的文件的大小,由于文件较小直接备份到/tmp/home_bak目录下
#备份home数据到root下[root@server ~ 13:39:09]# df -hT /home
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos_centos7-home xfs 146G 33M 146G 1% /home
[root@server ~ 13:39:48]# cp -a /home /tmp/home_bak
注意:这里使用了参数-T查看到/home的文件系统格式为xfs,说明home分区在缩容后还需重新格式化。
- 卸载home分区
[root@server ~ 13:39:54]# umount /home
[root@server ~ 13:56:35]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 145.1G 0 lvm
sr0 11:0 1 4.4G 0 rom
- 调整home分区大小
[root@server ~ 13:45:47]# lvreduce -L 45G /dev/centos_centos7/homeWARNING: Reducing active logical volume to 45.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos_centos7/home? [y/n]: ySize of logical volume centos_centos7/home changed from <145.12 GiB (37150 extents) to 45.00 GiB (11520 extents).Logical volume centos_centos7/home successfully resized.#格式化文件系统
[root@server ~ 14:02:01]# mkfs.xfs -f /dev/centos_centos7/home
meta-data=/dev/centos_centos7/home isize=512 agcount=4, agsize=2949120 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=11796480, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=5760, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 挂载home分区并恢复文件
[root@server ~ 14:02:49]# mount /dev/centos_centos7/home /home# 查看是否成功挂载
[root@server ~ 14:03:09]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part ├─centos_centos7-root 253:0 0 50G 0 lvm /├─centos_centos7-swap 253:1 0 3.9G 0 lvm [SWAP]└─centos_centos7-home 253:2 0 45G 0 lvm /home
sr0 11:0 1 4.4G 0 rom # 恢复数据
[root@server ~ 14:03:12]# cp -a /tmp/home_bak/* /home/# 切换到普通用户并验证权限
[root@server ~ 14:13:39]# su - zhang
- 扩容root分区
[root@server ~ 14:25:35]# lvextend -L +100G -r /dev/centos_centos7/root Size of logical volume centos_centos7/root changed from 50.00 GiB (12800 extents) to 150.00 GiB (38400 extents).Logical volume centos_centos7/root successfully resized.
meta-data=/dev/mapper/centos_centos7-root isize=512 agcount=4, agsize=3276800 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 39321600# 查看是否已成功扩容
[root@server ~ 14:27:37]# df -hT /
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos_centos7-root xfs 150G 1.6G 149G 2% /
实验总结
1.在对分区进行操作之前(尤其是对分区进行删除和格式化操作时),需要先备份重要数据。
2.xfs格式与ext4格式的区别:XFS文件系统只支持扩展,不支持缩减;ext4文件系统即支持扩展,也支持缩减。
3.在root用户下对普通用户的家目录进行复制操作时,可能会因为目录权限问题导致切换到普通用户后无法访问家目录。可以使用cp -a选项避免该问题。
