-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
248 lines (214 loc) · 5.87 KB
/
install.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
host_ip_addr=$(hostname -I | awk '{print $1}')
default_storage=$(pvesm status --content rootdir | grep active | cut -d' ' -f1)
default_hostname=rancher.$(hostname -d)
default_id=$(pvesh get /cluster/nextid)
default_bridge=$(brctl show | awk 'NR>1 {print $1}' | grep vmbr | head -n1)
cores=${cores:-4}
memory=${memory:-8192}
swap=${swap:-4096}
firewall=${firewall:-1}
if [ -z "${bridge}" ]; then
bridge=${default_bridge}
fi
ip=${ip:-dhcp}
ip6=${ip6:-}
default_network="name=eth0,firewall=${firewall},bridge=${bridge}"
if [ "${ip}" ]; then
default_network="${default_network},ip=${ip}"
fi
if [ "${ip6}" ]; then
default_network="${default_network},ip6=${ip6}"
fi
if [ -z "${hostname}" ]; then
hostname=${default_hostname}
fi
if [ -z "${network}" ]; then
network=${default_network}
fi
if [ -z "${storage}" ]; then
storage=${default_storage}
fi
if [ -z "${id}" ]; then
id=${default_id}
fi
proxmox_api=${proxmox_api:-1}
proxmox_api_create_user=${proxmox_api_create_user:-1}
if [ "${proxmox_api}" == "1" ]; then
if [ -z "${proxmox_api_host}" ]; then
proxmox_api_host="${host_ip_addr}"
fi
if [ -z "${proxmox_api_username}" ]; then
proxmox_api_username="rancher@pve"
fi
if [ -z "${proxmox_api_password}" ]; then
proxmox_api_password=$(openssl rand -base64 18)
fi
if [ "${proxmox_api_create_user}" == "1" ]; then
if ! pveum user add "${proxmox_api_username}" --password "${proxmox_api_password}"; then
echo "Error creating API user" > /dev/stderr
exit 1
fi
pveum aclmod / -user "${proxmox_api_username}" -role Administrator
fi;
fi;
size=${size:-64}
tretboot_url=${tretboot_url:-https://raw.githubusercontent.com/Deltachaos/tretboot/main/tretboot.yaml}
repository=${repository:-https://github.com/Deltachaos/tretboot-proxmox-lxc-rancher.git}
image=${image:-ubuntu-24.04-standard_24.04-2_amd64.tar.zst}
k3s_version=${k3s_version:-v1.28.10+k3s1}
pveam update
pveam download $storage $image
cat > /etc/modules-load.d/docker.conf <<EOF
aufs
overlay
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
br_netfilter
rbd
options nf_conntrack hashsize=196608
EOF
cat > /etc/sysctl.d/100-docker.conf <<EOF
net.netfilter.nf_conntrack_max=786432
EOF
sysctl net.netfilter.nf_conntrack_max=786432
while read p; do
modprobe "$p"
done </etc/modules-load.d/docker.conf
if pct status $id || qm status $id; then
echo "VM with $id already exists." > /dev/stderr
exit 1
fi
if [ "${configmap_rancher}" ]; then
configmap_rancher="$(sed 's/^/ /' <<EOF
$configmap_rancher
EOF
)"
fi
configmap_proxmox_api=""
if [ "${proxmox_api}" == "1" ]; then
configmap_proxmox_api="$(cat - <<EOF
proxmox:
host: "$proxmox_api_host"
username: "$proxmox_api_username"
password: "$proxmox_api_password"
EOF
)"
fi
configmap_extra_repositories=""
i=0
while true; do
fleet_name=$(eval echo \${local_fleet_${i}_name})
fleet_repo=$(eval echo \${local_fleet_${i}_repo})
fleet_branch=$(eval echo \${local_fleet_${i}_branch:-main})
fleet_path=$(eval echo \${local_fleet_${i}_path:-""})
fleet_auth=$(eval echo \${local_fleet_${i}_auth:-true})
if [ -z "${fleet_repo}" ]; then
break
fi
if [ -z "${fleet_name}" ]; then
break
fi
configmap_extra_repositories="$(cat - <<EOF
${fleet_name}:
repo: "${fleet_repo}"
branch: "${fleet_branch}"
path: "${fleet_path}"
auth: ${fleet_auth}
$configmap_extra_repositories
EOF
)"
((i++))
done
configmap_git_ssh=""
if [ "${fleet_ssh_key}" ]; then
temp_private_key=$(mktemp)
chmod 600 "$temp_private_key"
tee "$temp_private_key" <<EOF
$fleet_ssh_key
EOF
fleet_ssh_key_public=$(ssh-keygen -y -f "$temp_private_key")
fleet_ssh_key_private=$(cat $temp_private_key | awk '{printf "%s\\n", $0}')
rm -f "$temp_private_key"
configmap_git_ssh="$(cat - <<EOF
ssh: {"private": "${fleet_ssh_key_private}","public":"${fleet_ssh_key_public}"}
EOF
)"
fi
pct create $id $storage:vztmpl/$image --cores $cores --memory $memory --swap $swap --rootfs ${storage}:${size} --hostname=$hostname --onboot 1
(cat <<EOF
lxc.apparmor.profile: unconfined
lxc.cgroup2.devices.allow: a
lxc.cap.drop:
lxc.mount.auto: "proc:rw sys:rw"
EOF
) | cat - >> /etc/pve/lxc/$id.conf
pct set $id --net0 $network
if [ "$nameserver" ]; then
pct set $id --nameserver $nameserver
fi
pct start $id
pct exec $id -- mkdir -p /var/lib/rancher/k3s/server/manifests
pct exec $id -- mkdir -p /etc/rancher/k3s
(cat <<EOF
#!/bin/sh -e
if [ ! -e /dev/kmsg ]; then
ln -s /dev/console /dev/kmsg
fi
mount --make-rshared /
EOF
) | pct exec $id -- tee /usr/local/bin/k3s-lxc
pct exec $id -- chmod +x /usr/local/bin/k3s-lxc
(cat <<EOF
[Unit]
Description=Adds k3s compatability
After=basic.target
[Service]
Restart=no
Type=oneshot
ExecStart=/usr/local/bin/k3s-lxc
Environment=
[Install]
WantedBy=multi-user.target
EOF
) | pct exec $id -- tee /etc/systemd/system/k3s-lxc.service
pct exec $id -- systemctl daemon-reload
pct exec $id -- systemctl enable k3s-lxc.service
pct exec $id -- systemctl start k3s-lxc.service
(cat <<EOF
disable:
- servicelb
- traefik
- local-storage
EOF
) | pct exec $id -- tee /etc/rancher/k3s/config.yaml
pct exec $id -- wget -O /var/lib/rancher/k3s/server/manifests/tretboot.yaml $tretboot_url
(cat <<EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
name: tretboot-config
namespace: tretboot
data:
repository: "$repository"
path: "tretboot"
rancher.yaml: |
hostname: $hostname
$configmap_rancher
tretboot-fleet.yaml: |
repository:
url: "$repository"
extraRepositories:
$configmap_extra_repositories
$configmap_proxmox_api
$configmap_git_ssh
EOF
) | pct exec $id -- tee -a /var/lib/rancher/k3s/server/manifests/tretboot.yaml
pct exec $id -- ln -s /usr/local/bin/k3s /usr/bin/k3s
pct exec $id -- ln -s /usr/local/bin/kubectl /usr/bin/kubectl
pct exec $id -- ln -s /usr/local/bin/crictl /usr/bin/crictl
pct exec $id -- /bin/sh -c "wget -O - https://get.k3s.io | INSTALL_K3S_VERSION=${k3s_version} sh -"