逻辑卷管理器 (Logical Volume Manager) 之二!

放大 LV 容量

我们不是说 LVM 最大的特色就是弹性调整磁盘容量吗?好!那我们就来处理一下,如果要放大 LV 的容量时, 该如何进行完整的步骤呢?其实一点都不难喔!你只要这样做即可:
1. 用 fdisk 设定新的具有 8e system ID 的 partition
2. 利用 pvcreate 建置 PV
3. 利用 vgextend 将 PV 加入我们的 ingvg
4. 利用 lvresize 将新加入的 PV 内的 PE 加入 inglv 中
5. 透过 resize2fs 将文件系统的容量确实增加!

#1.处理一个新磁盘 /dev/sdf
[root@linux ~]# mkfs -t ext3 /dev/sdf
mke2fs 1.39 (29-May-2006)
/dev/sdf is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1048576 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

#2.新建新的PV
[root@linux ~]# pvcreate /dev/sdf
  Physical volume "/dev/sdf" successfully created
[root@linux ~]# pvscan
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  PV /dev/sdb1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdc1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdd1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sde1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdf                       lvm2 [8.00 GB]
  Total: 5 [39.94 GB] / in use: 4 [31.94 GB] / in no VG: 1 [8.00 GB]
  
#3.加大VG
[root@linux ~]# vgextend ingvg /dev/sdf
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  Volume group "ingvg" successfully extended
[root@linux ~]# vgdisplay
  --- Volume group ---
  VG Name               ingvg
  System ID             
  Format                lvm2
  Metadata Areas        5
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                5
  Act PV                5
  VG Size               39.92 GB
  PE Size               16.00 MB
  Total PE              2555
  Alloc PE / Size       2044 / 31.94 GB
  Free  PE / Size       511 / 7.98 GB
  VG UUID               NOdo5r-CHA0-W9D8-wBRj-ydV0-3UGm-XJx0vI

#4.放大LV
[root@linux ~]# lvresize -l +511 /dev/ingvg/inglv
  Extending logical volume inglv to 39.92 GB
  Logical volume inglv successfully resized
