1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
#!/bin/ksh93 -p
#
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#
# pkg2du - convert driver packages to Driver Update (DU) format
#
readonly PROG=$0
readonly ORIGPWD=$PWD
readonly TMP_DIR=${TMPDIR:-/tmp}/${PROG##*/}.$$
# Must-have utilities
readonly CPIO=/usr/bin/cpio
readonly GZIP=/usr/bin/gzip
readonly MKISOFS=/usr/bin/mkisofs
readonly PATCHADD=/usr/sbin/patchadd
readonly PKGTRANS=/usr/bin/pkgtrans
readonly ROOT_ARCHIVE=/usr/sbin/root_archive
readonly LOFIADM=/usr/sbin/lofiadm
readonly MKDIR=/usr/bin/mkdir
readonly RM=/usr/bin/rm
readonly CP=/usr/bin/cp
readonly HEAD=/usr/bin/head
readonly SORT=/usr/bin/sort
readonly MKBOOTMEDIA=/usr/bin/mkbootmedia
readonly PKG2DU=/usr/bin/pkg2du
readonly TOUCH=/usr/bin/touch
readonly NAWK=/usr/bin/nawk
readonly CHMOD=/usr/bin/chmod
readonly GREP=/usr/bin/grep
readonly LS=/usr/bin/ls
readonly LN=/usr/bin/ln
readonly SED=/usr/bin/sed
readonly CAT=/usr/bin/cat
readonly FIND=/usr/bin/find
readonly UNAME=/usr/bin/uname
readonly MACH=`$UNAME -p`
# for gettext
TEXTDOMAIN=SUNW_OST_OSCMD
export TEXTDOMAIN
function usage
{
gettext "Usage:\n${PROG##*/} -r <release> [-f] [-v] [-d <dir>] [-o <iso>] [-l <label>]\n <pkg> [<pkg> ...]\n"
gettext "Options:\n -d <dir>\n Directory where the Driver Update directory should be created.\n"
gettext " -o <iso>\n Create a Solaris ISO image of the Driver Update directory.\n"
gettext " -f\n If <dir>/DU or <iso> exists, remove it without asking first.\n"
gettext " -l <label>\n Label/volume name of the ISO image (if -o option is specified).\n"
gettext " -r <release>\n Solaris release number to use. It takes the form of 5.10.\n This option must be specified.\n"
gettext " -v\n Verbose. Multiple -v options increase verbosity.\n"
echo;
}
function check_prereqs
{
typeset f
# We must have these utilities.
for f in $GZIP ${ISO:+$MKISOFS} $PKGTRANS
do
if [[ ! -x "$f" ]]
then
gettext "Cannot find required utilty $f"
exit 1
fi
done
}
function cleanup
{
$RM -rf "$TMP_DIR"
}
function is_overwrite
{
typeset arg=$1
typeset -l ans
[[ $FORCE == yes ]] && return 0
while true
do
gettext "$arg already exists. Overwrite it? (y/n) "
read ans
case $ans in
y*|Y*) return 0 ;; # go ahead overwrite
n*|N*) return 1 ;; # don't overwrite
esac
done
echo;
}
function collect_objs # <pkg> ...
{
typeset obj fail=0
for obj
do
if [[ -f "$obj"/pkginfo ]]
then
PACKAGES[ ${#PACKAGES[*]} ]=$obj
else
gettext "$obj is not in package format\n"
(( fail += 1 ))
fi
done
(( fail )) && return 1
return 0
}
function mkdu
{
typeset distdir tmpdudir pkgs obj statusfile
trap '/bin/rm -rf $statusfile $tmpdudir' EXIT
# Create DU directory first.
distdir=$ROOTDIR/DU/sol_$VERSION/$MACH
$MKDIR -p "$distdir/Tools" "$distdir/Product"
# to the other UltraSPARC architecture
if [[ "$MACH" != "i386" ]]
then
cd $ROOTDIR/DU/sol_$VERSION
$LN -s sparc sun4v
$LN -s sparc sun4u
else
cd $ROOTDIR/DU/sol_$VERSION
$LN -s i386 i86pc
fi
# Unfortunately pkgtrans insists that all packages must be in
# <device1> (see pkgtrans(1)). The packages can't have any path
# components. So we'll create a temporary directory first and then
# symlinks to the specified packages. Then run pkgtrans with
# the temporary directory as <device1>.
tmpdudir=$TMP_DIR/mkdu
$MKDIR -p "$tmpdudir"
for obj in "${PACKAGES[@]}"
do
# Get rid of trailing /'s, if any.
[[ "$obj" == */ ]] && obj=${obj%%+(/)}
# Make sure it's a full pathname.
[[ "$obj" != /* ]] && obj=$ORIGPWD/$obj
$LN -s "$obj" "$tmpdudir" || exit 1
# Remember just the file component.
pkgs[ ${#pkgs[*]} ]=${obj##*/}
done
# Package up packages as compressed data stream.
statusfile=$TMP_DIR/.pkgtrans.status
{
$PKGTRANS -s "$tmpdudir" /dev/stdout "${pkgs[@]}"
echo $? > $statusfile
$TOUCH $statusfile # make sure file is created
} | $GZIP -9 > "$distdir/Product/pkgs.gz"
[[ -s $statusfile && $(<$statusfile) != 0 ]] && return 1
# Create admin file for pkgadd
$CAT > "$distdir/Tools/admin" <<"EOF"
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
EOF
# Create install.sh
$CAT > "$distdir/Tools/install.sh" <<"EOF"
#!/sbin/sh
# install.sh -R <basedir> - install packages to basedir
basedir=/
toolsdir=`dirname $0`
tmpfile=/tmp/`basename $0`.$$
while getopts "R:" arg
do
case "$arg" in
R) basedir=$OPTARG;;
esac
done
/usr/bin/gzip -c -d "$toolsdir/../Product/pkgs.gz" > $tmpfile &&
/usr/sbin/pkgadd -R "$basedir" -d "$tmpfile" -a "$toolsdir/admin" all
status=$?
/bin/rm -f "$tmpfile"
exit $status
EOF
$CHMOD a+rx "$distdir/Tools/install.sh"
}
function mkiso
{
typeset vflag
# Skip if no ISO image was specified.
[[ -z "$ISO" ]] && return 0
# Determine mkisofs' verbose flag depending on $VERBOSE_LEVEL.
case $VERBOSE_LEVEL in
0) vflag=-quiet
;;
1) vflag= # mkisofs' default verboseness
;;
*) vflag=
i=$VERBOSE_LEVEL
while ((i > 0))
do
vflag="-v $vflag"
(( i -= 1 ))
done
;;
esac
(( VERBOSE_LEVEL )) && gettext "Creating ISO image ..."
# Note: the "-log-file >(cat -u >&2)" and "2>/dev/null" below is a
# trick to filter out mkisofs's warning message about being
# non-conforming to ISO-9660.
# We do some funky architecture-specific stuff here so that we can
# actually create a bootable media image for UltraSPARC systems
ISOARGS_sparc="-B ... -joliet-long -R -U"
ISOARGS_i386="-d -N -r -relaxed-filenames"
if [[ "$MACH" = "i386" ]]
then
ISOARGS=$ISOARGS_i386
else
ISOARGS=$ISOARGS_sparc
fi
$MKISOFS -o "$ISO" \
-allow-leading-dots \
$ISOARGS \
-ldots -full-iso9660-filenames \
-R -J \
-V "$ISOLABEL" \
$vflag \
-log-file >($CAT -u >&2) \
"$ROOTDIR" 2>/dev/null
}
#
# Main
#
trap cleanup EXIT
FORCE=
ISO=
LABEL=
RELEASE=
ROOTDIR=
VERBOSE_LEVEL=0
while getopts ':d:fo:l:r:v' opt
do
case $opt in
d) ROOTDIR=$OPTARG
;;
f) FORCE=yes
;;
o) ISO=$OPTARG
if [ ! -z `echo $ISO | $GREP "^/tmp"` ]; then
gettext "ISO images will not be created on /tmp.\n"
gettext "Please choose a different output location.\n"
exit 3
fi
;;
l) LABEL=$OPTARG
;;
r) RELEASE=$OPTARG
;;
v) (( VERBOSE_LEVEL += 1 ))
;;
:) gettext "Option -$OPTARG missing argument."
usage
exit 1
;;
*) gettext "Option -$OPTARG invalid.\n"
usage
exit 2
;;
esac
done
shift 'OPTIND - 1'
# Release number must be specified.
if [[ -z "$RELEASE" ]]
then
gettext "Solaris release number must be specified (-r option).\n"
usage
exit 1
fi
# Verify release number. Allow major.minor or major.minor.micro format.
if [[ $RELEASE != +([0-9]).+([0-9])?(.+([0-9])) ]]
then
gettext "Invalid release number: $RELEASE\n"
exit 1
fi
VERSION=$(echo $RELEASE | $SED 's/5\./2/')
# Either or both of -d or -o option must be specified.
if [[ -z "$ROOTDIR" && -z "$ISO" ]]
then
gettext "Either -d or -o option (or both) must be specified.\n"
usage
exit 1
fi
# There must be at least one package.
if (( $# == 0 ))
then
gettext "No package was specified.\n"
usage
exit 1
fi
# Check and collect packages
unset PACKAGES
collect_objs "$@" || exit 1
# Default label for ISO image
LABEL=${LABEL:-DU sol_$VERSION}
check_prereqs # must be called after $ISO is possibly set
# If an ISO image was specified, check its parent directory and get its
# full pathname.
unset ISODIR
if [[ -n "$ISO" ]]
then
if [[ "$ISO" = */* ]]
then
ISODIR=$(cd "${ISO%/*}" 2>/dev/null && pwd -P)
if (( $? ))
then
gettext "Can't access parent directory of ISO image\n"
exit 1
fi
else
ISODIR=$(pwd -P)
fi
fi
# If user specified a media root directory, verify it exists, else use
# a temporary directory.
if [[ -n "$ROOTDIR" ]]
then
$MKDIR -p "$ROOTDIR";
if [ $? != 0 ]; then
gettext "$ROOTDIR is not a directory.\n"
exit 1
elif [[ ! -w "$ROOTDIR" ]] then
gettext "Directory $ROOTDIR is not writable.\n"
exit 1
fi
# If an ISO image path is also specified, make sure it's not under
# $ROOTDIR since we're going to take the ISO image of $ROOTDIR.
if [[ -n "$ISODIR" ]]
then
realroot=$(cd "$ROOTDIR" 2>/dev/null && pwd -P)
if [[ "$ISODIR" = "$realroot"?(/*) ]]
then
gettext "ISO image must not be under Driver Update's parent directory ($realroot)\n"
exit 1
fi
fi
else
ROOTDIR=$TMP_DIR/root
fi
# If DU directory already exists, ask user permission to remove it unless -f
# option was specified.
if [[ -d "$ROOTDIR/DU" ]]
then
is_overwrite "$ROOTDIR/DU" || exit 0
$RM -rf "$ROOTDIR/DU"
fi
# If ISO image already exists, ask user permission to remove it unless -f
# option was specified.
if [[ -f "$ISO" ]]
then
is_overwrite "$ISO" || exit 0
$RM -f "$ISO"
fi
# Create DU directory and the ISO image (if requested).
mkdu && mkiso
if (( $? ))
then
$RM -rf "$ROOTDIR/DU"
[[ -n "$ISO" ]] && $RM -f "$ISO"
exit 1
fi
exit 0
|