Bash Scripts

aus Metalab Wiki, dem offenen Zentrum für meta-disziplinäre Magier und technisch-kreative Enthusiasten.
Zur Navigation springenZur Suche springen

chrtsetup

set up mounts and enter a chroot


chrtsetup configuration file to set up a unionfs of a disk image and a filesystem tree:

#!bin/bash

# only $CHROOT and $MOUNTS are relevant to chrtsetup
ROOT=/home/user/ubuntu_unionfs
IMG=$ROOT/ext3_1GB.dd
IMG_MNT=$ROOT/diskimage
SHARED=$ROOT/shared
FS=$ROOT/chroot
CHROOT=$ROOT/chroot

#from           to                      fstype  mount-options   do-chroot
MOUNTS=`echo -e "\
/dev            $CHROOT/dev             none    bind            false   \n\
$ROOT/shared    $CHROOT/root/shared     none    bind            false   \n\
$IMG            $IMG_MNT                ext3    loop            false   \n\
unionfs         $CHROOT                 unionfs dirs=$IMAGE_MNT=rw:$FS=ro false \n\
none            /proc                   proc    defaults        true    \n\
none            /sys                    sysfs   defaults        true    \n\
none            /dev/pts                devpts  defaults        true    \n\

the script:

#!/bin/bash
PREPARE=; ENTER=; CLEAN=; VERBOSE=;
RUN=/bin/bash
CONFIG=

function verbose  { [ $VERBOSE ] && echo $@; $@ 1> /dev/null; }
function warn { [ $VERBOSE ] && echo "warn: $1" >&2; }
function error { echo "error: $1" >&2; exit 1; }
function usage {
  cat <<EOF
Usage: chrtsetup [OPTION] [CHRTCONF]
  -v, --verbose            print actions
  -p, --prepare            perform mount sequence only
  -c, --clean              perform umount sequence only
  -e, --enter              enter chroot only
  -r, --run=COMMAND        run specified COMMAND after entering the chroot

CHRTCONF is the name of the chroot configuration stored in /etc/chrtsetup/.
If neither -p, -c nor -e is set all actions are enabled.

Examples:
  chrtsetup ubuntu      # executed setup defined in /etc/chrtsetup/ubuntu, enter chroot and clean up after
  chrtsetup -c ubuntu   # undo mounts performed to setup the chroot
  chrtsetup -pe ubuntu  # mount, enter but do not clean up after
EOF

  exit 2
}

function chrtify { cmd=$1 doChroot=$2
  if [ "$doChroot" == "true" ]; then 
    echo "chroot -- $CHROOT $cmd"
  else
    echo $cmd
  fi
}

function doUmount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5
  umount=`chrtify 'umount' $doChroot`
  verbose $umount -t $fstype $to || error "umount failed: $@"
}

function doMount { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5
  mount=`chrtify 'mount' $doChroot`
  verbose $mount -t $fstype -o $opts $from $to || error "mount failed: $@"
}

function isMounted { from=$1 to=$2 fstype=$3 opts=$4 doChroot=$5
  grepMounts=`chrtify "grep -qF /etc/mtab -e" $doChroot`
  $grepMounts "$from $to $fstype"
} 

function clean {
  echo "$MOUNTS" | tac | while read mnt; do
    isMounted $mnt && doUmount $mnt \
      || warn "$mnt not mounted" >&2
  done
}

function prepare {
  echo "$MOUNTS" | while read mnt; do
    isMounted $mnt || doMount $mnt \
      && warn "$mnt already mounted" >&2
  done
}

[ $# -eq 0 ] && usage

eval set -- "`getopt -o vpecr --long verbose,prepare,enter,clean,run -n 'chrtsetup' -- \"$@\"`"
while true ; do
        case "$1" in
                -v|--verbose) VERBOSE=0 ; shift ;;
                -p|--prepare) PREPARE=0 ; shift ;;
                -e|--enter) ENTER=0 ; shift ;;
                -c|--clean) CLEAN=0 ; shift ;;
                -r|--run) RUN="$2" ; shift 2;;
                --) shift ; break ;;
        esac
done
CONFIG=$1

[ -z $CONFIG ] && usage
[ ! "$CLEAN" -a ! "$PREPARE" -a  ! "$ENTER" ] && CLEAN=set PREPARE=set ENTER=set

source "/etc/chrtsetup/$CONFIG"

[ $PREPARE ] && prepare
[ $ENTER ]  && chroot "$CHROOT" "$RUN"
[ $CLEAN ]  && clean


gstrtpcast

Grab audio from a pulse monitor src and stream it via rtp. though pulseaudio is capable of streaming sound via rtp it is not able to compress the stream. additionally its command line tools are crap and its not nearly as flexible as gstreamer. You need to load the module for the alsa dummy device (snd_dummy) before running the script.

#!/bin/bash
ENCODER=" vorbisenc ! rtpvorbispay ";
DEVICE="/dev/video0"
GST_OPTS=
SET_SINK=
CLIENTS=
VERBOSE=

TEMP=`getopt -o e:d:g:vs --long encoder:,device:,gst-opts:,verbose,set-default-sink \
     -n 'gstrtpcast' -- "$@"`

[ $? != 0 ] && exit 1

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-e|--encoder) ENCODER="$2" ; shift 2 ;;
                -d|--device) SRC_NAME="$2" ; shift 2 ;;
		-g|--gst-opts) GST_OPTS="$GST_OPTS $2" ; shift 2 ;;
		-v|--verbose) VERBOSE=0 ; shift ;;
		-s|--set-default-sink) SET_SINK=0; shift ;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
done

CLIENTS="$1"

[ -n $DEVICE ] && DEVICE=`pacmd list-sources | fgrep 'alsa_output.platform-snd_dummy' | awk -F '[<>]' '{ print $2 }'`
[ $SET_SINK ] && pacmd "set-default-sink $[ `echo $SRC_NAME | awk -F '.' '{ print $3 }'` + 1 ]" 1> /dev/null;

echo -e "---\n"
echo "cast: $DEVICE";
echo "to: $CLIENTS";
echo "encoder: $ENCODER"
echo -e "\n---\n"

[ $VERBOSE ] && GST_OPTS="$GST_OPTS -v" echo "gst-launch $GST_OPT pulsesrc device=\"$DEVICE\" ! $ENCODER ! multiudpsink clients=\"$CLIENTS\""

gst-launch $GST_OPTS pulsesrc device="$DEVICE" ! $ENCODER ! multiudpsink clients="$CLIENTS";