1. 程式人生 > >給kvm虛擬機器增加磁碟

給kvm虛擬機器增加磁碟

一,virsh attach-disk子命令

[[email protected] src]# virsh help attach-disk

NAME

attach-disk - attach disk device

 

SYNOPSIS

attach-disk <domain> <source> <target> [--targetbus <string>] [--driver <string>] [--subdriver <string>] [--iothread <string>] [--cache <string>] [--type <string>] [--mode <string>] [--so

urcetype <string>] [--serial <string>] [--wwn <string>] [--rawio] [--address <string>] [--multifunction] [--print-xml] [--persistent] [--config] [--live] [--current]

DESCRIPTION

Attach new disk device.

 

OPTIONS

[--domain] <string> domain name, id or uuid

[--source] <string> source of disk device

[--target] <string> target of disk device

--targetbus <string> target bus of disk device

--driver <string> driver of disk device

--subdriver <string> subdriver of disk device

--iothread <string> IOThread to be used by supported device

--cache <string> cache mode of disk device

--type <string> target device type

--mode <string> mode of device reading and writing

--sourcetype <string> type of source (block|file)

--serial <string> serial of disk device

--wwn <string> wwn of disk device

--rawio needs rawio capability

--address <string> address of disk device

--multifunction use multifunction pci under specified address

--print-xml print XML document rather than attach the disk

--persistent make live change persistent

--config affect next boot

--live affect running domain

--current affect current domain

 

attach-disk domain source target [[[--live] [--config] | [--current]] | [--persistent]] [--targetbus bus] [--driver driver] [--subdriver subdriver] [--iothread iothread] [--cache cache] [--type type] [--mode mode] [--sourcetype sourcetype] [--serial serial] [--wwn wwn] [--rawio] [--address address] [--multifunction] [--print-xml]

Attach a new disk device to the domain.

source is path for the files and devices.

target controls the bus or device under which the disk is exposed to the guest OS. It indicates the "logical" device name; 指定在客戶機中顯示的裝置名稱

targetbus attribute specifies the type of disk device to emulate; possible values are driver specific, with typical values being ide, scsi, virtio, xen, usb, sata, or sd, if omitted, the bus type is inferred from the style of the device name (e.g. a device named 'sda' will typically be exported using a SCSI bus).

driver can be file, tap or phy for the Xen hypervisor depending on the kind of access; or qemu for the QEMU emulator.(如果你使用的是qemu模擬器,則這裡的driver值為qemu) Further details to the driver can be passed using subdriver. For Xen subdriver can be aio, while for QEMU subdriver should match the format of the disk source, such as raw or qcow2.(QEMU的subdriver應該匹配磁碟原始檔的型別,比如raw或qcow2) Hypervisor default will be used if subdriver is not specified. However, the default may not be correct, esp. for QEMU as for security reasons it is configured not to detect disk formats.

type can indicate lun, cdrom or floppy as alternative to the disk default, although this use only replaces the media within the existing virtual cdrom or floppy device; consider using update-device for this usage instead.

mode can specify the two specific mode readonly or shareable.

sourcetype can indicate the type of source (block|file)

cache can be one of "default", "none", "writethrough", "writeback", "directsync" or "unsafe".

iothread is the number within the range of domain IOThreads to which this disk may be attached

(QEMU only).

serial is the serial of disk device.

wwn is the wwn of disk device.

rawio indicates the disk needs rawio capability.

address is the address of disk device in the form of pci:domain.bus.slot.function, scsi:controller.bus.unit, ide:controller.bus.unit or ccw:cssid.ssid.devno. Virtio-ccw devices must have their cssid set to 0xfe.

multifunction indicates specified pci address is a multifunction pci device address.

 

If --print-xml is specified, then the XML of the disk that would be attached is printed instead.

 

If --live is specified, affect a running domain. If --config is specified, affect the next startup of a persistent domain. If --current is specified, affect the current

domain state. Both --live and --config flags may be given, but --current is exclusive. When no flag is specified legacy API is used whose behavior depends on the hypervisor

driver.

For compatibility purposes, --persistent behaves like --config for an offline domain, and like --live --config for a running domain. Likewise, --shareable is an alias for

--mode shareable.

二,實際操作

未新增硬碟之前,到虛擬機器中檢視fdisk -l,確定只有一塊vda;

# cd /iptv/src

建立新的虛擬磁碟:

# qemu-img create -f qcow2 -o size=2G ./add_disk.img

新增虛擬磁碟到指定的虛擬機器:

# virsh attach-disk --domain template --source /iptv/src/add_disk.img --target vdb --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent

注意:這裡的--source後面必須跟絕對路徑;

引數說明:

--persistent:會讓此操作同時對執行的虛擬機器以及虛擬機器的xml檔案都會生效;

--target:設定在虛擬機器中此裝置的裝置檔名稱,最好在新增硬碟之前到虛擬機器中先檢視確認一下磁碟裝置名稱;然後此處設定名稱順延,比如我的虛擬機器中原來只有一塊盤,其裝置檔名為vda,所以我這裡設定為vdb。

--targetbus:用來設定客戶機磁碟的匯流排型別;

--driver:根據模擬器而定,如果模擬器是qemu則此處的值只能是qemu;

--subdriver:磁碟源的型別;

--sourcefile:磁碟源的大類,這裡用的是file,也可以使用block;

檢視虛擬機器的配置資訊:

# virsh dumpxml template

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2' cache='none'/>

<source file='/iptv/src/template-centos7-3-1611.img'/>

<backingStore/>

<target dev='vda' bus='virtio'/>

<alias name='virtio-disk0'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

</disk>

<disk type='file' device='disk'>----------此塊硬碟是剛才新增的

<driver name='qemu' type='qcow2' cache='none'/>

<source file='/iptv/src/add_disk.img'/>

<backingStore/>

<target dev='vdb' bus='virtio'/>

<alias name='virtio-disk1'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>

</disk>

編輯虛擬機器配置檔案:

# virsh edit template

<disk type='file' device='disk'>

<driver name='qemu' type='qcow2' cache='none'/>

<source file='/iptv/src/template-centos7-3-1611.img'/>

<target dev='vda' bus='virtio'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

</disk>

<disk type='file' device='disk'>---------已經永久生效了

<driver name='qemu' type='qcow2' cache='none'/>

<source file='/iptv/src/add_disk.img'/>

<target dev='vdb' bus='virtio'/>

<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>

</disk>

 

新增磁碟之後,到虛擬機器中檢視:

# fdisk -l

磁碟 /dev/vda:25.8 GB, 25769803776 位元組,50331648 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/物理):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁碟標籤型別:dos

磁碟識別符號:0x0000bb01

 

裝置 Boot Start End Blocks Id System

/dev/vda1 * 2048 411647 204800 83 Linux

/dev/vda2 411648 2508799 1048576 82 Linux swap / Solaris

/dev/vda3 2508800 41943039 19717120 8e Linux LVM

/dev/vda4 41943040 50331647 4194304 5 Extended

/dev/vda5 41945088 50331647 4193280 8e Linux LVM

 

磁碟 /dev/mapper/cl-root:24.5 GB, 24477958144 位元組,47808512 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/物理):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

 

磁碟 /dev/vdb:2147 MB, 2147483648 位元組,4194304 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/物理):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

 

然後對vdb磁碟進行分割槽格式化掛載,使用就行了;