和的主机热添加硬盘

和的主机热添加硬盘,第1张

Xen和KVM的guest主机热添加硬盘

或者XenKVM联机主机缺盘空,需要放大硬盘,无法重启。此时,需要热添加来扩展磁盘空。


说明:Xen和KVM主机是CentOS6.4,来宾磁盘是lvm格式。

先说Xen。 *** 作是一样的,在虚拟机上测试。

Xen的访客主机扩展根分区:

# xm list                     #test2主机增大磁盘空间 Name                                        ID   Mem VCPUs      State   Time(s) Domain-0                                     0  1024     4     r-----     80.8 test2                                        1   768     4     -b----     25.8   # qemu-img info test2.qcow2   #现在为10G的磁盘 p_w_picpath: test2.qcow2 file format: raw virtual size: 10G (10739318784 bytes)   disk size: 1.5G   # qemu-img create -f qcow2 -o size=5G,preallocation=metadata /data/testadd.qcow2  #新建一块5G的磁盘 Formatting '/data/testadd.qcow2', fmt=qcow2 size=5368709120 encryption=off  cluster_size=65536 preallocation='metadata' #防止出错,把preallocation=metadata去掉,或者qemu-img create -f raw /data/testadd.img 5G   # qemu-img info testadd.qcow2 #查看新建的磁盘 p_w_picpath: testadd.qcow2 file format: qcow2 virtual size: 5.0G (5368709120 bytes) disk size: 912K cluster_size: 65536   # virsh attach-disk test2 /data/testadd.qcow2 xvdb --cache=writeback --subdriver=qcow2   #对test2主机进行添加,xvdb Disk attached successfully   # vim test2          #修改guest的配置文件,添加xvdb name = "test2" memory = "768" disk = [ "file:/data/test2.qcow2,xvda,w","file:/data/testadd.qcow2,xvdb,w" ] vif = [ "bridge=br0" ] vcpus = 4 on_reboot = "restart" on_crash = "destroy"

下一步是进入guest虚拟机,并首先对其进行分区

# fdisk -l       #查看有没有刚添加的xvdb Disk /dev/xvdb: 5369 MB, 5369757696 bytes   255 heads, 63 sectors/track, 652 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    # fdisk /dev/xvdb             #进行分区,lvm格式 Command (m for help): n Command action    e   extended    p   primary partition (1-4) p   Partition number (1-4): 1 First cylinder (1-652, default 1):  Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652):  Using default value 652   Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM)   Command (m for help): p   Disk /dev/xvdb: 5369 MB, 5369757696 bytes 255 heads, 63 sectors/track, 652 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: 0xb1ef0943       Device Boot      Start         End      Blocks   Id  System /dev/xvdb1               1         652     5237158+  8e  Linux LVM   Command (m for help): w The partition table has been altered!   Calling ioctl() to re-read partition table. Syncing disks.

Lvm磁盘分区

[root@localhost ~]# partx -a /dev/xvdb BLKPG: Device or resource busy error adding partition 1 [root@localhost ~]# kpartx /dev/xvdb xvdb1 : 0 10474317 /dev/xvdb 63 [root@localhost ~]# partx -a /dev/xvdb1 /dev/xvdb [root@localhost ~]# pvcreate /dev/xvdb1    Physical volume "/dev/xvdb1" successfully created    [root@localhost ~]# vgs             #现在为10G的磁盘   VG       #PV #LV #SN Attr   VSize VFree   VolGroup   1   2   0 wz--n- 9.51g    0     [root@localhost ~]# vgextend VolGroup /dev/xvdb1    Volume group "VolGroup" successfully extended    [root@localhost ~]# vgs              #增大了5G的空间          VG       #PV #LV #SN Attr   VSize  VFree   VolGroup   2   2   0 wz--n- 14.50g 4.99g    [root@localhost ~]# lvs   LV      VG       Attr      LSize   Pool Origin Data%  Move Log Cpy%Sync Convert   lv_root VolGroup -wi-ao---   8.54g                                                lv_swap VolGroup -wi-ao--- 992.00m                                                 [root@localhost ~]# df -h Filesystem            Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root                       8.5G  695M  7.3G   9% / tmpfs                 371M     0  371M   0% /dev/shm /dev/xvda1            485M   32M  429M   7% /boot   [root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup/lv_root    Extending logical volume lv_root to 13.53 GiB   Logical volume lv_root successfully resized   [root@localhost ~]# 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 3547136 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 3547136 blocks long.   [root@localhost ~]# df -h            #磁盘增加好了 Filesystem            Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root                        14G  698M   12G   6% / tmpfs                 371M     0  371M   0% /dev/shm /dev/xvda1            485M   32M  429M   7% /boot

KVM的
来宾热扩展磁盘空

[root@localhost ~]# virsh list  Id    Name                           State ----------------------------------------------------  1     bbotte                         running   # qemu-img info bbotte.qcow2  p_w_picpath: bbotte.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 1.3G cluster_size: 65536   # qemu-img create -f qcow2 -o size=5G,preallocation=metadata /var/kvm/bbotteadd.qcow2 Formatting '/var/kvm/bbotteadd.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 preallocation='metadata'  #防止出错,把preallocation=metadata去掉,或者qemu-img create -f raw /var/kvm/bbotteadd.img 5G    # virsh attach-disk bbotte /var/kvm/bbotteadd.qcow2 vdb --cache=writeback --subdriver=qcow2 Disk attached successfully   # virsh edit bbotte <devices>     <emulator>/usr/libexec/qemu-kvm</emulator>     <disk type='file' device='disk'>       <driver name='qemu' type='qcow2' cache='writeback'/>       <source file='/var/kvm/bbotte.qcow2'/>       <target dev='vda' bus='virtio'/>       <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>     </disk> ##下面为添加的磁盘文件     <disk type='file' device='disk'>       <driver name='qemu' type='qcow2' cache='writeback'/>       <source file='/var/kvm/bbotteadd.qcow2'/>     #指定路径       <target dev='vdb' bus='virtio'/>              #指定磁盘名称vdb       <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>   #slot和其他的设备序列不要相同,如果相同,保存的时候会有提示    </disk>


进入来宾主机,你会看到如下提示

# pci 0000:00:07.0: BAR 1: assigned [mem 0x30000000-0x30000fff] pci 0000:00:07.0: BAR 1: set to [mem 0x30000000-0x30000fff] (PCI address [0x30000000-0x30000fff] pci 0000:00:07.0: BAR 0: assigned [io  0x1000-0x103f] pci 0000:00:07.0: BAR 0: set to [io  0x1000-0x103f] (PCI address [0x1000-0x103f] pci 0000:00:00.0: no hotplug settings from platform pci 0000:00:00.0: using default PCI settings pci 0000:00:01.0: no hotplug settings from platform pci 0000:00:01.0: using default PCI settings ata_piix 0000:00:01.1: no hotplug settings from platform ata_piix 0000:00:01.1: using default PCI settings piix4_smbus 0000:00:01.3: no hotplug settings from platform piix4_smbus 0000:00:01.3: using default PCI settings virtio-pci 0000:00:03.0: no hotplug settings from platform virtio-pci 0000:00:03.0: using default PCI settings uhci_hcd 0000:00:04.0: no hotplug settings from platform uhci_hcd 0000:00:04.0: using default PCI settings uhci_hcd 0000:00:04.1: no hotplug settings from platform uhci_hcd 0000:00:04.1: using default PCI settings uhci_hcd 0000:00:04.2: no hotplug settings from platform uhci_hcd 0000:00:04.2: using default PCI settings ehci_hcd 0000:00:04.7: no hotplug settings from platform ehci_hcd 0000:00:04.7: using default PCI settings virtio-pci 0000:00:05.0: no hotplug settings from platform virtio-pci 0000:00:05.0: using default PCI settings virtio-pci 0000:00:06.0: no hotplug settings from platform virtio-pci 0000:00:06.0: using default PCI settings pci 0000:00:07.0: no hotplug settings from platform pci 0000:00:07.0: using default PCI settings virtio-pci 0000:00:07.0: enabling device (0000 -> 0003) virtio-pci 0000:00:07.0: PCI INT A -> Link[LNKC] -> GSI 11 (level, high) -> IRQ 11  vdb: unknown partition table

