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        影響當前域
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章