linux的逻辑卷管理

linux的逻辑卷管理

一、逻辑卷介绍

逻辑卷
作用:1.整合分散的空间
2.空间支持扩大

逻辑卷制作过程:将众多的物理卷(PV)组建成卷组(VG),再从卷组中划分出逻辑卷(LV)

二、建立卷组和逻辑卷


// 建立卷组(VG)                    
// 格式:vgcreate   卷组名    设备路径…….

[root@www ~]# vgcreate systemvg /dev/sdb[1-2]
// 该处是因为之前的sdb1做了交换空间,正常是直接会成功
WARNING: swap signature detected on /dev/sdb1 at offset 4086. Wipe it? [y/n]: y
  Wiping swap signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
  Volume group "systemvg" successfully created
 // 查看物理卷的信息
[root@www ~]# pvs
  PV         VG       Fmt  Attr PSize    PFree
  /dev/sda2  centos   lvm2 a--   <59.00g    4.00m
  /dev/sdb1  systemvg lvm2 a--  1020.00m 1020.00m
  /dev/sdb2  systemvg lvm2 a--  1020.00m 1020.00m
  // 查看卷组的信息
[root@www ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   3   0 wz--n- <59.00g 4.00m
  systemvg   2   0   0 wz--n-   1.99g 1.99g
 // 可以看到此时的centos也是一个卷组
[root@www ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0    60G  0 disk
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0    59G  0 part
  ├─centos-root 253:0    0  38.3G  0 lvm  /
  ├─centos-swap 253:1    0     2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  18.7G  0 lvm  /home
sdb               8:16   0    10G  0 disk
├─sdb1            8:17   0     1G  0 part
├─sdb2            8:18   0     1G  0 part
├─sdb3            8:19   0     1G  0 part
├─sdb4            8:20   0     1K  0 part
└─sdb5            8:21   0     1G  0 part
sdc               8:32   0    10G  0 disk
├─sdc1            8:33   0 953.7M  0 part
└─sdc2            8:34   0   3.7G  0 part
sr0              11:0    1   4.3G  0 rom  /mydvd


// 建立逻辑卷(LV)  
//  格式: lvcreate  -L   大小G    -n  逻辑卷名字     卷组名


[root@www ~]# lvcreate -L 1G -n vo systemvg
  Logical volume "vo" created.
  // 查看逻辑卷信息
[root@www ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos   -wi-ao---- <18.70g
  root centos   -wi-ao---- <38.30g
  swap centos   -wi-ao----   2.00g
  vo   systemvg -wi-a-----   1.00g
[root@www ~]# ls /dev/systemvg/vo
/dev/systemvg/vo
// 此处是系统自动做了链接
[root@www ~]# ls -l /dev/systemvg/vo
lrwxrwxrwx. 1 root root 7 Dec 22 03:08 /dev/systemvg/vo -> ../dm-3
// 格式化刚才创建的逻辑卷
[root@www ~]# mkfs.xfs /dev/systemvg/vo
meta-data=/dev/systemvg/vo       isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, 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
// 查看逻辑卷的类型为xfs
[root@www ~]# blkid /dev/systemvg/vo
/dev/systemvg/vo: UUID="35d7aecf-5e01-42c2-916f-f52ba8fc8d7c" TYPE="xfs"
[root@www ~]# pvs
  PV         VG       Fmt  Attr PSize    PFree
  /dev/sda2  centos   lvm2 a--   <59.00g    4.00m
  /dev/sdb1  systemvg lvm2 a--  1020.00m       0
  /dev/sdb2  systemvg lvm2 a--  1020.00m 1016.00m
  // 此时可以看到们的systemvg增加了一个LV
[root@www ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   3   0 wz--n- <59.00g    4.00m
  systemvg   2   1   0 wz--n-   1.99g 1016.00m
  // 通过lsblk命令也可以看到
[root@www ~]# lsblk
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0    60G  0 disk
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0    59G  0 part
  ├─centos-root 253:0    0  38.3G  0 lvm  /
  ├─centos-swap 253:1    0     2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  18.7G  0 lvm  /home
sdb               8:16   0    10G  0 disk
├─sdb1            8:17   0     1G  0 part
│ └─systemvg-vo 253:3    0     1G  0 lvm
├─sdb2            8:18   0     1G  0 part
│ └─systemvg-vo 253:3    0     1G  0 lvm
├─sdb3            8:19   0     1G  0 part
├─sdb4            8:20   0     1K  0 part
└─sdb5            8:21   0     1G  0 part
sdc               8:32   0    10G  0 disk
├─sdc1            8:33   0 953.7M  0 part
└─sdc2            8:34   0   3.7G  0 part
sr0              11:0    1   4.3G  0 rom  /mydvd
[root@www ~]#
// 通过挂载来使用逻辑卷
[root@www ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   39G  3.7G   35G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sr0                 4.3G  4.3G     0 100% /mydvd
/dev/sda1               1014M  133M  882M  14% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
tmpfs                     98M     0   98M   0% /run/user/0
[root@www ~]# mkdir /mylv
[root@www ~]# mount /dev/systemvg/vo /mylv/
[root@www ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   39G  3.7G   35G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sr0                 4.3G  4.3G     0 100% /mydvd
/dev/sda1               1014M  133M  882M  14% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/systemvg-vo 1014M   33M  982M   4% /mylv
[root@www ~]#

三、卷组和逻辑卷的扩展


[root@www ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   3   0 wz--n- <59.00g    4.00m
  systemvg   2   1   0 wz--n-   1.99g 1016.00m
[root@www ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos   -wi-ao---- <18.70g
  root centos   -wi-ao---- <38.30g
  swap centos   -wi-ao----   2.00g
  vo   systemvg -wi-ao----   1.00g
  // 1.扩展卷组的空间
[root@www ~]# vgextend systemvg /dev/sdb3 /dev/sdb5
  Physical volume "/dev/sdb3" successfully created.
  Physical volume "/dev/sdb5" successfully created.
  Volume group "systemvg" successfully extended
[root@www ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   3   0 wz--n- <59.00g 4.00m
  systemvg   4   1   0 wz--n-   3.98g 2.98g
[root@www ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos   -wi-ao---- <18.70g
  root centos   -wi-ao---- <38.30g
  swap centos   -wi-ao----   2.00g
  vo   systemvg -wi-ao----   1.00g
[root@www ~]# pvs
  PV         VG       Fmt  Attr PSize    PFree
  /dev/sda2  centos   lvm2 a--   <59.00g    4.00m
  /dev/sdb1  systemvg lvm2 a--  1020.00m       0
  /dev/sdb2  systemvg lvm2 a--  1020.00m 1016.00m
  /dev/sdb3  systemvg lvm2 a--  1020.00m 1020.00m
  /dev/sdb5  systemvg lvm2 a--  1020.00m 1020.00m
// 2.扩展逻辑卷的空间
[root@www ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  centos     1   3   0 wz--n- <59.00g 4.00m
  systemvg   4   1   0 wz--n-   3.98g 2.98g
[root@www ~]# lvs
  LV   VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos   -wi-ao---- <18.70g
  root centos   -wi-ao---- <38.30g
  swap centos   -wi-ao----   2.00g
  vo   systemvg -wi-ao----   1.00g
[root@www ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   39G  3.7G   35G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sr0                 4.3G  4.3G     0 100% /mydvd
/dev/sda1               1014M  133M  882M  14% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/systemvg-vo 1014M   33M  982M   4% /mylv
[root@www ~]#
[root@www ~]# lvextend -L 2G /dev/systemvg/vo
  Size of logical volume systemvg/vo changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).
  Logical volume systemvg/vo successfully resized.
[root@www ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   39G  3.7G   35G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sr0                 4.3G  4.3G     0 100% /mydvd
/dev/sda1               1014M  133M  882M  14% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/systemvg-vo 1014M   33M  982M   4% /mylv
// 扩展后,我们需要刷新文件系统
[root@www ~]# xfs_growfs /dev/systemvg/vo
meta-data=/dev/mapper/systemvg-vo isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=262144, 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
data blocks changed from 262144 to 524288
[root@www ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   39G  3.7G   35G  10% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sr0                 4.3G  4.3G     0 100% /mydvd
/dev/sda1               1014M  133M  882M  14% /boot
/dev/mapper/centos-home   19G   33M   19G   1% /home
tmpfs                     98M     0   98M   0% /run/user/0
/dev/mapper/systemvg-vo  2.0G   33M  2.0G   2% /mylv
[root@www ~]#

四、逻辑卷的删除


// 逻辑卷的删除
// 删除卷组的前提:基于此卷组创建的所有逻辑卷,要全部删除
// 删除逻辑卷的前提:不能删除正在挂载使用的逻辑卷
[root@www ~]# lvremove /dev/systemvg/vo
  Logical volume systemvg/vo contains a filesystem in use.
  // 我们需要先卸载正在挂载使用的逻辑卷
[root@www ~]# umount /mylv/
// 此时就能删除成功
[root@www ~]# lvremove /dev/systemvg/vo
Do you really want to remove active logical volume systemvg/vo? [y/n]: y
  Logical volume "vo" successfully removed
  
// 删除卷组,我们输入y即可
[root@www ~]# vgremove systemvg
[root@www ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   3   0 wz--n- <59.00g 4.00m
[root@www ~]#

上一篇:linxu磁盘分区与格式化和自动挂载


下一篇:ln命令即链接的制作