dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / boot / scripts / create_ramdisk.ksh
blob390fbbd4d980236e038b01c6aed87d4890682159
1 #!/bin/ksh -p
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
20 # CDDL HEADER END
23 # Copyright 2016 Toomas Soome <tsoome@me.com>
24 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
29 # Copyright (c) 2014 by Delphix. All rights reserved.
32 ALT_ROOT=
33 EXTRACT_ARGS=
34 ERROR=0
36 usage() {
37 echo "This utility is a component of the bootadm(1M) implementation"
38 echo "and it is not recommended for stand-alone use."
39 echo "Please use bootadm(1M) instead."
40 echo ""
41 echo "Usage: ${0##*/}: [-R \<root\>] [-p \<platform\>]"
42 echo "where \<platform\> is one of: i86pc"
43 exit
46 # default platform is what we're running on
47 PLATFORM=`uname -m`
49 export PATH=/usr/sbin:/usr/bin:/sbin
50 export GZIP_CMD=/usr/bin/gzip
52 EXTRACT_FILELIST="/boot/solaris/bin/extract_boot_filelist"
55 # Parse options
57 while [ "$1" != "" ]
59 case $1 in
60 -R) shift
61 ALT_ROOT="$1"
62 if [ "$ALT_ROOT" != "/" ]; then
63 echo "Creating boot_archive for $ALT_ROOT"
64 EXTRACT_ARGS="${EXTRACT_ARGS} -R ${ALT_ROOT}"
65 EXTRACT_FILELIST="${ALT_ROOT}${EXTRACT_FILELIST}"
68 -p) shift
69 PLATFORM="$1"
70 EXTRACT_ARGS="${EXTRACT_ARGS} -p ${PLATFORM}"
72 *) usage
74 esac
75 shift
76 done
78 shift `expr $OPTIND - 1`
80 if [ $# -eq 1 ]; then
81 ALT_ROOT="$1"
82 echo "Creating boot_archive for $ALT_ROOT"
85 case $PLATFORM in
86 i386) PLATFORM=i86pc
88 i86pc) ;;
89 *) usage
91 esac
93 BOOT_ARCHIVE=platform/$PLATFORM/boot_archive
95 function cleanup
97 [ -n "$rddir" ] && rm -fr "$rddir" 2> /dev/null
100 function create_cpio
102 archive=$1
104 cpio -o -H odc < "$list" > "$cpiofile"
106 [ -x /usr/bin/digest ] && /usr/bin/digest -a sha1 "$cpiofile" > "$archive.hash-new"
109 # Check if gzip exists in /usr/bin, so we only try to run gzip
110 # on systems that have gzip.
112 if [ -x $GZIP_CMD ] ; then
113 gzip -c "$cpiofile" > "${archive}-new"
114 else
115 mv "$cpiofile" "${archive}-new"
118 if [ $? -ne 0 ] ; then
119 rm -f "${archive}-new" "$achive.hash-new"
123 function create_archive
125 archive=$1
127 echo "updating $archive"
129 create_cpio "$archive"
131 if [ ! -e "${archive}-new" ] ; then
133 # Two of these functions may be run in parallel. We
134 # need to allow the other to clean up, so we can't
135 # exit immediately. Instead, we set a flag.
137 echo "update of $archive failed"
138 ERROR=1
139 else
140 # remove the hash first, it's better to have a boot archive
141 # without a hash, than one with a hash for its predecessor
142 rm -f "$archive.hash"
143 mv "${archive}-new" "$archive"
144 mv "$archive.hash-new" "$archive.hash" 2> /dev/null
148 function fatal_error
150 print -u2 $*
151 exit 1
155 # get filelist
157 if [ ! -f "$ALT_ROOT/boot/solaris/filelist.ramdisk" ] &&
158 [ ! -f "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk" ]
159 then
160 print -u2 "Can't find filelist.ramdisk"
161 exit 1
163 filelist=$($EXTRACT_FILELIST $EXTRACT_ARGS \
164 /boot/solaris/filelist.ramdisk \
165 /etc/boot/solaris/filelist.ramdisk \
166 2>/dev/null | sort -u)
169 # We assume there is enough space on $ALT_ROOT. We create the temporary
170 # file in /platform because then it is just a local filesystem move (instead
171 # of a cross-mountpoint move). So, if we successufully create the boot
172 # archive temp file, we will likely succeed in moving it into place.
174 rddir="/$ALT_ROOT/platform/create_ramdisk.$$.tmp"
175 rm -rf "$rddir"
176 mkdir "$rddir" || fatal_error "Could not create temporary directory $rddir"
178 # Clean up upon exit.
179 trap 'cleanup' EXIT
181 list="$rddir/filelist"
183 touch $list
186 # This loop creates the lists of files. The list is written to stdout,
187 # which is redirected at the end of the loop.
189 cd "/$ALT_ROOT"
190 find $filelist -print 2>/dev/null > "$list"
192 cpiofile="$rddir/cpio.file"
194 create_archive "$ALT_ROOT/$BOOT_ARCHIVE"
196 if [ $ERROR = 1 ]; then
197 cleanup
198 exit 1
201 [ -n "$rddir" ] && rm -rf "$rddir"