写在前面:这里宿主机是桥接模式,即通过网桥br0和外部机器进行通信。
步骤如下:
# 下载模板镜像
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
# 重命名,移动到/opt目录下
mv -i focal-server-cloudimg-amd64.img /opt/ubuntu-20-server.qcow2
# 定义变量,hostname,登录用户及密码
VM_NAME="ubuntu"
USERNAME="ubuntu"
PASSWORD="password"
# 虚拟机镜像
mkdir /data/kvm/$VM_NAME \
&& sudo qemu-img convert \
-f qcow2 \
-O qcow2 \
/opt/ubuntu-20-server.qcow2 \
/data/kvm/$VM_NAME/$VM_NAME.qcow2
# 设置存储为200G
qemu-img resize \
/data/kvm/$VM_NAME/$VM_NAME.qcow2 200G
# 生成cloud-init.cfg配置文件
sudo echo "#cloud-config
system_info:
default_user:
name: $USERNAME
home: /home/$USERNAME
password: $PASSWORD
chpasswd: { expire: False }
hostname: $VM_NAME
# configure sshd to allow users logging in using password
# rather than just keys
ssh_pwauth: True
" | sudo tee /data/kvm/$VM_NAME/cloud-init.cfg
# 创建包含cloud-init用户数据的iso文件
cloud-localds cloud-init.iso cloud-init.cfg
# 新建ubuntu机器
virt-install \
--name $VM_NAME \
--memory 8192 \
--vcpus=4 \
--disk $VM_NAME.qcow2,device=disk,bus=virtio \
--disk cloud-init.iso,device=cdrom \
--os-type linux \
--os-variant ubuntu19.04 \
--virt-type kvm \
--graphics vnc,listen=0.0.0.0,port=5900 \
--network bridge=br0,model=virtio \
--import
# 通过vnc连接后需要配置网络信息,20.04配置在/etc/netplan/路径下
network:
version: 2
renderer: networkd
ethernets:
enp0:
addresses: [192.168.172.x/24]
gateway4: 192.168.172.1
nameservers:
addresses: [8.8.8.8,114.114.114.114]
# 使之生效
netplan apply
标签:NAME,--,qcow2,虚拟机,kvm,VM,ubuntu,cloud
From: https://www.cnblogs.com/even160941/p/17605840.html