-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_iso
executable file
·209 lines (175 loc) · 6.58 KB
/
create_iso
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
#!/usr/bin/env bash
# @file create_iso
# Create an unattended Ubuntu Server ISO
# Based on original work by Rinck Sonnenberg (Netson)
# @see https://github.com/netson/ubuntu-unattended
# @author Alister Lewis-Bowen <alister@lewis-bowen.org>
set -e
spinner(){
# @see http://fitnr.com/showing-a-bash-spinner.html
local pid=$1
local delay=0.25
while [ $(ps -eo pid | grep $pid) ]; do
for i in \| / - \\; do
printf ' [%c]\b\b\b\b' $i
sleep $delay
done
done
echo -ne "\b\b\b\b"
}
download() {
# @see http://fitnr.com/showing-file-download-progress-using-wget.html
local url=$1
echo -n " "
wget --progress=dot "$url" 2>&1 | grep --line-buffered "%" | \
sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
echo -ne "\b\b\b\b"
echo " Done"
}
program_is_installed() {
# @see https://gist.github.com/JamieMason/4761049
local return_=1
type "$1" >/dev/null 2>&1 || { local return_=0; }
echo $return_
}
# ============================================================================
# Environment
TMP="/tmp"
HOSTNAME="ubuntu"
TIMEZONE="America/New_York"
USER="admin"
PASSWD="\\!QAZ2wsx"
[ "$(id -u)" != "0" ] && {
echo "This script should be run as root" 1>&2
exit 1
}
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
[ "$UNAME" == "linux" ] && {
[ -f /etc/lsb-release ] && DISTRO=$(lsb_release -is)
}
[ "$DISTRO" == "" ] && {
echo "Run this script on an Ubuntu system"
exit 1;
}
[ "$(program_is_installed "mkpasswd")" -eq 0 ] || \
[ "$(program_is_installed "mkisofs")" -eq 0 ] && {
(apt-get -y update > /dev/null 2>&1) &
spinner $!
(apt-get -y install whois genisoimage > /dev/null 2>&1) &
spinner $!
}
# ============================================================================
# User prompts
echo
while true; do
echo "Which Ubuntu version should be remastered:"
echo
echo " [1] Ubuntu 14.04.3 LTS Server amd64 (Trusty Tahr)"
echo " [2] Ubuntu 15.04 Server amd64 (Vivid Vervet)"
echo " [3] Ubuntu 15.10 Server amd64 (Wily Werewolf)"
echo
read -rp " [1|2|3]: " ubver
case $ubver in
[1]* ) DL_FILE="ubuntu-14.04.3-server-amd64.iso"
DL_LOC="http://releases.ubuntu.com/trusty/"
ISO="ubuntu-14.04.3-server-amd64-unattended.iso"
break;;
[2]* ) DL_FILE="ubuntu-15.04-server-amd64.iso"
DL_LOC="http://releases.ubuntu.com/vivid/"
ISO="ubuntu-15.04-server-amd64-unattended.iso"
break;;
[3]* ) DL_FILE="ubuntu-15.10-server-amd64.iso"
DL_LOC="http://releases.ubuntu.com/wily/"
ISO="ubuntu-15.10-server-amd64-unattended.iso"
break;;
* ) echo " please answer 1 or 2 or 3";;
esac
done
echo
read -erp "Timezone: " -i "$TIMEZONE" TIMEZONE
read -erp "User name: " -i "$USER" USER
read -erp "Password: " -i "$PASSWD" PASSWD
read -erp "Make ISO bootable via USB [Y|n]: " -i 'Y' BOOTABLE
# ============================================================================
# Download files
cd "$TMP"
[[ -f "$TMP/$DL_FILE" ]] || {
echo
echo -n "Downloading $DL_FILE: "
download "$DL_LOC$DL_FILE"
}
DEFAULTS="ubuntu.seed"
[[ -f "$TMP/$DEFAULTS" ]] || {
echo
echo -n "Downloading $DEFAULTS: "
download "http://gitlab.different.com/alister/ubuntu_install_tools/raw/master/$DEFAULTS"
}
# ============================================================================
# Optional software for bootable USB
[[ $BOOTABLE == "Y" ]] && {
[ "$(program_is_installed isohybrid)" -eq 0 ] && {
echo
(apt-get -y install syslinux > /dev/null 2>&1) &
spinner $!
}
}
# ============================================================================
# Reconstruct the ISO image
ISO_SRC="$TMP/iso_src"
ISO_NEW="$TMP/iso_new"
mkdir -p "$ISO_SRC"
mkdir -p "$ISO_NEW"
## Mount the downloaded ISO image and copy the contents to tmp directory
grep -qs "$ISO_SRC" /proc/mounts || \
mount -o loop "$TMP/$DL_FILE" "$ISO_SRC" > /dev/null 2>&1
(cp -rT "$ISO_SRC" "$ISO_NEW" > /dev/null 2>&1) &
spinner $!
## Set the language for the installation menu
echo en > "$ISO_NEW/isolinux/lang"
## Create the late command for post installation configuration
LATE_CMD="chroot /target wget -O /home/$USER/post_install http://gitlab.different.com/alister/ubuntu_install_tools/raw/master/post_install;\
chroot /target chmod +x /home/$USER/post_install ;"
## Copy the defaults file to the ISO
cp -rT "$TMP/$DEFAULTS" "$TMP/iso_new/preseed/$DEFAULTS"
## Append the late command
echo "
# Post installation script
d-i preseed/late_command string $LATE_CMD" >> "$ISO_NEW/preseed/$DEFAULTS"
## Generate the password hash
EPASSWD="$(echo $PASSWD | mkpasswd -s -m sha-512)"
## Update the defaults with the user prompted data
sed -i "s%{{username}}%$USER%g" "$ISO_NEW/preseed/$DEFAULTS"
sed -i "s%{{pwhash}}%$EPASSWD%g" "$ISO_NEW/preseed/$DEFAULTS"
sed -i "s%{{hostname}}%$HOSTNAME%g" "$ISO_NEW/preseed/$DEFAULTS"
sed -i "s%{{timezone}}%$TIMEZONE%g" "$ISO_NEW/preseed/$DEFAULTS"
## Calculate the checksum for the defaults file
CHECKSUM=$(md5sum $ISO_NEW/preseed/$DEFAULTS)
## Add the autoinstall option to the menu
cd "$ISO_NEW"
sed -i "/label install/ilabel autoinstall\n\
menu label ^Autoinstall CarryBag Ubuntu Server\n\
kernel /install/vmlinuz\n\
append file=/cdrom/preseed/ubuntu-server.seed initrd=/install/initrd.gz auto=true priority=high preseed/file=/cdrom/preseed/ubuntu.seed preseed/file/checksum=$CHECKSUM --" "$ISO_NEW/isolinux/txt.cfg"
## Automate the selection of prompt 0 in the menu
sed -i 's/^timeout 0$/timeout 1/' "$ISO_NEW/isolinux/isolinux.cfg"
sed -i '/set menu_color_highlight/ a set default=0' "$ISO_NEW/boot/grub/grub.cfg"
sed -i '/set menu_color_highlight/ a set timeout=10' "$ISO_NEW/boot/grub/grub.cfg"
## Create the remastered ISO
(mkisofs -D -r -V "CARRYBAG_UBUNTU" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 5 -boot-info-table -o $TMP/$ISO . > /dev/null 2>&1) &
spinner $!
## Make ISO bootable (for dd'ing to USB stick)
[[ $BOOTABLE == "Y" ]] && isohybrid "$TMP/$ISO"
# ============================================================================
# Clean up
umount "$ISO_SRC"
rm -rf "$ISO_NEW" "$ISO_SRC"
echo
echo "Remastered Ubuntu Server ISO complete and available at"
echo "$TMP/$ISO"
echo
echo "The ISO is preconfigured with the following values:"
echo " Hostname: $HOSTNAME"
echo " User name: $USER"
echo " Password: $PASSWD"
echo
echo "Note that the post installation script is configured to run."