-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmrepackage
executable file
·402 lines (376 loc) · 9.31 KB
/
mrepackage
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
#!/bin/ksh
#
# SPDX-License-Identifier: CDDL-1.0
#
# {{{ CDDL HEADER
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# }}}
#
# Copyright 2024 Peter Tribble
#
#
# This script will take one or more illumos zap files, and construct a
# new package that combines the constituent package(s) and applies
# a fixed set of transforms
#
# there are assumptions baked into this about the way that zap files
# get generated and the default layout they have
#
THOME=${THOME:-/packages/localsrc/Tribblix}
bail() {
echo "ERROR: $1"
exit 1
}
builtin mkdir
builtin cp
builtin chmod
builtin ln
NEWPKG=""
ZAPFILE=""
# content in zap files comes under reloc
RELOC="reloc"
# filter paths that won't exist in a zone, enable with -Z
ZONED=""
# filter paths for MVI, enable with -X
MVI=""
# true for quiet, echo to see debug
DEBUG=":"
# where output files end up
OUTDIR="/tmp/pkgs"
# temporary installation (working) directory
DESTDIR="/tmp/repack.$$.$(date '+%s')"
while getopts "O:V:XZ" opt; do
case $opt in
O)
NEWPKG=${OPTARG}
;;
V)
VERSION=${OPTARG}
;;
X)
MVI="yes"
;;
Z)
ZONED="yes"
;;
*)
bail "invalid option"
;;
esac
done
shift $((OPTIND-1))
case $# in
0)
echo "Usage: $0 [-X] [-Z] -V version -O new_pkg pkg [pkg...]"
exit 1
;;
esac
#
# the version is supposed to match the release we source the packages from
#
ILDIR="${THOME}/illumos-pkgs-m${VERSION}/pkgs"
if [ ! -d "${ILDIR}" ]; then
bail "cannot find illumos package directory"
fi
#
# we use pkg2zap from tribblix-build
#
if [ ! -x "${THOME}/tribblix-build/pkg2zap" ]; then
bail "cannot find the tribblix-build repo"
fi
#
# sanity checks
#
if [ -z "${VERSION}" ]; then
bail "output version not defined"
fi
for INFILE in "$@"
do
ZAPFILE="${ILDIR}/${INFILE}.0.${VERSION}.zap"
if [ ! -f "${ZAPFILE}" ]; then
bail "source for ${INFILE} not found"
fi
done
if [ -z "${DESTDIR}" ]; then
bail "destination directory not set"
fi
if [ -z "${NEWPKG}" ]; then
bail "output package not defined"
fi
case $DESTDIR in
'/')
bail "destination directory cannot be /"
;;
/*)
:
;;
*)
bail "must use absolute path to destination"
;;
esac
#
# output package name mustn't be the same as an existing package
#
if [ -f "${ILDIR}/${NEWPKG}.0.${VERSION}.zap" ]; then
bail "name $NEWPKG is already in use"
fi
#
# filter out pathnames for MVI
#
mvi_allowed() {
[ -z "${MVI}" ] && return 0
case $1 in
usr/share/man*|usr/include*)
return 1
;;
etc/svc*|lib/svc*|var/svc*|usr/sadm/install/miniroot.db)
return 1
;;
usr/lib/inet*|usr/lib/libc|usr/lib/libc/*|usr/lib/lib.b|*/sbin/inetd*)
return 1
;;
var/spool/cron*|etc/cron.d*|var/cron|usr/bin/at*|usr/xpg*|usr/has*|usr/ccs*)
return 1
;;
etc/mail*|etc/logadm*|var/logadm|etc/ftp*|etc/dhcp*|*ike*|*mail*)
return 1
;;
*spell*|usr/lib/reparse*|usr/lib/pkgconfig*|usr/lib/*/pkgconfig*|usr/lib/audit*)
return 1
;;
usr/sbin/snoop|usr/sbin/svccfg|usr/sbin/svcadm|usr/bin/svcs|usr/bin/svcprop)
return 1
;;
*audit*|*stat|usr/sbin/nwam*|lib/inet/nwamd|etc/nwam*|usr/lib/dl|usr/lib/dl/*)
return 1
;;
*flowadm*|*/README|*syslog*|usr/share/lib/tabset*|*/cron*|usr/lib/makekey)
return 1
;;
usr/sbin/*share*|etc/security/lib*|*/adt_*|usr/sbin/ipsec*|usr/sbin/keyserv|usr/sbin/zic|usr/sbin/nscd|usr/sbin/format|usr/sbin/rpcbind)
return 1
;;
*pwconv|*inetconv|*pwck|*grpck|*/whodo|*/csh*)
return 1
;;
etc/versions*|*/crle|*/moe|*/finger|*/whois|*/tip|usr/lib/expreserve|usr/lib/exrecover)
return 1
;;
esac
[ -z "${ZONED}" ] && return 0
case $1 in
usr/lib/raidcfg*|usr/sbin/raidctl|usr/lib/cfgadm*|usr/lib/scsi*|usr/lib/devfsadm*|usr/lib/rcm*|usr/lib/sysevent*|usr/sbin/smbios|usr/sbin/*_drv)
return 1
;;
usr/lib/class*|usr/lib/pci*|usr/lib/spd*|usr/lib/fs/ufs*|*/fsirand|*/tunefs|*/lockfs|*/newfs|etc/fs/ufs*|usr/sbin/quota*|usr/sbin/ufs*|usr/sbin/tunefs|usr/sbin/newfs|usr/sbin/growfs|usr/sbin/clri|*/mkfs*|*/labelit*|*/dcopy*|*/fssnap*|*/fsdb*|usr/sbin/fmthard|usr/sbin/prtvtoc|usr/sbin/fsck|usr/sbin/lockfs|usr/sbin/fsirand|usr/lib/fs/hsfs*|etc/fs/hsfs*)
return 1
;;
lib/mpxio*|usr/sbin/*adm|usr/sbin/installboot|usr/sbin/stmsboot|usr/sbin/ndd|usr/sbin/rtc|usr/sbin/hotplug|usr/sbin/mod*load|usr/sbin/locator)
return 1
;;
*/eject|*/fdformat|*/tic|*/tput|*/dumpkeys|*/loadkeys|*/captoinfo|*/infocmp|*/dumpcs|*/zdump|usr/bin/strchg|usr/bin/strconf|usr/bin/fdetach|*/coreadm|*/keylog*)
return 1
;;
usr/lib/intrd|usr/lib/hotplugd|*/fdisk|*/fiocompress|*sbin/swap*|*/root_archive|*/dispadmin|usr/sbin/sysdef|usr/sbin/prtdiag|usr/sbin/eeprom)
return 1
;;
esac
return 0
}
#
# filter out pathnames that should not exist in a zone
#
zone_allowed() {
[ -z "${ZONED}" ] && return 0
case $1 in
kernel*|platform*|usr/kernel*)
return 1
;;
esac
return 0
}
#
# filter for mvi and zones
#
is_allowed() {
if zone_allowed "$1"
then
if mvi_allowed "$1"
then
return 0
else
return 1
fi
else
return 1
fi
}
#
# put the objects into the alternate root
#
handle_dir() {
if [ -d "$DESTDIR/$1" ]; then
chmod "$2" "$DESTDIR/$1"
else
mkdir -p -m "$2" "$DESTDIR/$1"
fi
echo "d none ${1} ${2} ${3} ${4}" >> "${DESTDIR}/prototype"
}
handle_file() {
ndir=${1%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
cp "$TDIR/${PKGNAME}/$RELOC/$1" "$DESTDIR/$1"
chmod "$2" "$DESTDIR/$1"
echo "${5} ${6} ${1}=${1} ${2} ${3} ${4}" >> "${DESTDIR}/prototype"
}
handle_softlink() {
lname=${1##*=}
rname=${1%%=*}
ndir=${rname%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
ln -s "$lname" "$DESTDIR/$rname"
echo "s none ${1}" >> "${DESTDIR}/prototype"
}
handle_hardlink() {
lname=${1##*=}
rname=${1%%=*}
ndir=${rname%/*}
if [ ! -d "$DESTDIR/${ndir}" ]; then
mkdir -p -m 755 "$DESTDIR/${ndir}"
fi
# the hardlink target must be relative to the source name
ln "$DESTDIR/${ndir}/$lname" "$DESTDIR/$rname"
echo "l none ${1}" >> "${DESTDIR}/prototype"
}
#
# we also populate a prototype file
#
mkdir -p -m 755 "${DESTDIR}"
touch "${DESTDIR}/prototype"
#
# now loop over all the input zap files
#
for INFILE in "$@"
do
ZAPFILE="${ILDIR}/${INFILE}.0.${VERSION}.zap"
PKGNAME=${ZAPFILE##*/}
PKGNAME=${PKGNAME%%.*}
VISH=$(digest -a md5 "${ZAPFILE}")
TDIR=/tmp/zapadd.${VISH}.$$
rm -fr "$TDIR"
mkdir -p "$TDIR"
if [ ! -d "$TDIR" ]; then
bail "unable to create temporary directory"
fi
#
# extract the zap file
#
unzip -q "${ZAPFILE}" -d "$TDIR"
if [ ! -d "${TDIR}/${PKGNAME}" ]; then
bail "unpack of ${INFILE} failed"
fi
if [ ! -f "${TDIR}/${PKGNAME}/pkginfo" ]; then
bail "unpack of ${INFILE} missing pkginfo"
fi
if [ ! -f "${TDIR}/${PKGNAME}/pkgmap" ]; then
bail "unpack of ${INFILE} missing pkgmap"
fi
#
# now we need to parse the pkgmap file
#
while read -r part type class name perms user group
do
case $type in
i)
$DEBUG "Ignoring metadata $class"
;;
b|c)
$DEBUG "Ignoring device file $name"
;;
e|f|v)
$DEBUG "File $name, perms $perms"
if [ -f "${TDIR}/${PKGNAME}/${RELOC}/$name" ]; then
is_allowed "$name" && handle_file "$name" "$perms" "$user" "$group" "$type" "$class"
fi
;;
d|x)
$DEBUG "Directory $name, perms $perms"
is_allowed "$name" && handle_dir "$name" "$perms" "$user" "$group"
;;
l)
$DEBUG "Defer hard link $name"
;;
s)
$DEBUG "Soft link $name"
is_allowed "$name" && handle_softlink "$name"
;;
1)
:
# the pkgmap header, presumably
;;
*)
echo "Unrecognized $part"
;;
esac
done < "${TDIR}/${PKGNAME}/pkgmap"
#
# do the hard links in a second pass, to try and guarantee that all the
# target files exist (won't necessarily work for cross-package hard links)
#
while read -r part type class name perms user group
do
case $type in
l)
$DEBUG "Hard link $name"
is_allowed "$name" && handle_hardlink "$name"
;;
esac
done < "${TDIR}/${PKGNAME}/pkgmap"
#
# clean up
#
rm -fr "$TDIR"
#
# end of loop over zap files
#
done
#
# now we need to create a pkginfo file
#
ARCH=$(uname -p)
touch "${DESTDIR}/pkginfo"
echo "PKG=\"${NEWPKG}\"" >> "${DESTDIR}/pkginfo"
echo "NAME=\"${NEWPKG}\"" >> "${DESTDIR}/pkginfo"
echo "ARCH=\"${ARCH}\"" >> "${DESTDIR}/pkginfo"
echo "VERSION=\"${VERSION}\"" >> "${DESTDIR}/pkginfo"
echo "CATEGORY=\"system\"" >> "${DESTDIR}/pkginfo"
echo "VENDOR=\"Tribblix\"" >> "${DESTDIR}/pkginfo"
echo "BASEDIR=\"/\"" >> "${DESTDIR}/pkginfo"
echo "PSTAMP=\"tribblix\"" >> "${DESTDIR}/pkginfo"
#
# and create the package
#
cd "$DESTDIR" || bail "Unable to cd to $DESTDIR"
echo "i pkginfo=./pkginfo" >> prototype
mkdir -p "${OUTDIR}"
/usr/bin/pkgmk -d "${OUTDIR}" -f prototype -r $(pwd) "${NEWPKG}"
/usr/bin/pkgtrans -s "${OUTDIR}" "${OUTDIR}"/"${NEWPKG}.0.${VERSION}.pkg" "${NEWPKG}"
"${THOME}"/tribblix-build/pkg2zap "${OUTDIR}"/"${NEWPKG}.0.${VERSION}.pkg" "${OUTDIR}"
#
# clean up
#
cd /
rm -fr "$DESTDIR"