summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_comp/files/pkg_comp.sh
blob: 24b1e471db17088c701c4e9a9e59b9f658b12ee2 (plain)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#!/bin/sh
#
# $NetBSD: pkg_comp.sh,v 1.4 2002/12/24 16:28:34 jmmv Exp $
#
# pkg_comp - Build packages inside a clean chroot environment
# Copyright (c) 2002, Julio Merino <jmmv@netbsd.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of author nor the names of its contributors may
#    be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# ----------------------------------------------------------------------
# Misc functions
# ----------------------------------------------------------------------
err()
{
    echo "$progname: $1"
    exit 1
}

usage()
{
    echo "usage: $progname [-c conf_file] target [pkg_names]"
    exit 1
}

# ----------------------------------------------------------------------
# Filesystem functions
# ----------------------------------------------------------------------
fsmount()
{
    printf "PKG_COMP ==> Mounting chroot filesystems:"
    if [ -f $fsstate ]; then
        count=`cat $fsstate`
        count=$(($count + 1))
        echo "$count" > $fsstate
        echo " already mounted."
        return
    else
        echo "1" > $fsstate
    fi

    if [ -n "$REAL_SRC" ]; then
        if [ ! -d "$REAL_SRC" ]; then
            echo " failed."
            fsumount
            err "REAL_SRC $REAL_SRC disappeared"
        fi
        mount $REAL_SRC_OPTS $REAL_SRC $DESTDIR/usr/src
    fi

    if [ -n "$REAL_PKGSRC" ]; then
        if [ ! -d "$REAL_PKGSRC" ]; then
            echo " failed."
            fsumount
            err "REAL_PKGSRC $REAL_PKGSRC disappeared"
        fi
        mount $REAL_PKGSRC_OPTS $REAL_PKGSRC $DESTDIR/usr/pkgsrc
    fi

    if [ -n "$REAL_DISTFILES" ]; then
        if [ ! -d "$REAL_DISTFILES" ]; then
            echo " failed."
            fsumount
            err "REAL_DISTFILES $REAL_DISTFILES disappeared"
        fi
        mount $REAL_DISTFILES_OPTS $REAL_DISTFILES $DESTDIR/pkg_comp/distfiles
    fi

    if [ -n "$REAL_PACKAGES" ]; then
        if [ ! -d "$REAL_PACKAGES" ]; then
            echo " failed."
            fsumount
            err "REAL_PACKAGES $REAL_PACKAGES disappeared"
        fi
        mount $REAL_PACKAGES_OPTS $REAL_PACKAGES $DESTDIR/pkg_comp/packages
    fi

    touch $fsstate
    echo " done."
}

fsumount()
{
    printf "PKG_COMP ==> Unmounting chroot filesystems:"
    if [ ! -f $fsstate ]; then
        echo " none mounted."
        return
    fi

    count=`cat $fsstate`
    if [ $count -gt 1 ]; then
        count=$(($count - 1))
        echo "$count" > $fsstate
        echo " still in use."
        return
    fi

    fsfailed=no

    if [ -n "$REAL_SRC" -a -d "$REAL_SRC" ]; then
        umount $DESTDIR/usr/src
        if [ $? != 0 ]; then fsfailed=yes; fi
    fi

    if [ -n "$REAL_PKGSRC" -a -d "$REAL_PKGSRC" ]; then
        umount $DESTDIR/usr/pkgsrc
        if [ $? != 0 ]; then fsfailed=yes; fi
    fi

    if [ -n "$REAL_DISTFILES" -a -d "$REAL_DISTFILES" ]; then
        umount $DESTDIR/pkg_comp/distfiles
        if [ $? != 0 ]; then fsfailed=yes; fi
    fi

    if [ -n "$REAL_PACKAGES" -a -d "$REAL_PACKAGES" ]; then
        umount $DESTDIR/pkg_comp/packages
        if [ $? != 0 ]; then fsfailed=yes; fi
    fi

    echo " done."

    printf "Syncing: 1"
    sync ; sleep 1
    printf " 2"
    sync ; sleep 1
    printf " 3"
    sync ; sleep 1
    echo " done."
    if [ "$fsfailed" = "yes" ]; then
        err "FATAL: failed to umount all filesystems"
    else
        rm $fsstate
    fi
}

