#pve
#初始化
init.sh
# 修改 pve 源
mv /etc/apt/sources.list /etc/apt/sources.list.bak
cat << EOF > /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
# deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
# deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware
EOF
sed -i 's|http://download.proxmox.com|https://mirrors.ustc.edu.cn/proxmox|g' /usr/share/perl5/PVE/APLInfo.pm
mv /etc/apt/sources.list.d/pve-enterprise.list /etc/apt/sources.list.d/pve-enterprise.list.bak
cat << EOF > /etc/apt/sources.list.d/pve-no-subscription.list
deb http://mirrors.ustc.edu.cn/proxmox/debian bookworm pve-no-subscription
EOF
apt update
# apt upgrade -y
systemctl restart pvedaemon.service
# 时间同步
# 查看可用时区
timedatectl set-timezone "Asia/Shanghai"
apt install -y systemd-timesyncd
cat << EOF > /etc/systemd/timesyncd.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details.
[Time]
#NTP=
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
#ConnectionRetrySec=30
#SaveIntervalSec=60
NTP=ntp.ntsc.ac.cn cn.ntp.org.cn
EOF
timedatectl set-ntp true
systemctl restart systemd-timesyncd
systemctl enable systemd-timesyncd#删除 local-lvm 合并至 local 分区
lvremove pve/data
lvextend -rl +100%FREE pve/root
resize2fs /dev/mapper/pve-root
# 然后在网页上删除 local-lvm#硬盘直通
ls -i /dev/disk/by-id/
# 找到想要的 ID
qm set 100 --ide1 /dev/disk/by-id/ata-ST2000LM015-2E8174_ZDZ1KP7W#复制虚拟机
copy_pve.sh
#!/bin/bash
# 模板 VM ID
TEMPLATE_ID=101
# 需要克隆的pve虚拟机个数
CLONE_COUNT=$1
# 虚拟机当前已经存在的最大编号,分配的ip与该字段有关
PVE_ID=$2
if [ ! -n "$CLONE_COUNT" ] || [ ! -n "$PVE_ID" ]; then
echo "CLONE_COUNT 或 PVE_ID 为空"
exit 1
fi
# 获取当前最大 VM ID
current_max_id=$(pvesh get /cluster/resources --type vm |awk -F'qemu' '{print $2}'|awk '{print $1}'|awk -F'/' '{print $2}'|awk 'NF'|tail -n 1)
# 开始批量克隆
for i in $(seq 1 "$CLONE_COUNT"); do
# 自增获取新的 VM ID
new_id=$((current_max_id + i))
new_pve_id=$((PVE_ID + i))
# 克隆虚拟机
qm clone "$TEMPLATE_ID" "$new_id" --name "vm-${new_pve_id}" --full
echo "克隆完成: 新虚拟机 ID 为 $new_id"
# 为虚拟机新增一个 20GB 的硬盘设备
#qm set "$new_id" --scsi1 local-lvm:20G
#echo "已为虚拟机 $new_id 添加新的 20GB 硬盘设备"
# 获取 MAC 地址
mac_address=$(qm config "$new_id" | grep -i "net0" | grep -oE "([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}")
if [ -z "$mac_address" ];then
echo "请检查虚拟机${new_id}是否创建"
exit 0
fi
done
#不使用 swap
echo "vm.swappiness = 0" >> /etc/sysctl.conf
sysctl -w vm.swappiness=0#制作cloud-init模板
这里以debian为例
云镜像下载地址
#制作镜像
apt-get update && apt-get install -y libguestfs-tools axel qemu-utils
docker run --rm -it --network host -v ./:/app -w /app buyfakett/libguestfs-tools
virt-customize -a debian-13-genericcloud-amd64-src.qcow2 \
--smp 4 --verbose \
--timezone "Asia/Shanghai" \
--append-line "/etc/default/grub:# disables OS prober to avoid loopback detection which breaks booting" \
--append-line "/etc/default/grub:GRUB_DISABLE_OS_PROBER=true" \
--run-command "update-grub" \
--run-command "systemctl enable serial-getty@ttyS1.service" \
--run-command "sed -i 's|Types: deb deb-src|Types: deb|g' /etc/apt/sources.list.d/debian.sources" \
--run-command "sed -i 's|generate_mirrorlists: true|generate_mirrorlists: false|g' /etc/cloud/cloud.cfg.d/01_debian_cloud.cfg" \
--update --install "sudo,qemu-guest-agent,spice-vdagent,bash-completion,unzip,wget,curl,axel,net-tools,iputils-ping,iputils-arping,iputils-tracepath,most,screen,less,vim,htop,dnsutils,zstd,telnet" \
--run-command "apt-get -y autoremove --purge && apt-get -y clean" \
--append-line "/etc/systemd/timesyncd.conf:NTP=time.apple.com time.windows.com" \
--delete "/var/log/*.log" \
--delete "/var/lib/apt/lists/*" \
--delete "/var/cache/apt/*" \
--truncate "/etc/apt/mirrors/debian.list" \
--append-line "/etc/apt/mirrors/debian.list:https://mirrors.tuna.tsinghua.edu.cn/debian" \
--truncate "/etc/apt/mirrors/debian-security.list" \
--append-line "/etc/apt/mirrors/debian-security.list:https://mirrors.tuna.tsinghua.edu.cn/debian-security" \
--truncate "/etc/machine-id"
virt-sparsify --compress debian-13-genericcloud-amd64.qcow2 debian-13-genericcloud-amd64.qcow2#创建模板
qm create 100001 \
--cpu cputype=host \
--name "debian-13-cloud-template" \
--scsi2 "ssd_pool:cloudinit" \
--serial0 socket \
--scsihw virtio-scsi-pci \
--net0 virtio,bridge=vmbr1 \
--agent 1 \
--ostype l26 \
--memory 1024
qm importdisk 100001 "/root/debian-13-genericcloud-amd64.qcow2" ssd_pool -format qcow2
qm set 100001 --scsi0 "ssd_pool:vm-100001-disk-0,discard=on,ssd=1"
qm set 100001 --boot order=scsi0
qm set 100001 --ipconfig0 ip=dhcp
qm cloudinit dump 100001 user
qm cloudinit dump 100001 network
qm template 100001#克隆模板
qm clone 100001 100002 \
--name "debian-13-cloud-vm" \
--full 1 \
--storage "ssd_pool"
qm resize 100002 scsi0 +2G
qm set 100002 --ciuser root --cipassword ".#123abc"