需求,给你的kvm机器增加500g的neicun
1.vmware增家50g的硬盘
2.重启vm虚拟机
- mkfs.xfs /dev/sdb
- mkdir /new_data
- mount -t xfs /dev/sdb /new_data
- #将虚拟机的磁盘放到/new_data下
- 1先给虚拟机guanji
- [root@localhost ~]# virsh shutdown linux0224_cento7
- Domain linux0224_cento7 is being shutdown
-
- #挪磁盘
- [root@localhost ~]# mv /data/* /new_data
- #修修改虚拟机的配置,识别新磁盘的路径
- [root@localhost ~]# virsh edit linux0224_cento7
- 32
- 33
- 34
/usr/libexec/qemu-kvm - 35
type='file' device='disk'> - 36
'qemu' type='raw'/> - 37 <source file='/data/linux0224_cento7.raw'/>
- 38
'vda' bus='virtio'/> - 39 type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
- 找到37行,更改一下,开机即可
-
-
- #虚拟机改名
- [root@localhost ~]# virsh list --all
- Id Name State
- ----------------------------------------------------
- - linux0224_cento7 shut off
-
-
- [root@localhost ~]# virsh domrename linux0224_cento7
- error: command 'domrename' requires
option - [root@localhost ~]# virsh domrename linux0224_cento7 centos7
- Domain successfully renamed
-
- [root@localhost ~]# virsh list
- Id Name State
- ----------------------------------------------------
-
- [root@localhost ~]# virsh list --all
- Id Name State
- ----------------------------------------------------
- - centos7 shut off
-
-
-
- #磁盘格式转换
- /data/linux0224_cento7.raw
-
- raw缺点
- 占空间
- 不支持快照
- qcow2
- 省空间
- 支持快照
-
-
-
- virsh命令是管理虚拟机的
- qemu-img是管理底层虚拟化设备的,如查看信心
-
- [root@localhost ~]# qemu-img info /data/linux0224_cento7.raw
- image: /data/linux0224_cento7.raw
- file format: raw
- virtual size: 5.0G (5368709120 bytes)
- disk size: 1.6G
- #更改文件类型 给他3g的存储空间
- [root@localhost ~]# qemu-img create -f qcow2 /data/linux0224_cento7.qcow2 3G
- Formatting '/data/linux0224_cento7.qcow2', fmt=qcow2 size=3221225472 encryption=off cluster_size=65536 lazy_refcounts=off
-
-
- 给3G感觉太少了,多给4G,只能增加不能减少
- [root@localhost ~]# qemu-img resize /data/linux0224_cento7.qcow2 +4G
- Image resized.
-
-
- #镜像转换,将raw镜像换成 qcow 格式,需要先关机
- -f指定源格式 -O指定输出格式
- convert,转换
- [root@localhost ~]# qemu-img convert -f raw -O qcow2 /data/linux0224_cento7.raw /data/linux0224_cento7.qcow2
- 修改磁盘的配置文件,读取新的磁盘格式
-
- virsh edit centos7
- 35
type='file' device='disk'> - 36
'qemu' type='qcow2'/> - 37 <source file='/data/linux0224_cento7.qcow2'/>
-
- #访问测试
- [root@localhost ~]# virsh domifaddr centos7
- Name MAC address Protocol Address
- -------------------------------------------------------------------------------
- vnet0 52:54:00:f2:60:4e ipv4 192.168.122.218/24
-
-
- [root@localhost ~]# ssh root@192.168.122.218
-
- root@192.168.122.218's password:
- Last login: Sun Jun 2 12:07:42 2024
- [root@localhost ~]#
- [root@localhost ~]# hostname -I
- 192.168.122.218
-
3.添加虚拟磁盘
- 1,进入磁盘目录创建一个新的硬盘
- -f qcow2 更省空间,支持快照
- [root@localhost ~]# qemu-img create -f qcow2 /data/centos7 5G
- Formatting '/data/centos7', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off
-
- #额外在添加一块盘,无需在关机
- attch 附属
- --live 激活他
- --cache=none 要不要无所谓,不要缓存
- vdb 磁盘名字
- attach-disk 临时热添加磁盘
- [root@localhost ~]# virsh attach-disk centos7 /data/centos7 vdb --live --cache=none --subdriver=qcow2
- Disk attached successfully
-
- 删除磁盘
- [root@localhost ~]# virsh detach-disk centos7 vdb
- Disk detached successfully
-
-
-
- 永久化添加kvm磁盘
- [root@localhost ~]# qemu-img create -f qcow2 /data/centos7 5G
- [root@localhost ~]# virsh attach-disk centos7 /data/centos7 vdb --live --cache=none --subdriver=qcow2
- [root@localhost data]# virsh dumpxml centos7 > /opt/lastest_centos7.xml
- [root@localhost data]# virsh define /opt/lastest_centos7.xml
- Domain centos7 defined from /opt/lastest_centos7.xml
-
- 在关机重启测试一下
-
-
创建快照
- 创建虚拟机快照
- --name init-ok就是给这快照起一个名字
- [root@localhost data]# virsh snapshot-create-as --name init-ok centos7
- Domain snapshot init-ok created
-
- #查看有哪些快照
- [root@localhost data]# virsh snapshot-list centos7
- Name Creation Time State
- ------------------------------------------------------------
- init-ok 2024-06-05 20:49:03 +0800 running
-
-
-
- 恢复快照
- --snapshotname init-ok 指定恢复快照的名称
- [root@localhost data]# virsh snapshot-revert centos7 --snapshotname init-ok
-
-
- 免密登录到虚拟机
- [root@localhost data]# ssh-keygen -t rsa
- [root@localhost data]# ssh-copy-id root@ip
-
-
-
- 删除快照
- [root@localhost data]# virsh snapshot-delete centos7 --snapshotname init-ok
- Domain snapshot init-ok deleted
-
- [root@localhost data]# virsh snapshot-list centos7
- Name Creation Time State
- ------------------------------------------------------------
-
kvm克隆
- 克隆时必须关机
-
- [root@localhost data]# virsh shutdown centos7
- Domain centos7 is being shutdown
-
- --auto-clone从原始客户机配置自动生成克隆名称以及存储路径
- -o指定原有的虚拟机
- -n指定新的虚拟机的名字
-
- [root@localhost data]# virt-clone --auto-clone -o centos7 -n new_centos7
- Allocating 'linux0224_cento7-clone.qcow2' | 5.0 GB 00:00:03
- Allocating 'new_centos7' | 5.0 GB 00:00:00
-
- Clone 'new_centos7' created successfully.
-
-
- [root@localhost data]# virsh list --all
- Id Name State
- ----------------------------------------------------
- - centos7 shut off
- - new_centos7 shut off
-
-
- 修改端口
- [root@localhost data]# virsh vncdisplay centos7
- :0
-
- virsh edit centos7
- 105
type='vnc' port='-1' autoport='yes' listen='0.0.0.0'> 把autoport改为no port改称你想要端口 比如port='5' - 106
type='address' address='0.0.0.0'/> - 107
-
-
- [root@localhost data]# virsh define /etc/libvirt/qemu/new_centos7.xml
-
- 在关机重启即可
- virsh shutdown centos7
- virsh start centos7
添加虚拟网桥
- 此时在kvm虚拟机里面跑了一个nginx服务,想在宿主机访问,显示没有路由
- [root@test3 ~]# virsh domifaddr centos7
- Name MAC address Protocol Address
- -------------------------------------------------------------------------------
- vnet0 52:54:00:24:be:9e ipv4 192.168.122.232/24
-
- [root@test3 ~]# curl 192.168.122.232
- curl: (7) Failed connect to 192.168.122.232:80; No route to host
-
-
- #创建一个虚拟网桥,和ens33做一个桥接
- [root@test3 ~]# virsh iface-bridge ens33 kvm-bridge0
- Created bridge kvm-bridge0 with attached device ens33
-