centos7 虚拟机根分区扩容实战

来源:互联网 发布:地方债务数据 2016 编辑:程序博客网 时间:2024/05/29 21:28

运维虚拟机时很容易碰到根分区磁盘空间满了的情况,又不知道删什么东西,这个时候如果需要对磁盘的根分区进行扩容,那么可以按照下列步骤进行
1、检查虚拟机是否有足够的磁盘剩余,若无需扩虚拟机的磁盘可直接从第三步开始根目录的扩容

[root@wukong06 ~]# fdisk -l 磁盘 /dev/vda:20 GB, 32212254720 字节,62914560 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x00021695    设备 Boot      Start         End      Blocks   Id  System /dev/vda1   *        2048     1026047      512000   83  Linux /dev/vda2         1026048    41943039    20458496   8e  Linux LVM 磁盘 /dev/mapper/centos-root:18.8 GB, 18756927488 字节,36634624 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节
[root@wukong06 ~]# df -h 文件系统                 容量  已用  可用 已用% 挂载点 /dev/mapper/centos-root   18G   14G  4.1G   78% / devtmpfs                 1.9G     0  1.9G    0% /dev tmpfs                    1.9G     0  1.9G    0% /dev/shm tmpfs                    1.9G  8.4M  1.9G    1% /run tmpfs                    1.9G     0  1.9G    0% /sys/fs/cgroup /dev/vda1                497M  125M  373M   26% /boot tmpfs                    380M     0  380M    0% /run/user/0

此时发现整个虚拟机的系统已经没有多余的分区和空间用来扩容啦,那么需要在虚拟机的磁盘文件上进行扩容
2、对虚拟机磁盘进行扩容,目前只支持磁盘格式为qcow2的虚拟机

[root@sugon03 wukong06]# virsh shutdown wukong06Domain wukong06 is being shutdown[root@sugon03 wukong06]# qemu-img info wukong06image: wukong06file format: qcow2virtual size: 20G (21474836480 bytes)disk size: 18Gcluster_size: 65536Format specific information:    compat: 1.1    lazy refcounts: false[root@sugon03 wukong06]# qemu-img resize wukong06 +10GImage resized.[root@sugon03 wukong06]# qemu-img info wukong06image: wukong06file format: qcow2virtual size: 30G (32212254720 bytes)disk size: 18Gcluster_size: 65536Format specific information:    compat: 1.1    lazy refcounts: false[root@sugon03 wukong06]# virsh start wukong06Domain wukong06 started

3、磁盘扩容后在创建新的分区vda3

[root@wukong06 ~]# fdisk /dev/vda欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。命令(输入 m 获取帮助):nPartition type:   p   primary (2 primary, 0 extended, 2 free)   e   extendedSelect (default p): Using default response p分区号 (3,4,默认 3):起始 扇区 (41943040-62914559,默认为 41943040):将使用默认值 41943040Last 扇区, +扇区 or +size{K,M,G} (41943040-62914559,默认为 62914559):将使用默认值 62914559分区 3 已设置为 Linux 类型,大小设为 10 GiB命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)正在同步磁盘。[root@wukong06 ~]# partprobe [root@wukong06 ~]# fdisk -l磁盘 /dev/vda:32.2 GB, 32212254720 字节,62914560 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x00021695   设备 Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048     1026047      512000   83  Linux/dev/vda2         1026048    41943039    20458496   8e  Linux LVM/dev/vda3        41943040    62914559    10485760   83  Linux磁盘 /dev/mapper/centos-root:18.8 GB, 18756927488 字节,36634624 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节

4、新磁盘/dev/vda3添加到物理卷中

[root@wukong06 ~]# pvcreate /dev/vda3  Physical volume "/dev/vda3" successfully created

然后pvdisplay查看已经有了/dev/vda3这张磁盘的物理卷

[root@wukong06 ~]# pvdisplay  --- Physical volume ---  PV Name               /dev/vda2  VG Name               centos  PV Size               19.51 GiB / not usable 3.00 MiB  Allocatable           yes   PE Size               4.00 MiB  Total PE              4994  Free PE               10  Allocated PE          4984  PV UUID               ZPMYiW-FYyw-LoeB-HlLp-I1l6-Vl1z-gxdB9Q  "/dev/vda3" is a new physical volume of "10.00 GiB"  --- NEW Physical volume ---  PV Name               /dev/vda3  VG Name                 PV Size               10.00 GiB  Allocatable           NO  PE Size               0     Total PE              0  Free PE               0  Allocated PE          0  PV UUID               oxxrCP-d3e4-2HIb-oq0X-GBkI-vKy6-FnLelj

5、把物理卷里的空间添加到卷组中

