summaryrefslogtreecommitdiff
path: root/src/devtools/makechrootpkg.in
blob: 9e5b04f49255d7d5f36802f255e6e62ae158a63e (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
# Copyright 2011-2012 The Arch Linux Development Team
# Copyright 2012 Luke Shumaker
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# Because of how we pull changes from devtools.git, some of the function
# bodies are not indented.  I appologize for my sins against the reader.

# Any function beginning with "_makechrootpkg_" is basically a multiline
# comment to minimize the diff. These functions may use different variable
# names.

# Otherwise, a function may be affected by the variables:
#  makepkg.conf
#  - PKGDEST
#  - SRCDEST
#  chroot.conf (and derived)
#  - CHROOT
#  - CHROOTCOPY
#  - rootdir     # = $CHROOTDIR/$CHROOT/root
#  - copydir     # = $CHROOTDIR/$CHROOT/$CHROOTCOPY
#  environment
#  - LIBREUSER   # conf.sh
#  - LIBREHOME   # conf.sh
#  - INCHROOT    # libremakepkg
#  invocation
#  - repack

[[ -n ${repack:-} ]] || repack=false

_makechrootpkg_init() {
m4_include(lib/common.sh)

shopt -s nullglob

makepkg_args='-s --noconfirm -L'
repack=false
update_first=false
clean_first=false
install_pkg=
add_to_db=false
run_namcap=false
chrootdir=
passeddir=

default_copy=$USER
[[ -n $SUDO_USER ]] && default_copy=$SUDO_USER
[[ -z $default_copy || $default_copy = root ]] && default_copy=copy
src_owner=${SUDO_USER:-$USER}
}

_makechrootpkg_usage() {
	echo "Usage: ${0##*/} [options] -r <chrootdir> [--] [makepkg args]"
	echo ' Run this script in a PKGBUILD dir to build a package inside a'
	echo ' clean chroot. All unrecognized arguments passed to this script'
	echo ' will be passed to makepkg.'
	echo ''
	echo ' The chroot dir consists of the following directories:'
	echo ' <chrootdir>/{root, copy} but only "root" is required'
	echo ' by default. The working copy will be created as needed'
	echo ''
	echo 'The chroot "root" directory must be created via the following'
	echo 'command:'
	echo '    mkarchroot <chrootdir>/root base base-devel sudo'
	echo ''
	echo "Default makepkg args: $makepkg_args"
	echo ''
	echo 'Flags:'
	echo '-h         This help'
	echo '-c         Clean the chroot before building'
	echo '-u         Update the working copy of the chroot before building'
	echo '           This is useful for rebuilds without dirtying the pristine'
	echo '           chroot'
	echo '-d         Add the package to a local db at /repo after building'
	echo '-r <dir>   The chroot dir to use'
	echo '-I <pkg>   Install a package into the working copy of the chroot'
	echo '-l <copy>  The directory to use as the working copy of the chroot'
	echo '           Useful for maintaining multiple copies.'
	echo "           Default: $default_copy"
	echo '-n         Run namcap on the package'
	exit 1
}

_makechrootpkg_parse_options_init() {
while getopts 'hcudr:I:l:n' arg; do
	case "$arg" in
		h) usage ;;
		c) clean_first=true ;;
		u) update_first=true ;;
		d) add_to_db=true ;;
		r) passeddir="$OPTARG" ;;
		I) install_pkg="$OPTARG" ;;
		l) copy="$OPTARG" ;;
		n) run_namcap=true; makepkg_args="$makepkg_args -i" ;;
		*) makepkg_args="$makepkg_args -$arg $OPTARG" ;;
	esac
done

# Canonicalize chrootdir, getting rid of trailing /
chrootdir=$(readlink -e "$passeddir")

if [[ ${copy:0:1} = / ]]; then
	copydir=$copy
else
	[[ -z $copy ]] && copy=$default_copy
	copydir="$chrootdir/$copy"
fi

# Pass all arguments after -- right to makepkg
makepkg_args="$makepkg_args ${*:$OPTIND}"

# See if -R was passed to makepkg
for arg in ${*:$OPTIND}; do
	if [[ $arg = -R ]]; then
		repack=true
		break
	fi
done

if (( EUID )); then
	die 'This script must be run as root.'
fi

if [[ ! -f PKGBUILD && -z $install_pkg ]]; then
	die 'This must be run in a directory containing a PKGBUILD.'
fi

if [[ ! -d $chrootdir ]]; then
	die "No chroot dir defined, or invalid path '$passeddir'"
fi

if [[ ! -d $chrootdir/root ]]; then
	die "Missing chroot dir root directory. Try using: mkarchroot $chrootdir/root base base-devel sudo"
fi

umask 0022

# Lock the chroot we want to use. We'll keep this lock until we exit.
# Note this is the same FD number as in mkarchroot
lock_open_write 9 "$copydir.lock" "Locking chroot copy '$copy'"
}

