~ubuntu-branches/ubuntu/trusty/debian-installer-utils/trusty-proposed

4 by Colin Watson
* Resynchronise with Debian.
1
# Setup for using apt to install packages in /target.
2
3
mountpoints () {
4
	cut -d" " -f2 /proc/mounts | sort | uniq
5
}
6
1.2.9 by Otavio Salvador, Colin Watson, Samuel Thibault, Otavio Salvador, Updated translations
[ Colin Watson ]
7
# Make sure mtab in the chroot reflects the currently mounted partitions.
8
update_mtab() {
9
	mtab=/target/etc/mtab
10
11
	if [ -h "$mtab" ]; then
12
		logger -t $0 "warning: $mtab won't be updated since it is a symlink."
13
		return 0
14
	fi
15
16
	egrep '^[^ ]+ /target' /proc/mounts | (
17
	while read devpath mountpoint fstype options n1 n2 ; do
18
		devpath=`mapdevfs $devpath || echo $devpath`
19
		mountpoint="${mountpoint#/target}"
20
		# mountpoint for root will be empty
21
		if [ -z "$mountpoint" ] ; then
22
			mountpoint="/"
23
		fi
24
		echo $devpath $mountpoint $fstype $options $n1 $n2
25
	done ) > $mtab
26
}
27
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
28
divert () {
1.2.18 by Colin Watson, Scott Moser
[ Scott Moser ]
29
	chroot /target dpkg-divert --quiet --add --divert "$1.REAL" --rename "$1"
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
30
}
31
32
undivert () {
33
	rm -f "/target$1"
1.2.18 by Colin Watson, Scott Moser
[ Scott Moser ]
34
	chroot /target dpkg-divert --quiet --remove --rename "$1"
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
35
}
36
4 by Colin Watson
* Resynchronise with Debian.
37
chroot_setup () {
17 by Colin Watson
* Resynchronise with Debian. Remaining changes:
38
	# Bail out if directories we need are not there
39
	if [ ! -d /target/sbin ] || [ ! -d /target/usr/sbin ] || \
40
	   [ ! -d /target/proc ]; then
41
		return 1
42
	fi
1.2.8 by Colin Watson, Aurelien Jarno, Colin Watson, Jeremie Koenig
[ Aurelien Jarno ]
43
	if [ -d /sys/devices ] && [ ! -d /target/sys ]; then
26 by Colin Watson
* Resynchronise with Debian. Remaining changes:
44
		return 1
45
	fi
17 by Colin Watson
* Resynchronise with Debian. Remaining changes:
46
13 by Colin Watson
Resynchronise with Debian.
47
	if [ -e /var/run/chroot-setup.lock ]; then
48
		cat >&2 <<EOF
49
apt-install or in-target is already running, so you cannot run either of
50
them again until the other instance finishes. You may be able to use
51
'chroot /target ...' instead.
52
EOF
17 by Colin Watson
* Resynchronise with Debian. Remaining changes:
53
		return 1
13 by Colin Watson
Resynchronise with Debian.
54
	fi
55
	touch /var/run/chroot-setup.lock
56
4 by Colin Watson
* Resynchronise with Debian.
57
	# Create a policy-rc.d to stop maintainer scripts using invoke-rc.d 
58
	# from running init scripts. In case of maintainer scripts that don't
59
	# use invoke-rc.d, add a dummy start-stop-daemon.
60
	cat > /target/usr/sbin/policy-rc.d <<EOF
61
#!/bin/sh
62
exit 101
63
EOF
64
	chmod a+rx /target/usr/sbin/policy-rc.d
65
	
66
	if [ -e /target/sbin/start-stop-daemon ]; then
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
67
		divert /sbin/start-stop-daemon
4 by Colin Watson
* Resynchronise with Debian.
68
	fi
69
	cat > /target/sbin/start-stop-daemon <<EOF
70
#!/bin/sh
71
echo 1>&2
72
echo 'Warning: Fake start-stop-daemon called, doing nothing.' 1>&2
73
exit 0
74
EOF
75
	chmod a+rx /target/sbin/start-stop-daemon
76
	
1.2.2 by Colin Watson, Colin Watson, Aurelien Jarno, Chris Lamb, Frans Pop, Ryan Niebur, Updated translations
[ Colin Watson ]
77
	# If Upstart is in use, add a dummy initctl to stop it starting jobs.
78
	if [ -x /target/sbin/initctl ]; then
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
79
		divert /sbin/initctl
1.2.2 by Colin Watson, Colin Watson, Aurelien Jarno, Chris Lamb, Frans Pop, Ryan Niebur, Updated translations
[ Colin Watson ]
80
		cat > /target/sbin/initctl <<EOF
81
#!/bin/sh
82
echo 1>&2
83
echo 'Warning: Fake initctl called, doing nothing.' 1>&2
84
exit 0
85
EOF
86
		chmod a+rx /target/sbin/initctl
87
	fi
88
4 by Colin Watson
* Resynchronise with Debian.
89
	# Record the current mounts
90
	mountpoints > /tmp/mount.pre
91
1.2.4 by Aurelien Jarno, Updated translations
* Add GNU/kFreeBSD specific code to setup the chroot.
92
	case `udpkg --print-os` in
93
	        "linux")
