summaryrefslogtreecommitdiff
path: root/scripts/build/chroot_apt
blob: 466d1fe2ef1768b85dfede4f09bc0f8e130fb0f4 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

# Including common functions
[ -e "${LIVE_BUILD}/scripts/build.sh" ] && . "${LIVE_BUILD}/scripts/build.sh" || . /usr/lib/live/build.sh

# Setting static variables
DESCRIPTION="Manage /etc/apt/apt.conf"
USAGE="${PROGRAM} {install|install-binary|remove} [--force]"

# Processing arguments and configuration files
Init_config_data "${@}"

_ACTION="${1}"
shift

# Requiring stage file
Require_stagefiles config bootstrap

CONFD="chroot/etc/apt/apt.conf.d"

case "${_ACTION}" in
	install|install-binary)
		Echo_message "Configuring file /etc/apt/apt.conf"

		# Checking stage file
		Check_stagefile

		# Acquire lock file
		Acquire_lockfile

		mkdir -p ${CONFD}

		# Configuring apt ftp proxy
		if [ -n "${LB_APT_FTP_PROXY}" ]
		then
			echo "Acquire::ftp::Proxy \"${LB_APT_FTP_PROXY}\";" > ${CONFD}/00ftp-proxy
		fi

		# Configuring apt http proxy
		if [ -n "${LB_APT_HTTP_PROXY}" ]
		then
			echo "Acquire::http::Proxy \"${LB_APT_HTTP_PROXY}\";" > ${CONFD}/00http-proxy
		fi

		# Configuring apt pipeline
		if [ -n "${LB_APT_PIPELINE}" ]
		then
			echo "Acquire::http::Pipeline-Depth \"${LB_APT_PIPELINE}\";" > ${CONFD}/00pipeline
		fi

		# Configuring apt recommends
		case "${LB_APT_RECOMMENDS}" in
			true)
				echo "APT::Install-Recommends \"true\";" > ${CONFD}/00recommends
				echo "Aptitude::Recommends-Important \"true\";" >> ${CONFD}/00recommends
				;;

			false)
				echo "APT::Install-Recommends \"false\";" > ${CONFD}/00recommends
				echo "Aptitude::Recommends-Important \"false\";" >> ${CONFD}/00recommends
				;;
		esac

		# Configuring apt secure
		case "${LB_APT_SECURE}" in
			true)
				echo "APT::Get::AllowUnauthenticated \"false\";" > ${CONFD}/00secure
				echo "Aptitude::CmdLine::Ignore-Trust-Violations \"false\";" >> ${CONFD}/00secure
				echo "Acquire::AllowInsecureRepositories \"false\";" >> ${CONFD}/00secure
				;;

			false)
				echo "APT::Get::AllowUnauthenticated \"true\";" > ${CONFD}/00secure
				echo "Aptitude::CmdLine::Ignore-Trust-Violations \"true\";" >> ${CONFD}/00secure
				echo "Acquire::AllowInsecureRepositories \"true\";" >> ${CONFD}/00secure
				;;
		esac

		# Configuring apt config
		CONFIG_FILE="config/apt/apt.conf"
		CHROOT_FILE="chroot/etc/apt/apt.conf"
		if [ -f ${CONFIG_FILE} ]; then
			if [ -f ${CHROOT_FILE} ]; then
				mv ${CHROOT_FILE} ${CHROOT_FILE}.orig
			fi
			cp ${CONFIG_FILE} ${CHROOT_FILE}
		fi

		# Configuring apt preferences
		CONFIG_FILE="config/apt/preferences"
		CHROOT_FILE="chroot/etc/apt/preferences"
		if [ -f ${CONFIG_FILE} ]; then
			if [ -f ${CHROOT_FILE} ]; then
				mv ${CHROOT_FILE} ${CHROOT_FILE}.orig
			fi
			cp ${CONFIG_FILE} ${CHROOT_FILE}
		fi

		if Find_files config/apt/*.pref; then
			for CONFIG_FILE in config/apt/*.pref; do
				CHROOT_FILE="chroot/etc/apt/preferences.d/$(basename ${CONFIG_FILE})"
				if [ -f ${CHROOT_FILE} ]; then
					mv ${CHROOT_FILE} ${CHROOT_FILE}.orig
				fi
				cp -aL ${CONFIG_FILE} chroot/etc/apt/preferences.d
			done
		fi

		if Find_files config/packages.chroot/*.deb || Find_files config/packages/*.deb
		then
			CHROOT_FILE="chroot/etc/apt/preferences"
			echo >> ${CHROOT_FILE}
			echo "# Added by lb_chroot_apt ${@}" >> ${CHROOT_FILE}
			echo "Package: *" >> ${CHROOT_FILE}
			echo "Pin: release o=config/packages.chroot" >> ${CHROOT_FILE}

			case "${1}" in
				install)
					# Ensure local packages have priority
					echo "Pin-Priority: 1001" >> ${CHROOT_FILE}
					;;

				install-binary)
					# Ensure local packages are not re-installed during lb_binary
					echo "Pin-Priority: 99" >> ${CHROOT_FILE}

					echo >> ${CHROOT_FILE}
					echo "Package: *" >> ${CHROOT_FILE}
					echo "Pin: release o=debian" >> ${CHROOT_FILE}
					echo "Pin-Priority: 99" >> ${CHROOT_FILE}
					;;
			esac
		fi

		# Creating stage file
		Create_stagefile
		;;

	remove)
		Echo_message "Deconfiguring file /etc/apt/apt.conf"

		# Checking stage file
		Ensure_stagefile_exists

		# Acquire lock file
		Acquire_lockfile

		# Deconfiguring aptitude ftp proxy
		rm -f ${CONFD}/00ftp-proxy

		# Deconfiguring aptitude http proxy
		rm -f ${CONFD}/00http-proxy

		# Deconfiguring aptitude pipeline
		rm -f ${CONFD}/00pipeline

		# Deconfiguring aptitude recommends
		if [ "${LB_APT_RECOMMENDS}" = "true" ]; then
			rm -f ${CONFD}/00recommends
		fi

		# Deconfiguring aptitude secure
		if [ "${LB_APT_SECURE}" = "true" ]; then
			rm -f ${CONFD}/00secure
		fi

		# Configuring apt config
		CONFIG_FILE="config/apt/apt.conf"
		CHROOT_FILE="chroot/etc/apt/apt.conf"
		if [ -f ${CONFIG_FILE} ]; then
			rm -f ${CHROOT_FILE}

			if [ -f ${CHROOT_FILE}.orig ]; then
				mv ${CHROOT_FILE}.orig ${CHROOT_FILE}
			fi
		fi

		# Deconfiguring apt preferences
		CONFIG_FILE="config/apt/preferences"
		CHROOT_FILE="chroot/etc/apt/preferences"
		if [ -f ${CONFIG_FILE} ]; then
			rm -f ${CHROOT_FILE}

			if [ -f ${CHROOT_FILE}.orig ]; then
				mv ${CHROOT_FILE}.orig ${CHROOT_FILE}
			fi
		elif [ -f ${CHROOT_FILE} ]; then
			# delete additions from lb_chroot_apt install|install-binary to preferences
			sed -i '/# Added by lb_chroot_apt/,$d' ${CHROOT_FILE}
			# delete the last empty line
			sed -i '${/^[[:blank:]]*$/d;}' ${CHROOT_FILE}
			# if the resulting preferences file is empty, we must have created it, remove it
			if [ ! -s ${CHROOT_FILE} ]; then
				rm -f ${CHROOT_FILE}
			fi
		fi

		if Find_files config/apt/*.pref; then
			for CONFIG_FILE in config/apt/*.pref; do
				CHROOT_FILE="chroot/etc/apt/preferences.d/$(basename ${CONFIG_FILE})"

				rm -f ${CHROOT_FILE}

				if [ -f ${CHROOT_FILE}.orig ]; then
					mv ${CHROOT_FILE}.orig ${CHROOT_FILE}
				fi
			done
		fi

		# Removing stage file
		Remove_stagefile
		;;

	*)
		Echo_error "Invalid action parameter: '${_ACTION}'"
		Usage --fail
		;;
esac