[root@wukong06 ~]# vgextend centos  /dev/vda3  Volume group "centos" successfully extended

vgextend centos /dev/vda3中centos表示卷组名,卷组名同/dev/vda2
6、查看扩展后的卷组容量,容量增加了/dev/vda3的容量,说明卷组centos 扩展成功

[root@wukong06 ~]# vgdisplay  --- Volume group ---  VG Name               centos  System ID               Format                lvm2  Metadata Areas        2  Metadata Sequence No  4  VG Access             read/write  VG Status             resizable  MAX LV                0  Cur LV                2  Open LV               2  Max PV                0  Cur PV                2  Act PV                2  VG Size               29.50 GiB  PE Size               4.00 MiB  Total PE              7553  Alloc PE / Size       4984 / 19.47 GiB  Free  PE / Size       2569 / 10.04 GiB  VG UUID               S9q81v-L2Eo-dZF1-EdEA-qTig-bVkO-RxxcP3
[root@wukong06 ~]# pvdisplay  --- Physical volume ---  PV Name               /dev/vda2  VG Name               centos  PV Size               19.51 GiB / not usable 3.00 MiB  Allocatable           yes   PE Size               4.00 MiB  Total PE              4994  Free PE               10  Allocated PE          4984  PV UUID               ZPMYiW-FYyw-LoeB-HlLp-I1l6-Vl1z-gxdB9Q  --- Physical volume ---  PV Name               /dev/vda3  VG Name               centos  PV Size               10.00 GiB / not usable 4.00 MiB  Allocatable           yes   PE Size               4.00 MiB  Total PE              2559  Free PE               2559  Allocated PE          0  PV UUID               oxxrCP-d3e4-2HIb-oq0X-GBkI-vKy6-FnLelj

7、把卷组的空间添加到逻辑卷中
现在要对根分区“/”进行容量扩展,其对应的逻辑卷设备名是/dev/mapper/centos-root

[root@wukong06 ~]# lvextend -l +100%FREE /dev/mapper/centos-root  Size of logical volume centos/root changed from 17.47 GiB (4472 extents) to 27.50 GiB (7041 extents).  Logical volume root successfully resized.[root@wukong06 ~]# lvdisplay /dev/mapper/centos-root  --- Logical volume ---  LV Path                /dev/centos/root  LV Name                root  VG Name                centos  LV UUID                feZtNe-1pX7-9KVv-RcdG-OpEs-nGJG-VhQjjk  LV Write Access        read/write  LV Creation host, time localhost.localdomain, 2017-01-16 16:08:15 +0800  LV Status              available  # open                 1  LV Size                27.50 GiB  Current LE             7041  Segments               2  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0

8、最后如果磁盘文件类型是xfs的话,用xfs_growfs进行空间刷新(否则用resize2fs),然后df -hT查看,扩容根目录成功!

[root@wukong06 ~]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1144832 blks         =                       sectsz=512   attr=2, projid32bit=1         =                       crc=0        finobt=0data     =                       bsize=4096   blocks=4579328, imaxpct=25         =                       sunit=0      swidth=0 blksnaming   =version 2              bsize=4096   ascii-ci=0 ftype=0log      =internal               bsize=4096   blocks=2560, version=2         =                       sectsz=512   sunit=0 blks, lazy-count=1realtime =none                   extsz=4096   blocks=0, rtextents=0data blocks changed from 4579328 to 7209984[root@wukong06 ~]# df -HT文件系统                类型      容量  已用  可用 已用% 挂载点/dev/mapper/centos-root xfs        30G   15G   16G   49% /devtmpfs                devtmpfs  2.0G     0  2.0G    0% /devtmpfs                   tmpfs     2.0G     0  2.0G    0% /dev/shmtmpfs                   tmpfs     2.0G   76M  2.0G    4% /runtmpfs                   tmpfs     2.0G     0  2.0G    0% /sys/fs/cgroup/dev/vda1               xfs       521M  131M  391M   26% /boottmpfs                   tmpfs     398M     0  398M    0% /run/user/0您在 /var/spool/mail/root 中有新邮件[root@wukong06 ~]# fdisk -l磁盘 /dev/vda:32.2 GB, 32212254720 字节,62914560 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x00021695   设备 Boot      Start         End      Blocks   Id  System/dev/vda1   *        2048     1026047      512000   83  Linux/dev/vda2         1026048    41943039    20458496   8e  Linux LVM/dev/vda3        41943040    62914559    10485760   83  Linux磁盘 /dev/mapper/centos-root:29.5 GB, 29532094464 字节,57679872 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节

最后感谢钱同学提供的初稿和操作步骤。
本操作文档只在qcow2 创建的centos7虚拟机上验证过

原创粉丝点击