void-packages

Void Source Packages
git clone git://ezup.dev/void-packages.git
Log | Files | Refs | README | LICENSE

ethereal.sh (3421B)


      1#!/bin/sh
      2#
      3# This chroot script uses symlinks to emulate being in a chroot using
      4# the host system as the masterdir
      5#
      6# It will damage your host system, only use it in disposable
      7# containers.
      8#
      9# 2 extra steps required when using this chroot-style:
     10# 1. Symlink / to masterdir inside the void-packages repo
     11# 2. write the arch of the host system, as dictated by xbps-uhelper arch
     12# into /.xbps_chroot_init
     13#
     14# The supported way to make use of thie chroot-style is to create
     15# a root filesystem that has base-chroot and git installed and 
     16# have it inside a container engine like Docker.
     17#
     18# Docker example:
     19# $ mkdir -p /tmp/image
     20# $ xbps-install -y -r /tmp/image \
     21#				 -R http://mirrors.servercentral.com/voidlinux/current \
     22#				 -S base-chroot
     23# $ tar -pC /tmp/image -c . | sudo docker import - voidlinux/masterdir
     24# $ rm -rf /tmp/image 
     25# # docker run --rm -it \
     26#			   -e XBPS_CHROOT_CMD=ethereal \
     27#			   -e XBPS_ALLOW_CHROOT_BREAKOUT=yes \
     28#			   -v $(pwd):/hostrepo voidlinux/masterdir \
     29#			   /bin/bash -c 'ln -s / /hostrepo/masterdir && /hostrepo/xbps-src pkg <pkgname>'
     30#
     31
     32readonly MASTERDIR="$1"
     33readonly DISTDIR="$2"
     34readonly HOSTDIR="$3"
     35readonly EXTRA_ARGS="$4"
     36readonly CMD="$5"
     37shift 5
     38
     39if [ -z "$MASTERDIR" -o -z "$DISTDIR" ]; then
     40	echo "$0 MASTERDIR/DISTDIR not set"
     41	exit 1
     42fi
     43
     44msg_red() {
     45	# error messages in bold/red
     46	[ -n "$NOCOLORS" ] || printf >&2 "\033[1m\033[31m"
     47	printf "=> ERROR: %s\\n" "$@" >&2
     48	[ -n "$NOCOLORS" ] || printf >&2 "\033[m"
     49}
     50
     51fake_mount() {
     52	# If we already have a symlink from the desired place
     53	# to the base location then just return 0
     54	if [ -L "$2" -a "$(readlink "$2")" = "$1" ]; then
     55		return 0
     56	fi
     57
     58	if [ -d "$2" ] && ! rmdir "$2" >/dev/null 2>&1; then
     59		msg_red "Failed to remove $2, not empty ?\n"
     60		exit 1
     61	fi
     62
     63	[ -f "$2" -o -L "$2" ] && rm -f "$2"
     64
     65	ln -s "$1" "$2"
     66	echo "linked $2 -> $1"
     67}
     68
     69if [ "${XBPS_ALLOW_CHROOT_BREAKOUT}" != "yes" ]; then
     70	msg_red "chroot-style 'ethereal' requires XBPS_ALLOW_CHROOT_BREAKOUT=yes\n"
     71	msg_red "This chroot-style is meant for disposable containers and will destroy your system\n"
     72	exit 1
     73fi
     74
     75if [ ! -L "$MASTERDIR" -o "$(readlink "$MASTERDIR")" != "/" ]; then
     76	msg_red "$MASTERDIR isn't symlinked to /!\n"
     77	exit 1
     78fi
     79
     80fake_mount "$DISTDIR" "$MASTERDIR"/void-packages
     81
     82# Do the same for hostdir
     83if [ -n "$HOSTDIR" ]; then
     84	fake_mount "$HOSTDIR" "$MASTERDIR"/host
     85fi
     86
     87# xbps-src may send some other binds, parse them here
     88while getopts 'b:' c -- "$EXTRA_ARGS"; do
     89	# Skip everything that's not a bind
     90	[ "$c" = "b" ] || continue
     91
     92	from="${OPTARG%:*}"
     93	to="${OPTARG#*:}"
     94
     95	fake_mount "$from" "$to"
     96
     97	mounts="${mounts} $to"
     98done
     99
    100# Store current directory for returning later
    101OLDPWD="$(pwd)"
    102
    103# To give the illusion we entered the chroot, cd to /
    104cd / || {
    105	msg_red "Failed to change directory to root!\n"
    106	exit 1 ; }
    107
    108# Tell xbps-src that we are "in the chroot"
    109# Start with `env` so our environment var's stay the same
    110env IN_CHROOT=1 $CMD $@
    111
    112# Store return of the command we care about
    113ret="$?"
    114
    115# Return to OLDPWD
    116cd "${OLDPWD}"
    117
    118# Remove the symlink and restore an empty dir to simulate
    119# an umount operation.
    120if [ -n "$HOSTDIR" ]; then
    121	rm -f "$MASTERDIR"/host
    122	mkdir -p "$MASTERDIR"/host
    123fi
    124
    125# Same as the operation above, do it all for all mountpoints
    126# that were passed to us.
    127for m in $mounts; do
    128	rm -f "$m"
    129	mkdir -p "$m"
    130done
    131
    132rm -f "$MASTERDIR"/void-packages
    133mkdir -p "$MASTERDIR"/void-packages
    134
    135exit $ret