Skip to content

Instantly share code, notes, and snippets.

@7h3730B
Last active January 30, 2024 10:21
Show Gist options
  • Save 7h3730B/e0dc270d66abefa06822392e27df503b to your computer and use it in GitHub Desktop.
Save 7h3730B/e0dc270d66abefa06822392e27df503b to your computer and use it in GitHub Desktop.
create_template
#!/bin/bash
set -xeu
export IMGID=9007
export BUILD_DAY="20240102-1614"
export BASE_IMG="debian-12-genericcloud-amd64-${BUILD_DAY}.qcow2"
export IMG="debian-12-genericcloud-amd64-${IMGID}.qcow2"
export STORAGEID="local"
if [ ! -f "${BASE_IMG}" ];then
wget https://cloud.debian.org/images/cloud/bookworm/${BUILD_DAY}/${BASE_IMG}
fi
if [ ! -f "${IMG}" ];then
cp -f "${BASE_IMG}" "${IMG}"
fi
# if [ ! -f "/mnt/tmp" ];then
# mkdir /mnt/tmp
# fi
# prepare mounts
guestmount -a ${IMG} -m /dev/sda1 /mnt/tmp/
mount --bind /dev/ /mnt/tmp/dev/
mount --bind /proc/ /mnt/tmp/proc/
# add dns server
rm /mnt/tmp/etc/resolv.conf
echo "nameserver 1.1.1.1" > /mnt/tmp/etc/resolv.conf
echo "nameserver 8.8.4.4" >> /mnt/tmp/etc/resolv.conf
echo "151.101.246.132 deb.debian.org" >> /mnt/tmp/etc/hosts
# install desired apps
chroot /mnt/tmp /bin/bash -c "apt-get update"
chroot /mnt/tmp /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get install -y net-tools curl qemu-guest-agent nfs-common open-iscsi lsscsi sg3-utils multipath-tools scsitools"
# https://www.electrictoolbox.com/sshd-hostname-lookups/
sed -i 's:#UseDNS no:UseDNS no:' /mnt/tmp/etc/ssh/sshd_config
sed -i '/package-update-upgrade-install/d' /mnt/tmp/etc/cloud/cloud.cfg
cat > /mnt/tmp/etc/cloud/cloud.cfg.d/99_custom.cfg << '__EOF__'
#cloud-config
# Install additional packages on first boot
#
# Default: none
#
# if packages are specified, this apt_update will be set to true
#
# packages may be supplied as a single package name or as a list
# with the format [<package>, <version>] wherein the specifc
# package version will be installed.
#packages:
# - qemu-guest-agent
# - nfs-common
ntp:
enabled: true
datasource_list: [ NoCloud, ConfigDrive ]
__EOF__
cat > /mnt/tmp/etc/multipath.conf << '__EOF__'
defaults {
user_friendly_names yes
find_multipaths yes
}
__EOF__
# enable services
chroot /mnt/tmp systemctl enable open-iscsi.service || true
chroot /mnt/tmp systemctl enable multipath-tools.service || true
# copy ssh keys
cp -a --force /root/.ssh/authorized_keys /mnt/tmp/root/.ssh/authorized_keys
# umount everything
umount /mnt/tmp/dev
umount /mnt/tmp/proc
umount /mnt/tmp
# create template
qm create ${IMGID} --memory 512 --net0 virtio,bridge=vmbr0
qm importdisk ${IMGID} ${IMG} ${STORAGEID} --format qcow2
qm set ${IMGID} --scsihw virtio-scsi-pci --scsi0 ${STORAGEID}:$IMGID/vm-$IMGID-disk-0.qcow2
qm set ${IMGID} --ide2 ${STORAGEID}:cloudinit
qm set ${IMGID} --boot c --bootdisk scsi0
qm set ${IMGID} --serial0 socket --vga serial0
qm template ${IMGID}
# set host cpu, ssh key, etc
qm set ${IMGID} --scsihw virtio-scsi-pci
qm set ${IMGID} --cpu host
qm set ${IMGID} --agent enabled=1
qm set ${IMGID} --autostart
qm set ${IMGID} --onboot 1
qm set ${IMGID} --ostype l26
qm set ${IMGID} --ipconfig0 "ip=dhcp"

Now you can just create new cluters with it, make sure to select rke1 and NOT rke2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment