KVM虚拟机添加磁盘

未添加硬盘之前,到虚拟机中查看 fdisk -l ,确定只有一块vda

[[email protected]01 rules.d]# fdisk -l

磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b337a

   设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 104857599 51379200 8e Linux LVM

磁盘 /dev/mapper/rhel-root:47.2 GB, 47240445952 字节,92266496 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/rhel-swap:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[[email protected]-oracle-01 rules.d]# 

在宿主机上进行操作

[[email protected] images]# cd /var/lib/libvirt/images

创建新的虚拟磁盘:

[[email protected]04 images]# qemu-img create -f qcow2 -o size=4G /var/lib/libvirt/images/ehs-rac-01-b.qcow2
Formatting ‘/var/lib/libvirt/images/ehs-rac-01-b.qcow2‘, fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off
[[email protected]-as-04 images]# qemu-img create -f qcow2 -o size=4G /var/lib/libvirt/images/ehs-rac-01-c.qcow2
Formatting ‘/var/lib/libvirt/images/ehs-rac-01-c.qcow2‘, fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off
[[email protected]-as-04 images]# qemu-img create -f qcow2 -o size=4G /var/lib/libvirt/images/ehs-rac-01-d.qcow2
Formatting ‘/var/lib/libvirt/images/ehs-rac-01-d.qcow2‘, fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off
[[email protected]-as-04 images]# qemu-img create -f qcow2 -o size=40G /var/lib/libvirt/images/ehs-rac-01-e.qcow2
Formatting ‘/var/lib/libvirt/images/ehs-rac-01-e.qcow2‘, fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
[[email protected]-as-04 images]# qemu-img create -f qcow2 -o size=20G /var/lib/libvirt/images/ehs-rac-01-f.qcow2
Formatting ‘/var/lib/libvirt/images/ehs-rac-01-f.qcow2‘, fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off 

[[email protected]-as-04 images]# ll
总用量 127525264
-rw------- 1 qemu qemu 21478375424 9月   9 17:47 ehs-jboss-01.qcow2
-rw------- 1 qemu qemu  4642897920 9月   9 17:47 ehs-jboss-02.qcow2
-rw------- 1 qemu qemu  3873898496 9月   9 17:46 ehs-mq-01.qcow2
-rw------- 1 qemu qemu  3718119424 9月   9 17:43 ehs-mq-02.qcow2
-rw-r--r-- 1 root root      197120 9月   6 16:20 ehs-oracle-01-a.img
-rw-r--r-- 1 root root      197120 9月   6 16:21 ehs-oracle-01-b.img
-rw-r--r-- 1 root root      197120 9月   6 16:21 ehs-oracle-01-c.img
-rw-r--r-- 1 root root      197120 9月   6 16:21 ehs-oracle-01-d.img
-rw-r--r-- 1 root root      197120 9月   6 16:21 ehs-oracle-01-e.img
-rw------- 1 root root 53695545344 9月   9 15:42 ehs-oracle-01.qcow2
-rw------- 1 root root  3783524352 9月   9 15:42 ehs-oracle-02.qcow2
-rw-r--r-- 1 root root      197120 9月   9 17:46 ehs-rac-01-b.qcow2
-rw-r--r-- 1 root root      197120 9月   9 17:46 ehs-rac-01-c.qcow2
-rw-r--r-- 1 root root      197120 9月   9 17:46 ehs-rac-01-d.qcow2
-rw-r--r-- 1 root root      197120 9月   9 17:46 ehs-rac-01-e.qcow2
-rw-r--r-- 1 root root      197120 9月   9 17:46 ehs-rac-01-f.qcow2
-rw------- 1 qemu qemu 36369137664 9月   9 17:42 ehs-rac-01.qcow2
-rw------- 1 qemu qemu  4957863936 9月   9 17:47 ehs-rac-02.qcow2

添加虚拟磁盘到指定的虚拟机:

[[email protected]04 images]# virsh attach-disk --domain ehs-rac-01 --source /var/lib/libvirt/images/ehs-rac-01-b.qcow2 --target sdb --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
成功附加磁盘

[[email protected]-as-04 images]# virsh attach-disk --domain ehs-rac-01 --source /var/lib/libvirt/images/ehs-rac-01-c.qcow2 --target sdc --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
成功附加磁盘

