# 下载原始镜像
wget https://cloud-images.ubuntu.com/releases/jammy/release/ubuntu-22.04-server-cloudimg-amd64.img
cp ubuntu-22.04-server-cloudimg-amd64.img ubuntu2204.img
# 硬盘大小加20G
qemu-img resize ubuntu2204.img +20G
# 创建初始化脚本ubuntu2204.sh,内容如下
cat >ubuntu2204.sh <<EOF
systemctl disable apt-daily.timer
systemctl disable apt-daily.service
systemctl disable apt-daily-upgrade.timer
systemctl disable apt-daily-upgrade.service
rm /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
apt purge -y snapd cloud-init
ufw enable
useradd -m -s /bin/bash ubuntu
echo ubuntu:ubuntu | chpasswd
chage -d 0 ubuntu
echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers
echo 'PermitRootLogin no' >>/etc/ssh/sshd_config
ssh-keygen -A
echo 'Acquire::HTTP::Proxy "http://172.26.1.7:3142";' >/etc/apt/apt.conf.d/01proxy
echo 'Acquire::HTTPS::Proxy "http://172.26.1.7:3142";' >>/etc/apt/apt.conf.d/01proxy
apt update
apt install -y qemu-guest-agent
systemctl enable qemu-guest-agent.service
EOF
> 以上脚本内容可根据实际情况修改
# 使用脚本初始化模板
sudo virt-customize -v -x -a ubuntu2204.img --network --run ubuntu2204.sh
# 导入基础镜像,制作出模板镜像
virt-install --os-variant ubuntu22.04 \
--name ubuntu2204 \
--memory 4096 \
--vcpus 2 \
--network bridge=default \
--disk ubuntu2204.img,device=disk,bus=virtio \
--graphics none \
--import \
--noreboot