94
			# Some packages (eg. the kernel-image package) require a mounted
95
			# /proc/. Only mount it if not mounted already
96
			if [ ! -f /target/proc/cmdline ]; then
97
				mount -t proc proc /target/proc
98
			fi
99
100
			# For installing >=2.6.14 kernels we also need sysfs mounted
101
			# Only mount it if not mounted already
102
			if [ ! -d /target/sys/devices ]; then
103
				mount -t sysfs sysfs /target/sys
104
			fi
105
106
			# In Lenny, /dev/ lacks the pty devices, so we need devpts mounted
107
			if [ ! -e /target/dev/pts/0 ]; then
108
				mkdir -p /target/dev/pts
109
				mount -t devpts devpts -o noexec,nosuid,gid=5,mode=620 \
110
					/target/dev/pts
111
			fi
1.2.13 by Colin Watson, Samuel Thibault, Colin Watson
[ Samuel Thibault ]
112
1.2.15 by Colin Watson, Updated translations
* Fix detection of whether /target/run has already been mounted (closes:
113
			if ! mountpoints | grep -q '^/target/run$'; then
1.2.13 by Colin Watson, Samuel Thibault, Colin Watson
[ Samuel Thibault ]
114
				mount --bind /run /target/run
115
			fi
1.2.4 by Aurelien Jarno, Updated translations
* Add GNU/kFreeBSD specific code to setup the chroot.
116
		;;
117
	        "kfreebsd")
118
			# Some packages (eg. the kernel-image package) require a mounted
119
			# /proc/. Only mount it if not mounted already
120
			if [ ! -f /target/proc/cmdline ]; then
121
				mount -t procfs proc /target/proc
122
			fi
1.2.8 by Colin Watson, Aurelien Jarno, Colin Watson, Jeremie Koenig
[ Aurelien Jarno ]
123
			# Some package might need sysfs mounted
124
			# Only mount it if not mounted already
125
			if [ ! -d /target/sys/devices ]; then
126
				mount -t linsysfs sysfs /target/sys
127
			fi
1.2.4 by Aurelien Jarno, Updated translations
* Add GNU/kFreeBSD specific code to setup the chroot.
128
		;;
129
	esac
29 by Colin Watson
* Resynchronise with Debian. Remaining changes:
130
1.2.9 by Otavio Salvador, Colin Watson, Samuel Thibault, Otavio Salvador, Updated translations
[ Colin Watson ]
131
	mountpoints > /tmp/mount.post
132
133
	update_mtab
134
4 by Colin Watson
* Resynchronise with Debian.
135
	# Try to enable proxy when using HTTP.
136
	# What about using ftp_proxy for FTP sources?
137
	RET=$(debconf-get mirror/protocol || true)
138
	if [ "$RET" = "http" ]; then
139
		RET=$(debconf-get mirror/http/proxy || true)
29 by Colin Watson
* Resynchronise with Debian. Remaining changes:
140
		if [ "$RET" ]; then
4 by Colin Watson
* Resynchronise with Debian.
141
			http_proxy="$RET"
142
			export http_proxy
143
		fi
144
	fi
5 by Colin Watson
* Resynchronise with Debian.
145
4 by Colin Watson
* Resynchronise with Debian.
146
	# Pass debconf priority through.
147
	DEBIAN_PRIORITY=$(debconf-get debconf/priority || true)
148
	export DEBIAN_PRIORITY
149
30 by Colin Watson
* Resynchronise with Debian. Remaining changes:
150
	LANG=${IT_LANG_OVERRIDE:-$(debconf-get debian-installer/locale || true)}
5 by Colin Watson
* Resynchronise with Debian.
151
	export LANG
12 by Colin Watson
Resynchronise with Debian.
152
	export PERL_BADLANG=0
5 by Colin Watson
* Resynchronise with Debian.
153
4 by Colin Watson
* Resynchronise with Debian.
154
	# Unset variables that would make scripts in the target think
