forked from yannbreizh/debian-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreseed-creator.sh
executable file
·290 lines (260 loc) · 6.75 KB
/
preseed-creator.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
290
#!/bin/bash
function usage {
cat <<EOF
Preseed Creator
./preseed_creator.sh [options]
Options:
-i <image.iso> ISO image to preseed. MANDATORY.
-f <preseed_file.cfg> Preseed file. MANDATORY.
-o <preseeded_image.iso> Output preseeded ISO image. Default to "preseed_creator/debian-with-preseed.iso".
-r <pub_root_key.pub> Root SSH public key to add to the initrd (this key will then be retrieved and copied to /root/.ssh/authorized_keys with a dedicated preseed late_command).
-p <pri_root_key.pub> Root SSH private key to add to the initrd (this key will then be retrieved and copied to /root/.ssh/id_rsa with a dedicated preseed late_command).
-a <ansible_key.pub> Ansible SSH key to add to the initrd (this key will then be retrieved and copied to /home/ansible/.ssh/authorized_keys with a dedicated preseed late_command).
-x Use xorriso instead of genisoimage, to create an iso-hybrid.
-h Print this help and exit.
EOF
exit
}
INPUT=""
PRESEED=""
MYPWD=$(pwd)
OUTPUT=""
XORRISO=""
while getopts ":i:o:f:r:p:a:xh" opt; do
case $opt in
i)
INPUT=$OPTARG;;
o)
OUTPUT=$OPTARG;;
f)
PRESEED=$OPTARG;;
r)
ROOTPUBSSHKEY=$OPTARG;;
p)
ROOTPRISSHKEY=$OPTARG;;
a)
ANSIBLESSHKEY=$OPTARG;;
x)
XORRISO='yes';;
h)
usage;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage;;
esac
done
# test arguments consistency
if [[ ! -z $PRESEED ]] # test if the $PRESEED string's size is not zero
then
if [ ${PRESEED:0:1} != / ]
then
PRESEED="${MYPWD}/${PRESEED}"
fi
if [[ ! -e $PRESEED ]] # test if the $PRESEED file exists
then
echo "$PRESEED does not exists. Aborting"
exit 1
fi
if [[ ! -r $PRESEED ]] # test if the $PRESEED file is readable
then
echo "$PRESEED is not readable. Aborting"
exit 1
fi
fi
if [[ ! -z $OUTPUT ]]
then
if [ ${OUTPUT:0:1} != / ]
then
OUTPUT="${MYPWD}/${OUTPUT}"
fi
else
OUTPUT="debian-with-preseed.iso"
fi
if [[ -z $INPUT ]]
then
echo "No ISO image provided. Aborting"
exit 1
else
if [ ${INPUT:0:1} != / ]
then
INPUT="${MYPWD}/${INPUT}"
fi
if [[ ! -e $INPUT ]]
then
echo "$INPUT does not exists. Aborting"
exit 1
fi
if [[ ! -r $INPUT ]]
then
echo "$INPUT is not readable. Aborting"
exit 1
fi
fi
if [[ -z $ROOTPUBSSHKEY ]]
then
echo "No SSH root public key provided."
else
if [ ${ROOTPUBSSHKEY:0:1} != / ]
then
INPUT="${MYPWD}/${ROOTPUBSSHKEY}"
fi
if [[ ! -e $ROOTPUBSSHKEY ]]
then
echo "$ROOTPUBSSHKEY does not exists. Aborting"
exit 1
fi
if [[ ! -r $ROOTPUBSSHKEY ]]
then
echo "$ROOTPUBSSHKEY is not readable. Aborting"
exit 1
fi
fi
if [[ -z $ROOTPRISSHKEY ]]
then
echo "No SSH root private key provided."
else
if [ ${ROOTPRISSHKEY:0:1} != / ]
then
INPUT="${MYPWD}/${ROOTPRISSHKEY}"
fi
if [[ ! -e $ROOTPRISSHKEY ]]
then
echo "$ROOTPRISSHKEY does not exists. Aborting"
exit 1
fi
if [[ ! -r $ROOTPRISSHKEY ]]
then
echo "$ROOTPRISSHKEY is not readable. Aborting"
exit 1
fi
fi
if [[ -z $ANSIBLESSHKEY ]]
then
echo "No SSH ansible key provided."
else
if [ ${ANSIBLESSHKEY:0:1} != / ]
then
INPUT="${MYPWD}/${ANSIBLESSHKEY}"
fi
if [[ ! -e $ANSIBLESSHKEY ]]
then
echo "$ANSIBLESSHKEY does not exists. Aborting"
exit 1
fi
if [[ ! -r $ANSIBLESSHKEY ]]
then
echo "$ANSIBLESSHKEY is not readable. Aborting"
exit 1
fi
fi
mkdir preseed_creator -p
cd preseed_creator
echo "Mount ISO image..."
mkdir loopdir -p
mount -o loop $INPUT loopdir > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Error while mounting the ISO image. Aborting"
exit 1
fi
mkdir cd
echo "Extract ISO image..."
rsync -a -H --exclude=TRANS.TBL loopdir/ cd
echo "Umount ISO image..."
umount loopdir
echo "Decompress initrd..."
mkdir irmod -p
cd irmod
gzip -d < ../cd/install.amd/initrd.gz | cpio --extract --make-directories --no-absolute-filenames 2>/dev/null
if [ $? -ne 0 ]
then
echo "Error while getting ../cd/install.amd/initrd.gz content. Aborting"
exit 1
fi
echo "Change linux boot menu..."
cd ../cd/isolinux
chmod +w isolinux.cfg
sed -i "s/timeout 0/timeout 10/g" ./isolinux.cfg
chmod -w isolinux.cfg
chmod +w menu.cfg
cat << EOF > ./menu.cfg
menu hshift 7
menu width 61
menu title Orange CDN cPoP
include stdmenu.cfg
include x86menu.cfg
default cpop
label cpop
menu label ^Orange cPoP autoinstall
menu default
kernel /install.amd/vmlinuz
append vga=788 initrd=/install.amd/initrd.gz preseed/file=/preseed.cfg grub-installer/bootdev="/dev/sda" --- quiet
EOF
chmod -w menu.cfg
cd ../../irmod
echo "Add the preseed file to the initrd..."
cp $PRESEED preseed.cfg
mkdir ./custom
echo "Add sudoer ansible nopasswd specifics..."
cat << EOF > ./custom/ansible.sudoers
ansible ALL=(ALL) NOPASSWD: ALL
EOF
if [[ ! -z $ROOTPUBSSHKEY ]]
then
echo "Add the root SSH public key to the initrd..."
cp $ROOTPUBSSHKEY ./custom/root_pub_key.pub
fi
if [[ ! -z $ROOTPRISSHKEY ]]
then
echo "Add the root SSH private key to the initrd..."
cp $ROOTPRISSHKEY ./custom/root_pri_key.pub
fi
if [[ ! -z $ANSIBLESSHKEY ]]
then
echo "Add the ansible SSH key to the initrd..."
cp $ANSIBLESSHKEY ./custom/ansible_key.pub
fi
echo "Recompress the initrd..."
find . | cpio -H newc --create 2>/dev/null | gzip -9 > ../cd/install.amd/initrd.gz 2>/dev/null
if [ $? -ne 0 ]
then
echo "Error while putting new content into ../cd/install.amd/initrd.gz. Aborting"
exit 1
fi
cd ../
rm -rf irmod/
echo "Fix md5sums..."
cd cd
md5sum `find -follow -type f 2>/dev/null` > md5sum.txt 2>/dev/null
if [ $? -ne 0 ]
then
echo "Error while fixing md5sums. Aborting"
exit 1
fi
cd ..
echo "Create preseeded ISO image for LEGACY BIOS mode..."
if [[ -z $XORRISO ]]
then
genisoimage -quiet -o $OUTPUT -r -J -no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/boot.cat ./cd > /dev/null 2>&1
else
xorriso -as mkisofs \
-quiet \
-o $OUTPUT \
-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
-c isolinux/boot.cat \
-b isolinux/isolinux.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
-eltorito-alt-boot \
-e boot/grub/efi.img \
-no-emul-boot \
-isohybrid-gpt-basdat \
./cd /dev/null 2>$1
fi
if [ $? -ne 0 ]
then
echo "Error while creating the preseeded ISO image. Aborting"
exit 1
fi
cd ..
rm -rf preseed_creator/
echo "Preseeded ISO image created at $OUTPUT"