kvm虚拟机添加虚拟磁盘

1. 概述

本篇博客介绍为kvm虚拟机添加虚拟磁盘的操作,以便在日常工作学习使用。

涉及到的命令包括:qemu-img,virsh-edit,virsh attach-disk

qemu-img:用于创建虚拟磁盘块文件

virsh attch-disk:用于临时添加,重启虚拟机后失效

virsh-edit:用于修改虚拟机xml文件,修改后永久生效

sed:用于批量快速处理

2. 试验过程

首先创建一个kvm虚拟机node32,操作系统centos7.9

通过qemu-img创建一个虚拟磁盘:

#选择一个虚拟磁盘放置目录
cd /bootstrap/kvm/disks/ 
# 创建 100G虚拟磁盘,镜像文件名称kvm_disk_001.img,虚拟化磁盘格式raw
qemu-img create -f raw kvm_disk_001.img 100G
#Formatting 'kvm_disk_001.img', fmt=raw size=107374182400 

2.1 临时添加和卸载

将虚拟磁盘临时添加到虚拟机node32

virsh  attach-disk node32 $(pwd)/kvm_disk_001.img  vda
#成功附加磁盘
virsh  detach-disk node32   vda
#成功分离磁盘

登陆node32,执行lsblk可以查看到/dev/vda

2.2 永久添加

virsh destroy node32,先关闭虚拟机

virsh edit node32,在<disk>标签下,继续添加新的

<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/bootstrap/kvm/disks/kvm_disk_001.img'/>
  <target dev='vda' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x01' slot='0x07' function='0x0'/>
</disk>

需要注意的是,需要根据实际情况调整pci 标签的bus和slot值,避免与现有设备冲突

virsh start node32 开机后,登陆虚拟机lsblk可以查看到/dev/vda

2.3 快速添加多快磁盘

给虚拟机node32添加6块虚拟磁盘。virsh destroy node32

通过shell的for循环产生新的虚拟磁盘,或者操作多个虚拟机。

把下面内容保存为/etc/libvirt/qemu/add_disk.xml

然后执行:sed -i '/\/disk/r /etc/libvirt/qemu/all_disk.xml' node11.xml ,重新加载配置文件virsh define /etc/libvirt/qemu//node32.xml

<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost000.img'/>
  <target dev='vdb' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x14' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost001.img'/>
  <target dev='vdc' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x24' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost002.img'/>
  <target dev='vdd' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x34' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost003.img'/>
  <target dev='vde' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x44' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost004.img'/>
  <target dev='vdf' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x54' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/var/lib/libvirt/images/ost005.img'/>
  <target dev='vdg' bus='virtio'/>
  <shareable/>
  <address type='pci' domain='0x0000' bus='0x64' slot='0x00' function='0x0'/>
</disk>

注意:<shareable/>标签表示磁盘共享,即多个虚拟机添加此虚拟磁盘时,都可以看到此盘。常用于高可用场景。

3. 帮助信息

virsh attach-disk --help

  NAME
    attach-disk - 附加磁盘设备

  SYNOPSIS
    attach-disk <domain> <source> <target> [--targetbus <string>] [--driver <string>] [--subdriver <string>] [--iothread <string>] [--cache <string>] [--io <string>] [--type <string>] [--mode <string>] [--sourcetype <string>] [--serial <string>] [--wwn <string>] [--rawio] [--address <string>] [--multifunction] [--print-xml] [--persistent] [--config] [--live] [--current]

  DESCRIPTION
    附加新磁盘设备.

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    [--source] <string>  磁盘设备源
    [--target] <string>  磁盘设备目标
    --targetbus <string>  target bus of disk device
    --driver <string>  磁盘设备驱动
    --subdriver <string>  磁盘设备副驱动
    --iothread <string>  IOThread to be used by supported device
    --cache <string>  磁盘设备的缓存模式
    --io <string>    io policy of disk device
    --type <string>  目标设备类型
    --mode <string>  设备读写模式
    --sourcetype <string>  源类型  (block|file)
    --serial <string>  磁盘设备序列号
    --wwn <string>   磁盘设备的 wwn
    --rawio          需要 rawio 容量
    --address <string>  磁盘设备地址
    --multifunction  在指定地址中使用多功能 pci
    --print-xml      输出 XML 文档而不是附加该磁盘
    --persistent     让实时更改持久
    --config         影响下一次引导
    --live           影响运行的域
    --current        影响当前域
posted @ 2022-11-25 10:40  liwldev  阅读(184)  评论(0编辑  收藏  举报