1、安装桌面
sudo apt install ubuntu-desktop
2、安装KVM
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager -y
启动
sudo systemctl start libvirtd sudo systemctl enable libvirtd
3、验证KVM安装情况
sudo systemctl is-active libvirtd
输出:active
#确定正确加载模块
lsmod |grep kvm
将当前用户加入到 libvirt 和 kvm 用户组
sudo usermod -aG libvirt ubuntu sudo usermod -aG kvm ubuntu
4、配置网桥
删除默认网口
sudo virsh net-destroy default sudo virsh net-undefine default
如果还有,则需要删除
sudo ip link delete virbr0 type brigde sudo ip link delete virbr0-nic
5、修改网络配置文件
sudo vim /etc/netplan/00-installer-config.yaml
network: ethernets: ens33: addresses: [] dhcp4: no bridges: br0: interfaces: [ens33] dhcp4: no addresses: [192.168.1.10/24] gateway4: 192.168.1.1 nameservers: addresses: [192.168.1.1] search: [] version: 2
sudo netplan apply
6、图型界面创建虚拟机
sudo virt-manager
qumu-img命令
qemu-img 是虚拟机的磁盘管理命令,支持非常多的磁盘格式,例如raw qcow2 vdi vmdk等等
qemu-img 命令格式
-qemu-img 命令 参数 块文件名称 大小
常用的命令有
-create 创建一个磁盘
-convert 转换磁盘格式
-info 查看磁盘信息
-resize 扩容磁盘空间
#查看磁盘文件的信息
[root@server02 ~]# qemu-img info /var/lib/libvirt/images/centos6.7.img
[root@server02 images]# qemu-img create -f qcow2 centos6.7-01.img 30G Formatting 'centos6.7-01.img', fmt=qcow2 size=32212254720 encryption=off cluster_size=65536 lazy_refcounts=off
[root@server02 images]# ls centos6.7-01.img centos6.7.img CentOS-6.7-x86_64-bin-DVD1.iso
7、virsh 虚拟机管理
1 列出在运行的虚拟机,所有虚拟机
2 [root@Server01 ~]# virsh list
3 [root@Server01 ~]# virsh list
4 虚拟机启动 停止 重启
5 [root@Server01 ~]# virsh start C7-02
6 Domain C7-02 started
7 [root@Server01 ~]# virsh shutdown C7-01
8 Domain C7-01 is being shutdown
9 强制停止虚拟机
10 [root@Server01 ~]# virsh destroy C7-02
11 Domain C7-02 destroyed
12 挂起或恢复虚拟机
13 [root@server02 ~]# virsh suspend --domain centos6.7
14 Domain centos6.7 suspended
15 [root@server02 ~]# virsh resume --domain centos6.7
16 Domain centos6.7 resumed
17 虚拟机自动启动
18 [root@server02 ~]# virsh autostart --domain centos6.7
19 Domain centos6.7 marked as autostarted
20 [root@server02 ~]# virsh autostart --disable centos6.7
21 根据XML文件创建删除虚拟机
22 [root@Server01 qemu]# virsh undefine C7-03
23 Domain C7-03 has been undefined
24 连接虚拟机的console
25 [root@server02 ~]# virsh console centos6.7
26 注:如果无法使用 console 进入console虚拟,使用下面的命令。在虚拟机中使用
27 改kvm的虚拟机的内核参数
28 [root@centos6 qemu]# grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
29 修改虚拟机的配置文件 需要关机或重启生效
30 [root@server02 ~]# virsh edit --domain centos6.7
31 查看虚拟机网卡信息,
32 [root@server02 ~]# virsh domiflist centos6.7
8、新加磁盘
[root@server02 images]# qemu-img create -f qcow2 centos7-01.img 20G
[root@server02 images]# virsh attach-disk centos6.7 /var/lib/libvirt/images/centos7-01.img vdb --subdriver qcow2 --config Disk attached successfully
9、永久删除磁盘
[root@server02 images]# virsh detach-disk centos6.7 vdb --config