fix sed -i invocations
[unleashed-kayak.git] / build_image.sh
blobd3950a6d5b569f7261c20391d5d013695f03a2fb
1 #!/bin/bash
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License, Version 1.0 only
7 # (the "License"). You may not use this file except in compliance
8 # with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
21 # CDDL HEADER END
24 # Copyright 2012 OmniTI Computer Consulting, Inc. All rights reserved.
25 # Use is subject to license terms.
27 fail() {
28 echo "ERROR: $*"
29 exit 1
32 # NOTE --> The URL needs to be updated with every release.
33 # Change "bloody" to whatever release the current branch is.
34 PUBLISHER=unleashed
35 : ${PKGURL:=/ws/unleashed/packages/i386/nightly/repo.redist}
36 USERLAND=/ws/oi-userland/i386/repo
37 : ${GZIP_CMD:=gzip}
38 SRCDIR=$(dirname $0)
39 DIDWORK=0
40 BUILDNUM=${VERSION//r/}
41 if [[ ${SRCDIR:0:1} != "/" ]]; then
42 SRCDIR=`pwd`/$SRCDIR
44 if [[ -z "${1}" ]]; then
45 echo "$0 <zfs pool> [checkpoint]"
46 exit 1
47 else
48 BASE=${1}
49 shift
50 BASEDIR=`zfs get -o value -H mountpoint $BASE`
52 MKFILEDIR=/tmp
53 WORKDIR=$BASEDIR
54 ROOTDIR=$WORKDIR/root
55 if [[ ! -d $ROOTDIR ]]; then
56 zfs create -o compression=off $BASE/root || fail "zfs create failed"
58 SVCCFG_DTD=${ROOTDIR}/usr/share/lib/xml/dtd/service_bundle.dtd.1
59 SVCCFG_REPOSITORY=${ROOTDIR}/etc/svc/repository.db
60 if [[ -f ${PREBUILT_ILLUMOS}/usr/src/cmd/svc/svccfg/svccfg-native ]]; then
61 SVCCFG=${PREBUILT_ILLUMOS}/usr/src/cmd/svc/svccfg/svccfg-native
62 else
63 echo "WARNING -- Not using 'native' svccfg, may hang on build."
64 echo " We recommend a pre-built illumos's svccfg-native."
65 echo " Set PREBUILT_ILLUMOS in your environment to point"
66 echo " to a built illumos-omnios repository."
67 SVCCFG=/usr/sbin/svccfg
69 export WORKDIR ROOTDIR SVCCFG_DTD SVCCFG_REPOSITORY SVCCFG
71 # This was uber-helpful
72 # http://alexeremin.blogspot.com/2008/12/preparing-small-miniroot-with-zfs-and.html
74 PKG=/bin/pkg
76 UNNEEDED_MANIFESTS="application/management/net-snmp.xml
77 application/pkg/pkg-server.xml application/pkg/pkg-mdns.xml
78 system/rmtmpfiles.xml system/mdmonitor.xml
79 system/fm/notify-params.xml system/device/allocate.xml
80 system/device/devices-audio.xml system/auditd.xml
81 system/metasync.xml system/pkgserv.xml system/fcoe_initiator.xml
82 system/metainit.xml system/zonestat.xml
83 system/cron.xml system/rbac.xml system/sac.xml
84 system/auditset.xml system/hotplug.xml
85 system/wusb.xml system/zones.xml
86 system/intrd.xml system/coreadm.xml
87 system/extended-accounting.xml
88 system/scheduler.xml
89 system/logadm-upgrade.xml system/resource-mgmt.xml
90 system/idmap.xml
91 network/ldap/client.xml network/shares/reparsed.xml
92 network/shares/group.xml network/inetd-upgrade.xml
93 network/smb/client.xml network/smb/server.xml
94 network/network-iptun.xml network/ipsec/policy.xml
95 network/ipsec/ipsecalgs.xml network/ipsec/ike.xml
96 network/ipsec/manual-key.xml network/forwarding.xml
97 network/inetd.xml network/npiv_config.xml
98 network/ssl/kssl-proxy.xml network/rpc/metamed.xml
99 network/rpc/mdcomm.xml network/rpc/gss.xml
100 network/rpc/bind.xml network/rpc/keyserv.xml
101 network/rpc/meta.xml network/rpc/metamh.xml
102 network/socket-filter-kssl.xml network/network-netcfg.xml
103 network/nfs/status.xml network/nfs/cbd.xml
104 network/nfs/nlockmgr.xml network/nfs/mapid.xml
105 network/nfs/client.xml network/network-ipqos.xml
106 network/security/ktkt_warn.xml network/security/krb5kdc.xml
107 network/security/kadmin.xml network/network-install.xml
108 network/bridge.xml network/network-initial.xml
109 network/network-ipmgmt.xml network/routing/legacy-routing.xml
110 network/network-service.xml network/network-physical.xml
111 network/network-netmask.xml network/dlmgmt.xml
112 network/network-location.xml network/ibd-post-upgrade.xml
113 network/network-routing-setup.xml network/network-loopback.xml
114 network/dns/client.xml network/dns/install.xml
115 network/dns/multicast.xml platform/i86pc/acpihpd.xml
116 system/hostid.xml system/power.xml system/pfexecd.xml
117 system/consadm.xml system/pools.xml system/console-login.xml
118 system/stmf.xml system/fmd.xml system/utmp.xml
119 system/poold.xml system/dumpadm.xml"
121 SYSTEM="system/boot/grub system/boot/real-mode system/boot/wanboot/internal
122 system/boot/loader system/boot/wanboot system/data/hardware-registry
123 system/data/keyboard/keytables system/data/terminfo
124 system/data/zoneinfo system/extended-system-utilities
125 system/file-system/autofs system/file-system/nfs
126 system/file-system/smb system/file-system/udfs
127 system/file-system/zfs system/flash/fwflash
128 system/fru-id/platform system/fru-id system/ipc
129 system/kernel/dynamic-reconfiguration/i86pc
130 system/kernel/security/gss system/library/math
131 system/library/platform system/library/policykit
132 system/library/processor
133 system/library/storage/fibre-channel/hbaapi
134 system/library/storage/fibre-channel/libsun_fc
135 system/library/storage/ima/header-ima
136 system/library/storage/ima
137 system/library/storage/libmpapi
138 system/library/storage/libmpscsi_vhci
139 system/library/storage/scsi-plugins
140 system/library system/network
141 system/storage/luxadm
142 system/storage/fibre-channel/port-utility"
144 DEBUG_PKGS="developer/debug/mdb system/dtrace developer/dtrace"
146 DRIVERS="driver/audio driver/crypto/dca driver/crypto/tpm driver/firewire
147 driver/graphics/agpgart driver/graphics/atiatom driver/graphics/drm
148 driver/i86pc/fipe driver/i86pc/ioat driver/i86pc/platform
149 driver/misc/virtio
150 driver/network/afe driver/network/amd8111s driver/network/atge
151 driver/network/bfe driver/network/bge driver/network/bnx
152 driver/network/bnxe driver/network/bpf driver/network/chxge
153 driver/network/dmfe driver/network/e1000g driver/network/elxl
154 driver/network/emlxs driver/network/eoib driver/network/fcip
155 driver/network/fcp driver/network/fcsm driver/network/fp
156 driver/network/hermon driver/network/hme driver/network/hxge
157 driver/network/ib driver/network/ibdma driver/network/ibp
158 driver/network/igb driver/network/iprb driver/network/ixgb
159 driver/network/ixgbe driver/network/mxfe driver/network/myri10ge
160 driver/network/nge driver/network/ntxn driver/network/nxge
161 driver/network/vmxnet3s driver/network/sfxge driver/network/i40e
162 driver/network/ofk driver/network/pcn driver/network/platform
163 driver/network/qlc driver/network/rds driver/network/rdsv3
164 driver/network/rge driver/network/rpcib driver/network/rtls
165 driver/network/sdp driver/network/sdpib driver/network/sfe
166 driver/network/tavor driver/network/usbecm driver/network/vr
167 driver/network/xge driver/network/yge driver/pcmcia
168 driver/serial/usbftdi driver/serial/usbsacm driver/serial/usbser
169 driver/serial/usbser_edge driver/serial/usbsksp
170 driver/serial/usbsksp/usbs49_fw driver/serial/usbsprl
171 driver/storage/aac driver/storage/ahci
172 driver/storage/amr driver/storage/arcmsr driver/storage/ata
173 driver/storage/bcm_sata driver/storage/blkdev driver/storage/cpqary3
174 driver/storage/glm driver/storage/lsimega driver/storage/marvell88sx
175 driver/storage/mega_sas driver/storage/mpt_sas driver/storage/mr_sas
176 driver/storage/nv_sata driver/storage/pmcs
177 driver/storage/sbp2 driver/storage/scsa1394 driver/storage/sdcard
178 driver/storage/ses driver/storage/si3124 driver/storage/smp
179 driver/usb driver/usb/ugen driver/xvm/pv driver/storage/vioblk
180 driver/network/vioif driver/storage/nvme driver/storage/pvscsi"
182 PARTS="service/picl install/beadm SUNWcs SUNWcsd
183 text/less editor/vim
184 developer/linker openssh
185 diagnostic/diskinfo developer/illumos-gcc"
187 PKGS="$PARTS $SYSTEM $DRIVERS"
189 if [ -n "$DEBUG" ]; then
190 PKGS="$PKGS $DEBUG_PKGS"
191 BIGROOT=1
193 CULL="perl python package/pkg snmp"
194 RMRF="/var/pkg /usr/share/man /usr/lib/python2.7 /usr/lib/iconv"
196 ID=`id -u`
197 if [[ "$ID" != "0" ]]; then
198 echo "must run as root"
199 exit 1
202 chkpt() {
203 SNAP=`zfs list -H -t snapshot $BASE/root@${1} 2> /dev/null`
204 if [[ "$DIDWORK" -ne "0" ]]; then
205 if [[ -n "$SNAP" ]]; then
206 zfs destroy $BASE/root@${1} || \
207 fail "zfs destroy ${1} failed"
209 zfs snapshot $BASE/root@${1} || fail "zfs snapshot failed"
211 if [[ "${1}" != "begin" ]]; then
212 echo " === Proceeding to phase $1 (zfs @${1}) ==="
213 zfs rollback -r $BASE/root@${1} || fail "zfs rollback failed"
214 else
215 echo " === Proceeding to phase $1 ==="
217 CHKPT=$1
218 DIDWORK=1
221 if [[ -n "$1" ]]; then
222 echo "Explicit checkpoint requested: '$1'"
223 CHKPT=$1
224 chkpt $CHKPT
226 if [[ -z "$CHKPT" ]]; then
227 CHKPT="begin"
230 declare -A keep_list
231 load_keep_list() {
232 for datafile in $*
234 FCNT=0
235 while read file
237 if [[ -n "$file" ]]; then
238 keep_list+=([$file]="x")
239 FCNT=$(($FCNT + 1))
241 done < <(cut -f2- -d/ $datafile)
242 echo " --- keeping $FCNT files from $datafile"
243 done
246 step() {
247 CHKPT=""
248 case "$1" in
250 "begin")
251 zfs destroy -r $BASE/root 2> /dev/null
252 zfs create -o compression=off $BASE/root || fail "zfs create failed"
253 chkpt pkg
256 "pkg")
258 echo "Creating image of $PUBLISHER from $PKGURL"
259 $PKG image-create -F -p $PUBLISHER=$PKGURL $ROOTDIR || fail "image-create"
260 $PKG -R $ROOTDIR set-publisher -p $USERLAND || fail 'userland'
261 $PKG -R $ROOTDIR install $PKGS || fail "install"
262 chkpt fixup
265 "fixup")
267 echo "Fixing up install root"
268 (cp $ROOTDIR/etc/vfstab $WORKDIR/vfstab && \
269 awk '{if($3!="/"){print;}}' $WORKDIR/vfstab > $ROOTDIR/etc/vfstab && \
270 echo "/devices/ramdisk:a - / ufs - no nologging" >> $ROOTDIR/etc/vfstab) || \
271 fail "vfstab / updated"
272 rm $WORKDIR/vfstab
273 cp $ROOTDIR/lib/svc/seed/global.db $ROOTDIR/etc/svc/repository.db
275 sed -i '' 's,PASSREQ=YES,PASSREQ=NO,' $ROOTDIR/etc/default/login
277 ${SVCCFG} import ${ROOTDIR}/lib/svc/manifest/milestone/sysconfig.xml
278 for xml in $UNNEEDED_MANIFESTS; do
279 rm -f ${ROOTDIR}/lib/svc/manifest/$xml && echo " --- tossing $xml"
280 done
281 echo " --- initial manifest import"
282 # See if we can transform manifest-import to use the 'native' svccfg.
283 sed 's/\/usr\/sbin\/svccfg/\$SVCCFG/g' \
284 < ${ROOTDIR}/lib/svc/method/manifest-import \
285 > /tmp/manifest-import.$$
286 chmod 0755 /tmp/manifest-import.$$
287 export SVCCFG
288 /tmp/manifest-import.$$ -f ${ROOTDIR}/etc/svc/repository.db \
289 -d ${ROOTDIR}/lib/svc/manifest
290 /bin/rm -f /tmp/manifest-import.$$
292 ${SVCCFG} -s 'system/boot-archive' setprop 'start/exec=:true'
293 ${SVCCFG} -s 'system/manifest-import' setprop 'start/exec=:true'
294 ${SVCCFG} -s "system/intrd:default" setprop "general/enabled=false"
295 ${SVCCFG} -s "system/initial-boot" setprop "start/timeout_seconds=600"
296 echo " --- neutering the manifest import"
297 echo "#!/bin/ksh" > ${ROOTDIR}/lib/svc/method/manifest-import
298 echo "exit 0" >> ${ROOTDIR}/lib/svc/method/manifest-import
299 chmod 555 ${ROOTDIR}/lib/svc/method/manifest-import
300 chkpt cull
303 "cull")
304 if [[ -z "$BIGROOT" ]]; then
305 load_keep_list data/*
306 while read file
308 if [[ -n "$file" && \
309 ${keep_list[$file]} == "" && \
310 -e "$ROOTDIR/$file" && \
311 ! -d $ROOTDIR/$file ]] ; then
312 rm -f $ROOTDIR/$file
314 done < <(cd $ROOTDIR && find ./ | cut -c3-)
315 for path in $RMRF ; do
316 rm -rf ${ROOTDIR}$path && echo " -- tossing $path"
317 done
320 chkpt mkfs
323 "mkfs")
324 size=`/usr/bin/du -ks ${ROOTDIR}|/usr/bin/nawk '{print $1+10240}'`
325 echo " --- making image of size $size"
326 /usr/sbin/mkfile ${size}k $MKFILEDIR/miniroot || fail "mkfile"
327 lofidev=`/usr/sbin/lofiadm -a $MKFILEDIR/miniroot`
328 rlofidev=`echo $lofidev |sed s/lofi/rlofi/`
329 yes | /usr/sbin/newfs -m 0 $rlofidev 2> /dev/null > /dev/null || fail "newfs"
330 chkpt mount
333 "mount")
334 mkdir -p $WORKDIR/mnt
335 /usr/sbin/mount -o nologging $lofidev $WORKDIR/mnt || fail "mount"
336 chkpt copy
339 "copy")
340 pushd $ROOTDIR >/dev/null
341 /usr/bin/find . | /usr/bin/cpio -pdum $WORKDIR/mnt 2> /dev/null > /dev/null || fail "populate root"
342 /usr/sbin/devfsadm -r $WORKDIR/mnt > /dev/null
343 popd >/dev/null
344 mkdir $WORKDIR/mnt/kayak
345 cp $SRCDIR/*.sh $WORKDIR/mnt/kayak/
346 chmod a+x $WORKDIR/mnt/kayak/*.sh
347 make_initial_boot $WORKDIR/mnt/.initialboot
348 if [[ -n "$DEBUG" ]]; then
349 cp $SRCDIR/anon.system $WORKDIR/mnt/etc/system
350 cp $SRCDIR/anon.dtrace.conf $WORKDIR/mnt/kernel/drv/dtrace.conf
352 chkpt umount
355 "umount")
356 /usr/sbin/umount $WORKDIR/mnt || fail "umount"
357 /usr/sbin/lofiadm -d $MKFILEDIR/miniroot || fail "lofiadm delete"
358 chkpt compress
361 "compress")
362 $GZIP_CMD -c -f $MKFILEDIR/miniroot > $WORKDIR/miniroot.gz
363 rm -f $MKFILEDIR/miniroot
364 chmod 644 $WORKDIR/miniroot.gz
365 echo " === Finished ==="
366 ls -l $WORKDIR/miniroot.gz
369 esac
372 make_initial_boot() {
373 FILE=$1
374 cat > $FILE <<EOF
375 /kayak/install_image.sh
376 exit \$?
380 while [[ -n "$CHKPT" ]]; do
381 step $CHKPT
382 done