# ----------------------------------------------------------------------
# maketemplate target
# ----------------------------------------------------------------------
pkg_maketemplate()
{
    if [ -f "$conffile" ]; then
        err "$conffile already exists"
    fi

    echo "Creating template: $conffile"
    mkdir -p $confdir
    cat > $conffile <<EOF
# -*- sh -*-
#
# pkg_comp configuration file
#

DESTDIR="/var/chroot/pkg_comp/default"
ROOTSHELL="/bin/sh"
COPYROOTCFG="no"
COPTS="-pipe"

# Target used to build packages.
BUILD_TARGET="package"

# Binary sets (tgz) are found in \$DISTRIBDIR/binary/sets
DISTRIBDIR="/var/pub/NetBSD"

# These sets are unpacked to setup the initial chroot.
SETS="base.tgz comp.tgz etc.tgz text.tgz"

# X configuration. Leave SETS_X11 empty to disable X11.
SETS_X11="xbase.tgz xcomp.tgz xcontrib.tgz xfont.tgz xmisc.tgz xserver.tgz"
USE_XPKGWEDGE="yes"
USE_XF86_4="yes"

# pkgsrc configuration.
LOCALBASE="/usr/pkg"
PKG_SYSCONFBASE="\${LOCALBASE}/etc"

# Special directories. They are mounted inside the chroot jail using
# mount_null. Leave empty to avoid mounting.
REAL_SRC="/usr/src"
REAL_SRC_OPTS="-t null -o ro"
REAL_PKGSRC="/usr/pkgsrc"
REAL_PKGSRC_OPTS="-t null -o ro"
REAL_DISTFILES="/usr/pkgsrc/distfiles"
REAL_DISTFILES_OPTS="-t null -o rw"
REAL_PACKAGES="/usr/pkgsrc/packages"
REAL_PACKAGES_OPTS="-t null -o rw"

# Specify which packages to build automatically after building the chroot.
MAKE_PACKAGES=""

# Install these packages after building the chroot. They must be present in
# inside REAL_PACKAGES. You must specify the complete name.
INSTALL_PACKAGES=""

# Append this file to the generated mk.conf.
EXTRAMK=
EOF
}

# ----------------------------------------------------------------------
# makeroot target
# ----------------------------------------------------------------------
pkg_makeroot()
{
    # Check for directories that will be null mounted.
    if [ -n "$REAL_SRC" -a ! -d "$REAL_SRC" ]; then
        err "REAL_SRC $REAL_SRC does not exist"
    fi

    if [ -n "$REAL_PKGSRC" -a ! -d "$REAL_PKGSRC" ]; then
        err "REAL_PKGSRC $REAL_PKGSRC does not exist"
    fi

    if [ -n "$REAL_DISTFILES" -a ! -d "$REAL_DISTFILES" ]; then
        err "REAL_DISTFILES $REAL_DISTFILES does not exist"
    fi

    if [ -n "$REAL_PACKAGES" -a ! -d "$REAL_PACKAGES" ]; then
        err "REAL_PACKAGES $REAL_PACKAGES does not exist"
    fi

    # Check for required directories.
    if [ ! -d $DISTRIBDIR ]; then
        err "DISTRIBDIR $DISTRIBDIR does not exist"
    fi

    if [ -d $DESTDIR ]; then
        err "DESTDIR $DESTDIR already exists"
    fi
    mkdir -p $DESTDIR
    cd $DESTDIR

    printf "Unpacking sets:"
    allsets="$SETS $SETS_X11"
    for s in $allsets; do
        printf " $s"
        tar xzpf $DISTRIBDIR/binary/sets/$s
    done
    echo

    printf "Making device nodes:"
    cd $DESTDIR/dev
    ./MAKEDEV all
    cd $DESTDIR
    echo " done."

    printf "Setting root's environment:"
    chroot $DESTDIR chpass -s $ROOTSHELL
    if [ "$COPYROOTCFG" = "yes" ]; then
        cp /root/.* $DESTDIR/root 2>&1 | > /dev/null
    fi
    echo " done."

    printf "Setting up initial configuration:"

    mkdir -p $DESTDIR/usr/src
    mkdir -p $DESTDIR/usr/pkgsrc
    mkdir -p $DESTDIR/pkg_comp/distfiles
    mkdir -p $DESTDIR/pkg_comp/packages
    mkdir -p $DESTDIR/pkg_comp/tmp
    mkdir -p $DESTDIR/pkg_comp/obj/pkgsrc

    # Set sh configuration
    echo "umask 022" >> $DESTDIR/etc/profile
    echo "ENV=/etc/shrc" >> $DESTDIR/etc/profile
    echo "export PS1=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/shrc

    # Set csh configuration
    echo "umask 022" >> $DESTDIR/etc/csh.login
    echo "set prompt=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/csh.login
    echo "set prompt=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/csh.cshrc

    cp /etc/resolv.conf $DESTDIR/etc/resolv.conf

    makeroot_mkconf

    echo " done."

    makeroot_xpkgwedge

    if [ -n "$MAKE_PACKAGES" ]; then
        pkg_build $MAKE_PACKAGES
    fi

    if [ -n "$INSTALL_PACKAGES" ]; then
        pkg_install $INSTALL_PACKAGES
    fi
}

