-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubuntu.sh
110 lines (87 loc) · 3.08 KB
/
ubuntu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
set -xe
export IMGID=9000
export STORAGEID="local-lvm"
export VLAN_TAG=
export CODENAME=jammy
export BASE_IMG="$CODENAME-server-cloudimg-amd64.img"
export IMG="$CODENAME-server-cloudimg-amd64-${IMGID}.qcow2"
# vlan settings
if [ -z "$VLAN_TAG" ];then
export VLAN_SECTION=
else
export VLAN_SECTION=,tag=$VLAN_TAG
fi
if [ ! -f "${BASE_IMG}" ];then
wget https://cloud-images.ubuntu.com/$CODENAME/current/$CODENAME-server-cloudimg-amd64.img
fi
if [ ! -f "${IMG}" ];then
cp -f "${BASE_IMG}" "${IMG}"
fi
# prepare mounts
mkdir -p /tmp/img/$CODENAME
guestmount -a ${IMG} -m /dev/sda1 /tmp/img/$CODENAME/
mount --bind /dev/ /tmp/img/$CODENAME/dev/
mount --bind /proc/ /tmp/img/$CODENAME/proc/
# get resolving working
mv /tmp/img/$CODENAME/etc/resolv.conf /tmp/img/$CODENAME/etc/resolv.conf.orig
cp -a --force /etc/resolv.conf /tmp/img/$CODENAME/etc/resolv.conf
# install desired apps
chroot /tmp/img/$CODENAME /bin/bash -c "apt-get update"
chroot /tmp/img/$CODENAME /bin/bash -c "DEBIAN_FRONTEND=noninteractive apt-get install -y net-tools curl cloud-initramfs-growroot 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:' /tmp/img/$CODENAME/etc/ssh/sshd_config
sed -i '/package-update-upgrade-install/d' /tmp/img/$CODENAME/etc/cloud/cloud.cfg
cat > /tmp/img/$CODENAME/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 > /tmp/img/$CODENAME/etc/multipath.conf << '__EOF__'
defaults {
user_friendly_names yes
find_multipaths yes
}
__EOF__
# enable services
chroot /tmp/img/$CODENAME systemctl enable open-iscsi.service || true
chroot /tmp/img/$CODENAME systemctl enable multipath-tools.service || true
# restore systemd-resolved settings
mv /tmp/img/$CODENAME/etc/resolv.conf.orig /tmp/img/$CODENAME/etc/resolv.conf
# umount everything
umount /tmp/img/$CODENAME/dev
umount /tmp/img/$CODENAME/proc
umount /tmp/img/$CODENAME
rm -rf /tmp/img/$CODENAME
# create template
qm create ${IMGID} --memory 512 --name ubuntu-${CODENAME} --net0 virtio,bridge=vmbr0${VLAN_SECTION}
qm importdisk ${IMGID} ${IMG} ${STORAGEID} --format qcow2
qm set ${IMGID} --scsihw virtio-scsi-pci --scsi0 ${STORAGEID}:vm-${IMGID}-disk-0
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"
# cleaning up
rm $BASE_IMG
rm $IMG