setup-lbu.in 3.21 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6 7
#!/bin/sh

PREFIX=

. "$PREFIX/lib/libalpine.sh"

usage() {
8 9
	cat <<-__EOF__
		usage: setup-lbu [-hq] [MEDIA]
Natanael Copa's avatar
Natanael Copa committed
10

11
		Setup lbu media settings.
Natanael Copa's avatar
Natanael Copa committed
12

13
		MEDIA is optional mountpoint under /media
Natanael Copa's avatar
Natanael Copa committed
14

15 16 17
		options:
		 -h  Show this help
		 -q  Quietly pick best suggestion. Only prompts user if unsure.
Natanael Copa's avatar
Natanael Copa committed
18

19
	__EOF__
20
	exit 1
Natanael Copa's avatar
Natanael Copa committed
21 22 23
}

get_mnt_line() {
24 25
	local mntpoint="$1"
	local mnttab="$2"
26 27 28 29 30 31 32 33
	local media=${mntpoint#/media/}
	local uuid=
	# replace the device with UUID since the device is in /proc/mounts
	# and we want the UUID in fstab
	if [ "${media#UUID}" != "$media" ]; then
		uuid="$media"
	fi

34 35
	# we need filter out codepage=... in mount option as it makes
	# mount fail if its there.
36 37 38 39
	awk -v uuid="$uuid" "\$2 == \"$mntpoint\" {
		if (uuid)
			\$1 = uuid;
		gsub(/,codepage=.*,/, \",\", \$4);
40 41
		print \$0;
	}" "$mnttab"
Natanael Copa's avatar
Natanael Copa committed
42
}
43

Natanael Copa's avatar
Natanael Copa committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
is_mounted() {
	test -n "$(get_mnt_line $1 /proc/mounts)"
}

is_in_fstab() {
	test -n "$(get_mnt_line $1 /etc/fstab)"
}

set_media() {
	local media="${1%/}"	# strip trailing /
	local mnt=/media/$media

	if [ -d "$media" ] && [ "${media#/media/}" != "$media" ]; then
		mnt="$media"
		media=${mnt#/media/}
	fi
60
	if [ "$ROOT" = "/" ] && ! [ -d "$mnt" ]; then
Natanael Copa's avatar
Natanael Copa committed
61 62 63 64 65
		echo "$mnt: not a directory" >&2
		exit 1
	fi

	# set  LBU_MEDIA in /etc/lbu/lbu.conf
66 67 68 69 70 71 72 73 74 75 76 77 78
	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
Natanael Copa's avatar
Natanael Copa committed
79 80 81 82 83 84 85
	fi

	# append to fstab if its missing
	if ! is_in_fstab $mnt && is_mounted $mnt; then
		get_mnt_line "$mnt" /proc/mounts >> /etc/fstab
	fi

86
	# hack in case we have alpine_dev mounted on /media/usbdisk but
Natanael Copa's avatar
Natanael Copa committed
87 88 89 90 91 92 93 94 95 96
	# 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
97
	case $opt in
Natanael Copa's avatar
Natanael Copa committed
98 99
		h) usage;;
		q) quiet=1;;
100
	esac
Natanael Copa's avatar
Natanael Copa committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114
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
	[ "$dir" = "/media/cdrom" ] && continue
Natanael Copa's avatar
Natanael Copa committed
115
	alternatives="$alternatives, '${dir#/media/}'"
Natanael Copa's avatar
Natanael Copa committed
116 117 118 119 120 121
	if is_mounted $dir; then
		suggestion=${dir#/media/}
		[ -n "$quiet" ] && media=$suggestion
	fi
done

Natanael Copa's avatar
Natanael Copa committed
122 123 124
# strip leading , + space
alternatives=${alternatives#, }

Natanael Copa's avatar
Natanael Copa committed
125 126 127 128 129 130 131 132 133 134
# 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
Natanael Copa's avatar
Natanael Copa committed
135
	echo -n "Enter where to store configs ($alternatives or 'none') [$suggestion]: "
Natanael Copa's avatar
Natanael Copa committed
136 137 138 139 140 141 142 143 144 145 146 147 148
	default_read media $suggestion
	if [ "$media" = "none" ] || [ -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"