makeroot_mkconf()
{
    file="$DESTDIR/etc/mk.conf"

    echo "OBJMACHINE=yes" >> $file
    echo "MKOBJDIRS=yes" >> $file
    echo "BSDSRCDIR=/usr/src" >> $file
    echo "WRKOBJDIR=/pkg_comp/obj/pkgsrc" >> $file
    echo "DISTDIR=/pkg_comp/distfiles" >> $file
    echo "PACKAGES=/pkg_comp/packages" >> $file
    echo "PKG_DEVELOPER?=yes" >> $file
    echo "CLEANDEPENDS?=yes" >> $file

    echo "COPTS=$COPTS" >> $file
    echo "USE_XF86_4=$USE_XF86_4" >> $file

    echo "LOCALBASE=$LOCALBASE" >> $file
    echo "PKG_SYSCONFBASE=$PKG_SYSCONFBASE" >> $file

    if [ ! -f "$EXTRAMK" ]; then
        err "Cannot find $EXTRAMK"
    fi
    cat $EXTRAMK >> $file
}

makeroot_xpkgwedge()
{
    if [ -n "$SETS_X11" ]; then
        if [ $USE_XPKGWEDGE = "yes" ]; then
            pkg_build "pkgtools/xpkgwedge"
            echo "export XAPPLRESDIR=${LOCALBASE}/lib/X11/app-defaults" >> $DESTDIR/etc/profile
            echo "setenv XAPPLRESDIR ${LOCALBASE}/lib/X11/app-defaults" >> $DESTDIR/etc/csh.login
        fi
    fi
}

# ----------------------------------------------------------------------
# build target
# ----------------------------------------------------------------------
pkg_build()
{
    pkgs="$*"

    # Check if all packages exist
    invalid=""
    for p in $pkgs; do
        if [ ! -d $REAL_PKGSRC/$p ]; then
            invalid="$invalid $p"
        fi
    done
    if [ -n "$invalid" ]; then
        err "invalid packages:$invalid"
    fi

    # Build them
    fsmount
    failed=""
    for p in $pkgs; do
        echo "PKG_COMP ==> Building and installing $p"
        prefix=`mktemp $DESTDIR/pkg_comp/tmp/pkg_comp-XXXX`
        rm $prefix
        script="$prefix.sh"
        statfile="$prefix.stat"
        cat > $script <<EOF
#!/bin/sh
cd /usr/pkgsrc/$p
make $BUILD_TARGET
if [ \$? != 0 ]; then
    touch /pkg_comp/tmp/`basename $statfile`
fi
make clean
EOF
        chmod +x $script
        chroot $DESTDIR /pkg_comp/tmp/`basename $script`
        rm $script
        if [ -f $statfile ]; then
            failed="$failed $p"
            rm $statfile
        fi
    done
    fsumount
    if [ -n "$failed" ]; then
        echo "PKG_COMP ==> Build error summary"
        echo "Build failed for:"
        for p in $failed; do
            echo "    $p"
        done
    fi
}

# ----------------------------------------------------------------------
# install target
# ----------------------------------------------------------------------
pkg_install()
{
    pkgs="$*"

    if [ -z "$REAL_PACKAGES" ]; then
        err "REAL_PACKAGES is not set"
    fi

    fsmount
    failed=""
    for p in $pkgs; do
        if [ -f $DESTDIR/pkg_comp/packages/All/$p ]; then
            echo "PKG_COMP ==> Installing binary package: $p"
            stat=$DESTDIR/pkg_comp/tmp/install.sh
            cat >> $stat <<EOF
#!/bin/sh
cd /pkg_comp/packages/All
pkg_add $p
EOF
            chmod +x $stat
            chroot $DESTDIR /pkg_comp/tmp/install.sh
            rm $stat
        else
            failed="$failed $p"
        fi
    done
    fsumount
    if [ -n "$failed" ]; then
        echo "Installation failed for:$failed"
    fi
}

# ----------------------------------------------------------------------
# chroot target
# ----------------------------------------------------------------------
pkg_chroot()
{
    if [ ! -d $DESTDIR ]; then
        err "$DESTDIR does not exist"
    fi

    fsmount
    echo "PKG_COMP ==> Entering chroot: $DESTDIR"
    chroot $DESTDIR $ROOTSHELL
    echo
    fsumount
}

# ----------------------------------------------------------------------
# removeroot target
# ----------------------------------------------------------------------
pkg_removeroot()
{
    if [ -f $fsstate ]; then
        err "filesystems may still be mounted; cannot remove"
    fi
    printf "PKG_COMP ==> Removing chroot:"
    rm -rf $DESTDIR
    echo " done."
}

