---------建立逻辑卷---------
1. 新建2个分区,sda5 5G,sd6 10G,完成之后如下
[root@yangcan /]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x000ed173
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2167 17091584 83 Linux
/dev/sda3 2167 2611 3571712 82 Linux swap / Solaris
/dev/sda4 2611 5221 20966162+ 5 Extended
/dev/sda5 2611 3264 5246528+ 83 Linux
/dev/sda6 3265 4570 10490413+ 83 Linux
- 把sda6初始化物理卷
[root@yangcan /]# pvcreate /dev/sda6
Device vg6 not found (or ignored by filtering).
Writing physical volume data to disk "/dev/sda6"
Physical volume "/dev/sda6" successfully created
3. 创建卷组vg6,PE为16M
[root@yangcan /]# vgcreate -s 16M vg6 /dev/sda6
Volume group "vg6" successfully created
[root@yangcan /]# vgdisplay
--- Volume group ---
VG Name vg6
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 10.00 GiB
PE Size 16.00 MiB
Total PE 640
Alloc PE / Size 0 / 0
Free PE / Size 640 / 10.00 GiB
VG UUID 35tmMm-Hyec-i4f1-xkd7-pMRS-RgRl-sqFa7l
4. 创建逻辑卷ftpdata,大小为6G
[root@yangcan /]# lvcreate -L 6G -n ftpdata vg6
Logical volume "ftpdata" created
[root@yangcan /]# lvdisplay
--- Logical volume ---
LV Name /dev/vg6/ftpdata
VG Name vg6
LV UUID LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
LV Write Access read/write
LV Status available
# open 0
LV Size 6.00 GiB
Current LE 384
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
5. 格式化ext4,并挂载,并cp一些数据进去
[root@yangcan /]# mke2fs -T ext4 /dev/vg6/ftpdata
[root@yangcan /]# mount /dev/vg6/ftpdata /ftpdata/
[root@yangcan /]# cd /ftpdata/
[root@yangcan ftpdata]# cp /etc/*.conf ./
[root@yangcan ftpdata]# cp /usr/share/dict/linux.words ./
--------扩容--------
1. 新增一个磁盘到卷组vg6,就用到之前剩下的sda5了
[root@yangcan ftpdata]# vgextend vg6 /dev/sda5
Volume group "vg6" successfully extended
[root@yangcan ftpdata]# vgdisplay
--- Volume group ---
VG Name vg6
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 15.00 GiB
PE Size 16.00 MiB
Total PE 960
Alloc PE / Size 384 / 6.00 GiB
Free PE / Size 576 / 9.00 GiB
VG UUID 35tmMm-Hyec-i4f1-xkd7-pMRS-RgRl-sqFa7l
2. 扩容ftpdata,当我们文件不段增加的时候,扩容就很有必要了
[root@yangcan ftpdata]# lvextend -L +4G /dev/vg6/ftpdata
或者使用,就是想让你明白-L 和 -L +的区别,-L 后面直接加容量,是指直接加到容量,而-L +容量,是指在原有容量上加多少容量!
[root@yangcan ftpdata]# lvextend -L 10G /dev/vg6/ftpdata
Extending logical volume ftpdata to 10.00 GiB
Logical volume ftpdata successfully resized
[root@yangcan ftpdata]# lvdisplay
--- Logical volume ---
LV Name /dev/vg6/ftpdata
VG Name vg6
LV UUID LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
LV Write Access read/write
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 640
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@yangcan ftpdata]# df -h|grep ftpdata
/dev/mapper/vg6-ftpdata
6.0G 145M 5.5G 3% /ftpdata
此时查看ftpdata文件系统容量的时候,还是之前的大小,所以需要resize2fs
[root@yangcan ftpdata]# resize2fs /dev/vg6/ftpdata
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg6/ftpdata is mounted on /ftpdata; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg6/ftpdata to 2621440 (4k) blocks.
The filesystem on /dev/vg6/ftpdata is now 2621440 blocks long.
[root@yangcan ftpdata]# df -h|grep ftpdata
/dev/mapper/vg6-ftpdata
9.9G 147M 9.3G 2% /ftpdata
[root@yangcan ftpdata]# lvdisplay
--- Logical volume ---
LV Name /dev/vg6/ftpdata
VG Name vg6
LV UUID LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
LV Write Access read/write
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 640
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
现在就文件系统和逻辑卷空间一致了
-----------缩容----------
扩容实验做完了,接下来做缩容,缩容比扩容复杂,平时工作也不能轻易的指定缩容的大小.
[root@yangcan ftpdata]# cd ..
[root@yangcan /]# umount /ftpdata/
[root@yangcan /]# e2fsck -f /dev/vg6/ftpdata
e2fsck 1.41.12 (17-May-2010)
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/vg6/ftpdata: 41/655360 files (0.0% non-contiguous), 78604/2621440 blocks
注意:我们大概要缩到6G左右,那我们就不能直接resize2fs 到6G,这样肯定是错误的,因为PE是可以被LVM寻址的最小单元,本次实验PE为16M.我们去做缩容的时候,不可能是0.6个PE.所有尤其注意,网上很多资料就是直接指定,而没有做测试环节!!!会造成数据的丢失,所以指定,先用lvreduce -t 测试一下.用bc算出6G的位置,到底那个值合适
[root@yangcan /]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
--6000刚好整除16
6000%16
0
--6001除16为1
6001%16
1
5999%16
15
--刚好6000M,取余为0,我们比较一下6001和6000的区别
[root@yangcan /]# lvreduce -L 6001M /dev/vg6/ftpdata -t
Test mode: Metadata will NOT be updated and volumes will not be (de)activated.
Rounding up size to full physical extent 5.88 GiB
WARNING: Reducing active logical volume to 5.88 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]: n
Logical volume ftpdata NOT reduced
显示了警告信息!
[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata -t
Test mode: Metadata will NOT be updated and volumes will not be (de)activated.
WARNING: Reducing active logical volume to 5.86 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]:
提示为6000M是,没问题
当-t 测试时,没问题了,那我们就resize2fs了
[root@yangcan /]# resize2fs /dev/vg6/ftpdata 6000M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg6/ftpdata to 1536000 (4k) blocks.
The filesystem on /dev/vg6/ftpdata is now 1536000 blocks long.
挂载,查看文件系统的大小,为最新的6000M,数据也没有丢失!!
[root@yangcan /]# mount -a
[root@yangcan /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 17G 2.1G 14G 14% /
tmpfs 504M 264K 504M 1% /dev/shm
/dev/sda1 291M 31M 246M 11% /boot
/dev/sr0 2.9G 2.9G 0 100% /media/cdrom
/dev/mapper/vg6-ftpdata 5.8G 145M 5.4G 3% /ftpdata
我们用lvdisplay查看一下逻辑卷的大小还是为10G
[root@yangcan /]# lvdisplay
--- Logical volume ---
LV Name /dev/vg6/ftpdata
VG Name vg6
LV UUID LSOtbB-k3mz-DVbb-ATcE-IhBk-V9B7-uaSStM
LV Write Access read/write
LV Status available
# open 1
LV Size 10.00 GiB
Current LE 640
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
所以我们用lvreduce进行调整!
[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata
WARNING: Reducing active and open logical volume to 5.86 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ftpdata? [y/n]: n
Logical volume ftpdata NOT reduced
--提示逻辑卷正在打开,所有我们需要停止逻辑卷,再去调整大小
[root@yangcan /]# lvchange -an /dev/vg6/ftpdata
[root@yangcan /]# lvscan
inactive '/dev/vg6/ftpdata' [10.00 GiB] inherit
[root@yangcan /]# lvreduce -L 6000M /dev/vg6/ftpdata
Reducing logical volume ftpdata to 5.86 GiB
Logical volume ftpdata successfully resized
[root@yangcan /]# mount -a
mount: special device /dev/vg6/ftpdata does not exist
--当我们去挂载的时候,提示没有这个设备.所以需要启用
[root@yangcan /]# lvchange -ay /dev/vg6/ftpdata
[root@yangcan /]# lvscan
ACTIVE '/dev/vg6/ftpdata' [5.86 GiB] inherit
重新挂载,里面的文件还在!
[root@yangcan /]# mount -a
[root@yangcan /]# cd /ftpdata/
[root@yangcan ftpdata]# ll
total 4996
-rw-r--r--. 1 root root 148 Jul 30 19:37 asound.conf
-rw-r--r--. 1 root root 21214 Jul 30 19:37 dnsmasq.conf
-rw-r--r--. 1 root root 559 Jul 30 19:37 dracut.conf
-rw-r--r--. 1 root root 20 Jul 30 19:37 fprintd.conf
-rw-r--r--. 1 root root 0 Jul 30 19:37 gai.conf
-rw-------. 1 root root 781 Jul 30 19:37 grub.conf
-rw-r--r--. 1 root root 9 Jul 30 19:37 host.conf
-rw-r--r--. 1 root root 5309 Jul 30 19:37 kdump.conf
ps: 你也可以不做-t测试,直接指定大小去缩容,看数据是否还在?
--------移除物理卷--------
1. 移除之前,物理卷里面必须没有数据,用下以下命令查看各物理卷的空间,是否有足够的空间可以被用来移动数据!
[root@yangcan Desktop]# pvscan
2. 搬移要移除物理卷的数据到其他空闲且满足大小的物理卷
[root@yangcan Desktop]# pvmove /dev/sda6 /dev/sda7
完成之后
3. 从卷组移除物理卷
[root@yangcan Desktop]#vgchange -an vg5
[root@yangcan Desktop]# vgreduce vg5 /dev/sda6