欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux中的LVM之增加容量与删除卷的方法详解

程序员文章站 2023-02-02 23:24:10
这篇文章主要介绍了Linux中的LVM之增加容量与删除卷的方法详解,LVM是Linux环境下对磁盘分区进行管理的一种机制,需要的朋友可以参考下... 15-10-20...
放大lv容量涉及到lvm管理中的命令lvresize,我们先创建一个卷组vg volgroup02,它建立在磁盘/dev/sdc (大小为8g)上。创建逻辑卷lv时,我们故意只使用了一小部分。具体情况如下所示

复制代码
代码如下:

[root@localhost ~]# vgdisplay
--- volume group ---
vg name volgroup02
system id
format lvm2
metadata areas 1
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 1
act pv 1
vg size 7.97 gib
pe size 32.00 mib
total pe 255
alloc pe / size 0 / 0
free pe / size 255 / 7.97 gib
vg uuid setgjx-dobd-uayt-awgn-hkid-hsye-teotis
[root@localhost ~]# lvcreate -l7.97 -n logvol00 volgroup02
rounding up size to full physical extent 32.00 mib
logical volume "logvol00" created
[root@localhost ~]# mkfs -t ext4 /dev/volgroup02/logvol00
mke2fs 1.41.12 (17-may-2010)
filesystem label=
os type: linux
block size=1024 (log=0)
fragment size=1024 (log=0)
stride=0 blocks, stripe width=0 blocks
8192 inodes, 32768 blocks
1638 blocks (5.00%) reserved for the super user
first data block=1
maximum filesystem blocks=33554432
4 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
superblock backups stored on blocks:
8193, 24577

writing inode tables: done
creating journal (4096 blocks): done
writing superblocks and filesystem accounting information: done

this filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. use tune2fs -c or -i to override.
[root@localhost ~]# cd /
[root@localhost /]# mkdir /u01
[root@localhost /]# mount /dev/volgroup02/logvol00 /u01
[root@localhost /]# vi /etc/fstab

#
# /etc/fstab
# created by anaconda on mon aug 17 15:08:21 2015
#
# 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
#
uuid=3440ad55-6486-45ed-876f-e942b08013bf / ext4 defaults 1 1
uuid=d3abb655-db70-4c42-967d-57c421abfda0 /boot ext4 defaults 1 2
uuid=660624ff-335d-42ca-b779-f130a80d9da8 /home ext4 defaults 1 2
uuid=6f534bf0-e486-4937-84ae-ed1221cf34f1 swap swap defaults 0 0
/dev/volgroup02/logvol00 /u01 ext4 defaults 1 1
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0


Linux中的LVM之增加容量与删除卷的方法详解
此时,我们要放大文件系统/u01的大小,那么就需要使用到lvresize 放大lv容量

复制代码
代码如下:

[root@localhost ~]# lvscan
active '/dev/volgroup02/logvol00' [32.00 mib] inherit
[root@localhost ~]# lvdisplay /dev/volgroup02/logvol00
--- logical volume ---
lv path /dev/volgroup02/logvol00
lv name logvol00
vg name volgroup02
lv uuid ochwx1-el9p-6c5j-rnuz-2xu5-4215-h3xt5s
lv write access read/write
lv creation host, time localhost.localdomain, 2015-09-01 10:50:10 +0800
lv status available
# open 1
lv size 32.00 mib
current le 1
segments 1
allocation inherit
read ahead sectors auto
- currently set to 256
block device 253:0

[root@localhost ~]# lvresize -l +7.89g /dev/volgroup02/logvol00
rounding size to boundary between physical extents: 7.91 gib
size of logical volume volgroup02/logvol00 changed from 32.00 mib (1 extents) to 7.94 gib (254 extents).
logical volume logvol00 successfully resized

Linux中的LVM之增加容量与删除卷的方法详解

复制代码
代码如下:

[root@localhost ~]# resize2fs -p /dev/volgroup02/logvol00
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev/volgroup02/logvol00 is mounted on /u01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 32
performing an on-line resize of /dev/volgroup02/logvol00 to 8323072 (1k) blocks.
the filesystem on /dev/volgroup02/logvol00 is now 8323072 blocks long.

[root@localhost ~]# df -h
filesystem size used avail use% mounted on
/dev/sda2 27g 6.0g 20g 24% /
tmpfs 5.9g 0 5.9g 0% /dev/shm
/dev/sda1 477m 32m 420m 8% /boot
/dev/sdb1 99g 60m 94g 1% /home
/dev/mapper/volgroup02-logvol00
7.7g 2.7m 7.3g 1% /u01

Linux中的LVM之增加容量与删除卷的方法详解
如果我们此时在虚拟机上将磁盘从8g扩展为10g,那么要如何利用扩展的磁盘空间呢?
Linux中的LVM之增加容量与删除卷的方法详解

复制代码
代码如下:

[root@localhost u01]# fdisk -l

disk /dev/sda: 42.9 gb, 42949672960 bytes
64 heads, 32 sectors/track, 40960 cylinders
units = cylinders of 2048 * 512 = 1048576 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x000462cf