chroot_sync() {
	if [[ $CHROOTCOPY = root ]]; then
		error "Cannot sync the root copy with itself"
		exit 1
	fi
	# Get a read lock on the root chroot to make
	# sure we don't clone a half-updated chroot
	lock_open_read 8 "$rootdir" \
		"Waiting for existing lock on \`$rootdir' to be released"

	stat_busy 'Creating clean working copy'
	local use_rsync=false
	if type -P btrfs >/dev/null; then
		[[ -d $copydir ]] && btrfs subvolume delete "$copydir" &>/dev/null
		btrfs subvolume snapshot "$rootdir" "$copydir" &>/dev/null ||
			use_rsync=true
	else
		use_rsync=true
	fi

	if $use_rsync; then
		mkdir -p "$copydir"
		rsync -a --delete -q -W -x "$rootdir/" "$copydir"
	fi
	stat_done

	# Drop the read lock again
	lock_close 8
}

_makechrootpkg_install_pkg() {
	pkgname="${install_pkg##*/}"
	cp "$install_pkg" "$copydir/$pkgname"

	mkarchroot -r "pacman -U /$pkgname --noconfirm" "$copydir"
	ret=$?

	rm "$copydir/$pkgname"

	# Exit early, we've done all we need to
	exit $ret
}

chroot_init() {
# make sure the chroot exists
librechroot -n "$CHROOT" -l "$CHROOTCOPY" -m

mkdir -p "$copydir/build"
mkdir -p "$copydir/pkgdest"
mkdir -p "$copydir/srcdest"

# Remove anything in there UNLESS -R (repack) was passed to makepkg
$repack || rm -rf "$copydir"/build/*

if [[ -r "$LIBREHOME/.gnupg/pubring.gpg" ]]; then
	install -D "$LIBREHOME/.gnupg/pubring.gpg" "$copydir/build/.gnupg/pubring.gpg"
fi
rm -f "$copydir/build/.makepkg.conf"

MAKEPKG_CONF="$copydir/etc/makepkg.conf" set_conf_makepkg PKGDEST /pkgdest
MAKEPKG_CONF="$copydir/etc/makepkg.conf" set_conf_makepkg SRCDEST /srcdest

if grep -q '^\[repo\]' "$copydir/etc/pacman.conf"; then
	cat >> "$copydir/etc/makepkg.conf" <<EOF
[repo]
SigLevel = Optional TrustAll
Server = file:///repo
EOF
fi

_let_nobody_use_pacman
}

chroot_copy_in() {
# Copy PKGBUILD and sources
cp PKGBUILD "$copydir/build/"
(
	set +euE
	source PKGBUILD
	# Copy source files
	for file in "${source[@]}"; do
		file="${file%%::*}"
		file="${file##*://*/}"
		if [[ -f $file ]]; then
			cp "$file" "$copydir/srcdest/"
		elif [[ -f $SRCDEST/$file ]]; then
			cp "$SRCDEST/$file" "$copydir/srcdest/"
		fi
	done

	# Find all changelog and install files, even inside functions
	for i in 'changelog' 'install'; do
		while read -r file; do
			# evaluate any bash variables used
			eval file=\"$(sed 's/^\(['\''"]\)\(.*\)\1$/\2/' <<< "$file")\"
			[[ -f $file ]] && cp "$file" "$copydir/build/"
		done < <(sed -n "s/^[[:space:]]*$i=//p" PKGBUILD)
	done
)

chown -R nobody "$copydir"/{build,pkgdest,srcdest}
}

_let_nobody_use_pacman() {
cat > "$copydir/etc/sudoers.d/nobody-pacman" <<EOF
Defaults env_keep += "HOME"
nobody ALL = NOPASSWD: /usr/bin/pacman
EOF
chmod 440 "$copydir/etc/sudoers.d/nobody-pacman"
}

chroot_exec() {
local HASNET=true
[[ $1 == -N ]] && { HASNET=false; shift; }

local cmd="$*"
# This is a little gross, but this way the script is recreated every time in the
# working copy
cat >"$copydir/chrootexec" <<EOF
#!/bin/bash
. /etc/profile
${INCHROOT} || export HOME=/build
${INCHROOT} || cd /build

${cmd}
EOF
chmod 755 "$copydir/chrootexec"

local flags=''
if $INCHROOT; then
	$HASNET || flags='-n'
	unshare $flags -- /chrootexec
else
	$HASNET || flags='-N'
	librechroot $flags -n "$CHROOT" -l "$CHROOTCOPY" -r /chrootexec
fi
}

add_to_local_repo() {
	for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
		if true; then
			mkdir -p "$copydir/repo"
			pushd "$copydir/repo" >/dev/null
			cp "$pkgfile" .
			repo-add repo.db.tar.gz "${pkgfile##*/}"
			popd >/dev/null
		fi
	done
}
_chroot_copy_out_pkgs() {
	for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
		chown "$LIBREUSER" "$pkgfile"
		mv "$pkgfile" "$PKGDEST"
		if [[ $PKGDEST != . ]]; then
			ln -s "$PKGDEST/${pkgfile##*/}" .
		fi
	done
}

_chroot_copy_out_logs() {
	for l in "$copydir"/build/*.log; do
		chown "$LIBREUSER" "$l"
		[[ -f $l ]] && mv "$l" .
	done
}

_chroot_copy_out_srcs() {
for f in "$copydir"/srcdest/*; do
	chown "$LIBREUSER" "$f"
	mv "$f" "$SRCDEST"
done
}

chroot_copy_out() {
	_chroot_copy_out_pkgs
	_chroot_copy_out_logs
	_chroot_copy_out_srcs
}