Ubuntu20安装KVM,创建cloud镜像虚拟机

安装kvm

apt update &&apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils cloud-utils virtinst -y

创建用户

sudo adduser `id -un` libvirt
sudo adduser `id -un` kvm

创建桥接网卡

nano /etc/netplan/50-cloud-init.yaml
network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
            dhcp6: true
            match:
                macaddress: c6:fb:0f:c3:59:6d
            set-name: eth0
    bridges:
      vmbr0:
        interfaces: [eth0]
        dhcp4: true
        dhcp6: true
~                                                                                                                       ~                             
#先测试是否可以
netplan try 
#输出成功,则应用配置
netplan apply

创建KVM所需要的文件夹

#创建iso仓库
mkdir /var/kvm/iso -p
#创建镜像长裤
mkdir /var/kvm/images

创建一个cloud-init USER-DATA配置文件

cat > /var/kvm/iso/cloud-init.cfg << EOF
#cloud-config
hostname: ubuntu
manage_etc_hosts: true
user: root
disable_root: False
password: rootroot                                               
chpasswd:
  expire: False
packages:
  - qemu-guest-agent

EOF

生成cloud drive

cloud-localds  /var/kvm/iso/cloud.img /var/kvm/iso/cloud-init.cfg

下载ubuntu-cloud镜像到iso仓库,这里自己可以上传镜像。

wget -P /var/kvm/images/ http://10.13.14.22/bionic-server-cloudimg-amd64.img

创建虚拟机

virt-install \
--name ubuntu \
--os-type Linux --os-variant ubuntu18.04 \
--memory 1024 \
--vcpus 2 \
--network bridge=vmbr0,model=virtio \
--cdrom=/var/kvm/iso/cloud.img \
--disk /var/kvm/images/bionic-server-cloudimg-amd64.img,bus=virtio \
--graphics=none \
--noautoconsole 

查看虚拟机

root@kvm:/var/kvm/iso# virsh list
 Id   Name     State
------------------------
 4    ubuntu   running

利用virsh console命令进入虚拟机串口控制台

virsh console 4

Author: 佛西

Leave a Reply

Your email address will not be published.