Print this page
XXX Remove nawk(1)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/boot/scripts/root_archive.ksh
+++ new/usr/src/cmd/boot/scripts/root_archive.ksh
1 1 #!/bin/ksh -p
2 2 #
3 3 # CDDL HEADER START
4 4 #
5 5 # The contents of this file are subject to the terms of the
6 6 # Common Development and Distribution License (the "License").
7 7 # You may not use this file except in compliance with the License.
8 8 #
9 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 # or http://www.opensolaris.org/os/licensing.
11 11 # See the License for the specific language governing permissions
12 12 # and limitations under the License.
13 13 #
14 14 # When distributing Covered Code, include this CDDL HEADER in each
15 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 # If applicable, add the following below this CDDL HEADER, with the
17 17 # fields enclosed by brackets "[]" replaced with your own identifying
18 18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 19 #
20 20 # CDDL HEADER END
21 21 #
22 22
23 23 # Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 # Use is subject to license terms.
25 25 #
26 26 # Copyright 2012 Nexenta Systems, Inc. All rights reserved.
27 27
28 28 # utility to pack and unpack a boot/root archive
29 29 # both ufs and hsfs (iso9660) format archives are unpacked
30 30 # only ufs archives are generated
31 31 #
32 32 # usage: pack <archive> <root>
33 33 # unpack <archive> <root>
34 34 #
35 35 # Where <root> is the directory to unpack to and will be cleaned out
36 36 # if it exists.
37 37 #
38 38
39 39 usage()
40 40 {
41 41 printf "usage: root_archive pack <archive> <root>\n"
42 42 printf " root_archive unpack <archive> <root>\n"
43 43 exit 1
44 44 }
45 45
46 46 cleanup()
47 47 {
48 48 if [ -d $MNT ] ; then
49 49 umount $MNT 2> /dev/null
50 50 rmdir $MNT
51 51 fi
52 52
53 53 lofiadm -d "$TMR" 2>/dev/null
54 54 if [ "$REALTHING" != true ] ; then
55 55 rm -f "$TMR"
56 56 fi
57 57 rm -f "$TMR.gz"
58 58 rm -f /tmp/flist$$
59 59 }
60 60
61 61 do_unpack()
62 62 {
63 63 (
64 64 cd $MNT
65 65 find . -print | cpio -pdum "$UNPACKED_ROOT" 2> /dev/null
66 66 )
67 67 # increase the chances the unmount will succeed
68 68 umount -f $MNT
69 69 }
70 70
71 71 unpack()
72 72 {
73 73 MR=$1
74 74 if [ ! -f "$MR" ] ; then
75 75 printf "$MR: not found\n"
76 76 usage
77 77 fi
78 78
79 79 if [ `uname -i` = i86pc ] ; then
80 80 gzcat "$MR" > $TMR
81 81 else
82 82 REALTHING=true ; export REALTHING
83 83 TMR="$MR"
84 84 fi
85 85
86 86 LOFIDEV=`/usr/sbin/lofiadm -a $TMR`
87 87 if [ $? != 0 ] ; then
88 88 echo lofi plumb failed
89 89 exit 2
90 90 fi
91 91
92 92 mkdir -p $MNT
93 93
94 94 FSTYP=`fstyp $LOFIDEV`
95 95
96 96 if [ "$FSTYP" = ufs ] ; then
97 97 /usr/sbin/mount -o ro,nologging $LOFIDEV $MNT
98 98 do_unpack
99 99 elif [ "$FSTYP" = hsfs ] ; then
100 100 /usr/sbin/mount -F hsfs -o ro $LOFIDEV $MNT
101 101 do_unpack
102 102 else
103 103 printf "invalid root archive\n"
104 104 fi
105 105
106 106
107 107 rmdir $MNT
108 108 lofiadm -d $TMR ; LOFIDEV=
109 109 if [ "$REALTHING" != true ] ; then
110 110 rm $TMR
111 111 fi
112 112 }
113 113
114 114 compress()
115 115 {
116 116 SRC=$1
117 117 DST=$2
118 118
119 119 (
120 120 cd $SRC
121 121 filelist=`find .`
122 122
123 123 for file in $filelist ; do
124 124
125 125 file=`echo $file | sed s#^./##`
126 126
127 127 # copy all files over to preserve hard links
128 128 #
129 129 echo $file | cpio -pdum $DST 2> /dev/null
130 130
131 131 if [ -f $file ] && [ -s $file ] && [ ! -h $file ] ; then
132 132 fiocompress -mc $file $DST/$file &
133 133 fi
134 134
135 135 done
136 136
137 137 wait `pgrep fiocompress`
138 138
139 139 # now re-copy a couple of uncompressed files
140 140
141 141 if [ -d "$SRC/platform/i86pc" ] ; then
142 142 find `cat boot/solaris/filelist.ramdisk` -type file \
143 143 -print 2> /dev/null > /tmp/flist$$
144 144 find usr/kernel -type file -print 2> /dev/null \
145 145 >> /tmp/flist$$
146 146 # some of the files are replaced with links into
147 147 # tmp/root on the miniroot, so find the backing files
148 148 # from there as well and add them to the list ti
149 149 # be copied uncompressed
150 150 (
151 151 cd $SRC/tmp/root
152 152 find `cat ../../boot/solaris/filelist.ramdisk` \
153 153 -type file -print 2> /dev/null | \
154 154 sed 's#^#tmp/root/#' >> /tmp/flist$$
155 155 )
156 156 flist=`cat /tmp/flist$$`
157 157 (
158 158 cd $DST
159 159 rm -f $flist
160 160 )
161 161 for file in $flist ; do
162 162 echo $file | cpio -pdum $DST 2> /dev/null
163 163 done
164 164 else
165 165 find kernel platform -name unix | \
166 166 cpio -pdum $DST 2> /dev/null
167 167 find kernel platform -name genunix | cpio -pdum $DST \
168 168 2> /dev/null
169 169 find kernel platform -name platmod | cpio -pdum $DST \
170 170 2> /dev/null
171 171 find `find kernel platform -name cpu` | \
172 172 cpio -pdum $DST 2> /dev/null
173 173 find `find kernel platform -name kmdb\*` | \
174 174 cpio -pdum $DST 2> /dev/null
175 175 find kernel/misc/sparcv9/ctf kernel/fs/sparcv9/dcfs \
176 176 etc/system etc/name_to_major etc/path_to_inst \
177 177 etc/name_to_sysnum etc/driver_aliases \
178 178 etc/driver_classes etc/minor_perm | \
179 179 cpio -pdum $DST 2> /dev/null
180 180 fi
181 181 )
182 182 }
183 183
184 184 root_is_ramdisk()
185 185 {
186 186 grep -v "set root_is_ramdisk=" "$UNPACKED_ROOT"/etc/system | \
187 187 grep -v "set ramdisk_size=" > /tmp/system.$$
188 188 cat /tmp/system.$$ > "$UNPACKED_ROOT"/etc/system
189 189 rm /tmp/system.$$
190 190
191 191 echo set root_is_ramdisk=1 >> "$UNPACKED_ROOT"/etc/system
192 192 echo set ramdisk_size=$1 >> "$UNPACKED_ROOT"/etc/system
193 193 }
194 194
195 195 pack()
196 196 {
197 197 MR="$1"
198 198 [ -d "$UNPACKED_ROOT" ] || usage
↓ open down ↓ |
198 lines elided |
↑ open up ↑ |
199 199
200 200 # always compress if fiocompress exists
201 201 #
202 202 if [ -x /usr/sbin/fiocompress ] ; then
203 203 COMPRESS=true
204 204 fi
205 205
206 206 # Estimate image size and add %10 overhead for ufs stuff.
207 207 # Note, we can't use du here in case $UNPACKED_ROOT is on a filesystem,
208 208 # e.g. zfs, in which the disk usage is less than the sum of the file
209 - # sizes. The nawk code
209 + # sizes. The awk code
210 210 #
211 211 # {t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
212 212 #
213 213 # below rounds up the size of a file/directory, in bytes, to the
214 214 # next multiple of 1024. This mimics the behavior of ufs especially
215 215 # with directories. This results in a total size that's slightly
216 216 # bigger than if du was called on a ufs directory.
217 217 #
218 218 # if the operation in turn is compressing the files the amount
219 219 # of typical shrinkage is used to come up with a useful archive
220 220 # size
221 - size=$(find "$UNPACKED_ROOT" -ls | nawk '
221 + size=$(find "$UNPACKED_ROOT" -ls | /usr/xpg4/bin/awk '
222 222 {t += ($7 % 1024) ? (int($7 / 1024) + 1) * 1024 : $7}
223 223 END {print int(t * 1.10 / 1024)}')
224 224 if [ "$COMPRESS" = true ] ; then
225 - size=`echo $size | nawk '{s = $1} END {print int(s * 0.6)}'`
225 + size=`echo $size | /usr/xpg4/bin/awk '{s = $1} END {print int(s * 0.6)}'`
226 226 fi
227 227
228 228 /usr/sbin/mkfile ${size}k "$TMR"
229 229
230 230 LOFIDEV=`/usr/sbin/lofiadm -a "$TMR"`
231 231 if [ $? != 0 ] ; then
232 232 echo lofi plumb failed
233 233 exit 2
234 234 fi
235 235
236 236 RLOFIDEV=`echo $LOFIDEV | sed s/lofi/rlofi/`
237 237 newfs $RLOFIDEV < /dev/null 2> /dev/null
238 238 mkdir -p $MNT
239 239 mount -o nologging $LOFIDEV $MNT
240 240 rmdir $MNT/lost+found
241 241
242 242 if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
243 243 root_is_ramdisk $size
244 244 fi
245 245
246 246 (
247 247 cd "$UNPACKED_ROOT"
248 248 if [ "$COMPRESS" = true ] ; then
249 249 compress . $MNT
250 250 else
251 251 find . -print | cpio -pdum $MNT 2> /dev/null
252 252 fi
253 253 )
254 254 lockfs -f $MNT
255 255 umount $MNT
256 256 rmdir $MNT
257 257
258 258 if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
259 259 "$UNPACKED_ROOT/usr/sbin/installboot" \
260 260 "$UNPACKED_ROOT/platform/sun4u/lib/fs/ufs/bootblk" \
261 261 $RLOFIDEV
262 262 fi
263 263
264 264 lofiadm -d $LOFIDEV
265 265 LOFIDEV=
266 266
267 267 rm -f "$TMR.gz"
268 268
269 269 if [ -d "$UNPACKED_ROOT/kernel/drv/sparcv9" ] ; then
270 270 mv "$TMR" "$MR"
271 271 else
272 272 gzip -f "$TMR"
273 273 mv "$TMR.gz" "$MR"
274 274 fi
275 275
276 276 chmod a+r "$MR"
277 277 }
278 278
279 279 strip_amd64()
280 280 {
281 281 find "$UNPACKED_ROOT" -name amd64 -type directory | xargs rm -rf
282 282 }
283 283
284 284 # main
285 285 #
286 286
287 287 EXTRA_SPACE=0
288 288 STRIP_AMD64=
289 289 COMPRESS=
290 290
291 291 PATH=/usr/sbin:/usr/bin:/opt/sfw/bin ; export PATH
292 292
293 293 while getopts s:6c opt ; do
294 294 case $opt in
295 295 s) EXTRA_SPACE="$OPTARG"
296 296 ;;
297 297 6) STRIP_AMD64=false
298 298 ;;
299 299 c) COMPRESS=true
300 300 ;;
301 301 *) usage
302 302 ;;
303 303 esac
304 304 done
305 305 shift `expr $OPTIND - 1`
306 306
307 307 [ $# == 3 ] || usage
308 308
309 309 UNPACKED_ROOT="$3"
310 310 BASE="`pwd`"
311 311 MNT=/tmp/mnt$$
312 312 TMR=/tmp/mr$$
313 313 LOFIDEV=
314 314 MR="$2"
315 315
316 316 # sanity check
317 317 [ "$UNPACKED_ROOT" != "/" ] || usage
318 318
319 319 if [ "`dirname $MR`" = . ] ; then
320 320 MR="$BASE/$MR"
321 321 fi
322 322 if [ "`dirname $UNPACKED_ROOT`" = . ] ; then
323 323 UNPACKED_ROOT="$BASE/$UNPACKED_ROOT"
324 324 fi
325 325
326 326 trap cleanup EXIT
327 327
328 328 # always unpack into a fresh root
329 329 case $1 in
330 330 unpack)
331 331 rm -rf "$UNPACKED_ROOT"
332 332 mkdir -p "$UNPACKED_ROOT"
333 333 ;;
334 334 esac
335 335 [ -d "$UNPACKED_ROOT" ] || usage
336 336
337 337 case $1 in
338 338 pack) pack "$MR"
339 339 ;;
340 340 unpack) unpack "$MR"
341 341 ;;
342 342 *) usage
343 343 ;;
344 344 esac
↓ open down ↓ |
109 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX