QEMU 虚拟机磁盘迁移

虚拟机的磁盘文件(比如qcow2或raw格式)存放在当前目录,导致该目录所在的磁盘分区空间不足。现将这个磁盘文件迁移到另一个有更多空间的磁盘上。

1. 关闭虚拟机

确保虚拟机完全关闭,避免磁盘文件被占用:

[root@localhost test]# virsh list --all  #查看虚拟化状态
 Id   名称        状态
---------------------------
 2    linux2022   running

[root@localhost test]# virsh shutdown linux2022  #关闭虚拟机
Domain 'linux2022' is being shutdown

[root@localhost test]# virsh destroy linux2022   #强制关闭虚拟机
Domain 'linux2022' destroyed

[root@localhost test]# virsh list --all     #查看虚拟化状态
 Id   名称        状态
------------------------
 -    linux2022   关闭

2. 确认虚拟机磁盘文件路径

查看当前磁盘文件的位置和格式:

[root@localhost test]# virsh dumpxml  linux2022  | grep "source file="
      <source file='/home/test/linux2022.qcow2'/>
[root@localhost test]# 

3. 复制磁盘文件到新位置

将磁盘文件复制到新磁盘(如挂载在 /mnt/的磁盘):

[root@localhost test]# cp /home/test/linux2022.qcow2  /mnt/

4. 修改虚拟机配置

更新虚拟机配置文件,指向新的磁盘路径:

virsh edit linux2022 

在这里插入图片描述

5. 启动虚拟机并验证

[root@localhost test]# virsh start linux2022  #启动虚拟机
Domain 'linux2022' started

[root@localhost test]# virsh list --all
 Id   名称        状态
---------------------------
 3    linux2022   running

[root@localhost ~]# virsh  console linux2022  #进入控制台检查磁盘是否正常
Connected to domain 'linux2022'
Escape character is ^] (Ctrl + ])

​6. 清理旧磁盘文件(可选)​

确认新位置磁盘工作正常后,删除旧文件释放空间:

rm -f /home/test/linux2022.qcow2 

7.​其他方案:符号链接(快速绕过)​

如果不想修改配置文件,可创建符号链接:

mv /home/test/linux2022.qcow2  /mnt/
ln -s /mnt/linux2022.qcow2 /home/test/linux2022.qcow2 

但需确保 QEMU 和 libvirt 支持符号链接(通常可行)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑蛋同志

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值