虚拟机的磁盘文件(比如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 支持符号链接(通常可行)。