device boot start end blocks id system
/dev/sda1 * 2 501 512000 83 linux
partition 1 does not end on cylinder boundary.
/dev/sda2 502 28672 28847104 83 linux
partition 2 does not end on cylinder boundary.
/dev/sda3 28673 40960 12582912 82 linux swap / solaris
partition 3 does not end on cylinder boundary.

disk /dev/sdc: 8589 mb, 8589934592 bytes
64 heads, 32 sectors/track, 8192 cylinders
units = cylinders of 2048 * 512 = 1048576 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x98c391fe

device boot start end blocks id system
/dev/sdc1 1 8192 8388592 83 linux

disk /dev/sdb: 107.4 gb, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
units = cylinders of 16065 * 512 = 8225280 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x0002488e

device boot start end blocks id system
/dev/sdb1 1 13055 104856576 83 linux

disk /dev/mapper/volgroup02-logvol00: 33 mb, 33554432 bytes
255 heads, 63 sectors/track, 4 cylinders
units = cylinders of 16065 * 512 = 8225280 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x00000000

Linux中的LVM之增加容量与删除卷的方法详解
重启过后才能看到/dev/sdc的空间变化情况。
Linux中的LVM之增加容量与删除卷的方法详解

删除物理卷
删除卷lvm管理中的命令vgreduce, pvremove:

   vgreduce:通过删除lvm卷组中的物理卷来减少卷组容量。注意:不能删除lvm卷组中剩余的最后一个物理卷

   pvremove: 用于删除一个存在的物理卷。使用pvremove指令删除物理卷时,它将lvm分区上的物理卷信息删除,使其不再被视为一个物理卷。

在删除物理卷pv之前,必须将服务器的分区信息、卷组信息、物理卷信息、逻辑卷信息都掌握清楚,避免出现失误或误操作。

复制代码
代码如下:

[root@localhost ~]# fdisk -l

disk /dev/sda: 128.8 gb, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
units = cylinders of 16065 * 512 = 8225280 bytes

device boot start end blocks id system
/dev/sda1 * 1 13 104391 83 linux
/dev/sda2 14 10443 83778975 8e linux lvm
/dev/sda3 10444 15665 41945715 83 linux
[root@localhost ~]# vgscan
reading all physical volumes. this may take a while...
found volume group "volgroup00" using metadata type lvm2
[root@localhost ~]# pvscan
pv /dev/sda2 vg volgroup00 lvm2 [79.88 gb / 0 free]
pv /dev/sda3 vg volgroup00 lvm2 [40.00 gb / 40.00 gb free]
total: 2 [119.88 gb] / in use: 2 [119.88 gb] / in no vg: 0 [0 ]
[root@localhost ~]# pvdisplay
--- physical volume ---
pv name /dev/sda2
vg name volgroup00
pv size 79.90 gb / not usable 23.41 mb
allocatable yes (but full)
pe size (kbyte) 32768
total pe 2556
free pe 0
allocated pe 2556
pv uuid ygtb2j-zkjr-mv62-nluq-2dgy-vuut-ccc1lo

--- physical volume ---
pv name /dev/sda3
vg name volgroup00
pv size 40.00 gb / not usable 2.61 mb
allocatable yes
pe size (kbyte) 32768
total pe 1280
free pe 1280
allocated pe 0
pv uuid wsnv13-7j1h-sh8q-hl6k-hpnc-x4wu-gm7lzw

[root@localhost ~]# lvscan
active '/dev/volgroup00/logvol00' [77.91 gb] inherit
active '/dev/volgroup00/logvol01' [1.97 gb] inherit
[root@localhost ~]# lvs
lv vg attr lsize origin snap% move log copy% convert
logvol00 volgroup00 -wi-ao 77.91g
logvol01 volgroup00 -wi-ao 1.97g

vgreduce参数:
-a:如果命令行中没有指定要删除的物理卷,则删除所有的空物理卷;

--removemissing:删除卷组中丢失的物理卷,使卷组恢复正常状态。

复制代码
代码如下:

[root@localhost ~]# vgreduce volgroup00 /dev/sda3
removed "/dev/sda3" from volume group "volgroup00"
[root@localhost ~]#

Linux中的LVM之增加容量与删除卷的方法详解

复制代码
代码如下:

[root@localhost ~]# pvscan
pv /dev/sda2 vg volgroup00 lvm2 [79.88 gb / 0 free]
pv /dev/sda3 lvm2 [40.00 gb]
total: 2 [119.88 gb] / in use: 1 [79.88 gb] / in no vg: 1 [40.00 gb]
[root@localhost ~]# pvs
pv vg fmt attr psize pfree
/dev/sda2 volgroup00 lvm2 a- 79.88g 0
/dev/sda3 lvm2 a- 40.00g 40.00g
[root@localhost ~]# pvremove /dev/sda3
labels on physical volume "/dev/sda3" successfully wiped
[root@localhost ~]# pvscan
pv /dev/sda2 vg volgroup00 lvm2 [79.88 gb / 0 free]
total: 1 [79.88 gb] / in use: 1 [79.88 gb] / in no vg: 0 [0 ]