#!/bin/bash
# 通用配置
PASSWORD=”Digital!!29″
STORAGE=”local-lvm”
BRIDGE=”vmbr0″
MEMORY_LINUX=”2048″
MEMORY_WINDOWS=”4096″
CORES_LINUX=”2″
CORES_WINDOWS=”4″
DISK_SIZE_LINUX=”40G”
DISK_SIZE_WINDOWS=”80G”
ISO_DIR=”/var/lib/vz/template/iso”
CLOUDINIT_DIR=”/var/lib/vz/snippets”
# 创建ISO和Cloud-Init目录
mkdir -p $ISO_DIR
mkdir -p $CLOUDINIT_DIR
# 获取当前最大VM ID
get_next_vmid() {
local next_id=$(($(qm list | awk ‘NR>1 {print $1}’ | sort -n | tail -n 1) + 1))
echo $next_id
}
# 下载ISO镜像函数
download_iso() {
local url=$1
local output=$2
if [ ! -f “$ISO_DIR/$output” ]; then
echo “正在下载 $output…”
wget -O “$ISO_DIR/$output” “$url”
else
echo “$output 已存在,跳过下载。”
fi
}
# 创建Linux Cloud-Init文件
create_linux_cloudinit() {
local vmid=$1
cat > $CLOUDINIT_DIR/${vmid}-user-data <<EOF
#cloud-config
hostname: ${name}
password: ${PASSWORD}
chpasswd: { expire: False }
ssh_pwauth: True
ssh_authorized_keys:
– $(cat ~/.ssh/id_rsa.pub)
EOF
}
# 创建Windows无人值守安装文件
create_windows_autounattend() {
local vmid=$1
cat > $ISO_DIR/autounattend.xml <<EOF
<?xml version=”1.0″ encoding=”utf-8″?>
<unattend xmlns=”urn:schemas-microsoft-com:unattend”>
<settings pass=”oobeSystem”>
<component name=”Microsoft-Windows-Shell-Setup” processorArchitecture=”amd64″ publicKeyToken=”31bf3856ad364e35″ language=”neutral” versionScope=”nonSxS”>
<AutoLogon>
<Password>
<Value>${PASSWORD}</Value>
<PlainText>true</PlainText>
</Password>
<Enabled>true</Enabled>
<Username>Administrator</Username>
</AutoLogon>
<UserAccounts>
<AdministratorPassword>
<Value>${PASSWORD}</Value>
<PlainText>true</PlainText>
</AdministratorPassword>
</UserAccounts>
<OOBE>
<HideEULAPage>true</HideEULAPage>
<NetworkLocation>Work</NetworkLocation>
</OOBE>
</component>
</settings>
</unattend>
EOF
}
# 创建Linux虚拟机函数
create_linux_vm() {
local name=$1
local iso=$2
local vmid=$(get_next_vmid)
create_linux_cloudinit $vmid
qm create $vmid \
–name $name \
–memory $MEMORY_LINUX \
–cores $CORES_LINUX \
–net0 virtio,bridge=$BRIDGE \
–ostype l26 \
–scsihw virtio-scsi-pci \
–scsi0 $STORAGE:$DISK_SIZE_LINUX \
–ide2 $STORAGE:cloudinit \
–boot order=scsi0 \
–cdrom $ISO_DIR/$iso \
–cicustom “user=$CLOUDINIT_DIR/${vmid}-user-data” \
–agent enabled=1
qm start $vmid
echo “$name 虚拟机 (VM ID: $vmid) 创建完成并已启动。”
}
# 创建Windows虚拟机函数
create_windows_vm() {
local name=$1
local iso=$2
local vmid=$(get_next_vmid)
download_iso “https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/latest-virtio/virtio-win.iso” “virtio-win.iso”
create_windows_autounattend $vmid
qm create $vmid \
–name $name \
–memory $MEMORY_WINDOWS \
–cores $CORES_WINDOWS \
–net0 virtio,bridge=$BRIDGE \
–ostype win11 \
–scsihw virtio-scsi-pci \
–scsi0 $STORAGE:$DISK_SIZE_WINDOWS \
–ide2 $STORAGE:cloudinit \
–boot order=scsi0 \
–cdrom $ISO_DIR/$iso \
–ide1 $STORAGE:iso/virtio-win.iso,media=cdrom \
–ide3 $STORAGE:iso/autounattend.xml,media=cdrom \
–agent enabled=1
qm start $vmid
echo “$name 虚拟机 (VM ID: $vmid) 创建完成并已启动。”
}
# 系统选择菜单
echo “请选择要安装的操作系统:”
echo “1) Ubuntu Server 最新版”
echo “2) Debian Server 最新版”
echo “3) CentOS 最新版”
echo “4) Rocky Linux 最新版”
echo “5) AlmaLinux 最新版”
echo “6) Windows 11 Pro 最新版”
echo “7) Windows Server 2022 Datacenter”
echo “8) Windows Server 2024 Datacenter”
echo -n “请输入选项 (1-8): ”
read choice
case $choice in
1) download_iso “https://releases.ubuntu.com/24.04/ubuntu-24.04-live-server-amd64.iso” “ubuntu.iso”; create_linux_vm “Ubuntu-Server” “ubuntu.iso”;;
2) download_iso “https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-12.4.0-amd64-netinst.iso” “debian.iso”; create_linux_vm “Debian-Server” “debian.iso”;;
3) download_iso “https://mirror.centos.org/centos/8.5.2111/isos/x86_64/CentOS-8.5.2111-x86_64-dvd1.iso” “centos.iso”; create_linux_vm “CentOS-Server” “centos.iso”;;
4) download_iso “https://download.rockylinux.org/pub/rocky/9/isos/x86_64/Rocky-9.1-x86_64-dvd.iso” “rocky.iso”; create_linux_vm “Rocky-Server” “rocky.iso”;;
5) download_iso “https://repo.almalinux.org/almalinux/9/isos/x86_64/AlmaLinux-9.1-x86_64-dvd.iso” “almalinux.iso”; create_linux_vm “AlmaLinux-Server” “almalinux.iso”;;
6) download_iso “https://software-download.microsoft.com/db/Win11_Pro_English_x64.iso” “win11pro.iso”; create_windows_vm “Windows11Pro” “win11pro.iso”;;
7) download_iso “https://software-download.microsoft.com/db/Win2022_Datacenter.iso” “win2022.iso”; create_windows_vm “WinServer2022” “win2022.iso”;;
8) download_iso “https://software-download.microsoft.com/db/Win2024_Datacenter.iso” “win2024.iso”; create_windows_vm “WinServer2024” “win2024.iso”;;
*) echo “无效选项,请输入1-8之间的数字。”;;
esac
echo “虚拟机安装完成。”