[[email protected]-as-04 images]# virsh attach-disk --domain ehs-rac-01 --source /var/lib/libvirt/images/ehs-rac-01-d.qcow2 --target sdd --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
成功附加磁盘

[[email protected]-as-04 images]# virsh attach-disk --domain ehs-rac-01 --source /var/lib/libvirt/images/ehs-rac-01-e.qcow2 --target sde --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
成功附加磁盘

[[email protected]-as-04 images]# virsh attach-disk --domain ehs-rac-01 --source /var/lib/libvirt/images/ehs-rac-01-f.qcow2 --target sdf --targetbus virtio --driver qemu --subdriver qcow2 --sourcetype file --cache none --persistent
成功附加磁盘

注意:这里的 --source 后面必须跟绝对路径;

参数说明:

--persistent:会让此操作同时对运行的虚拟机以及虚拟机的xml文件都会生效;

--target:设定在虚拟机中此设备的设备文件名称,最好在添加硬盘之前到虚拟机中先查看确认一下磁盘设备名称;然后此处设置名称顺延,比如我的虚拟机中原来只有一块盘,其设备文件名为vda,所以我这里设定为vdb。

--targetbus:用来设定客户机磁盘的总线类型;

--driver:根据模拟器而定,如果模拟器是qemu则此处的值只能是qemu;

--subdriver:磁盘源的类型;

--sourcetype:磁盘源的大类,这里用的是file,也可以使用block;

查看虚拟机的配置信息:

[[email protected] images]# virsh dumpxml ehs-rac-01
 ...
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01.qcow2‘/>
      <backingStore/>
      <target dev=‘vda‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk0‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x07‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-a.img‘/>
      <backingStore/>
      <target dev=‘vdb‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk1‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0b‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-b.img‘/>
      <backingStore/>
      <target dev=‘vdc‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk2‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0c‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-c.img‘/>
      <backingStore/>
      <target dev=‘vdd‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk3‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0d‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-d.img‘/>
      <backingStore/>
      <target dev=‘vde‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk4‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0e‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-e.img‘/>
      <backingStore/>
      <target dev=‘vdf‘ bus=‘virtio‘/>
      <alias name=‘virtio-disk5‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0f‘ function=‘0x0‘/>
    </disk>
 ...

编辑虚拟机配置文件:

[[email protected] images]# virsh edit ehs-rac-01
 ...
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01.qcow2‘/>
      <target dev=‘vda‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x07‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-a.img‘/>
      <target dev=‘vdb‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0b‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-b.img‘/>
      <target dev=‘vdc‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0c‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-c.img‘/>
      <target dev=‘vdd‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0d‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-d.img‘/>
      <target dev=‘vde‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0e‘ function=‘0x0‘/>
    </disk>
    <disk type=‘file‘ device=‘disk‘>
      <driver name=‘qemu‘ type=‘qcow2‘ cache=‘none‘/>
      <source file=‘/var/lib/libvirt/images/ehs-oracle-01-e.img‘/>
      <target dev=‘vdf‘ bus=‘virtio‘/>
      <address type=‘pci‘ domain=‘0x0000‘ bus=‘0x00‘ slot=‘0x0f‘ function=‘0x0‘/>
    </disk>
 ...

添加磁盘之后,到虚拟机中查看:

[[email protected]01 rules.d]# fdisk -l

磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b337a

   设备 Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 104857599 51379200 8e Linux LVM

磁盘 /dev/mapper/rhel-root:47.2 GB, 47240445952 字节,92266496 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/mapper/rhel-swap:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vdb:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vdc:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vdd:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vde:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

磁盘 /dev/vdf:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

然后对磁盘进行分区格式化挂载,使用就行了;

原文地址:https://www.cnblogs.com/opma/p/11606973.html

时间: 2024-08-11 03:30:14

KVM虚拟机添加磁盘的相关文章

KVM 虚拟机增加磁盘

背景: 由于在KVM虚机上做glusterfs测试,发现KVM虚拟存储空间不够. 思路:创建一个kvm img 文件指定大小200G,然后通过Virtual Machine Manager导入到对应的虚拟机 步骤: 这里以虚拟机vm2为例 创建kvm img [[email protected] 2]cd /d1/virtual_machine/vm/2  #进入虚机文件目录 [[email protected] 2]# qemu-img create vm2-1.img 200G Format