展开磁盘

# df -h Filesystem                    Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root  8.4G  918M  7.1G  12% / tmpfs                         372M     0  372M   0% /dev/shm /dev/vda1                     485M   32M  428M   7% /boot   # fdisk -l Disk /dev/vdb: 5368 MB, 5368709120 bytes 16 heads, 63 sectors/track, 10402 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@localhost ~]# lvs   LV      VG       Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert   lv_root VolGroup -wi-ao---- 8.51g                                                lv_swap VolGroup -wi-ao---- 1.00g                                                 [root@localhost ~]# vgextend VolGroup /dev/vdb   No physical volume label read from /dev/vdb   Physical volume /dev/vdb not found   Physical volume "/dev/vdb" successfully created   Volume group "VolGroup" successfully extended   [root@localhost ~]# lvextend -l +100%FREE /dev/mapper/VolGroup-lv_root   Extending logical volume lv_root to 13.50 GiB   Logical volume lv_root successfully resized    [root@localhost ~]# resize2fs /dev/mapper/VolGroup-lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/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/mapper/VolGroup-lv_root to 3539968 (4k) blocks. The filesystem on /dev/mapper/VolGroup-lv_root is now 3539968 blocks long.    [root@localhost ~]# df -h Filesystem                    Size  Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root   14G  920M   12G   8% / tmpfs                         372M     0  372M   0% /dev/shm /dev/vda1                     485M   32M  428M   7% /boot


扩展磁盘有危险,请根据自己的环境和设置进行调整,先在测试机上 *** 作。



重启模式添加磁盘文件扩展磁盘空:

# qemu-img create -f qcow2 -o size=100G,preallocation=metadata /var/xen/centos3.qcow2   # qemu-img info centos3.qcow2 p_w_picpath: centos3.qcow2 file format: qcow2 virtual size: 100G (107374182400 bytes) disk size: 49G cluster_size: 65536 #已经是100G空间的guest主机,下面再增大100G空间   # qemu-img resize centos3.qcow2 +100G                 #增加100G空间 # qemu-img info centos3.qcow2 p_w_picpath: centos3.qcow2 file format: qcow2 virtual size: 200G (214748364800 bytes) disk size: 49G cluster_size: 65536   #img格式增大硬盘容量 dd if=/dev/zero bs=8k count=125000 >> /data/centos65.img 125000+0 records in 125000+0 records out 1024000000 bytes (1.0 GB) copied, 14.1461 s, 72.4 MB/s

然后进入来宾主机,按照Xen主机扩展磁盘的方式添加。

您还可以进入来宾主机,对新添加的磁盘文件进行分区,创建新文件夹,并挂载它们。

# fdisk -l # fdisk /dev/xvdb # partx -a /dev/xvdb BLKPG: Device or resource busy error adding partition 1 # kpartx /dev/xvdb xvdb1 : 0 4294961622 /dev/xvdb 63 # partx -a /dev/xvdb1 /dev/xvdb # mkfs.ext4 /dev/xvdb1 # mkdir /data # vim /etc/fstab /dev/xvdb1 /data ext4 defaults 0 0 # mount -a # df -h


另外,qcow2文件只能增不能减,lvm磁盘格式没有ext


增加磁盘容量的方法很灵活,要谨慎[/s2/]

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/783577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存