[root@linux ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/ingvg/inglv
  VG Name                ingvg
  LV UUID                4KbMKd-AJkf-z0fi-KtWP-zT5C-qirS-IkVFDv
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                39.92 GB
  Current LE             2555
  Segments               5
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
[root@linux ~]# df
文件系统               1K-块        已用     可用 已用% 挂载点
/dev/sda3              5991232   2655248   3026736  47% /
/dev/sda1               101086     11373     84494  12% /boot
tmpfs                   517548         0    517548   0% /dev/shm
/dev/mapper/ingvg-inglv
                      32963440    180292  31108704   1% /mnt/lvm
                      
#5.完整的将LV的容量扩充到整个文件系统
[root@linux ~]# resize2fs /dev/ingvg/inglv 
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/ingvg/inglv is mounted on /mnt/lvm; on-line resizing required
Performing an on-line resize of /dev/ingvg/inglv to 10465280 (4k) blocks.
The filesystem on /dev/ingvg/inglv is now 10465280 blocks long.

[root@linux ~]# df
文件系统               1K-块        已用     可用 已用% 挂载点
/dev/sda3              5991232   2655248   3026736  47% /
/dev/sda1               101086     11373     84494  12% /boot
tmpfs                   517548         0    517548   0% /dev/shm
/dev/mapper/ingvg-inglv
                      41204288    180244  38934368   1% /mnt/lvm

缩小 LV 容量

#1.先找出/dev/sdf的容量大小,并尝试计算文件系统需要缩小到多少。
[root@linux ~]# pvdisplay
  ... ...
  --- Physical volume ---
  PV Name               /dev/sdf
  VG Name               ingvg
  PV Size               8.00 GB / not usable 16.00 MB
  Allocatable           yes (but full)
  PE Size (KByte)       16384
  Total PE              511
  Free PE               0
  Allocated PE          511
  PV UUID               sMs5WX-L3jO-8pyE-PBVj-WR70-5PTx-ZXrI4g
#从上面可以看出/dev/sdf含有511个PE,那么如果使用resize2fs,则总量减去8GB就对了!
[root@linux ~]# pvscan
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  PV /dev/sdb1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sdc1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sdd1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sde1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sdf    VG ingvg   lvm2 [7.98 GB / 0    free]
  Total: 5 [39.92 GB] / in use: 5 [39.92 GB] / in no VG: 0 [0   ]
#从上面可以看出扣除/dev/sdf,则剩余容量有:7.98 * 4 = 31.92GB
  
#2.直接降低文件系统容量
[root@linux ~]# resize2fs /dev/ingvg/inglv 31920M
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/ingvg/inglv is mounted on /mnt/lvm; on-line resizing required
On-line shrinking from 10465280 to 8171520 not supported.
#从上面可以看见,增加可以在线进行,减少就不可以啦!所以要这样做:
 [root@linux ~]# umount /dev/ingvg/inglv 
[root@linux ~]# resize2fs /dev/ingvg/inglv 31920M
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/ingvg/inglv' first.
#它要我们先进行磁盘检查。
[root@linux ~]# e2fsck -f /dev/ingvg/inglv
e2fsck 1.39 (29-May-2006)
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/ingvg/inglv: 12/5232640 files (8.3% non-contiguous), 209269/10465280 blocks
[root@linux ~]# resize2fs /dev/ingvg/inglv 31920M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/ingvg/inglv to 8171520 (4k) blocks.
The filesystem on /dev/ingvg/inglv is now 8171520 blocks long.

[root@linux ~]# mount /dev/ingvg/inglv /mnt/lvm/
[root@linux ~]# df /mnt/lvm/
文件系统               1K-块        已用     可用 已用% 挂载点
/dev/mapper/ingvg-inglv
                      32172936    180292  30685204   1% /mnt/lvm

#3.降低LV的容量,同时我们知道/dev/sdf有511个PE。
[root@linux ~]# lvresize -l -511 /dev/ingvg/inglv 
  WARNING: Reducing active and open logical volume to 31.94 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce inglv? [y/n]: y
  Reducing logical volume inglv to 31.94 GB
  Logical volume inglv successfully resized
[root@linux ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/ingvg/inglv
  VG Name                ingvg
  LV UUID                4KbMKd-AJkf-z0fi-KtWP-zT5C-qirS-IkVFDv
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                31.94 GB
  Current LE             2044
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
  
#4.先确认/dev/sdf是否将PE都删除了。
[root@linux ~]# pvdisplay
	... ...
  --- Physical volume ---
  PV Name               /dev/sdf
  VG Name               ingvg
  PV Size               8.00 GB / not usable 16.00 MB
  Allocatable           yes 
  PE Size (KByte)       16384
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               sMs5WX-L3jO-8pyE-PBVj-WR70-5PTx-ZXrI4g
  
#5.移动PE(这里因为第4步中Free PE已经有511个,所以这一步在这里不用执行。)
[root@linux ~]# pvmove /dev/sdf /dev/sdb 
# pvmove 来源PV 目标PV ,可以将 /dev/sdf 内的 PE 全部移动到 /dev/sdb 
# 尚未被使用的 PE 去 (Free PE)。

#6.将/dev/sdf从VG中移除。
[root@linux ~]# vgdisplay
  --- Volume group ---
  VG Name               ingvg
  System ID             
  Format                lvm2
  Metadata Areas        5
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                5
  Act PV                5
  VG Size               39.92 GB
  PE Size               16.00 MB
  Total PE              2555
  Alloc PE / Size       2044 / 31.94 GB
  Free  PE / Size       511 / 7.98 GB
  VG UUID               NOdo5r-CHA0-W9D8-wBRj-ydV0-3UGm-XJx0vI
   
[root@linux ~]# vgreduce ingvg /dev/sdf
  Removed "/dev/sdf" from volume group "ingvg"
[root@linux ~]# vgdisplay
  --- Volume group ---
  VG Name               ingvg
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               31.94 GB
  PE Size               16.00 MB
  Total PE              2044
  Alloc PE / Size       2044 / 31.94 GB
  Free  PE / Size       0 / 0   
  VG UUID               NOdo5r-CHA0-W9D8-wBRj-ydV0-3UGm-XJx0vI
  
[root@linux ~]# pvscan
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  PV /dev/sdb1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdc1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdd1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sde1   VG ingvg           lvm2 [7.98 GB / 0    free]
  PV /dev/sdf                       lvm2 [8.00 GB]
  Total: 5 [39.94 GB] / in use: 4 [31.94 GB] / in no VG: 1 [8.00 GB]
[root@linux ~]# pvremove /dev/sdf
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  Labels on physical volume "/dev/sdf" successfully wiped
[root@linux ~]# pvscan
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  PV /dev/sdb1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sdc1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sdd1   VG ingvg   lvm2 [7.98 GB / 0    free]
  PV /dev/sde1   VG ingvg   lvm2 [7.98 GB / 0    free]
  Total: 4 [31.94 GB] / in use: 4 [31.94 GB] / in no VG: 0 [0   ]

你可能感兴趣的:(linux,manager,System,Access,sms,Allocation)