-
Notifications
You must be signed in to change notification settings - Fork 245
/
build_iso
executable file
·307 lines (279 loc) · 7.94 KB
/
build_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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
# Copyright (c) 2013 Joyent, Inc.
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# Automates the process of building iso and usb images of a smartos build.
# It uses the latest platform file that's been built.
#
shopt -s xpg_echo
set -o pipefail
export PATH=/usr/bin:/opt/local/bin:/usr/sbin
bi_arg0=$(basename $0)
bi_type=${bi_arg0/build_/}
bi_tmpdir=/tmp/build_$bi_type-$USER.$$
bi_output_dir="output-$bi_type"
bi_grub_dir="proto/boot/grub"
bi_output_grub_dir="$bi_tmpdir/boot/grub"
bi_grub_files="stage1 stage2 stage2_eltorito"
bi_archive_path="i86pc/amd64/boot_archive"
bi_unix_path="i86pc/kernel/amd64/unix"
bi_platform_dir="output/"
bi_platform_name=
bi_platform=
bi_grub_optstr=
bi_dont_clean=0
no_smartos=0
bi_usbpath=
bi_lofidev=
function usage
{
local msg="$*"
[[ -z "$msg" ]] && echo "$msg"
cat <<USAGE >&2
Usage: $bi_arg0 [ -o options ] [ -k ]
Generate a bootable $bi_type image from the proto area and newest platform directory.
-o options Append all the options to every GRUB menu.lst entry
-k Don't delete temporary files
-n Don't build for SmartOS
-h Display this message
USAGE
exit 2
}
function fail
{
if [[ x"$bi_lofidev" != x ]]; then
#(unmount and) cleanup the lofi device
pfexec umount -f ${bi_lofidev}:c
pfexec lofiadm -d $bi_lofidev
fi
# an uncompressed usb file is probably incomplete. remove it
[[ -f $bi_usbpath ]] && rm $bi_usbpath
local msg="$*"
[[ -z "$msg" ]] && msg="failed"
echo "$bi_arg0: $msg" >&2
exit 1
}
function bi_verif_mkisofs
{
echo "Checking for mkisofs ... \c "
which mkisofs >/dev/null
[[ $? -eq 0 ]] || fail "missing mkisofs"
echo "done"
}
function bi_get_build
{
echo "Determining platform... \c "
bi_platform_name=$(ls $bi_platform_dir | grep platform | grep -v .tgz | \
sort -r | head -n1)
[[ $? -eq 0 ]] || fail "failed to get platform file"
[[ -z $bi_platform_name ]] && fail "couldn't find platform file"
echo "done"
echo "Using platform: $bi_platform_name"
bi_platform=$bi_platform_dir$bi_platform_name
}
#
# Check to make sure the 64-bit kernel is actually there as a brief sanity check
#
function bi_verif_platform
{
echo "Verifying platform... \c "
[[ -d $bi_platform ]] || fail "invalid platform directory"
[[ -f $bi_platform/$bi_archive_path ]] || fail "missing boot archive"
[[ -f $bi_platform/$bi_unix_path ]] || fail "missing kernel image"
echo "done"
}
function bi_verif_grub
{
echo "Verifying grub files... \c "
[[ -d $bi_grub_dir ]] || fail "missing /boot/grub from proto area"
for file in $bi_grub_files; do
[[ -f $bi_grub_dir/$file ]] ||
fail "missing required file from grub proto area: $file"
done
echo "done"
}
function bi_generate_usb_file
{
if [[ ! -d $bi_output_dir ]]; then
mkdir -p $bi_output_dir > /dev/null
[[ $? -eq 0 ]] || fail "failed to make output directory"
fi
echo "Generating usb image file... \c "
bi_usbpath="$bi_output_dir/$bi_platform_name.usb"
rm -f $bi_usbpath
mkfile -n 2000000000 $bi_usbpath || fail "failed to generate usb file"
bi_lofidev=$(pfexec lofiadm -a $bi_usbpath) || fail "failed to create lofi device"
pfexec fdisk -F $(dirname $0)/usb_fdisk_table ${bi_lofidev/lofi/rlofi}
[[ $? -eq 0 ]] || fail "failed to partition ${bi_lofidev/lofi/rlofi}"
echo "y" | pfexec mkfs -F pcfs -o fat=32 ${bi_lofidev/lofi/rlofi}:c
[[ $? -eq 0 ]] || fail "failed to format the usb image"
echo "done"
echo "Mounting usb image file... \c "
pfexec mount -F pcfs ${bi_lofidev}:c $bi_tmpdir || fail "failed to mount usb image"
echo "done"
}
function bi_setup_work_dir
{
echo "Creating temporary directory... \c "
mkdir $bi_tmpdir >/dev/null
[[ $? -eq 0 ]] || fail "failed to make temporary directory"
echo "done"
if [[ $bi_type == usb ]]; then
bi_generate_usb_file
fi
}
function bi_cleanup_work_dir
{
[[ $bi_dont_clean -eq 1 ]] && return
echo "Removing temporary directory... \c "
[[ ! -d $bi_tmpdir ]] && return
rm -rf $bi_tmpdir/*
[[ $? -eq 0 ]] || fail "failed to remove temporary directory contents"
rmdir $bi_tmpdir
[[ $? -eq 0 ]] || fail "failed to remove temporary directory"
echo "done"
}
function bi_copy_contents
{
echo "Copying platform directory... \c "
cp -r $bi_platform $bi_tmpdir/platform >/dev/null
[[ $? -eq 0 ]] || fail "failed to copy platform file"
echo "done"
echo "Copying grub components... \c "
mkdir -p $bi_output_grub_dir >/dev/null
[[ $? -eq 0 ]] || fail "failed to iso9960 grub output directory"
for file in $bi_grub_files; do
cp $bi_grub_dir/$file $bi_output_grub_dir > /dev/null
[[ $? -eq 0 ]] || fail "failed to copy grub file: $file"
done
echo "done"
}
function bi_generate_grub_conf
{
if [[ $no_smartos != 1 ]]; then
bi_grub_optstr="${bi_grub_optstr},smartos=true"
fi
local shadow='$5$2HOHRnK3$NvLlm.1KQBbB0WjoP7xcIwGnllhzp2HnT.mDO7DpxYA'
echo "Generating grub menu.lst... \c "
cat > $bi_output_grub_dir/menu.lst <<EOF
default=0
timeout=10
min_mem64 1024
title Live 64-bit (text)
kernel /platform/i86pc/kernel/amd64/unix -B console=text,root_shadow='$shadow'$bi_grub_optstr
module /platform/i86pc/amd64/boot_archive
title Live 64-bit (noinstall)
kernel /platform/i86pc/kernel/amd64/unix -B console=text,root_shadow='$shadow',standalone=true,noimport=true
module /platform/i86pc/amd64/boot_archive
title Live 64-bit (text) +kmdb
kernel /platform/i86pc/kernel/amd64/unix -kd -B console=text,root_shadow='$shadow'$bi_grub_optstr
module /platform/i86pc/amd64/boot_archive
title Live 64-bit (ttya) +kmdb
kernel /platform/i86pc/kernel/amd64/unix -kd -B console=ttya,ttya-mode="115200,8,n,1,-",root_shadow='$shadow'$bi_grub_optstr
module /platform/i86pc/amd64/boot_archive
title Live 64-bit (ttya)
kernel /platform/i86pc/kernel/amd64/unix -B console=ttya,ttya-mode="115200,8,n,1,-",root_shadow='$shadow'$bi_grub_optstr
module /platform/i86pc/amd64/boot_archive
EOF
[[ $? -eq 0 ]] || fail "failed to create menu.lst"
echo "done"
}
function bi_generate_iso
{
local isopath
if [[ ! -d $bi_output_dir ]]; then
mkdir $bi_output_dir > /dev/null
[[ $? -eq 0 ]] || fail "failed to make output directory"
fi
echo "Generating iso... \c "
isopath="$bi_output_dir/$bi_platform_name.iso"
LC_ALL=C mkisofs -R -b \
boot/grub/stage2_eltorito \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-quiet \
-o $isopath \
$bi_tmpdir/ >/dev/null
[[ $? -eq 0 ]] || fail "Failed to create iso"
echo "done"
echo "iso is available at $(pwd)/$isopath"
}
function bi_generate_usb
{
echo "Installing grub... \c "
grub --batch <<____ENDOFGRUBCOMMANDS
device (hd0) $bi_usbpath
root (hd0,0)
setup (hd0)
quit
____ENDOFGRUBCOMMANDS
[[ $? -eq 0 ]] || fail "failed to install grub"
echo "done"
pfexec umount ${bi_lofidev}:c || fail "failed to unmount ${bi_lofidev}:c"
pfexec lofiadm -d $bi_lofidev || fail "failed to remove lofi device $bi_lofidev"
bi_lofidev= #unset that variable so fail won't try to clean it up again
echo "Compressing usb image... \c "
pbzip2 $bi_usbpath || fail "failed to compress $bi_usbpath"
echo "done"
echo "usb image is available at $(pwd)/${bi_usbpath}.bz2"
}
while getopts "ko:nh" c $@; do
case "$c" in
o)
echo "Building $bi_type image with additional options: $OPTARG"
bi_grub_optstr=",$OPTARG"
;;
k)
bi_dont_clean=1
;;
n)
no_smartos=1
;;
h)
usage
;;
:)
usage "missing required argument -- $OPTARG"
;;
*)
usage "invalid option: $OPTARG"
;;
esac
done
#
# Get us to the root of the workspace
#
cd $(dirname $0)
cd ..
case "$bi_type" in
iso)
bi_verif_mkisofs
;;
usb)
;;
esac
bi_verif_grub
bi_get_build
bi_verif_platform
bi_setup_work_dir
bi_copy_contents
bi_generate_grub_conf
case "$bi_type" in
iso)
bi_generate_iso
;;
usb)
bi_generate_usb
;;
esac
bi_cleanup_work_dir