This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkickstart-usb-creator
executable file
·211 lines (188 loc) · 5.3 KB
/
kickstart-usb-creator
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
#!/bin/bash
#
# Creates usb stick for kickstart installations
script=$(readlink -f "$0")
script_path=$(dirname "$script")
script_name=$(basename "$script")
# shellcheck source=/dev/null
if ! source "$script_path/_functions"; then
echo "Function inclusion failed."
exit 1
fi
# 8GB usb min size limit
# todo determine min usb size based on iso size
device_size_limit=$((3 * 1000 * 1000 * 1000))
function help {
echo
echo "Usage: $scriptName --iso <isoFile> --device </dev/sdX> --ks <ksFile>"
echo
echo "Creates bootable USB stick for kickstart installations."
echo " --iso Path to install .iso file"
echo " --device Use the specified unmounted device as target"
echo " --ks Path to kickstart config"
echo
exit 1
}
function checkOptions {
if [ -z "$ks" ]; then
_error "Missing path to kickstart file."
return 1
fi
if [ -z "$iso" ]; then
_error "Missing path to .iso file."
return 1
fi
if [ -z "$device" ]; then
_error "Missing device."
return 1
fi
}
# process options
while [[ $# -ge 1 ]]; do
key="$1"
shift
case "$key" in
--device)
device="$1"
shift
;;
--iso)
iso="$1"
shift
;;
--ks)
ks="$1"
shift
;;
--help)
help
;;
*)
# unknown
;;
esac
done
# required cmd line tools
_check_tools syslinux mkfs.vfat partprobe
if ! mbr_bin_path=$(_get_mbr_bin_path); then
exit 1
fi
if ! checkOptions; then
help
fi
echo -n "All data on ´$device´ will be deleted, proceed? [y/N] "
read -rn 1 confirm
echo ""
if [ "$confirm" != "y" ]; then
echo ""
exit 1
fi
cleanup() {
_umount "$(_mnt_point "${device}1")"
_umount "$(_mnt_point "${device}2")"
_umount "$(_mnt_point "${iso}")"
}
execute() {
if ! _exec "$*"; then
handle_code 1
fi
}
handle_code() {
local code="$1"
if [ "$code" -ne 0 ]; then
cleanup
exit 1
fi
}
check_device() {
_log "Check device ´$1´"
local device="$1"
local deviceSize
deviceSize=$((512 * $(cat "/sys/block/$(basename "$device")/size")))
if [ "${deviceSize}" -lt "${device_size_limit}" ]; then
_error "Device is too small. Current: ${deviceSize} Expected min: ${device_size_limit}"
handle_code 1
fi
}
prepare_device() {
_log "Prepare device ´$1´"
local device="$1"
_log "Create partitions..."
execute "echo -e 'o\nn\np\n1\n\n+300M\na\nn\np\n2\n\n\nw' | fdisk ${device}"
execute "mkfs.vfat -n BOOT ${device}1"
execute "mkfs.ext4 -L DATA ${device}2"
execute "dd conv=notrunc bs=440 count=1 if=\"$mbr_bin_path\" of=${device}"
execute "syslinux -i ${device}1"
}
prepare_boot() {
_log "Fill boot partition content..."
local device="$1"
local iso_mnt="$2"
device_mnt=$(_mount_device "${device}1")
handle_code $?
## BIOS-Boot
execute "cp $iso_mnt/isolinux/* $device_mnt/"
execute "mv \"$device_mnt/isolinux.cfg\" \"$device_mnt/syslinux.cfg\""
execute "sed -i '0,/.*append.*/s//\ \ append\ biosdevname=0\ net.ifnames=0\ initrd=initrd.img\ method=hd:LABEL=DATA:\/\ ks=hd:LABEL=BOOT:\/biosks.cfg/' \"$device_mnt/syslinux.cfg\""
execute "sed -i 's/^TIMEOUT.*/timeout\ 100/i' \"$device_mnt/syslinux.cfg\""
execute "sed -i 's/menu\ default//i' \"$device_mnt/syslinux.cfg\""
execute "sed -i '/label\ linux/a \ \ menu\ default' \"$device_mnt/syslinux.cfg\""
execute "cp \"$ks\" \"$device_mnt/biosks.cfg\""
execute "cp \"$ks\" \"$device_mnt/efiks.cfg\""
# overwrite .c32
local syslinuxDir=$(dirname "$mbr_bin_path")
execute "cp -f \"$syslinuxDir/../modules/bios/ldlinux.c32\" $device_mnt/"
execute "cp -f \"$syslinuxDir/../modules/bios/libcom32.c32\" $device_mnt/"
execute "cp -f \"$syslinuxDir/../modules/bios/libutil.c32\" $device_mnt/"
execute "cp -f \"$syslinuxDir/../modules/bios/menu.c32\" $device_mnt/"
execute "cp -f \"$syslinuxDir/../modules/bios/vesamenu.c32\" $device_mnt/"
## UEFI-Boot
execute "cp -rf $iso_mnt/EFI $device_mnt/"
execute "cp -rf $iso_mnt/images $device_mnt/"
execute "sed -i 's/set default=\"1\"/set default=\"0\"/g' \"$device_mnt/EFI/BOOT/grub.cfg\""
execute "sed -i 's/set timeout=60/set timeout=5/g' \"$device_mnt/EFI/BOOT/grub.cfg\""
execute "sed -i 's/inst.stage2=hd:LABEL=CentOS\\\x207\\\x20x86_64 quiet/biosdevname=0 net.ifnames=0 initrd=initrd.img method=hd:LABEL=DATA:\/ ks=hd:LABEL=BOOT:\/efiks.cfg/g' \"$device_mnt/EFI/BOOT/grub.cfg\""
cat <<EOF > $device_mnt/startup.nsh
@echo -on
cls
for %m run (0 9)
if exist FS%m:\EFI\BOOT\BOOTX64.EFI then
FS%m:
\EFI\BOOT\BOOTX64.EFI
endif
endfor
for %m run (0 9)
if exist FS%m:\BOOT\BOOTX64.EFI then
FS%m:
\BOOT\BOOTX64.EFI
endif
endfor
EOF
}
prepare_data() {
_log "Fill data partiton content..."
local device="$1"
local iso_mnt="$2"
device_mnt=$(_mount_device "${device}2")
handle_code $?
execute "cp -r $iso_mnt/. \"$device_mnt\""
# sometimes using path to iso in kickstart install works better
execute "cp $iso \"$device_mnt/install.iso\""
}
check_iso_content() {
_log "Check ISO content ´$1´"
local mnt="$1"
if [[ ! -d "$mnt/isolinux" ]]; then
_error "Missing isolinux/ at .iso directory structure. Maybe wrong .iso?"
handle_code 1
fi
}
check_device "$device"
iso_mnt=$(_mount_iso "$iso")
handle_code $?
check_iso_content "$iso_mnt"
prepare_device "$device"
prepare_boot "$device" "$iso_mnt"
prepare_data "$device" "$iso_mnt"
cleanup
echo "Kickstart Usb Stick Ready."