-
Notifications
You must be signed in to change notification settings - Fork 0
/
gabs-build-common
executable file
·151 lines (130 loc) · 4.08 KB
/
gabs-build-common
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
#!/bin/zsh -x
# https://stackoverflow.com/questions/16860877/remove-an-element-from-a-bash-array
# Array names cannot start with RFA_
remove_from_array() {
RFA_RETURN=()
local RFA_ARRAY=${1}
local RFA_TO_REMOVE=${2}
for mem in "${(P)${RFA_ARRAY}[@]}"; do
for remove in "${(P)${RFA_TO_REMOVE}[@]}"; do
local KEEP=true
if [[ "${mem}" == "${remove}" ]]; then
local KEEP=false
break
fi
done
if ${KEEP}; then
RFA_RETURN+=(${mem})
fi
done
}
array_contains_element () {
local e match="${1}"
shift
for e; do [[ "${e}" == "${match}" ]] && return 0; done
return 1
}
nspawn() {
setarch x86_64 /usr/bin/systemd-nspawn -q \
-D "${1}" \
-E "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin" \
--register=no --keep-unit --as-pid2 \
--bind=/var/cache/pacman/pkg \
"${@:2}"
}
print_fallback_param() {
>&2 echo "Falling back to using \"${1}\" of \"${@:2}\"."
}
error_blank_param() {
if [[ "${(P)1}" == "" ]]; then
>&2 echo "\"${1}\" was never passed."
exit 1
fi
}
error_blank_nondir_param() {
error_blank_param "${2}"
if [[ ("${1}" == "-f" && ! -f "${(P)2}") || ("${1}" == "-d" && ! -d "${(P)2}") ]]; then
>&2 echo "\"${2}\"'s directory \"${(P)2}\" does not exist."
exit 1
fi
}
if_buildenv_uses() {
if [[ "$(awk "{
if (/^ *BUILDENV *= *\\( */) {
split(\$0, a, \"(\");
for (i in a) if (i == 2) {
split(a[i], b, \")\");
for (j in b) if (j == 1) {
split(b[j], c, \" \")
for(k in c) if(c[k] == tolower(\"${1}\") || c[k] == tolower(\"!${1}\"))
printf c[k]
}
}
}
}" "${EPHEMERAL_DIR}/custom.${CHROOT_NAME}/makepkg.conf")"
== "$(echo "${1}" | awk '{ printf tolower($0) }')" ]]; then
eval USE_${1}=true
else
eval USE_${1}=false
fi
}
clone_file() {
for CHROOT_DIR2 in "${CHROOT_DIR}"/*; do
[[ ! -d "${CHROOT_DIR2}" ]] && continue
cp "${1}" "${CHROOT_DIR2}${2}"
chown ${3} "${CHROOT_DIR2}${2}"
chmod ${4} "${CHROOT_DIR2}${2}"
done
}
cleanup_loopdevice1() {
sleep 2
until losetup -d "${LOOP_DEVICE}"; do sleep 2; done
}
cleanup_loopdevice2() {
if [[ "${UNMOUNT_LOOP_DEVICE}" == "true" ]]; then
sleep 2
until umount "${CHROOT_MOUNT}"; do sleep 2; done
until losetup -d "${LOOP_DEVICE}"; do sleep 2; done
fi
}
# Unmount old loopdevices that where kept due to bad cleanup
cleanup_loopdevice() {
losetup -j "${CHROOT_FILE}" \
| cut -d":" -f1 \
| xargs -I% bash -xc '
lsblk "%" -o MOUNTPOINT -n | xargs umount
losetup -d "%"
'
}
# We use a loop device cause sometimes the partitions have nosuid on them,
# which breaks the /bin/sudo in the chroot when used from outside the chroot,
# which breaks makepkg for some reason.
setup_loopdevice() {
if [[ "${REMAKE_CHROOT}" == "true" ]]; then
rm "${CHROOT_FILE}"
fallocate -l ${LOOP_DEVICE_SIZE} "${CHROOT_FILE}" || exit $?
fi
LOOP_DEVICE="$(losetup -f)"
losetup "${LOOP_DEVICE}" "${CHROOT_FILE}" || exit $?
trap cleanup_loopdevice1 EXIT
if [[ "${REMAKE_CHROOT}" == "true" ]]; then
mkfs.ext4 "${LOOP_DEVICE}" || exit $?
fi
mkdir -p "${CHROOT_MOUNT}"
mount "${LOOP_DEVICE}" "${CHROOT_MOUNT}" || exit $?
trap -- EXIT
}
builtin_config_script_try_dirs() {
try_build_config_script() {
if [[ "${BUILD_CONFIG_SCRIPT}" == "" || ! -f "${BUILD_CONFIG_SCRIPT}" ]]; then
BUILD_CONFIG_SCRIPT="${1}"
fi
}
if [[ "${BUILD_CONFIG_SCRIPT}" == "" ]]; then
try_build_config_script "$(pwd)/gabs-build-config"
try_build_config_script "${EPHEMERAL_DIR}/gabs-build-config"
try_build_config_script "${HOME}/.config/gabs-build-config"
try_build_config_script "/etc/gabs-build-config"
try_build_config_script "${SCRIPTS_DIR}/gabs-build-config"
fi
}