在linux中减小和增大LV的过程与思考
今天在安装oracle 11 rac的时候,查看操作系统df -lh,发现/home目录竟然分了500多G,/根目录才有50G,当时我就爆了句粗口,这TM系统是怎么做的。
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_db5-lv_root 50G 6.2G 41G 14% /
tmpfs 127G 84K 127G 1% /dev/shm
/dev/sdq1 477M 37M 415M 9% /boot
/dev/mapper/vg_db5-lv_home 496G 70M 471G 1% /home
#vgdisplay
--- Volume group ---
VG Name vg_db5
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 557.26 GiB
PE Size 4.00 MiB
Total PE 142658
Alloc PE / Size 142658 / 557.26 GiB
Free PE / Size 0 / 0
VG UUID plc2K9-lRVy-bcck-B9Qp-3F8k-H6cW-W4vRdS
#lvdisplay
--- Logical volume ---
LV Path /dev/vg_db6/lv_root
LV Name lv_root
VG Name vg_db6
LV UUID 6HlVnp-6RGm-kORQ-eOx6-nQQV-8PWB-Gg4WNE
LV Write Access read/write
LV Creation host, time db6, 2015-02-02 19:25:11 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/vg_db6/lv_home
LV Name lv_home
VG Name vg_db6
LV UUID yuBJai-96D6-JFwX-cmXR-EeXE-cUuU-uqRl3l
LV Write Access read/write
LV Creation host, time db6, 2015-02-02 19:25:17 +0800
LV Status available
# open 1
LV Size 503.26 GiB
Current LE 128834
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:10
--- Logical volume ---
LV Path /dev/vg_db6/lv_swap
LV Name lv_swap
VG Name vg_db6
LV UUID d5FqB1-tTHM-HSrb-b0tm-r1Ms-XnvP-dH5ovs
LV Write Access read/write
LV Creation host, time db6, 2015-02-02 19:27:23 +0800
LV Status available
# open 1
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
有三个lv在一个VG里,决定把这三个LV的大小给调整一下。把lv_home调小,然后把lv_root调大。注意,缩小空间是有风险的,所以我先把里面的oracle,grid用户还有所有的组都删除了,然后重启系统。
#userdel oracle
#userdel grid
#groudel oinstall
....................
重启之后,查看/etc/group,/etc/passwd,确认没有用户和组了,我们就可以开始了。
1.#umount /home1.#umount /home
如果不成功,可能会报错,解决方法如下:umount: /mnt/cdrom: device is busy.
(In some cases useful info about processes that
use the device is found by lsof(8) or user(1))
#umount /mnt/cdrom/ -f //强制卸载也不行
umount2: 设备或资源忙
umount: /mnt: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))umount2: 设备或资源忙
#fuser -m /home
/home: 1338c
# ps aux |grep 1338
root 1338 0.0 0.2 108292 1912 pts/2 Ss+ 14:27 0:00 -bash
root 1423 0.0 0.1 103236 884 pts/1 S+ 14:49 0:00 grep 1338
#kill -9 1338
#fuser -m /home
#umount /home
解析:不过感觉这种方法也只是用于那些无关紧要的目录,因为进程使用的少,如果是/目录,可能进程会杀不过来,杀完了,系统估计也崩了。
逻辑卷的缩减,有风险,先缩减逻辑边界——在缩减物理边界
注意: 1、不能在线缩减,得先卸载;
2、确保缩减后的空间大小依然能存储原有的所有数据;
3、在缩减之前应该强行检查文件,确保文件系统处于一致性状态
2. 检查一下
#e2fsck -f /dev/vg_db5/lv_home
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg_db5/lv_home: 29/32989184 files (0.0% non-contiguous), 2121120/131926016 blocks
3.开始缩小文件系统:
#resize2fs /dev/vg_db5/lv_home 1024M
resize2fs 1.41.12 (17-May-2010)
resize2fs: New size smaller than minimum (307344)
显示1G太小了,最小是307344个块。得了,10个G吧
#resize2fs /dev/vg_db5/lv_home 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_db5/lv_home to 2621440 (4k) blocks.
The filesystem on /dev/vg_db5/lv_home is now 2621440 blocks long.
4.缩小lv大小:
# lvreduce -L 10G /dev/vg_db5/lv_home
WARNING: Reducing active logical volume to 10.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
Size of logical volume vg_db5/lv_home changed from 503.26 GiB (128834 extents) to 10.00 GiB (2560 extents).
Logical volume lv_home successfully resized
#vgdisplay 发现空间已经释放出来了
--- Volume group ---
VG Name vg_db5
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 557.26 GiB
PE Size 4.00 MiB
Total PE 142658
Alloc PE / Size 16384 / 64.00 GiB
Free PE / Size 126274 / 493.26 GiB
VG UUID Ji4TQc-IbhS-O6Cb-XH5Q-q4Uk-DIer-DvSMMD
5.增大LV:lv_root 的大小,增大到500G
#lvextend -L 500G -f -r /dev/vg_db5/lv_root
Size of logical volume vg_db5/lv_root changed from 50.00 GiB (12800 extents) to 500.00 GiB (128000 extents).
Logical volume lv_root successfully resized
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_db5-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 32
Performing an on-line resize of /dev/mapper/vg_db5-lv_root to 131072000 (4k) blocks.
The filesystem on /dev/mapper/vg_db5-lv_root is now 131072000 blocks long.
想再增加操作系统大小,看来不用做了。
# resize2fs /dev/vg_db5/lv_root 500G
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 131072000 blocks long. Nothing to do!
#resize2fs -p /dev/vg_db5/lv_root
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 131072000 blocks long. Nothing to do!
重启之后:
#df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_db5-lv_root 493G 6.2G 461G 2% /
tmpfs 160G 144K 160G 1% /dev/shm
/dev/sdq1 477M 37M 415M 9% /boot
/dev/mapper/vg_db5-lv_home 9.8G 37M 9.2G 1% /home
终于搞定了。
更为详细的LVM的理论概念请参见博客:
http://80cto.blog.51cto.com/7503144/1610922