關聯塊設備到虛擬機
阿新 • • 發佈:2018-06-04
kvm關聯新的塊設備到KVM的虛擬機,運行以下命令
(1)創建個1GB的鏡像文件
root@kvm:~# dd if=/dev/zero of=/tmp/new_disk.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 0.670831 s, 1.6 GB/s
(2)關聯1GB的鏡像作為KVM實例的一個新的磁盤
root@kvm:~# virsh attach-disk kvm1 /tmp/new_disk.img vda --live
Disk attached successfully
(3)通過Console品連接KVM的實例
root@kvm:~# virsh console kvm1
Connected to domain kvm1
Escape character is ^]
Debian GNU/Linux 8 debian ttyS0
debian login: root
Password:
...
(4)顯示kernel 環的buffer信息和查檢新塊的設備
root@debian:~# dmesg | grep vda [ 3664.134978] sd 2:0:2:0: [vda] 2097152 512-byte logical blocks: (1.07 GB/1.00 GiB) [ 3664.135248] sd 2:0:2:0: [vda] Write Protect is off [ 3664.135251] sd 2:0:2:0: [vda] Mode Sense: 63 00 00 08 [ 3664.135340] sd 2:0:2:0: [vda] Write cache: enabled, read cache: enabled, doesn‘t support DPO or FUA [ 3664.138254] vda: unknown partition table [ 3664.139008] sd 2:0:2:0: [vda] Attached SCSI disk
(5)檢查這個新的塊設備
root@debian:~# fdisk -l /dev/vda
Disk /dev/vda: 1 GiB, 1073741824 bytes, 2097152 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
(6)顯示Host OS的實例配置信息
root@kvm:~# virsh dumpxml kvm1 <domain type=‘kvm‘ id=‘23‘> ... <devices> <emulator>/usr/bin/qemu-system-x86_64</emulator> <disk type=‘file‘ device=‘disk‘> <driver name=‘qemu‘ type=‘raw‘/> <source file=‘/tmp/kvm1.img‘/> <target dev=‘hda‘ bus=‘ide‘/> <alias name=‘ide0-0-0‘/> <address type=‘drive‘ controller=‘0‘ bus=‘0‘ target=‘0‘ unit=‘0‘/> </disk> <disk type=‘file‘ device=‘disk‘> <driver name=‘qemu‘ type=‘raw‘/> <source file=‘/tmp/new_disk.img‘/> <target dev=‘vda‘ bus=‘scsi‘/> <alias name=‘scsi0-0-2‘/> <address type=‘drive‘ controller=‘0‘ bus=‘0‘ target=‘0‘ unit=‘2‘/> </disk> </devices> </domain>
(7) 獲取這個新磁盤的詳細信息
root@kvm:~# virsh domblkstat kvm1 vda
vda rd_req 119
vda rd_bytes 487424
vda wr_req 0
vda wr_bytes 0
vda flush_operations 0
vda rd_total_times 29149092
vda wr_total_times 0
vda flush_total_times 0
(8)分離磁盤
root@kvm:~# virsh detach-disk kvm1 vda --live
Disk detached successfully
(9)復制或創建新的raw鏡像
root@kvm:~# cp /tmp/new_disk.img /tmp/other_disk.img
(10)寫下的配置文件
root@kvm:~# cat other_disk.xml
<disk type=‘file‘ device=‘disk‘>
<driver name=‘qemu‘ type=‘raw‘ cache=‘none‘/>
<source file=‘/tmp/other_disk.img‘/>
<target dev=‘vdb‘/>
</disk>
(11)關聯新的設備
root@kvm:~# virsh attach-device kvm1 --live other_disk.xml
Device attached successfully
(12)分離塊設備
root@kvm:~# virsh detach-device kvm1 other_disk.xml --live
Device detached successfully
關聯塊設備到虛擬機