Q132284591 发表于 2017-6-25 15:29:04

5. 磁盘扩容

8. 磁盘扩容

a. 对于raw格式的虚拟磁盘扩容

qemu-img info /data/kvm/test03.img //本身只有9G
image: /data/kvm/test03.img
file format: raw
virtual size: 9.0G (9663676416 bytes)
disk size: 1.1G


qemu-img resize /data/kvm/test03.img +2G

qemu-img info /data/kvm/test03.img //现在增加了2G
image: /data/kvm/test03.img
file format: raw
virtual size: 11G (11811160064 bytes)
disk size: 1.1G


virsh destroy test03//关闭test03虚拟机
virsh start test03//开启test03虚拟机
virsh console test03//进入虚拟机

fdisk -l   查看已经磁盘分区已经增加
# fdisk -l

Disk /dev/vda: 11.8 GB, 11811160064 bytes
16 heads, 63 sectors/track, 22885 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: 0x000099f3

   Device Boot      Start         End      Blocks   IdSystem
/dev/vda1   *         3      1018      512000   83Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       16645   7875584   8eLinux LVM
Partition 2 does not end on cylinder boundary.

Disk /dev/mapper/VolGroup-lv_root: 7205 MB, 7205814272 bytes
255 heads, 63 sectors/track, 876 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/VolGroup-lv_swap: 855 MB, 855638016 bytes
255 heads, 63 sectors/track, 104 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


但是磁盘挂载的空间并没有增加
# df -h
Filesystem            SizeUsed Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G579M5.6G10% /
tmpfs               250M   0250M   0% /dev/shm
/dev/vda1             477M   26M427M   6% /boot



因为新增加的空间还没有划分使用。所以要继续分区:
# fdisk /dev/vda

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): p

Disk /dev/vda: 11.8 GB, 11811160064 bytes
16 heads, 63 sectors/track, 22885 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: 0x000099f3

   Device Boot      Start         End      Blocks   IdSystem
/dev/vda1   *         3      1018      512000   83Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       <font color="#ff0000">16645</font>   7875584   8eLinux LVM
Partition 2 does not end on cylinder boundary.

Command (m for help):<font color="#ff0000"> n</font>
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): <font color="#ff0000">3</font>
First cylinder (1-22885, default 1): <font color="#ff0000">16646</font>
Last cylinder, +cylinders or +size{K,M,G} (16646-22885, default 22885):
Using default value 22885

Command (m for help): p

Disk /dev/vda: 11.8 GB, 11811160064 bytes
16 heads, 63 sectors/track, 22885 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: 0x000099f3

   Device Boot      Start         End      Blocks   IdSystem
/dev/vda1   *         3      1018      512000   83Linux
Partition 1 does not end on cylinder boundary.
/dev/vda2            1018       16645   7875584   8eLinux LVM
Partition 2 does not end on cylinder boundary.
<font color="#ff0000">/dev/vda3         16646       22885   3144960   83Linux</font>

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.


然后再把这个/dev/vda3 加入到lvm里面去:

ls/dev/vda3 如果没有这个分区,需要重启一下。

# pvcreate /dev/vda3 创建物理卷
<span >Physical volume "/dev/vda3" successfully created</span>


# pvs 查看
PV         VG       FmtAttr PSize PFree
/dev/vda2 <font color="#ff0000"> VolGroup</font> lvm2 a--7.51g    0
/dev/vda3         lvm2 ---3.00g 3.00g


# vgextend VolGroup /dev/vda3 加入到VolGroup
Volume group "VolGroup" successfully extended


# vgs
VG       #PV #LV #SN Attr   VSize<font color="#ff0000">VFree</font>
VolGroup   2   2   0 wz--n- 10.50g <font color="#ff0000">3.00g</font>


# lvs
LV      VG       Attr       LSize   Pool Origin Data%Meta%Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao----   6.71g
lv_swap VolGroup -wi-ao---- 816.00m


# lvextend -l +100%FREE /dev/VolGroup/lv_root
Size of logical volume VolGroup/lv_root changed from 6.71 GiB (1718 extents) to 9.71 GiB (2485 extents).
Logical volume lv_root successfully resized


# df -h
Filesystem            SizeUsed Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G618M5.6G10% /
tmpfs               250M   0250M   0% /dev/shm
/dev/vda1             477M   26M427M   6% /boot


# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/lv_root to 2544640 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 2544640 blocks long.


# df -h
Filesystem            SizeUsed Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                     <font color="#ff0000"> 9.5G</font>618M8.4G   7% /
tmpfs               250M   0250M   0% /dev/shm
/dev/vda1             477M   26M427M   6% /boot


另外,如果是增加磁盘,思路是:
创建磁盘: qemu-img create -f raw/data/kvm/test03_2.img 5G
关闭虚拟机: virsh destroy test03
编辑配置文件: virsh edit test03增加如下:
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/data/kvm/test03_2.img'/>
<target dev='vdb' bus='virtio'/>
</disk>


开启虚拟机:virsh start test03
进入虚拟机:virsh console test03
分区: fdisk /dev/vdb
格式化 (略) mkfs.ext4 /dev/vdb
挂载 (略) vi /etc/fstab
proc                  /proc                   proc    defaults      0 0
/dev/vdb                /mnt                  ext4    defaults      0 0

当然也可以按照上面的思路把 /dev/vdb1 加入到 lvm里面去


b. qcow2格式
步骤基本上和raw一样。

恢复删除掉的
virsh dumpxml aming5 > /etc/libvirt/qemu/aming1.xml
173vi /etc/libvirt/qemu/aming1.xml
175virsh list --all
178virsh define /etc/libvirt/qemu/aming1.xml
179virsh list
180virsh list --all
181virsh start aming1
页: [1]
查看完整版本: 5. 磁盘扩容