create_ramdisk: remove references to sparc
[unleashed.git] / usr / src / cmd / boot / scripts / create_ramdisk.ksh
blob24703cf69911ae461879e862832c2ff324aae7d0
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 SPLIT=unknown
35 ERROR=0
36 dirsize32=0
37 dirsize64=0
39 usage() {
40 echo "This utility is a component of the bootadm(1M) implementation"
41 echo "and it is not recommended for stand-alone use."
42 echo "Please use bootadm(1M) instead."
43 echo ""
44 echo "Usage: ${0##*/}: [-R \<root\>] [-p \<platform\>]"
45 echo "where \<platform\> is one of: i86pc"
46 exit
49 # default platform is what we're running on
50 PLATFORM=`uname -m`
52 export PATH=/usr/sbin:/usr/bin:/sbin
53 export GZIP_CMD=/usr/bin/gzip
55 EXTRACT_FILELIST="/boot/solaris/bin/extract_boot_filelist"
58 # Parse options
60 while [ "$1" != "" ]
62 case $1 in
63 -R) shift
64 ALT_ROOT="$1"
65 if [ "$ALT_ROOT" != "/" ]; then
66 echo "Creating boot_archive for $ALT_ROOT"
67 EXTRACT_ARGS="${EXTRACT_ARGS} -R ${ALT_ROOT}"
68 EXTRACT_FILELIST="${ALT_ROOT}${EXTRACT_FILELIST}"
71 -p) shift
72 PLATFORM="$1"
73 EXTRACT_ARGS="${EXTRACT_ARGS} -p ${PLATFORM}"
75 *) usage
77 esac
78 shift
79 done
81 shift `expr $OPTIND - 1`
83 if [ $# -eq 1 ]; then
84 ALT_ROOT="$1"
85 echo "Creating boot_archive for $ALT_ROOT"
88 case $PLATFORM in
89 i386) PLATFORM=i86pc
90 ISA=i386
91 ARCH64=amd64
93 i86pc) ISA=i386
94 ARCH64=amd64
96 *) usage
98 esac
100 BOOT_ARCHIVE=platform/$PLATFORM/boot_archive
101 BOOT_ARCHIVE_64=platform/$PLATFORM/$ARCH64/boot_archive
103 SPLIT=yes
105 function cleanup
107 [ -n "$rddir" ] && rm -fr "$rddir" 2> /dev/null
108 [ -n "$new_rddir" ] && rm -fr "$new_rddir" 2>/dev/null
111 function getsize
113 # Estimate image size and add 10% overhead for ufs stuff.
114 # Note, we can't use du here in case we're on a filesystem, e.g. zfs,
115 # in which the disk usage is less than the sum of the file sizes.
116 # The nawk code
118 # {t += ($5 % 1024) ? (int($5 / 1024) + 1) * 1024 : $5}
120 # below rounds up the size of a file/directory, in bytes, to the
121 # next multiple of 1024. This mimics the behavior of ufs especially
122 # with directories. This results in a total size that's slightly
123 # bigger than if du was called on a ufs directory.
124 size32=$(cat "$list32" | xargs -I {} ls -lLd "{}" 2> /dev/null |
125 nawk '{t += ($5 % 1024) ? (int($5 / 1024) + 1) * 1024 : $5}
126 END {print int(t * 1.10 / 1024)}')
127 (( size32 += dirsize32 ))
128 size64=$(cat "$list64" | xargs -I {} ls -lLd "{}" 2> /dev/null |
129 nawk '{t += ($5 % 1024) ? (int($5 / 1024) + 1) * 1024 : $5}
130 END {print int(t * 1.10 / 1024)}')
131 (( size64 += dirsize64 ))
132 (( total_size = size32 + size64 ))
136 # The first argument can be:
138 # "both" - create an archive with both 32-bit and 64-bit binaries
139 # "32-bit" - create an archive with only 32-bit binaries
140 # "64-bit" - create an archive with only 64-bit binaries
142 function create_cpio
144 which=$1
145 archive=$2
147 # should we exclude amd64 binaries?
148 if [ "$which" = "32-bit" ]; then
149 cpiofile="$cpiofile32"
150 list="$list32"
151 elif [ "$which" = "64-bit" ]; then
152 cpiofile="$cpiofile64"
153 list="$list64"
154 else
155 cpionfile="$cpiofile32"
156 list="$list32"
159 cpio -o -H odc < "$list" > "$cpiofile"
161 [ -x /usr/bin/digest ] && /usr/bin/digest -a sha1 "$cpiofile" > "$archive.hash-new"
164 # Check if gzip exists in /usr/bin, so we only try to run gzip
165 # on systems that have gzip.
167 if [ -x $GZIP_CMD ] ; then
168 gzip -c "$cpiofile" > "${archive}-new"
169 else
170 cat "$cpiofile" > "${archive}-new"
173 if [ $? -ne 0 ] ; then
174 rm -f "${archive}-new" "$achive.hash-new"
178 function create_archive
180 which=$1
181 archive=$2
183 echo "updating $archive"
185 create_cpio "$which" "$archive"
187 if [ ! -e "${archive}-new" ] ; then
189 # Two of these functions may be run in parallel. We
190 # need to allow the other to clean up, so we can't
191 # exit immediately. Instead, we set a flag.
193 echo "update of $archive failed"
194 ERROR=1
195 else
196 mv "${archive}-new" "$archive"
197 rm -f "$archive.hash"
198 mv "$archive.hash-new" "$archive.hash" 2> /dev/null
202 function fatal_error
204 print -u2 $*
205 exit 1
209 # get filelist
211 if [ ! -f "$ALT_ROOT/boot/solaris/filelist.ramdisk" ] &&
212 [ ! -f "$ALT_ROOT/etc/boot/solaris/filelist.ramdisk" ]
213 then
214 print -u2 "Can't find filelist.ramdisk"
215 exit 1
217 filelist=$($EXTRACT_FILELIST $EXTRACT_ARGS \
218 /boot/solaris/filelist.ramdisk \
219 /etc/boot/solaris/filelist.ramdisk \
220 2>/dev/null | sort -u)
223 # We use /tmp/ for scratch space now. This may be changed later if there
224 # is insufficient space in /tmp/.
226 rddir="/tmp/create_ramdisk.$$.tmp"
227 new_rddir=
228 rm -rf "$rddir"
229 mkdir "$rddir" || fatal_error "Could not create temporary directory $rddir"
231 # Clean up upon exit.
232 trap 'cleanup' EXIT
234 list32="$rddir/filelist.32"
235 list64="$rddir/filelist.64"
237 touch $list32 $list64
240 # This loop creates the 32-bit and 64-bit lists of files. The 32-bit list
241 # is written to stdout, which is redirected at the end of the loop. The
242 # 64-bit list is appended with each write.
244 cd "/$ALT_ROOT"
245 find $filelist -print 2>/dev/null | while read path
247 if [ $SPLIT = no ]; then
248 print "$path"
249 elif [ -d "$path" ]; then
250 size=`ls -lLd "$path" | nawk '
251 {print ($5 % 1024) ? (int($5 / 1024) + 1) * 1024 : $5}'`
252 if [ `basename "$path"` != "amd64" ]; then
253 (( dirsize32 += size ))
255 (( dirsize64 += size ))
256 else
257 case `LC_MESSAGES=C /usr/bin/file "$path" 2>/dev/null` in
258 *ELF\ 64-bit*)
259 print "$path" >> "$list64"
261 *ELF\ 32-bit*)
262 print "$path"
265 # put in both lists
266 print "$path"
267 print "$path" >> "$list64"
268 esac
270 done >"$list32"
272 # calculate image size
273 getsize
275 # check to see if there is sufficient space in tmpfs
277 tmp_free=`df -b /tmp | tail -1 | awk '{ printf ($2) }'`
278 (( tmp_free = tmp_free / 3 ))
279 if [ $SPLIT = yes ]; then
280 (( tmp_free = tmp_free / 2 ))
283 if [ $total_size -gt $tmp_free ] ; then
284 # assumes we have enough scratch space on $ALT_ROOT
285 new_rddir="/$ALT_ROOT/var/tmp/create_ramdisk.$$.tmp"
286 rm -rf "$new_rddir"
287 mkdir "$new_rddir" || fatal_error \
288 "Could not create temporary directory $new_rddir"
290 # Save the file lists
291 mv "$list32" "$new_rddir"/
292 mv "$list64" "$new_rddir"/
293 list32="/$new_rddir/filelist.32"
294 list64="/$new_rddir/filelist.64"
296 # Remove the old $rddir and set the new value of rddir
297 rm -rf "$rddir"
298 rddir="$new_rddir"
299 new_rddir=
302 cpiofile32="$rddir/cpio.file.32"
303 cpiofile64="$rddir/cpio.file.64"
305 if [ $SPLIT = yes ]; then
306 create_archive "32-bit" "$ALT_ROOT/$BOOT_ARCHIVE" &
307 create_archive "64-bit" "$ALT_ROOT/$BOOT_ARCHIVE_64"
308 wait
309 else
310 create_archive "both" "$ALT_ROOT/$BOOT_ARCHIVE"
312 if [ $ERROR = 1 ]; then
313 cleanup
314 exit 1
317 [ -n "$rddir" ] && rm -rf "$rddir"