给kvm虚拟机增加磁盘

版权声明: https://blog.csdn.net/zhuangzi123456/article/details/81453677

一,virsh attach-disk子命令

[root@localhost 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磁盘进行分区格式化挂载,使用就行了;

猜你喜欢

转载自blog.csdn.net/zhuangzi123456/article/details/81453677