155
	# that debconf is already running there.
156
	unset DEBIAN_HAS_FRONTEND
157
	unset DEBIAN_FRONTEND
158
	unset DEBCONF_FRONTEND
159
	unset DEBCONF_REDIR
160
	# Avoid debconf mailing notes.
161
	DEBCONF_ADMIN_EMAIL=""
162
	export DEBCONF_ADMIN_EMAIL
29 by Colin Watson
* Resynchronise with Debian. Remaining changes:
163
	# Avoid apt-listchanges doing anything.
164
	APT_LISTCHANGES_FRONTEND=none
165
	export APT_LISTCHANGES_FRONTEND
1.2.14 by Colin Watson, Samuel Thibault, Colin Watson, Updated translations
[ Samuel Thibault ]
166
	# Sometimes sudo may need to be removed (e.g. when installing
167
	# sudo-ldap).  There's no situation in which doing this during d-i
168
	# is a problem, so unconditionally override the guard in sudo.prerm.
169
	SUDO_FORCE_REMOVE=yes
170
	export SUDO_FORCE_REMOVE
17 by Colin Watson
* Resynchronise with Debian. Remaining changes:
171
172
	return 0
4 by Colin Watson
* Resynchronise with Debian.
173
}
174
175
chroot_cleanup () {
176
	rm -f /target/usr/sbin/policy-rc.d
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
177
	undivert /sbin/start-stop-daemon
1.2.2 by Colin Watson, Colin Watson, Aurelien Jarno, Chris Lamb, Frans Pop, Ryan Niebur, Updated translations
[ Colin Watson ]
178
	if [ -x /target/sbin/initctl.REAL ]; then
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
179
		undivert /sbin/initctl
1.2.2 by Colin Watson, Colin Watson, Aurelien Jarno, Chris Lamb, Frans Pop, Ryan Niebur, Updated translations
[ Colin Watson ]
180
	fi
4 by Colin Watson
* Resynchronise with Debian.
181
182
	# Undo the mounts done by the packages during installation.
183
	# Reverse sorting to umount the deepest mount points first.
184
	# Items with count of 1 are new.
185
	for dir in $( (cat /tmp/mount.pre /tmp/mount.pre; mountpoints ) | \
12 by Colin Watson
Resynchronise with Debian.
186
		     sort -r | uniq -c | grep "^[[:space:]]*1[[:space:]]" | \
187
		     sed "s/^[[:space:]]*[0-9][[:space:]]//"); do
29 by Colin Watson
* Resynchronise with Debian. Remaining changes:
188
		if ! umount $dir; then
4 by Colin Watson
* Resynchronise with Debian.
189
			logger -t $0 "warning: Unable to umount '$dir'"
190
		fi
191
	done
1.2.9 by Otavio Salvador, Colin Watson, Samuel Thibault, Otavio Salvador, Updated translations
[ Colin Watson ]
192
	rm -f /tmp/mount.pre /tmp/mount.post
193
194
	rm -f /var/run/chroot-setup.lock
195
}
196
197
# Variant of chroot_cleanup that only cleans up chroot_setup's mounts.
198
chroot_cleanup_localmounts () {
199
	rm -f /target/usr/sbin/policy-rc.d
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
200
	undivert /sbin/start-stop-daemon
1.2.9 by Otavio Salvador, Colin Watson, Samuel Thibault, Otavio Salvador, Updated translations
[ Colin Watson ]
201
	if [ -x /target/sbin/initctl.REAL ]; then
1.2.17 by Colin Watson, Colin Watson, Updated translations
[ Colin Watson ]
202
		undivert /sbin/initctl
1.2.9 by Otavio Salvador, Colin Watson, Samuel Thibault, Otavio Salvador, Updated translations
[ Colin Watson ]
203
	fi
204
205
	# Undo the mounts done by the packages during installation.
206
	# Reverse sorting to umount the deepest mount points first.
207
	# Items with count of 1 are new.
208
	for dir in $( (cat /tmp/mount.pre /tmp/mount.pre /tmp/mount.post ) | \
209
		     sort -r | uniq -c | grep "^[[:space:]]*1[[:space:]]" | \
210
		     sed "s/^[[:space:]]*[0-9][[:space:]]//"); do
211
		if ! umount $dir; then
212
			logger -t $0 "warning: Unable to umount '$dir'"
213
		fi
214
	done
215
	rm -f /tmp/mount.pre /tmp/mount.post
13 by Colin Watson
Resynchronise with Debian.
216
217
	rm -f /var/run/chroot-setup.lock
4 by Colin Watson
* Resynchronise with Debian.
218
}