下载云主机镜像
https://cloud-images.ubuntu.com/releases/
wget https://cloud-images.ubuntu.com/releases/23.10/release-20240307/ubuntu-23.10-server-cloudimg-amd64.img
yum install -y libvirt-client cloud-utils virt-install libguestfs-tools
# 创建模板镜像
qemu-img convert -f qcow2 -O qcow2 ubuntu-23.10-server-cloudimg-amd64.img root-disk.qcow2
# 设置模板镜像对应的虚拟机磁盘大小50G
qemu-img resize root-disk.qcow2 50G
# 配置cloud-init
echo "#cloud-config
system_info:
default_user:
name: ubuntu
home: /home/ubuntu
password: ubuntu
chpasswd: { expire: False }
hostname: ubuntu
# 配置sshd允许使用密码登录
ssh_pwauth: True
" | tee cloud-init.cfg
# 创建iso镜像
cloud-localds cloud-init.iso cloud-init.cfg
# 安装虚拟机
virt-install \
--name test \
--memory 4096 \
--disk root-disk.qcow2,device=disk,bus=virtio \
--disk cloud-init.iso,device=cdrom \
--os-type linux \
--virt-type kvm \
--graphics none \
--network network=default,model=virtio \
--import
# 允许root ssh登录
sudo sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
sudo systemctl restart ssh
# 设置root密码
sudo passwd root
# ctrl+]退出后,测试root登录虚拟机
virsh domifaddr test
ssh root@xxx
shutdown -h now
# 清理后配置已经刷入root-disk.qcow2
virt-sysprep -d test
virsh undefine test
# 压缩root-disk.qcow2
qemu-img convert -f qcow2 -O qcow2 -c root-disk.qcow2 ubuntu-23.10.qcow2
参考资料
https://waynerv.com/posts/create-out-of-box-ubuntu-qcow2-image/
标签:--,qcow2,Ubuntu,ubuntu,镜像,disk,root,cloud From: https://www.cnblogs.com/WJQ2017/p/18069831