#!/bin/sh PREFIX=@PREFIX@ : ${LIBDIR=$PREFIX/lib} . "$LIBDIR/libalpine.sh" usage() { cat <<-__EOF__ usage: setup-lbu [-hq] [MEDIA] Setup lbu media settings. MEDIA is optional mountpoint under /media options: -h Show this help -q Quietly pick best suggestion. Only prompts user if unsure. __EOF__ exit $1 } get_filesystem_type() { local mountpoint="$1" shift awk "\$2==\"$mountpoint\" {print \$3}" "$@" } get_dev() { local mountpoint="$1" shift awk "\$2==\"$mountpoint\" {print \$1}" "$@" } is_in_fstab() { test -n "$(get_filesystem_type $1 /etc/fstab)" } is_mounted() { test -n "$(get_filesystem_type $1 /proc/mounts)" } is_iso9660() { local fs for fs in $(get_filesystem_type $1 /proc/mounts /etc/fstab); do if [ "$fs" = "iso9660" ]; then return 0 fi done return 1 } set_media() { local media="${1%/}" # strip trailing / local mnt=/media/$media case "$media" in LABEL=*|UUID=*) mkdir -p $ROOT/media/$media;; esac if [ -d "$media" ] && [ "${media#/media/}" != "$media" ]; then mnt="$media" media=${mnt#/media/} fi if [ "$ROOT" = "/" ] && ! [ -d "$mnt" ]; then echo "$mnt: not a directory" >&2 exit 1 fi # set LBU_MEDIA in /etc/lbu/lbu.conf if [ -f "${ROOT}"etc/lbu/lbu.conf ]; then sed -e "/^\#\?[[:space:]]*LBU_MEDIA=.*/s/.*/LBU_MEDIA=$media/" \ -i "${ROOT}"etc/lbu/lbu.conf if ! egrep -q '^LBU_MEDIA=' "${ROOT}"etc/lbu/lbu.conf; then echo "LBU_MEDIA=$media" >> "${ROOT}"etc/lbu/lbu.conf fi else mkdir -p "${ROOT}"etc/lbu echo "LBU_MEDIA=$media" >> "${ROOT}"etc/lbu/lbu.conf fi if [ -n "$ROOT" ] && [ "$ROOT" != "/" ]; then return fi # append to fstab if its missing if ! is_in_fstab $mnt; then case "$media" in LABEL=*|UUID=*) dev=$(findfs $media) ;; *) dev=$(get_dev $mnt /proc/mounts) ;; esac if [ -z "$dev" ]; then echo "$media: Could not find device" >&2 exit 1 fi # get TYPE=... LABEL=... UUID=... from blkid eval $(blkid $dev | awk -F: '{print $2}') if [ -z "$TYPE" ]; then echo "$media: Could not find filesystem type" >&2 exit 1 fi # use LABEL= if it was specifically selected, otherwise use UUID case "$media" in LABEL=*|UUID=*) UUID="$media";; esac printf "%s\t%s\t%s\tnoauto,defaults 0 0\n" "${UUID:-$dev}" "$mnt" "$TYPE" >> /etc/fstab fi # hack in case we have alpine_dev mounted on /media/usbdisk but # lbu is stored on /media/usb # Otherwise we get issues when we do lbu commit. if [ "$media" = "usb" ] && is_mounted /media/usbdisk; then mount --move /media/usbdisk /media/usb elif [ "$media" = "usbdisk" ] && is_mounted /media/usb; then mount --move /media/usb /media/usbdisk fi } while getopts "hq" opt; do case $opt in h) usage 0;; q) quiet=1;; '?') usage "1" >&2;; esac done shift $(($OPTIND - 1)) # check if MEDIA option was given if [ -n "$1" ]; then set_media "$1" exit fi alternatives= suggestion="none" for dir in /media/*; do [ -d "$dir" ] || continue if is_iso9660 "$dir"; then continue fi alternatives="$alternatives, '${dir#/media/}'" if is_mounted "$dir"; then suggestion=${dir#/media/} [ -n "$quiet" ] && media=$suggestion fi done if findfs "LABEL=APKOVL" >/dev/null; then suggestion="LABEL=APKOVL" case ", $alternatives, " in *"'LABEL=APKOVL'"*) ;; *) alternatives="$alternatives, 'LABEL=APKOVL'" ;; esac fi # strip leading , + space alternatives=${alternatives#, } # if nothing is mounted (or boot from cdrom) usbmnt=$(awk '$1 == "/dev/usbdisk" {print $2}' /proc/mounts) if [ -z "$suggestion" ] && [ -n "$usbmnt" ]; then suggestion=${usbmnt#/media/} if [ -n "$quiet" ] && [ -e /dev/usbdisk ]; then media=$suggestion fi fi while [ -z "$media" ]; do ask "Enter where to store configs ($alternatives or 'none')" "$suggestion" media="$resp" case "$media" in none|LABEL=*) break;; esac if [ -d "/media/$media" ]; then break fi echo "/media/$media is not a directory. Please try again." media= done if [ "$media" = "none" ]; then exit 0 fi set_media "$media"