Merge remote-tracking branch 'danmcd/master'
[unleashed-kayak.git] / build_usb.sh
bloba59ac4386e954554ef8a366f63606359c739939c
1 #!/usr/bin/bash
3 set -e
6 # This file and its contents are supplied under the terms of the
7 # Common Development and Distribution License ("CDDL"), version 1.0.
8 # You may only use this file in accordance with the terms of version
9 # 1.0 of the CDDL.
11 # A full copy of the text of the CDDL should have accompanied this
12 # source. A copy of the CDDL is also available via the Internet at
13 # http://www.illumos.org/license/CDDL.
17 # Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved.
21 # Build a USB installer using the Kayak tools.
24 if [[ `id -u` != "0" ]]; then
25 echo "You must be root to run this script."
26 exit 1
29 if [[ -z $BUILDSEND_MP ]]; then
30 echo "Using /rpool/kayak_image for BUILDSEND_MP"
31 BUILDSEND_MP=/rpool/kayak_image
34 if [[ -z $VERSION ]]; then
35 echo "\$VERSION not set" >&2
36 exit 1
39 # Many of these depend on sufficient space in /tmp by default. Please
40 # modify as you deem appropriate.
41 PROTO=/tmp/proto
42 KAYAK_ROOTBALL=$BUILDSEND_MP/miniroot.gz
43 KAYAK_ROOT=/tmp/miniroot.$$
44 KR_FILE=/tmp/kr.$$
45 MNT=/mnt
46 UFS_LOFI=/tmp/boot_archive
47 LOFI_SIZE=2000M
48 DST_IMG=${BUILDSEND_MP}/${VERSION}.img
49 ZFS_IMG=$BUILDSEND_MP/kayak_${VERSION}.zfs.bz2
51 cleanup() {
52 echo "cleaning up"
53 set +e
54 umount $MNT 2>/dev/null
55 umount $KAYAK_ROOT 2>/dev/null
56 rm -rf $PROTO $UFS_LOFI $KR_FILE $KAYAK_ROOT
57 lofiadm -d $DST_IMG 2>/dev/null
58 lofiadm -d $LOFI_PATH 2>/dev/null
59 lofiadm -d $LOFI_RPATH 2>/dev/null
60 lofiadm -d $KR_FILE 2>/dev/null
62 trap cleanup 0 INT TERM
64 # Create a UFS lofi file and mount the UFS filesystem in $MNT. This will
65 # form the boot_archive for the USB.
66 mkfile $LOFI_SIZE $UFS_LOFI
67 LOFI_PATH=`lofiadm -a $UFS_LOFI`
68 echo 'y' | newfs $LOFI_PATH
69 mount $LOFI_PATH $MNT
71 # Clone the already-created Kayak miniroot and copy it into both $MNT, and
72 # into a now-created $PROTO. $PROTO will form the directory that gets
73 # sprayed onto the USB.
74 gunzip -c $KAYAK_ROOTBALL > $KR_FILE
75 LOFI_RPATH=`lofiadm -a $KR_FILE`
76 mkdir $KAYAK_ROOT
77 mount $LOFI_RPATH $KAYAK_ROOT
78 tar -cf - -C $KAYAK_ROOT . | tar -xf - -C $MNT
79 mkdir $PROTO
80 tar -cf - -C $KAYAK_ROOT . | tar -xf - -C $PROTO
81 umount $KAYAK_ROOT
82 rmdir $KAYAK_ROOT
83 lofiadm -d $LOFI_RPATH
84 rm $KR_FILE
87 # Put additional goodies into the boot-archive on $MNT, which is
88 # what'll be / (via ramdisk) once one boots the USB.
91 # The full ZFS image (also already-created) for actual installation.
92 cp $ZFS_IMG $MNT/root/.
94 # A cheesy way to get the boot menu to appear at boot time.
95 cp -p ./takeover-console $MNT/kayak/.
96 cat <<EOF > $MNT/root/.bashrc
97 export PATH=/usr/bin:/usr/sbin:/sbin
98 export HOME=/root
99 EOF
100 # Have initialboot muck with the console login service to make an interactive
101 # installer get invoked.
102 cat <<EOF > $MNT/.initialboot
103 /usr/sbin/svccfg -s console-login:default setprop startd/need_session = boolean: true
104 /usr/sbin/svcadm refresh console-login:default
105 /usr/sbin/svcadm restart console-login:default
107 cat <<EOF > $MNT/lib/svc/method/console-login
108 #!/bin/bash
110 # CHEESY way to get the kayak-menu running w/o interference.
111 export TERM=sun-color
112 /kayak/takeover-console /kayak/kayak-menu.sh
114 chmod 0755 $MNT/lib/svc/method/console-login
116 # Refresh the devices on the miniroot.
117 devfsadm -r $MNT
120 # The USB's miniroot is going to be larger than the PXE miniroot. To that
121 # end, some files not listed in the exception list do need to show up on
122 # the miniroot. Use PREBUILT_ILLUMOS if available, or the current system
123 # if not.
125 from_one_to_other() {
126 dir=$1
127 if [[ -z $PREBUILT_ILLUMOS || ! -d $PREBUILT_ILLUMOS/proto/root_i386/$dir ]]
128 then
129 FROMDIR=/
130 else
131 FROMDIR=$PREBUILT_ILLUMOS/proto/root_i386
134 shift
135 tar -cf - -C $FROMDIR/$dir ${@:-.} | tar -xf - -C $MNT/$dir
138 # Add from_one_to_other for any directory {file|subdir file|subdir ...} you need
139 from_one_to_other usr/share/lib/zoneinfo
140 from_one_to_other usr/share/lib/keytables
141 from_one_to_other usr/share/lib/terminfo
142 from_one_to_other usr/gnu/share/terminfo
143 from_one_to_other usr/sbin ping
144 from_one_to_other usr/bin netstat
146 cat <<EOF > $PROTO/boot/loader.conf.local
147 loader_menu_title="Welcome to the unleashed installer"
148 autoboot_delay=5
149 console="ttya,text"
153 # Okay, we've populated the new miniroot. Close it up and install it on $PROTO
154 # as the boot archive.
156 umount $MNT
157 lofiadm -d $LOFI_PATH
158 gzip -c $UFS_LOFI > $PROTO/platform/i86pc/amd64/boot_archive
159 digest -a sha1 $UFS_LOFI > $PROTO/platform/i86pc/amd64/boot_archive.hash
160 rm -rf $PROTO/{usr,bin,sbin,lib,kernel}
161 protosize=$(du -sm $PROTO/.|cut -f1)
162 imagesize=$((protosize * 11/10))
164 rm -f "${DST_IMG}"
165 mkfile -n ${imagesize}M "${DST_IMG}"
166 devs="$(lofiadm -la "${DST_IMG}")"
167 rdevs="${devs/dsk/rdsk}"
168 s0devs="${devs/p0/s0}"
169 rs0devs="${rdevs/p0/s0}"
170 rs2devs="${rdevs/p0/s2}"
171 fdisk -B "${rdevs}"
172 prtvtoc "${rs2devs}" | nawk '
173 /^[^\*]/ { r = $1; for(n = 1; n <= NF; n++) vtoc[r,n] = $n }
174 END {
175 vtoc[0,1] = 0;
176 vtoc[0,2] = 2;
177 vtoc[0,3] = 00;
178 vtoc[0,4] = vtoc[8,6] + 1;
179 vtoc[0,5] = vtoc[2,6] - vtoc[8,6];
180 vtoc[0,6] = vtoc[2,6];
181 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
182 vtoc[0,1], vtoc[0,2], vtoc[0,3], vtoc[0,4], vtoc[0,5], vtoc[0,6]);
183 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
184 vtoc[2,1], vtoc[2,2], vtoc[2,3], vtoc[2,4], vtoc[2,5], vtoc[2,6]);
185 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
186 vtoc[8,1], vtoc[8,2], vtoc[8,3], vtoc[8,4], vtoc[8,5], vtoc[8,6]);
187 }' | fmthard -s- "${rs2devs}"
188 # newfs doesn't ask questions if stdin isn't a tty.
189 newfs "${rs0devs}" </dev/null
190 mount -o nologging "${s0devs}" $MNT
191 tar cf - -C $PROTO . | tar xf - -C $MNT
192 installboot -mf "${MNT}/boot/pmbr" "${MNT}/boot/gptzfsboot" "${rs0devs}"
194 chmod 0444 $DST_IMG
195 echo "$DST_IMG is ready"
196 trap '' 0
197 cleanup || true