do not depend on initial-boot service
[unleashed-kayak.git] / build_usb.sh
blob41a48b0de640400491dd481f6b841d923663ccb6
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 TMPDIR=${TMPDIR-/tmp}
42 PROTO=${TMPDIR}/proto
43 KAYAK_ROOTBALL=$BUILDSEND_MP/miniroot.gz
44 KAYAK_ROOT=${TMPDIR}/miniroot.$$
45 KR_FILE=${TMPDIR}/kr.$$
46 MNT=/mnt
47 UFS_LOFI=${TMPDIR}/boot_archive
48 LOFI_SIZE=2000M
49 DST_IMG=${BUILDSEND_MP}/${VERSION}.img
50 ZFS_IMG=$BUILDSEND_MP/kayak_${VERSION}.zfs.bz2
52 cleanup() {
53 echo "cleaning up"
54 set +e
55 umount $MNT 2>/dev/null
56 umount $KAYAK_ROOT 2>/dev/null
57 rm -rf $PROTO $UFS_LOFI $KR_FILE $KAYAK_ROOT
58 lofiadm -d $DST_IMG 2>/dev/null
59 lofiadm -d $LOFI_PATH 2>/dev/null
60 lofiadm -d $LOFI_RPATH 2>/dev/null
61 lofiadm -d $KR_FILE 2>/dev/null
63 trap cleanup 0 INT TERM
65 # Create a UFS lofi file and mount the UFS filesystem in $MNT. This will
66 # form the boot_archive for the USB.
67 mkfile $LOFI_SIZE $UFS_LOFI
68 LOFI_PATH=`lofiadm -a $UFS_LOFI`
69 echo 'y' | newfs $LOFI_PATH
70 mount $LOFI_PATH $MNT
72 # Clone the already-created Kayak miniroot and copy it into both $MNT, and
73 # into a now-created $PROTO. $PROTO will form the directory that gets
74 # sprayed onto the USB.
75 gunzip -c $KAYAK_ROOTBALL > $KR_FILE
76 LOFI_RPATH=`lofiadm -a $KR_FILE`
77 mkdir $KAYAK_ROOT
78 mount $LOFI_RPATH $KAYAK_ROOT
79 tar -cf - -C $KAYAK_ROOT . | tar -xf - -C $MNT
80 mkdir $PROTO
81 tar -cf - -C $KAYAK_ROOT . | tar -xf - -C $PROTO
82 umount $KAYAK_ROOT
83 rmdir $KAYAK_ROOT
84 lofiadm -d $LOFI_RPATH
85 rm $KR_FILE
88 # Put additional goodies into the boot-archive on $MNT, which is
89 # what'll be / (via ramdisk) once one boots the USB.
92 # The full ZFS image (also already-created) for actual installation.
93 cp $ZFS_IMG $MNT/root/.
95 # A cheesy way to get the boot menu to appear at boot time.
96 cp -p ./takeover-console $MNT/kayak/.
97 cat <<EOF > $MNT/root/.bashrc
98 export PATH=/usr/bin:/usr/sbin:/sbin
99 export HOME=/root
102 # Refresh the devices on the miniroot.
103 devfsadm -r $MNT
106 # The USB's miniroot is going to be larger than the PXE miniroot. To that
107 # end, some files not listed in the exception list do need to show up on
108 # the miniroot. Use PREBUILT_ILLUMOS if available, or the current system
109 # if not.
111 from_one_to_other() {
112 dir=$1
113 if [[ -z $PREBUILT_ILLUMOS || ! -d $PREBUILT_ILLUMOS/proto/root_i386/$dir ]]
114 then
115 FROMDIR=/
116 else
117 FROMDIR=$PREBUILT_ILLUMOS/proto/root_i386
120 shift
121 tar -cf - -C $FROMDIR/$dir ${@:-.} | tar -xf - -C $MNT/$dir
124 # Add from_one_to_other for any directory {file|subdir file|subdir ...} you need
125 from_one_to_other usr/share/lib/zoneinfo
126 from_one_to_other usr/share/lib/keytables
127 from_one_to_other usr/share/lib/terminfo
128 from_one_to_other usr/gnu/share/terminfo
129 from_one_to_other usr/sbin ping
130 from_one_to_other usr/bin netstat
132 cat <<EOF > $PROTO/boot/loader.conf.local
133 loader_menu_title="Welcome to the unleashed installer"
134 autoboot_delay=5
135 console="ttya,text"
139 # Okay, we've populated the new miniroot. Close it up and install it on $PROTO
140 # as the boot archive.
142 umount $MNT
143 lofiadm -d $LOFI_PATH
144 gzip -c $UFS_LOFI > $PROTO/platform/i86pc/amd64/boot_archive
145 digest -a sha1 $UFS_LOFI > $PROTO/platform/i86pc/amd64/boot_archive.hash
146 rm -rf $PROTO/{usr,bin,sbin,lib,kernel}
147 protosize=$(du -sm $PROTO/.|cut -f1)
148 imagesize=$((protosize * 11/10))
150 rm -f "${DST_IMG}"
151 mkfile -n ${imagesize}M "${DST_IMG}"
152 devs="$(lofiadm -la "${DST_IMG}")"
153 rdevs="${devs/dsk/rdsk}"
154 s0devs="${devs/p0/s0}"
155 rs0devs="${rdevs/p0/s0}"
156 rs2devs="${rdevs/p0/s2}"
157 fdisk -B "${rdevs}"
158 prtvtoc "${rs2devs}" | nawk '
159 /^[^\*]/ { r = $1; for(n = 1; n <= NF; n++) vtoc[r,n] = $n }
160 END {
161 vtoc[0,1] = 0;
162 vtoc[0,2] = 2;
163 vtoc[0,3] = 00;
164 vtoc[0,4] = vtoc[8,6] + 1;
165 vtoc[0,5] = vtoc[2,6] - vtoc[8,6];
166 vtoc[0,6] = vtoc[2,6];
167 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
168 vtoc[0,1], vtoc[0,2], vtoc[0,3], vtoc[0,4], vtoc[0,5], vtoc[0,6]);
169 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
170 vtoc[2,1], vtoc[2,2], vtoc[2,3], vtoc[2,4], vtoc[2,5], vtoc[2,6]);
171 printf("\t%d\t%d\t%02d\t%d\t%d\t%d\n",
172 vtoc[8,1], vtoc[8,2], vtoc[8,3], vtoc[8,4], vtoc[8,5], vtoc[8,6]);
173 }' | fmthard -s- "${rs2devs}"
174 # newfs doesn't ask questions if stdin isn't a tty.
175 newfs "${rs0devs}" </dev/null
176 mount -o nologging "${s0devs}" $MNT
177 tar cf - -C $PROTO . | tar xf - -C $MNT
178 installboot -mf "${MNT}/boot/pmbr" "${MNT}/boot/gptzfsboot" "${rs0devs}"
180 chmod 0444 $DST_IMG
181 echo "$DST_IMG is ready"
182 trap '' 0
183 cleanup || true