# ----------------------------------------------------------------------
# removepkgs target
# ----------------------------------------------------------------------
pkg_removepkgs()
{
    fsmount
    echo "PKG_COMP ==> Removing packages"
    cat > $DESTDIR/pkg_comp/tmp/removeall.sh <<EOF
#!/bin/sh
list="\`pkg_info\`"
while [ -n "\$list" ]; do
    echo "Deleting all (non-recursive)"
    pkg_delete "*"
    list="\`pkg_info\`"
done
EOF
    chmod +x $DESTDIR/pkg_comp/tmp/removeall.sh
    chroot $DESTDIR /pkg_comp/tmp/removeall.sh
    rm $DESTDIR/pkg_comp/tmp/removeall.sh

    printf "Cleaning pkg tree ($LOCALBASE):"
    rm -rf $DESTDIR$LOCALBASE
    echo " done."

    if [ -d "$PKG_SYSCONFBASE" ]; then
        printf "Cleaning pkg configuration ($PKG_SYSCONFBASE):"
        rm -rf $DESTDIR$PKG_SYSCONFBASE
        echo " done."
    fi

    printf "Cleaning pkg database:"
    rm -rf $DESTDIR/var/db/pkg
    mkdir -p $DESTDIR/var/db/pkg
    echo " done."
    makeroot_xpkgwedge
    if [ -n "$MAKE_PACKAGES" ]; then
        pkg_build $MAKE_PACKAGES
    fi
    if [ -n "$INSTALL_PACKAGES" ]; then
        pkg_install $INSTALL_PACKAGES
    fi
    fsumount
}

# ----------------------------------------------------------------------
# Main program
# ----------------------------------------------------------------------

progname="`basename $0`"
confdir="$HOME/pkg_comp"

# Parse options
args=`getopt c: $*`
if [ $? != 0 ]; then
    usage
fi
set -- $args
conffile="$confdir/default.conf"
while [ $# -gt 0 ]; do
    case "$1" in
        -c)
            conffile="$confdir/$2.conf"
            shift
            ;;
        --)
            shift; break
            ;;
    esac
    shift
done

if [ $# -lt 1 ]; then
    usage
fi

target="$1"
shift
pkgnames="$*"

readconf()
{
    if [ -f $conffile ]; then
        if [ `id -u` -ne 0 ]; then
            err "must be run as root"
        fi
        echo "Reading config file: $conffile"
        . $conffile
    else
        err "$conffile does not exist"
    fi

    if [ -z "$LOCALBASE" ]; then
        LOCALBASE="/usr/pkg"
    fi

    if [ -z "$PKG_SYSCONFBASE" ]; then
        PKG_SYSCONFBASE="/usr/pkg/etc"
    fi

    fsstate="$DESTDIR/pkg_comp/tmp/mount.stat"

    if [ -n "$SETS_X11" ]; then
        MAKE_PACKAGES="$MAKE_PACKAGES pkgtools/x11-links"
    fi
}

checkroot()
{
   if [ ! -d "$DESTDIR" ]; then
       err "chroot not initialized; use makeroot first."
   fi
   if [ "$DESTDIR" = "/" ]; then
       err "DESTDIR cannot be /"
   fi

   # From now on, filesystems may be mounted, so we need to trap
   # signals to umount them.
   trap "echo \"*** Process aborted ***\" ; fsumount ; exit 1" INT QUIT
}

case "$target" in
    maketemplate)
        pkg_maketemplate
        exit 0
        ;;
    makeroot)
        readconf
        pkg_makeroot
        exit 0
        ;;
    build)
        readconf
        checkroot
        pkg_build $pkgnames
        exit 0
        ;;
    install)
        readconf
        checkroot
        pkg_install $pkgnames
        exit 0
        ;;
    chroot)
        readconf
        checkroot
        pkg_chroot
        exit 0
        ;;
    removepkgs)
        readconf
        checkroot
        pkg_removepkgs
        ;;
    removeroot)
        readconf
        checkroot
        pkg_removeroot
        ;;
    auto)
        readconf
        BUILD_TARGET="package"
        if [ -z "$REAL_PACKAGES" ]; then
            err "this is useless without REAL_PACKAGES"
        fi
        if [ -z "$MAKE_PACKAGES" -a -z "$pkgnames" ]; then
            err "this is useless without MAKE_PACKAGES nor package names"
        fi
        pkg_makeroot
        checkroot
        if [ -n "$pkgnames" ]; then
            pkg_build $pkgnames
        fi
        pkg_removeroot
        ;;
    *)
        err "unknown target '$target'"
        ;;
esac

exit 0