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
|
--- makechrootpkg.sh.in
+++ makechrootpkg.sh.ugly
@@ -12,12 +12,7 @@
shopt -s nullglob
-# So that usage conflicts between upstream and -par mkarchroot don't get hidden
-# silently in a merge.
-archroot() {
- mkarchroot "$@"
-}
-
+init_variables() {
makepkg_args='-s --noconfirm -L'
repack=false
update_first=false
@@ -28,13 +23,14 @@
temp_chroot=false
chrootdir=
passeddir=
-declare -a install_pkgs
-declare -i ret=0
+declare -ag install_pkgs
+declare -ig ret=0
copy=$USER
[[ -n $SUDO_USER ]] && copy=$SUDO_USER
[[ -z "$copy" || $copy = root ]] && copy=copy
src_owner=${SUDO_USER:-$USER}
+}
usage() {
echo "Usage: ${0##*/} [options] -r <chrootdir> [--] [makepkg args]"
@@ -69,6 +65,7 @@
exit 1
}
+parse_options_init() {
while getopts 'hcudr:I:l:nT' arg; do
case "$arg" in
h) usage ;;
@@ -129,8 +126,20 @@
# Note this is the same FD number as in mkarchroot
lock_open_write 9 "$copydir" \
"Waiting for existing lock on chroot copy to be released: [$copy]"
+}
+
+# Usage: chroot_sync $CHROOTDIR/$CHROOT [$CHROOTCOPY|$copydir]
+chroot_sync() {
+ local chrootdir=$1
+ local copy=$2
+ local copydir=''
+ if [[ ${copy:0:1} = / ]]; then
+ copydir=$copy
+ else
+ copydir="$chrootdir/$copy"
+ fi
+ local chroottype=$(stat -f -c %T "$chrootdir")
-if [[ ! -d $copydir ]] || $clean_first; then
# Get a read lock on the root chroot to make
# sure we don't clone a half-updated chroot
lock_open_read 8 "$chrootdir/root" \
@@ -152,9 +161,16 @@
# Drop the read lock again
lock_close 8
-fi
+}
+
+# Usage: chroot_install_pkgs $copydir $pkgs...
+chroot_install_pkgs() {
+ local copydir=$1
+ shift
+ declare -i ret=0
+ declare -a install_pkgs=("$@")
+ local pkgname
-if [[ -n "${install_pkgs[*]}" ]]; then
for install_pkg in "${install_pkgs[@]}"; do
pkgname="${install_pkg##*/}"
cp "$install_pkg" "$copydir/$pkgname"
@@ -165,10 +181,10 @@
rm "$copydir/$pkgname"
done
- # If there is no PKGBUILD we have done
- [[ -f PKGBUILD ]] || exit $ret
-fi
+ return $ret
+}
+func1() {
$update_first && archroot -u "$copydir"
mkdir -p "$copydir/build"
@@ -230,14 +246,24 @@
# Set target CARCH as it might be used within the PKGBUILD to select correct sources
eval $(grep '^CARCH=' "$copydir/etc/makepkg.conf")
export CARCH
+}
+# Usage: chroot_copy_in $copydir
+# Environment:
+# - In the dirctory of a PKGBUILD
+# - $SRCDEST is set
+chroot_copy_in() {
+local copydir=$1
# Copy PKGBUILD and sources
-cp PKGBUILD "$copydir/build/"
+for recipe in PKGBUILD SRCBUILD; do
+if [[ -f $recipe ]]; then
+cp $recipe "$copydir/build/"
(
- source PKGBUILD
- for file in "${source[@]}"; do
+ source $recipe
+ for file in "${source[@]}" "${mksource[@]}"; do
file="${file%%::*}"
file="${file##*://*/}"
+ file="${file##*://}"
if [[ -f $file ]]; then
cp "$file" "$copydir/srcdest/"
elif [[ -f $SRCDEST/$file ]]; then
@@ -251,18 +277,30 @@
# 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 < <(sed -n "s/^[[:space:]]*$i=//p" $recipe)
done
)
+fi
+done
chown -R nobody "$copydir"/{build,pkgdest,srcdest}
+}
+# Usage: chroot_let_nobody_use_pacman $copydir
+chroot_let_nobody_use_pacman() {
+local copydir=$1
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"
+}
+# Usage: chroot_build $copydir $makepkg_args $run_namcap
+chroot_build() {
+local copydir=$1
+local makepkg_args=$2
+local run_namcap=$3
# This is a little gross, but this way the script is recreated every time in the
# working copy
cat >"$copydir/chrootbuild" <<EOF
@@ -284,37 +322,62 @@
exit 0
EOF
chmod +x "$copydir/chrootbuild"
+archroot -r "$copydir" "/chrootbuild"
+}
-if archroot -r "$copydir" "/chrootbuild"; then
- for pkgfile in "$copydir"/pkgdest/*.pkg.tar.?z; do
- if $add_to_db; then
+# Usage: chroot_add_to_local_repo $copydir $pkgfiles...
+chroot_add_to_local_repo() {
+ local copydir=$1; shift
+ for pkgfile in "$@"; do
mkdir -p "$copydir/repo"
pushd "$copydir/repo" >/dev/null
cp "$pkgfile" .
repo-add repo.db.tar.gz "${pkgfile##*/}"
popd >/dev/null
- fi
+ done
+}
+# Usage: chroot_copy_out_pkgs $copydir $user
+# Environment:
+# - $PKGDEST is set
+chroot_copy_out_pkgs() {
+ local copydir=$1
+ local src_owner=$2
+ for pkgfile in "$copydir"/pkgdest/*.pkg.tar*; do
chown "$src_owner" "$pkgfile"
mv "$pkgfile" "$PKGDEST"
+ if [[ $PKGDEST != . ]]; then
+ ln -sf "$PKGDEST/${pkgfile##*/}" .
+ fi
done
+}
- for l in "$copydir"/build/*-{build,check,namcap,package,package_*}.log; do
+# Usage: chroot_copy_out_pkgs $copydir $user
+chroot_copy_out_logs() {
+ local copydir=$1
+ local src_owner=$2
+ for l in "$copydir"/build/*.log; do
chown "$src_owner" "$l"
[[ -f $l ]] && mv "$l" .
done
-else
- # Just in case. We returned 1, make sure we fail
- ret=1
-fi
+}
+# Usage: chroot_copy_out_srcs $copydir $user
+# Environment:
+# - $SRCDEST is set
+chroot_copy_out_srcs() {
+local copydir=$1
+local src_owner=$2
for f in "$copydir"/srcdest/*; do
chown "$src_owner" "$f"
mv "$f" "$SRCDEST"
done
+}
-if $temp_chroot; then
- stat_busy "Removing temporary directoy [$copy]"
+# Usage: chroot_delete $copydir
+chroot_delete() {
+ local chroottype=$(stat -f -c %T "$copydir")
+ stat_busy "Removing chroot copy [$copy]"
if [[ "$chroottype" == btrfs ]]; then
btrfs subvolume delete "$copydir" >/dev/null ||
die "Unable to delete subvolume $copydir"
@@ -326,8 +389,39 @@
# remove lock file
rm --force "$copydir.lock"
stat_done
-elif (( ret != 0 )); then
- die "Build failed, check $copydir/build"
-else
- true
-fi
+}
+
+main() {
+ init_variables
+ parse_options_init "$@"
+ if [[ ! -d $copydir ]] || $clean_first; then
+ chroot_sync "$chrootdir" "$copy"
+ fi
+ if [[ -n "${install_pkgs[*]}" ]]; then
+ chroot_install_pkgs "$copydir" "${install_pkgs[@]}"
+ ret=$?
+ # If there is no PKGBUILD we have done
+ [[ -f PKGBUILD ]] || exit $ret
+ fi
+ func1
+ chroot_copy_in "$copydir"
+ chroot_let_nobody_use_pacman "$copydir"
+ chroot_build "$copydir" "$makepkg_args" "$run_namcap"
+ func2
+ if chroot_build; then
+ chroot_add_to_local_repo "$copydir" "$copydir"/pkgdest/*.pkg.tar*
+ chroot_copy_out_pkgs "$copydir" "$src_owner"
+ chroot_copy_out_logs "$copydir" "$src_owner"
+ else
+ # Just in case. We returned 1, make sure we fail
+ ret=1
+ fi
+ chroot_copy_out_srcs "$copydir" "$src_owner"
+ if $temp_chroot; then
+ chroot_delete "$copydir"
+ elif (( ret != 0 )); then
+ die "Build failed, check $copydir/build"
+ else
+ true
+ fi
+}
|