forked from hetzneronline/installimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_options.sh
289 lines (267 loc) · 10.3 KB
/
get_options.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
# read config
#. /tmp/install.vars
#
# (c) 2009-2018, Hetzner Online GmbH
#
# check command line params / options
while getopts "han:b:r:l:i:p:v:d:f:c:R:s:z:x:gkK:t:u:G:" OPTION ; do
case $OPTION in
# help
h)
echo
echo "usage: installimage [options]"
echo
echo " without any options, installimage starts in interactive mode."
echo " possible options are:"
echo
echo " -h display this help"
echo
echo " -a automatic mode / batch mode - use this in combination"
echo " with the options below to install without further"
echo " interaction. there will be no further confirmations"
echo " for deleting disks / all your data, so use with care!"
echo
echo " -c <configfile> use the specified config file in"
echo " $CONFIGSPATH for autosetup. when using"
echo " this option, no other options except '-a' are accepted."
echo
echo " -x <post-install> Use this file as post-install script, that will be executed after"
echo " installation inside the chroot."
echo
echo " -n <hostname> set the specified hostNAME."
echo " -r <yes|no> activate software RAID or not."
echo " -l <0|1|5|6|10> set the specified raid LEVEL."
echo " -i <imagepath> use the specified IMAGE to install (full path to the OS image)"
echo " - supported image sources: local dir, ftp, http, nfs"
echo " - supported image types: tar,tar.gz,tar.bz,tar.bz2,tar.xz,tar.zst,tgz,tbz,txz"
echo " examples:"
echo " - local: /path/to/image/filename.tar.gz"
echo " - ftp: ftp://<user>:<password>@hostname/path/to/image/filename.tar.bz2"
echo " - http: http://<user>:<password>@hostname/path/to/image/filename.tbz"
echo " - https: https://<user>:<password>@hostname/path/to/image/filename.tbz"
echo " - nfs: hostname:/path/to/image/filename.tgz"
echo " -g Use this to force validation of the image file with detached GPG signature."
echo " If the image is not valid, the installation will abort."
echo " -p <partitions> define the PARTITIONS to create, example:"
echo " - regular partitions: swap:swap:4G,/:ext3:all"
echo " - lvm setup example: /boot:ext2:256M,lvm:vg0:all"
echo " -v <logical volumes> define the logical VOLUMES you want to be created"
echo " - example: vg0:root:/:ext3:20G,vg0:swap:swap:swap:4G"
echo " -d <drives> /dev names of DRIVES to use, e.g.: sda or sda,sdb"
echo " -f <yes|no> FORMAT the second drive (if not used for raid)?"
echo " -s <de|en> Language to use for different things (e.g.PLESK)"
echo " -z PLESK_<Version> Install optional software like PLESK with version <Version>"
echo " -K <path/url> Install SSH-Keys from file/URL"
echo ' -t <yes|no> Take over rescue system SSH public keys'
echo ' -u <yes|no> Allow usb drives'
echo ' -G <yes|no> Generate new SSH host keys (default: yes)'
echo
exit 0
;;
# config file (file.name)
c)
if [ -e "$CONFIGSPATH/$OPTARG" ] ; then
OPT_CONFIGFILE=$CONFIGSPATH/$OPTARG
elif [ -e "$OPTARG" ] ; then
OPT_CONFIGFILE=$OPTARG
else
msg="=> FAILED: config file $OPT_CONFIGFILE for autosetup not found"
debug "$msg"
echo -e "${RED}$msg${NOCOL}"
exit 1
fi
debug "# use config file $OPT_CONFIGFILE for autosetup"
echo "$OPT_CONFIGFILE" | grep "^/" >/dev/null || OPT_CONFIGFILE="$(pwd)/$OPT_CONFIGFILE"
cp "$OPT_CONFIGFILE" /autosetup
if grep -q PASSWD /autosetup ; then
echo -e "\n\n${RED}Please enter the PASSWORD for $OPT_CONFIGFILE:${NOCOL}"
echo -e "${YELLOW}(or edit /autosetup manually and run installimage without params)${NOCOL}\n"
echo -en "PASSWORD: "
read -s imagepasswd
sed -i /autosetup -e "s/PASSWD/$imagepasswd/"
fi
;;
# post-install file (file.name)
x)
if [ -e "$POSTINSTALLPATH/$OPTARG" ] ; then
OPT_POSTINSTALLFILE=$POSTINSTALLPATH/$OPTARG
elif [ -e "$OPTARG" ] ; then
OPT_POSTINSTALLFILE=$OPTARG
else
msg="=> FAILED: post-install file $OPT_POSTINSTALLFILE not found or not executable"
debug "$msg"
echo -e "${RED}$msg${NOCOL}"
exit 1
fi
debug "# use post-install file $OPT_POSTINSTALLFILE"
echo "$OPT_POSTINSTALLFILE" | grep "^/" >/dev/null || OPT_POSTINSTALLFILE="$(pwd)/$OPT_POSTINSTALLFILE"
ln -sf "$OPT_POSTINSTALLFILE" /post-install
;;
# automatic mode
a) OPT_AUTOMODE=1 ;;
# hostname (host.domain.tld)
n)
OPT_HOSTNAME=$OPTARG
if [ -e /autosetup ]; then
sed -i /autosetup -e "s/HOSTNAME.*/HOSTNAME $OPT_HOSTNAME/"
fi
;;
# raid (on|off|true|false|yes|no|0|1)
r)
case $OPTARG in
off|false|no|0) OPT_SWRAID=0 ;;
on|true|yes|1) OPT_SWRAID=1 ;;
esac
;;
# raidlevel (0|1)
l) OPT_SWRAIDLEVEL=$OPTARG ;;
# image
# e.g.: file.tar.gz | http://domain.tld/file.tar.gz
i)
[ -f "$IMAGESPATH/$OPTARG" ] && OPT_IMAGE="$IMAGESPATH/$OPTARG" || OPT_IMAGE="$OPTARG"
IMAGENAME=$(basename "$OPT_IMAGE")
IMAGENAME=${IMAGENAME/.tar.gz/}
IMAGENAME=${IMAGENAME/.tar.bz/}
IMAGENAME=${IMAGENAME/.tar.bz2/}
IMAGENAME=${IMAGENAME/.tar.xz/}
IMAGENAME=${IMAGENAME/.tar/}
IMAGENAME=${IMAGENAME/.tgz/}
IMAGENAME=${IMAGENAME/.tbz/}
IMAGENAME=${IMAGENAME/.txz/}
IMAGENAME=${IMAGENAME/.bin.bz2/}
IMAGENAME=${IMAGENAME/.bin/}
if [[ "$IMAGENAME" == 'Archlinux-2017-64-minimal' ]] && ! [[ -s "$OPT_IMAGE" ]]; then
IMAGENAME='archlinux-latest-64-minimal'
OPT_IMAGE="$IMAGESPATH$IMAGENAME.tar.gz"
fi
if [[ "$IMAGENAME" == 'Archlinux-latest-64-minimal' ]] && ! [[ -s "$OPT_IMAGE" ]]; then
IMAGENAME='archlinux-latest-64-minimal'
OPT_IMAGE="$IMAGESPATH$IMAGENAME.tar.gz"
fi
;;
# partitions
# e.g.: swap:swap:4G,/boot:ext2:256M,/:ext3:all | /boot:ext2:256M,lvm:vg0:all
p)
OPT_PARTITIONS=$OPTARG
OPT_PARTS=''
OLD_IFS="$IFS"
IFS=","
for part in $OPT_PARTITIONS ; do
OPT_PARTS="$OPT_PARTS\nPART "
IFS=":"
for val in $part ; do
OPT_PARTS="$OPT_PARTS $val "
done
done
IFS="$OLD_IFS"
;;
# logical volumes
# e.g.: vg0:swap:swap:swap:4G,vg0:root:/:ext3:20G,vg0:tmp:/tmp:ext3:5G
v)
OPT_VOLUMES=$OPTARG
OPT_LVS=''
OLD_IFS="$IFS"
IFS=","
for lv in $OPT_VOLUMES ; do
OPT_LVS="$OPT_LVS\nLV "
IFS=":"
for val in $lv ; do
OPT_LVS="$OPT_LVS $val "
done
done
IFS="$OLD_IFS"
;;
# drives
# e.g.: sda,sdb | sda
d)
OPT_DRIVES=$OPTARG
sel_drives="${OPT_DRIVES//,/ }"
i=1
for optdrive in $sel_drives ; do
eval OPT_DRIVE$i="$optdrive"
let i=i+1
done
;;
# format second drive (on|off|true|false|yes|no|0|1)
f)
case $OPTARG in
off|false|no|0) export OPT_FORMATDRIVE2=0 ;;
on|true|yes|1) export OPT_FORMATDRIVE2=1 ;;
esac
;;
s)
export OPT_LANGUAGE="$OPTARG"
;;
z)
export OPT_INSTALL="$OPTARG"
;;
# URL to open after first boot of the new system. Used by the
# Robot for automatic installations.
R)
export ROBOTURL="$OPTARG"
;;
# force signature validating of the image file
g)
export OPT_FORCE_SIGN="1"
;;
K)
if [ "$OPTARG" ]; then
export OPT_SSHKEYS_URL="$OPTARG"
export OPT_USE_SSHKEYS="1"
else
msg="=> FAILED: cannot install ssh-keys without a source"
debug "$msg"
echo -e "${RED}$msg${NOCOL}"
exit 1
fi
;;
t)
if [[ -z "$OPTARG" ]] || [[ "${OPTARG,,}" == 'yes' ]]; then
export OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS='yes'
else
export OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS='no'
fi
;;
u)
[[ -z "$OPTARG" ]] || [[ "${OPTARG,,}" == 'yes' ]] && export ALLOW_USB_DRIVES='1'
;;
G)
if [[ -n "$OPTARG" ]]; then
if [[ "${OPTARG,,}" == 'no' ]]; then
export GENERATE_NEW_SSH_HOST_KEYS=no
else
export GENERATE_NEW_SSH_HOST_KEYS=yes
fi
fi
;;
esac
done
# VALIDATION
if [ "$OPT_AUTOMODE" -a -z "$OPT_IMAGE" -a -z "$OPT_CONFIGFILE" ] ; then
echo -e "\n${RED}ERROR: in automatic mode you need to specify an image and a config file!${NOCOL}\n"
debug "=> FAILED, no image given"
exit 1
fi
if [ "$OPT_USE_SSHKEYS" -a -z "$OPT_SSHKEYS_URL" ]; then
msg="=> FAILED: Should install SSH keys, but key URL not set."
debug "$msg"
echo -e "${RED}$msg${NOCOL}"
exit 1
fi
# DEBUG:
[ "$OPT_CONFIGFILE" ] && debug "# OPT_CONFIGFILE: $OPT_CONFIGFILE"
[ "$OPT_HOSTNAME" ] && debug "# OPT_HOSTNAME: $OPT_HOSTNAME"
[ "$OPT_SWRAID" ] && debug "# OPT_SWRAID: $OPT_SWRAID"
[ "$OPT_SWRAIDLEVEL" ] && debug "# OPT_SWRAIDLEVEL: $OPT_SWRAIDLEVEL"
[ "$OPT_IMAGE" ] && debug "# OPT_IMAGE: $OPT_IMAGE"
[ "$OPT_PARTITIONS" ] && debug "# OPT_PARTITIONS: $OPT_PARTITIONS"
[ "$OPT_VOLUMES" ] && debug "# OPT_VOLUMES: $OPT_VOLUMES"
[ "$OPT_DRIVES" ] && debug "# OPT_DRIVES: $OPT_DRIVES"
[ "$OPT_FORMATDRIVE2" ] && debug "# OPT_FORMATDRIVE2: $OPT_FORMATDRIVE2"
[ "$OPT_INSTALL" ] && debug "# OPT_INSTALL: $OPT_INSTALL"
[ "$OPT_FORCE_SIGN" ] && debug "# OPT_FORCE_SIGN: $OPT_FORCE_SIGN"
[ "$OPT_USE_SSHKEYS" ] && debug "# OPT_USE_SSHKEYS: $OPT_USE_SSHKEYS"
[ "$OPT_SSHKEYS_URL" ] && debug "# OPT_SSHKEYS_URL: $OPT_SSHKEYS_URL"
[ "$OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS" ] && debug "# OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS: $OPT_TAKE_OVER_RESCUE_SYSTEM_SSH_PUBLIC_KEYS"
# vim: ai:ts=2:sw=2:et