由于之前给kvm虚拟机中的centos分配的根目录过少,导致根目录经常100%,下面我们来进行根目录扩容。

创建虚拟磁盘

在/data目录下创建100G的虚拟磁盘。

[root@localhost data1]# cd /data/
qemu-img create -f raw centos-add1.img 100G

给虚拟机添加磁盘

[root@localhost data1]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     test1                     running
 2     test2                     running
 3     test3                     running

我们给test1虚拟机增加磁盘

virsh attach-disk test1 /data/centos-add1.img vdb --cache none

ssh登陆该主机,查看新增的磁盘

[root@node1 ~]# fdisk -l

Disk /dev/vda: 327.0 GB, 327018325504 bytes
16 heads, 63 sectors/track, 633638 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009717a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018      624153   314059776   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_hadoop21-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x00000000

Disk /dev/mapper/vg_hadoop21-lv_swap: 5217 MB, 5217714176 bytes
255 heads, 63 sectors/track, 634 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: 0x00000000

Disk /dev/mapper/vg_hadoop21-lv_home: 262.7 GB, 262689259520 bytes
255 heads, 63 sectors/track, 31936 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: 0x00000000

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

根目录扩容

查看现有磁盘空间

[root@node1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_hadoop21-lv_root  50G   27G   20G  58% /
tmpfs                          2.4G   12K  2.4G   1% /dev/shm
/dev/vda1                      477M   32M  420M   8% /boot
/dev/mapper/vg_hadoop21-lv_home  241G  904M  228G   1% /home

对新增磁盘分区

[root@node1 ~]# fdisk /dev/vdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4540d03c.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 4
First cylinder (1-208050, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-208050, default 208050): 
Using default value 208050

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

fdisk –l 查看新的分区表

[root@node1 ~]# fdisk -l

Disk /dev/vda: 327.0 GB, 327018325504 bytes
16 heads, 63 sectors/track, 633638 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009717a

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *           3        1018      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018      624153   314059776   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/vg_hadoop21-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x00000000

Disk /dev/mapper/vg_hadoop21-lv_swap: 5217 MB, 5217714176 bytes
255 heads, 63 sectors/track, 634 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: 0x00000000

Disk /dev/mapper/vg_hadoop21-lv_home: 262.7 GB, 262689259520 bytes
255 heads, 63 sectors/track, 31936 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: 0x00000000

Disk /dev/vdb: 107.4 GB, 107374182400 bytes
16 heads, 63 sectors/track, 208050 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4540d03c

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb4               1      208050   104857168+  83  Linux

格式化新分区/dev/sdb

[root@node1 ~]# mkfs.ext4 /dev/vdb4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214292 blocks
1310714 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872

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

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

将/dev/sdb4创建为pv物理卷组

[root@node1 ~]# pvcreate /dev/vdb4
  Physical volume "/dev/vdb4" successfully created
  
[root@node1 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_hadoop21
  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               299.51 GiB
  PE Size               4.00 MiB
  Total PE              76674
  Alloc PE / Size       76674 / 299.51 GiB
  Free  PE / Size       0 / 0   
  VG UUID               mFmrBQ-y1vZ-gsH3-F53W-SKGL-4qg5-fLSNty

将/dev/sdb4的PV空间扩展到VG卷组:VolGroup

[root@node1 ~]# vgextend /dev/vg_hadoop21 /dev/vdb4
  Volume group "vg_hadoop21" successfully extended
[root@node1 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_hadoop21
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               399.50 GiB
  PE Size               4.00 MiB
  Total PE              102273
  Alloc PE / Size       76674 / 299.51 GiB
  Free  PE / Size       25599 / 100.00 GiB
  VG UUID               mFmrBQ-y1vZ-gsH3-F53W-SKGL-4qg5-fLSNty

扩展LV逻辑卷VolGroup-LogVo100(/)的空间容量

[root@node1 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_hadoop21-lv_root  50G   27G   20G  58% /
tmpfs                          2.4G   12K  2.4G   1% /dev/shm
/dev/vda1                      477M   32M  420M   8% /boot
/dev/mapper/vg_hadoop21-lv_home  241G  904M  228G   1% /home
                      
[root@node1 ~]# lvextend -L +90G /dev/mapper/vg_hadoop21-lv_root 
  Size of logical volume vg_hadoop21/lv_root changed from 50.00 GiB (12800 extents) to 140.00 GiB (35840 extents).
  Logical volume lv_root successfully resized

检查文件系统是否完整挂载

[root@node1 ~]# e2fsck /dev/mapper/vg_hadoop21-lv_root 
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/vg_hadoop21-lv_root is mounted.
e2fsck: Cannot continue, aborting.

将扩容的LV逻辑卷的容量写入根分区中(/)

[root@node1 ~]# resize2fs /dev/mapper/vg_hadoop21-lv_root 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_hadoop21-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 9
Performing an on-line resize of /dev/mapper/vg_hadoop21-lv_root to 36700160 (4k) blocks.
The filesystem on /dev/mapper/vg_hadoop21-lv_root is now 36700160 blocks long.

查看已经写入根分区的磁盘容量

[root@node1 ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
/dev/mapper/vg_hadoop21-lv_root   147G   27G  113G  20% /
tmpfs                           2.4G   12K  2.4G   1% /dev/shm
/dev/vda1                       477M   32M  420M   8% /boot
/dev/mapper/vg_hadoop21-lv_home   241G  904M  228G   1% /home

到此为止,简单,快速扩容根分区的问题成功结束。