zoukankan      html  css  js  c++  java
  • Linux----LVM扩容磁盘空间

    Linux系统LVM扩容一个分区相对于Windows来说没有那么直观,但是熟悉命令后,扩容起来也是蛮方便的。

    扩容场景如下:

    1 [root@rhel06 ~]# df -Th
    2 Filesystem            Type     Size  Used Avail Use% Mounted on
    3 /dev/sda2             ext4     9.9G  2.2G  7.3G  23% /
    4 tmpfs                 tmpfs    491M     0  491M   0% /dev/shm
    5 /dev/sda1             ext4     194M   29M  155M  16% /boot
    6 /dev/sr0              iso9660  3.6G  3.6G     0 100% /mnt
    7 /dev/mapper/VG001-lv1 ext4     2.0G   67M  1.9G   4% /lv1

    现有一个lvm分区lv1容量2G,希望扩容此分区空间

     1 [root@rhel06 home]# cat /proc/partitions 
     2 major minor  #blocks  name
     3 
     4    8        0   20971520 sda
     5    8        1     204800 sda1
     6    8        2   10485760 sda2
     7    8        3    2097152 sda3
     8    8        4          1 sda4
     9    8        5    2103487 sda5
    10  253        0    2097152 dm-0

    查看partitions  获取当前分区情况和磁盘总空间情况。需要计算20971520-204800-10485760-2097152-2103487-1=6080320,约6G的未使用空间。(dm-0是一个lv分区)

    当然使用这种手动方式获取未分区空间太过麻烦。可以用这个shell来完成大量的计算

     1 [root@rhel06 home]# cat sda.sh 
     2 #!/bin/bash
     3 total=$(grep 'sda$' /proc/partitions |awk '{print $3}');
     4 used=0 
     5 for i in $(grep 'sda[[:digit:]]+$' /proc/partitions |awk '{print $3}' |xargs) 
     6 do
     7 used=$(( used + i ));
     8 done
     9 echo $((( total - used ) / 1024 )) "MB"
    10 [root@rhel06 home]# ./sda.sh 
    11 5937 MB

    接下来执行具体的分区步骤:

    一、划分一个新分区并格式化

     1 [root@rhel06 ~]# fdisk /dev/sda
     2 
     3 Command (m for help): p 
     4 
     5 Disk /dev/sda: 21.5 GB, 21474836480 bytes
     6 255 heads, 63 sectors/track, 2610 cylinders
     7 Units = cylinders of 16065 * 512 = 8225280 bytes
     8 Sector size (logical/physical): 512 bytes / 512 bytes
     9 I/O size (minimum/optimal): 512 bytes / 512 bytes
    10 Disk identifier: 0x00029f55
    11 
    12    Device Boot      Start         End      Blocks   Id  System
    13 /dev/sda1   *           1          26      204800   83  Linux
    14 Partition 1 does not end on cylinder boundary.
    15 /dev/sda2              26        1332    10485760   83  Linux
    16 /dev/sda3            1332        1593     2097152   82  Linux swap / Solaris
    17 /dev/sda4            1593        2610     8176089    5  Extended
    18 /dev/sda5            1593        1854     2103487+  8e  Linux LVM
    19 
    20 Command (m for help): n   #新建分区,由于我已新建了四个分区(主分区和逻辑分区),所以这里只能新建扩展分区,并没有出现分区类型选项
    21 First cylinder (1855-2610, default 1855):      #开始点,直接回车即可
    22 Using default value 1855
    23 Last cylinder, +cylinders or +size{K,M,G} (1855-2610, default 2610): +2G    #+2G,划分一个2G的新分区
    24 
    25 Command (m for help): p
    26 
    27 Disk /dev/sda: 21.5 GB, 21474836480 bytes
    28 255 heads, 63 sectors/track, 2610 cylinders
    29 Units = cylinders of 16065 * 512 = 8225280 bytes
    30 Sector size (logical/physical): 512 bytes / 512 bytes
    31 I/O size (minimum/optimal): 512 bytes / 512 bytes
    32 Disk identifier: 0x00029f55
    33 
    34    Device Boot      Start         End      Blocks   Id  System
    35 /dev/sda1   *           1          26      204800   83  Linux
    36 Partition 1 does not end on cylinder boundary.
    37 /dev/sda2              26        1332    10485760   83  Linux
    38 /dev/sda3            1332        1593     2097152   82  Linux swap / Solaris
    39 /dev/sda4            1593        2610     8176089    5  Extended
    40 /dev/sda5            1593        1854     2103487+  8e  Linux LVM
    41 /dev/sda6            1855        2116     2104483+  83  Linux
    42 
    43 Command (m for help): w   #保存退出
    1 [root@rhel06 ~]# partprobe   #不要忘了刷新分区表,虚拟机还是需要重启才能刷新。
    1 [root@rhel06 ~]# mkfs.ext4 /dev/sda6  #格式化

    二、转换sda6分区为pv

     1 [root@rhel06 ~]# pvcreate /dev/sda6    #转换sda6分区
     2   dev_is_mpath: failed to get device for 8:6
     3   Physical volume "/dev/sda6" successfully created
     4 [root@rhel06 ~]# pvdisplay      #查看系统所有pv,这里sda6已成功转换
     5   --- Physical volume ---
     6   PV Name               /dev/sda5
     7   VG Name               VG001
     8   PV Size               2.01 GiB / not usable 2.19 MiB
     9   Allocatable           yes 
    10   PE Size               4.00 MiB
    11   Total PE              513
    12   Free PE               1
    13   Allocated PE          512
    14   PV UUID               dSHFLX-9Lui-0ZDb-6J2g-pp6A-azuX-vg5yGV
    15    
    16   "/dev/sda6" is a new physical volume of "2.01 GiB"
    17   --- NEW Physical volume ---
    18   PV Name               /dev/sda6
    19   VG Name               
    20   PV Size               2.01 GiB
    21   Allocatable           NO
    22   PE Size               0   
    23   Total PE              0
    24   Free PE               0
    25   Allocated PE          0
    26   PV UUID               TEGKht-876S-8MMd-vWkX-rjeI-4UqG-JqRsT2

    三、添加新pv到vg

     1 [root@rhel06 ~]# vgdisplay |grep Name    #查看当前vg名称
     2   VG Name               VG001
     3 [root@rhel06 ~]# vgextend VG001 /dev/sda6  #添加“sda6”pv到此vg
     4   Volume group "VG001" successfully extended
     5 [root@rhel06 ~]# vgdisplay     #查看vg详情,可以看到vg已经扩容到4g
     6   --- Volume group ---
     7   VG Name               VG001
     8   System ID             
     9   Format                lvm2
    10   Metadata Areas        2
    11   Metadata Sequence No  3
    12   VG Access             read/write
    13   VG Status             resizable
    14   MAX LV                0
    15   Cur LV                1
    16   Open LV               1
    17   Max PV                0
    18   Cur PV                2
    19   Act PV                2
    20   VG Size               4.01 GiB
    21   PE Size               4.00 MiB
    22   Total PE              1026
    23   Alloc PE / Size       512 / 2.00 GiB
    24   Free  PE / Size       514 / 2.01 GiB
    25   VG UUID               qG18Fp-agAP-gaTp-0crS-Ughm-UilM-2ZftQt

    四、调整lv1容量

    1 [root@rhel06 ~]# lvextend -L 4G /dev/VG001/lv1    #调整lv1分区容量为4G,原来是2G
    2   Extending logical volume lv1 to 4.00 GiB
    3   Logical volume lv1 successfully resized
    4 [root@rhel06 ~]# resize2fs /dev/VG001/lv1    #执行重设大小,对当前lv1有效
    5 resize2fs 1.41.12 (17-May-2010)
    6 Filesystem at /dev/VG001/lv1 is mounted on /lv1; on-line resizing required
    7 old desc_blocks = 1, new_desc_blocks = 1
    8 Performing an on-line resize of /dev/VG001/lv1 to 1048576 (4k) blocks.
    9 The filesystem on /dev/VG001/lv1 is now 1048576 blocks long.

    五、到此设置已完成,df命令查看已扩容到4G

    1 [root@rhel06 ~]# df -h
    2 Filesystem             Size  Used Avail Use% Mounted on
    3 /dev/sda2              9.9G  2.2G  7.3G  23% /
    4 tmpfs                  491M     0  491M   0% /dev/shm
    5 /dev/sda1              194M   29M  155M  16% /boot
    6 /dev/sr0               3.6G  3.6G     0 100% /mnt
    7 /dev/mapper/VG001-lv1  4.0G   68M  3.7G   2% /lv1

    六、新增lv分区

     [root@rhel06 ~]# lvcreate -L 4G -n lv0data lv1 #在lv1逻辑卷新增lv0data分区   -L指定大小   -n指定名称

    问题1:执行partprobe刷新分区表时会出现以下报错:

    [root@rhel06 ~]# partprobe
    Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙).  As a result, it may not reflect all of your changes until after reboot.

    这个问题貌似只存在于虚拟机上面,物理机上并没有发现这个问题。重启虚拟机即可。

    问题2:fdisk可以查看到sda5分区,但是使用mkfs格式化是提示“没有这个文件或目录”,那是分区表没有刷新或者刷新失败,刷新步骤可参考上一个问题。

     1    Device Boot      Start         End      Blocks   Id  System
     2 /dev/sda1   *           1          26      204800   83  Linux
     3 Partition 1 does not end on cylinder boundary.
     4 /dev/sda2              26        1332    10485760   83  Linux
     5 /dev/sda3            1332        1593     2097152   82  Linux swap / Solaris
     6 /dev/sda4            1593        2610     8176089    5  Extended
     7 /dev/sda5            1593        1854     2103487+  83  Linux
     8 [root@rhel06 ~]# mkfs.ext4 /dev/sda5
     9 mke2fs 1.41.12 (17-May-2010)
    10 无法对 /dev/sda5 进行 stat 调用 --- 没有那个文件或目录
  • 相关阅读:
    iOS代码段整理
    Git命令行
    Vue开发系列四- 网络请求库vue-resource的使用
    Vue开发系列三 调试
    Vue开发系列三-打包
    Vue开发系列二 熟悉项目结构
    错误及经验记录
    cocopods sh 项目运行权限问题
    关于添加UITextField添加输入内容判定导致九宫格键盘无法使用问题
    catagory添加属性
  • 原文地址:https://www.cnblogs.com/tail-f/p/6143085.html
Copyright © 2011-2022 走看看