File: preseed.sh

package info (click to toggle)
preseed 1.66
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, jessie-kfreebsd-proposed-updates
  • size: 776 kB
  • sloc: sh: 390; perl: 105; makefile: 5
file content (156 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (3)
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
#!/bin/sh

logfile=/var/lib/preseed/log

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

error () {
	error="$1"
	location="$2"
	db_subst preseed/$error LOCATION "$location"
	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 tmp=/tmp/debconf-seed
	
	if ! preseed_fetch "$location" "$tmp"; then
		error retrieve_error "$location"
	fi
	if [ -n "$checksum" ] && \
	   [ "$(md5sum $tmp | cut -d' ' -f1)" != "$checksum" ]; then
		error retrieve_error "$location"
	fi

	db_set preseed/include ""
	db_set preseed/include_command ""
	db_set preseed/run ""
	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
		local torun="$RET"

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

		[ -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
			location=$(make_absolute_url "$location" "$last_location")
			# BTW -- is this test for empty strings really needed?
			if [ -n "$location" ]; then
				if ! preseed_fetch "$location" "$tmp"; then
					log "error fetching \"$location\""
					error retrieve_error "$location"
				fi
				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
}