1. 程式人生 > >LVM學習筆記

LVM學習筆記

linux

LVM:Logical Volume Manager

一、創建、刪除物理卷

pvcreate /PATH/TO/PV

pvremove /PATH/TO/PV

pvdisplay

pvs

[[email protected] ~]# pvcreate /dev/sdb{1,2,3,5}
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdb3" successfully created.
Physical volume "/dev/sdb5" successfully created.

二、創建、刪除卷組

vgcreate VG_NAME /PATH/TO/PV

vgremove VG_NAME 刪除卷組

vgdisplay 顯示詳細信息

vgs 顯示卷組名、物理卷個數、容量大小(信息相較簡略)

-s #: PE大小,默認為4MB

[[email protected] ~]# vgcreate test_vg /dev/sdb{1,2,3}
  Volume group "test_vg" successfully created
[[email protected]
/* */ ~]# vgdisplay --- Volume group --- VG Name ol 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 99.00 GiB PE Size 4.00 MiB Total PE 25343 Alloc PE / Size 25342 / 98.99 GiB Free PE / Size 1 / 4.00 MiB VG UUID 0fRWww-EJtp-StyQ-Vt2v-IOvU-9kkT-V0spMP --- Volume group --- VG Name test_vg 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 2.99 GiB PE Size 4.00 MiB Total PE 765 Alloc PE / Size 0 / 0 Free PE / Size 765 / 2.99 GiB VG UUID 57ta1G-U2St-BY8s-XU1T-MZHN-NWjC-EucYKQ

三、創建邏輯卷

[[email protected] ~]# vgcreate test_vg /dev/sdb{1,2,3}
  Volume group "test_vg" successfully created

lvcreate -n LV_NAME -L(邏輯卷總大小) #G VG_NAME | -l (邏輯卷裏PE個數)#G VG_NAME

[[email protected] ~]# lvcreate -n test_lv -L 2G test_vg 
  Logical volume "test_lv" created.
[[email protected] ~]# lvs
  LV      VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home    ol      -wi-ao---- 45.12g                                                    
  root    ol      -wi-ao---- 50.00g                                                    
  swap    ol      -wi-ao----  3.88g                                                    
  test_lv test_vg -wi-a-----  1.56g

四、格式化為ext3

[[email protected] ~]# mke2fs -j /dev/test_vg/test_lv 
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
102544 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=419430400
13 block groups
32768 blocks per group, 32768 fragments per group
7888 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

五、擴展卷組

[[email protected] ~]# vgextend test_vg /dev/sdb5
  Volume group "test_vg" successfully extended

六、擴展邏輯卷

lvextend -L [+]# /PATH/TO/LV

[[email protected] ~]# lvextend -L +500M /dev/test_vg/test_lv 
  Rounding size to boundary between physical extents: 504.00 MiB.
  Size of logical volume test_vg/test_lv changed from 1.56 GiB (200 extents) to 2.05 GiB (263 extents).
  Logical volume test_vg/test_lv successfully resized.

七、指定文件系統

resize2fs /PATH/TO/PV 3G

[[email protected] ~]# resize2fs -p /dev/test_vg/test_lv 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test_vg/test_lv to 538624 (4k) blocks.
Begin pass 1 (max = 4)
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/test_vg/test_lv is now 538624 blocks long.

八、縮減邏輯卷

註意事項:

1.不能在線縮減,得先卸載

2.確保縮減後的空間大小依然能存儲原有的所有數據

3.在縮減之前應該先強行檢查文件,以確保文件系統處於一致性狀態

[[email protected] ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
devtmpfs                     1.8G     0  1.8G   0% /dev
tmpfs                        1.8G  8.0K  1.8G   1% /dev/shm
tmpfs                        1.8G  9.1M  1.8G   1% /run
tmpfs                        1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/mapper/ol-root           50G  8.7G   42G  18% /
/dev/sda1                   1014M  341M  674M  34% /boot
/dev/mapper/ol-home           46G   37M   46G   1% /home
tmpfs                        368M  8.0K  368M   1% /run/user/0
/dev/sr0                     4.4G  4.4G     0 100% /run/media/root/OL-7.3 Server.x86_64
/dev/mapper/test_vg-test_lv  2.0G  2.4M  1.9G   1% /mnt


[[email protected] ~]# umount /mnt


[[email protected] ~]# e2fsck -f /dev/test_vg/test_lv (強制檢查文件系統)
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/test_vg/test_lv: 11/134096 files (0.0% non-contiguous), 17228/538624 blocks


[[email protected] ~]# resize2fs /dev/test_vg/test_lv 300M(文件系統先縮小300M)
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/test_vg/test_lv to 76800 (4k) blocks.
The filesystem on /dev/test_vg/test_lv is now 76800 blocks long.


[[email protected] ~]# lvreduce -L 300M /dev/test_vg/test_lv (物理空間再縮小300M)
  Rounding size to boundary between physical extents: 304.00 MiB.
  WARNING: Reducing active logical volume to 304.00 MiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
  
Do you really want to reduce test_vg/test_lv? [y/n]: y

  Size of logical volume test_vg/test_lv changed from 2.05 GiB (263 extents) to 304.00 MiB (38 extents).
  Logical volume test_vg/test_lv successfully resized.

九、快照卷

知識點:

1、生命周期為整個數據時長;在這段時長內,數據的增長量不能超出快照卷大小;
2、快照卷應該是只讀的;
3、跟原卷在同一卷組內;

[[email protected] ~]# lvcreate -n test_plv -L 500M -p r /dev/test_vg
  Rounding up size to full physical extent 504.00 MiB
  WARNING: Logical volume test_vg/test_plv not zeroed.
  Logical volume "test_plv" created.


LVM學習筆記