kvm虚拟机扩展磁盘空间

kvm虚拟机磁盘空间扩展与xen虚拟机磁盘空间扩展思路一致.原因在于xen/kvm默认的虚拟机磁盘格式为raw,所以方式可以通用. raw磁盘格式扩展思路如下 (1) 新添加一块raw格式的磁盘加入到KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间. (2) 直接通过dd命令扩展现有虚拟机磁盘大小,扩展之后,原磁盘大小增大,进入虚拟机系统后,直接通过fdisk分区新添加的空间,然后将该分区并入lvm逻辑卷中,扩大磁盘空间. xen虚拟化实战系列(四)之xen虚拟机扩展磁

exsi中的虚拟机添加磁盘后虚拟机中磁盘不出现

exsi中的虚拟机添加磁盘后虚拟机中磁盘不出现解决: 计算机---> 管理: 这里可以选择磁盘,格式,分区, 改盘符等操作

VM虚拟机添加磁盘不重启系统,识别磁盘,添加到分区

VM虚拟机添加磁盘不重启系统,识别磁盘将其添加到分区 //首先找到您的主机总线编号 # grep mpt /sys/class/scsi_host/host?/proc_name /sys/class/scsi_host/host0/proc_name:mptspi //使用下面命令扫描SCSI总线# echo "- - -" > /sys/class/scsi_host/host0/scan # fdisk -l //创建分区 # fdisk /dev/sdc p //查看当前

烂泥:为KVM虚拟机添加网卡

本文首发于烂泥行天下. 前几篇文章介绍了有关KVM安装虚拟机以及如何给虚拟机添加硬盘,今天我们再来介绍下有关如何给KVM虚拟机添加网卡. 给KVM虚拟机添加网卡,可以分为两种形式:图形界面的和virsh attach-interface命令的. 图形界面的很简单,现在已虚拟机centos2为例:如下图操作: 注意该图中的MAC地址 more /etc/udev/rules.d/70-persistent-net.rules 现在上图,我们可以看到新增加的网卡,在系统中显示的是eth1,并且MAC

Linux虚拟机添加磁盘

1.虚拟机添加磁盘 2.查看添加完成磁盘 3.创建分区 4.对分区进行格式化 5.挂载 创建挂载目录mkdir /mnt/sdb1 (目录自定义)mount -t auto /dev/sdb1 /mnt/sdb1 设置自动挂载 备注:1.本文针对虚拟进系统进行测试实验,如有问题,请及时反馈. 原文地址:http://blog.51cto.com/sirstma/2113308

virsh命令添加kvm虚拟机共享磁盘

在使用kvm虚拟机的时候,有时候部署一些应用如Oracle RAC.corosync等高可用的时候,往往会用到共享硬盘.比如本人在部署corosync+mysql高可用的时候就遇到了一些小问题(像我们这种折腾达人,肯定不会用nfs做共享存储),因此记录一下. 环境: 宿主机当然是linux,否则也用不到kvm 没有图形界面,否则直接virt-manager也很快.. 操作过程 新建一块虚拟盘 [email protected]:~# qemu-img -f raw shared.img 10G要

kvm虚拟机映射磁盘设备

需求:使用openstack云主机+EMC存储实现oracle RAC 分析:由于openstack的磁盘设备由cinder管理,目前cinder同一个卷不支持同时连接到多台云主机,事实上其他公有云上的云磁盘也暂时不支持.故不能通过cinder添加磁盘到云主机. 临时解决方案: 手动把宿主机的磁盘(使用了multipath映射EMC存储)映射到kvm虚拟机上,命令如下: virsh list virsh attach-disk instance-xxxxxxxx /dev/mapper/mpat

kvm虚拟机扩展磁盘容量

virt-resize扩展 #关闭kvm虚拟机virsh shutdown testkvm2#安装工具yum install -y libguestfs-tools#查看磁盘文件位置virsh edit testkvm2#查看磁盘分区信息virt-filesystems --partitions --long -a /var/lib/libvirt/images/testkvm2.qcow2#创建新的磁盘文件qemu-img create -f qcow2 /var/lib/libvirt/im