summaryrefslogtreecommitdiff
path: root/setup-lbu.in
blob: 7c3c21e08034c031a0807484ed6579ca7f7d6177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/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}" "$@" 2>/dev/null
}

get_dev() {
	local mountpoint="$1"
	shift
	awk "\$2==\"$mountpoint\" {print \$1}" "$@" 2>/dev/null
}

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

is_mounted() {
	test -n "$(get_filesystem_type $1 "$ROOT"/proc/mounts)"
}

is_iso9660() {
	local fs
	for fs in $(get_filesystem_type $1 "$ROOT"/proc/mounts "$ROOT"/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

	# append to fstab if its missing
	if ! is_in_fstab $mnt; then
		case "$media" in
			LABEL=*|UUID=*)
				dev=$(findfs $media)
				;;
			*)	dev=$(get_dev $mnt "$ROOT"/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
		if [ -n "$UUID" ]; then
			UUID="UUID=$UUID"
		fi
		# use LABEL= if it was specifically selected, otherwise use UUID
		case "$media" in
			LABEL=*|UUID=*) UUID="$media";;
		esac
		mkdir -p "$ROOT"/etc
		printf "%s\t%s\t%s\tnoauto,ro 0 0\n" "${UUID:-$dev}" "$mnt" "$TYPE" >> "$ROOT"/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
	if [ "$1" = "none" ]; then
		exit
	fi
	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"