File: preseed.sh

package info (click to toggle)
preseed 1.102
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 812 kB
  • sloc: sh: 435; perl: 153; makefile: 5
file content (182 lines) | stat: -rw-r--r-- 4,181 bytes parent folder | download | duplicates (2)
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
#!/bin/sh

logfile=/var/lib/preseed/log

log () {
	logger -t preseed "$@"
}

error () {
	error="$1"
	location="$2"
	checksum="$3"
	db_subst preseed/$error LOCATION "$location"
	[ -z "$checksum" ] || {
		db_subst preseed/$error CHECKSUM "$checksum"
		db_subst preseed/$error ALGORITHM "MD5"
	}
	db_input critical preseed/$error || true
	db_go || true
	exit 1
}

# Function to implement the behaviour documented in README.preseed_fetch
make_absolute_url() {
	url="$1"
	last="$2"

	if [ -n "${url##*://*}" ]; then
		# url does not contain ://
		if [ -z "${url##/*}" ]; then
			# url starts with /
			if [ -z "${last##*/./*}" ]; then
				# if last has a /./, start the "root" there
				url="${last%%/./*}/.$url"
			else
				# if not, strip the path component of $last
				url="$(expr $last : '\([^:]*://[^/]*/\)')$url"
			fi
		else
			# for relative urls, just replace the old filename
			url="${last%/*}/$url"
		fi
	fi
	echo "$url"
}

preseed_location () {
	local location="$1"
	local checksum="$2"
	local sum torun torun_checksum
	
	local tmp=/tmp/debconf-seed
	
	local retval=0
	preseed_fetch "$location" "$tmp" "$checksum" || retval=$?
	case $retval in
	    0) ;;
	    2) error checksum_error "$location" "$checksum" ;;
	    *) error retrieve_error "$location" ;;
	esac

	db_set preseed/include ""
	db_set preseed/include_command ""
	db_set preseed/run ""
	db_set preseed/run/checksum ""
	UNSEEN=
	db_get preseed/interactive
	if [ "$RET" = true ]; then
		UNSEEN=--unseen
	fi
	if ! debconf-set-selections $UNSEEN $tmp; then
		error load_error "$location"
	fi
	rm -f $tmp

	log "successfully loaded preseed file from $location"
	[ -e /var/run/delay_choosers ] && rm /var/run/delay_choosers
	local last_location="$location"
	
	while true ; do
		db_get preseed/include
		local include="$RET"
		db_get preseed/include_command
		if [ -n "$RET" ]; then
			include="$include $(eval $RET)" || true # TODO error handling?
		fi
		if db_get preseed/include/checksum; then
			checksum="$RET"
		else
			checksum=""
		fi
		db_get preseed/run
		torun="$RET"
		if db_get preseed/run/checksum; then
			torun_checksum="$RET"
		else
			torun_checksum=""
		fi

		# not really sure if the ones above are required if this is here
		db_set preseed/include ""
		db_set preseed/include/checksum ""
		db_set preseed/include_command ""
		db_set preseed/run ""
		db_set preseed/run/checksum ""

		[ -n "$include" -o -n "$torun" ] || break

		for location in $include; do
			sum="${checksum%% *}"
			checksum="${checksum#$sum }"

			location=$(make_absolute_url "$location" "$last_location")
			# BTW -- is this test for empty strings really needed?
			if [ -n "$location" ]; then
				preseed_location "$location" "$sum"
			fi
		done
	
		echo $last_location > /var/run/preseed.last_location

		for location in $torun; do
			sum="${torun_checksum%% *}"
			torun_checksum="${torun_checksum#$sum }"

			location=$(make_absolute_url "$location" "$last_location")
			# BTW -- is this test for empty strings really needed?
			if [ -n "$location" ]; then
				retval=0
				preseed_fetch "$location" "$tmp" "$sum" || retval=$?
				case $retval in
				    0) ;;
				    2)
					log "checksum error \"$location\", expected \"$sum\""
					error checksum_error "$location" "$sum"
					;;
				    *)
					log "error fetching \"$location\""
					error retrieve_error "$location"
					;;
				esac
				chmod +x $tmp
				if ! log-output -t preseed/run $tmp; then
					log "error running \"$location\""
					error load_error "$location"
				fi
				log "successfully ran \"$location\""
				rm -f $tmp
			fi
		done
	done
}
	
preseed () {
	template="$1"

	db_get $template
	location="$RET"
	if db_get $template/checksum; then
		checksum="$RET"
	else
		checksum=""
	fi
	for loc in $location; do
		sum="${checksum%% *}"
		checksum="${checksum#$sum }"
		
		preseed_location "$loc" "$sum"
	done
}

# Check for DHCP filename preseeding
dhcp_preseed_url () {
	for file in /var/lib/dhcp/dhclient.leases /var/lib/dhcp3/dhclient.leases /var/lib/udhcp/udhcpc.leases; do
		if [ -r "$file" ]; then
			FN="$(sed -n -e '/ filename "/ s/.*"\(.*\)"./\1/p' $file | tail -n1)"
			if echo "$FN" | grep -q "://" ; then
				echo "$FN"
			fi
		fi
	done
}