| @@ -1,43 +1,44 @@ | | | @@ -1,43 +1,44 @@ |
1 | #! /bin/sh | | 1 | #! /bin/sh |
2 | | | 2 | |
3 | # $NetBSD: mksandbox,v 1.18 2021/01/22 11:59:52 jperkin Exp $ | | 3 | # $NetBSD: mksandbox,v 1.19 2023/12/05 20:45:27 agc Exp $ |
4 | | | 4 | |
5 | # Copyright (c) 2002,2012 Alistair Crooks <agc@NetBSD.org> | | 5 | # Copyright (c) 2002,2012 Alistair Crooks <agc@NetBSD.org> |
6 | # All rights reserved. | | 6 | # All rights reserved. |
7 | # | | 7 | # |
8 | # Redistribution and use in source and binary forms, with or without | | 8 | # Redistribution and use in source and binary forms, with or without |
9 | # modification, are permitted provided that the following conditions | | 9 | # modification, are permitted provided that the following conditions |
10 | # are met: | | 10 | # are met: |
11 | # 1. Redistributions of source code must retain the above copyright | | 11 | # 1. Redistributions of source code must retain the above copyright |
12 | # notice, this list of conditions and the following disclaimer. | | 12 | # notice, this list of conditions and the following disclaimer. |
13 | # 2. Redistributions in binary form must reproduce the above copyright | | 13 | # 2. Redistributions in binary form must reproduce the above copyright |
14 | # notice, this list of conditions and the following disclaimer in the | | 14 | # notice, this list of conditions and the following disclaimer in the |
15 | # documentation and/or other materials provided with the distribution. | | 15 | # documentation and/or other materials provided with the distribution. |
16 | # | | 16 | # |
17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # | | 27 | # |
28 | | | 28 | |
29 | # Usage: mksandbox [--mounthost=host] [--rodirs=dir1,...] [--rwdirs=dir1,...] | | 29 | # Usage: mksandbox [--mounthost=host] [--pkgsrc=dir] |
30 | # [--pkgsrc=dir] [--src=srcdir] [--xsrc=xsrcdir] | | 30 | # [--rodirs=dir1,...] [--rwdirs=dir1,...] |
| | | 31 | # [--src=srcdir] [--xsrc=xsrcdir] |
31 | # [--without-src] [--without-pkgsrc] [--without-xsrc] | | 32 | # [--without-src] [--without-pkgsrc] [--without-xsrc] |
32 | # [--without-x] [--verbose] sandbox-dir | | 33 | # [--without-x] [--verbose] sandbox-dir |
33 | # | | 34 | # |
34 | # A small shell script to set up a sandbox (usually for a pkgsrc bulk | | 35 | # A small shell script to set up a sandbox (usually for a pkgsrc bulk |
35 | # build), using null mounts. | | 36 | # build), using null mounts. |
36 | | | 37 | |
37 | pkgsrc=/usr/pkgsrc | | 38 | pkgsrc=/usr/pkgsrc |
38 | src=/usr/src | | 39 | src=/usr/src |
39 | xsrc=/usr/xsrc | | 40 | xsrc=/usr/xsrc |
40 | rodirs= | | 41 | rodirs= |
41 | rwdirs= | | 42 | rwdirs= |
42 | with_pkgsrc=yes | | 43 | with_pkgsrc=yes |
43 | with_x=yes | | 44 | with_x=yes |
| @@ -408,90 +409,110 @@ if [ -n "$rodirs" ]; then | | | @@ -408,90 +409,110 @@ if [ -n "$rodirs" ]; then |
408 | echo "$mounthost$dir $dir ro \\" >> $sandbox_script | | 409 | echo "$mounthost$dir $dir ro \\" >> $sandbox_script |
409 | done | | 410 | done |
410 | fi | | 411 | fi |
411 | | | 412 | |
412 | if [ -n "$rwdirs" ]; then | | 413 | if [ -n "$rwdirs" ]; then |
413 | for dir in `echo $rwdirs | $sedprog -e 's/,/ /g'`; do | | 414 | for dir in `echo $rwdirs | $sedprog -e 's/,/ /g'`; do |
414 | echo "Mount $dir from $sandbox" | | 415 | echo "Mount $dir from $sandbox" |
415 | $mkdirprog $sandbox$dir | | 416 | $mkdirprog $sandbox$dir |
416 | $mountprog $mountflags $mounthost$dir $sandbox$dir | | 417 | $mountprog $mountflags $mounthost$dir $sandbox$dir |
417 | echo "$mounthost$dir $dir rw \\" >> $sandbox_script | | 418 | echo "$mounthost$dir $dir rw \\" >> $sandbox_script |
418 | done | | 419 | done |
419 | fi | | 420 | fi |
420 | | | 421 | |
| | | 422 | date > $sandbox/.sandbox_mounted |
| | | 423 | |
421 | cat >> $sandbox_script <<EOS | | 424 | cat >> $sandbox_script <<EOS |
422 | " | | 425 | " |
423 | case x\$1 in | | 426 | case x\$1 in |
424 | xmount) | | 427 | xmount) |
| | | 428 | if [ -s \$sandbox/.sandbox_mounted ]; then |
| | | 429 | echo \$sandbox already mounted && exit 1 |
| | | 430 | fi |
425 | set dummy \$fses | | 431 | set dummy \$fses |
426 | shift | | 432 | shift |
427 | while [ \$# -ge 3 ]; do | | 433 | while [ \$# -ge 3 ]; do |
428 | fs=\$1; shift | | 434 | fs=\$1; shift |
429 | mntpoint=\$1; shift | | 435 | mntpoint=\$1; shift |
430 | rwro=\$1; shift | | 436 | rwro=\$1; shift |
431 | case \$rwro in | | 437 | case \$rwro in |
432 | ro) $mountprog $mountflags -r \$fs \$sandbox/\$mntpoint || exit 1 ;; | | 438 | ro) $mountprog $mountflags -r \$fs \$sandbox/\$mntpoint || exit 1 ;; |
433 | rw) $mountprog $mountflags \$fs \$sandbox/\$mntpoint || exit 1 ;; | | 439 | rw) $mountprog $mountflags \$fs \$sandbox/\$mntpoint || exit 1 ;; |
434 | esac | | 440 | esac |
435 | done | | 441 | done |
436 | case \$opsys in | | 442 | case \$opsys in |
437 | Darwin|DragonFly|FreeBSD) | | 443 | Darwin|DragonFly|FreeBSD) |
438 | $mountprog -t devfs devfs \$sandbox/dev | | 444 | $mountprog -t devfs devfs \$sandbox/dev |
439 | ;; | | 445 | ;; |
440 | Linux) | | 446 | Linux) |
441 | $mountprog $mountflags /dev \$sandbox/dev | | 447 | $mountprog $mountflags /dev \$sandbox/dev |
442 | $mountprog -t tmpfs tmpfs \$sandbox/dev/shm | | 448 | $mountprog -t tmpfs tmpfs \$sandbox/dev/shm |
443 | ;; | | 449 | ;; |
444 | SunOS) | | 450 | SunOS) |
445 | $mountprog -F lofs -r /dev \$sandbox/dev | | 451 | $mountprog -F lofs -r /dev \$sandbox/dev |
446 | $mountprog -F fd fd \$sandbox/dev/fd | | 452 | $mountprog -F fd fd \$sandbox/dev/fd |
447 | ;; | | 453 | ;; |
448 | esac | | 454 | esac |
| | | 455 | date > \$sandbox/.sandbox_mounted |
449 | ;; | | 456 | ;; |
450 | xumount) | | 457 | xumount) |
| | | 458 | if [ ! -s \$sandbox/.sandbox_mounted ]; then |
| | | 459 | echo \$sandbox not mounted && exit 1 |
| | | 460 | fi |
451 | set dummy \`r3 \$fses\` | | 461 | set dummy \`r3 \$fses\` |
452 | shift | | 462 | shift |
453 | while [ \$# -ge 3 ]; do | | 463 | while [ \$# -ge 3 ]; do |
454 | fs=\$1; shift | | 464 | fs=\$1; shift |
455 | mntpoint=\$1; shift | | 465 | mntpoint=\$1; shift |
456 | dummy=\$1; shift | | 466 | dummy=\$1; shift |
457 | umount \$sandbox/\$mntpoint | | 467 | umount \$sandbox/\$mntpoint |
458 | done | | 468 | done |
459 | case \$opsys in | | 469 | case \$opsys in |
460 | Linux) | | 470 | Linux) |
461 | umount \$sandbox/dev/shm | | 471 | umount \$sandbox/dev/shm |
462 | umount \$sandbox/dev | | 472 | umount \$sandbox/dev |
463 | ;; | | 473 | ;; |
464 | Darwin|DragonFly|FreeBSD) | | 474 | Darwin|DragonFly|FreeBSD) |
465 | umount \$sandbox/dev | | 475 | umount \$sandbox/dev |
466 | ;; | | 476 | ;; |
467 | SunOS) | | 477 | SunOS) |
468 | umount \$sandbox/dev/fd | | 478 | umount \$sandbox/dev/fd |
469 | umount \$sandbox/dev | | 479 | umount \$sandbox/dev |
470 | ;; | | 480 | ;; |
471 | esac | | 481 | esac |
| | | 482 | rm -f \$sandbox/.sandbox_mounted |
472 | ;; | | 483 | ;; |
473 | xchroot) | | 484 | xchroot) |
| | | 485 | if [ ! -s \$sandbox/.sandbox_mounted ]; then |
| | | 486 | echo \$sandbox not mounted && exit 1 |
| | | 487 | fi |
474 | case x\$2 in | | 488 | case x\$2 in |
475 | x) rootshell=/bin/ksh ;; | | 489 | x) rootshell=/bin/ksh ;; |
476 | *) rootshell="\$2" ;; | | 490 | *) rootshell="\$2" ;; |
477 | esac | | 491 | esac |
478 | script="\$sandbox/tmp/script.\$\$" | | 492 | script="\$sandbox/tmp/script.\$\$" |
479 | echo "#!/bin/sh" > \$script | | 493 | echo "#!/bin/sh" > \$script |
480 | echo "ENV=/etc/shrc \$rootshell" >> \$script | | 494 | echo "ENV=/etc/shrc \$rootshell" >> \$script |
481 | chmod +x \$script | | 495 | chmod +x \$script |
482 | ENV=/etc/shrc chroot \$sandbox /tmp/\`basename \$script\` | | 496 | ENV=/etc/shrc chroot \$sandbox /tmp/\`basename \$script\` |
483 | rm -f \$script | | 497 | rm -f \$script |
484 | ;; | | 498 | ;; |
| | | 499 | xismounted) |
| | | 500 | if [ -s \$sandbox/.sandbox_mounted ]; then |
| | | 501 | echo \$sandbox is mounted && exit 0 |
| | | 502 | else |
| | | 503 | echo \$sandbox is not mounted && exit 1 |
| | | 504 | fi |
| | | 505 | ;; |
485 | *) | | 506 | *) |
486 | if [ \$# -eq 0 ]; then | | 507 | if [ \$# -eq 0 ]; then |
487 | set dummy /bin/sh | | 508 | set dummy /bin/sh |
488 | shift | | 509 | shift |
489 | fi | | 510 | fi |
490 | chroot \$sandbox "\$@" | | 511 | chroot \$sandbox "\$@" |
491 | ;; | | 512 | ;; |
492 | esac | | 513 | esac |
493 | EOS | | 514 | EOS |
494 | | | 515 | |
495 | chmod +x $sandbox_script | | 516 | chmod +x $sandbox_script |
496 | | | 517 | |
497 | case $opsys in | | 518 | case $opsys in |