-
Notifications
You must be signed in to change notification settings - Fork 0
/
aurch-setup.sh
executable file
·298 lines (235 loc) · 10.6 KB
/
aurch-setup.sh
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
#!/bin/bash
# aurch-setup 2024-11-13
# dependencies: base-devel arch-install-scripts git pacutils jshon mc sudo devtools paccat
set -euo pipefail
if [[ $(id -u) != 0 ]]; then
echo " Needs elevated privileges. Run with sudo."
echo
echo " FYI: This script uses the 'SUDO_USER' variable to set a symlink."
echo " The default 'secure_path' sudo configuration is broken."
echo " If you're having issues, the sudo 'secure_path' may need setup. ie: "
echo " '/etc/sudoers' line: Defaults secure_path=\"/existing/path/entries:/APPEND/PATH\""
exit
fi
[[ ! -v BASEDIR ]] && BASEDIR=/usr/local/aurch/base # Set BASEDIR if unset
[[ ! -d ${BASEDIR} ]] && mkdir -p "${BASEDIR}" # Create BASEDIR dir if not present
[[ ! -v AURREPO ]] && AURREPO=/usr/local/aurch/repo # Set AURREPO if unset
[[ ! -d ${AURREPO} ]] && mkdir -p "${AURREPO}" 2>/dev/null # Create AURREPO dir if not present
[[ ! -v REPONAME ]] && REPONAME=aur # Set REPONAME if unset
[[ ! -s ${BASEDIR}/.#ID ]] && mktemp -u XXX > "${BASEDIR}"/.#ID # Create unique suffix file if not present
chroot="${BASEDIR}"/chroot-$(< "${BASEDIR}"/.#ID) # HOST path to chroot
chrbuilduser=/home/builduser # CHROOT builduser home directory
homebuilduser="${chroot}"/home/builduser # HOST builduser home directory
czm=$(echo -e '\033[1;96m'":: aurch ==>"'\033[00m') # Aurch color pointer
error=$(echo -e '\033[1;91m' "ERROR:" '\033[00m') # Red 'ERROR' text.
line2=$(printf %"$(tput cols)"s |tr " " "-") # Set line '---' to terminal width
#========================================================================================================================#
echo "
BASEDIR=${BASEDIR}
AURREPO=${AURREPO}
chroot=${chroot}
chrbuilduser=${chrbuilduser}
homebuilduser=${homebuilduser}" > "${BASEDIR}"/.#aurch-setup-vars
#========================================================================================================================#
help(){
cat << EOF
${line2}
NAME
aurch-setup - sets up a system for aurch
DESCRIPTION
Aurch-setup sets up an nspawn container for the aurch script to build AUR packages.
Aurutils is installed and setup in the nspawn container.
Sets up a local pacman repo for AUR packages on the host.
USAGE
sudo aurch-setup [operation]
OPERATIONS
-Sc --setupcontainer Sets up an nspawn container.
-Sh --setuphost Sets up the host aur repo.
-h --help Prints help.
-V --version Prints aurch version.
EXAMPLES
Setup an nspawn container for aurch: sudo aurch-setup -Sc
Setup host with a local AUR repo: sudo aurch-setup -Sh
MISC
Aurch-setup runtime messages will be proceeded with this: ${czm}
Aurch-setup runtime errors will be proceeded with this: ${czm}${error}
Aurch-setup prints additional important info while running.
${line2}
EOF
}
#========================================================================================================================#
check_depends(){
echo "${czm} Checking dependencies for aurch and arch-install."
pacman -S --needed --confirm base-devel arch-install-scripts git pacutils jshon mc sudo devtools paccat
echo
}
#========================================================================================================================#
setup_chroot(){
cd "${BASEDIR}" || { echo "[line ${LINENO}]" ; exit 1 ; }
existing=$(find "${BASEDIR}" -maxdepth 1 -type d -name "chroot-*" 2>/dev/null)
if [[ -n ${existing} ]]; then
echo "${czm}${error}An aurch chroot, '${chroot}' detected in '${BASEDIR}'."
echo " If you want to setup a new aurch container, the following needs removed."
echo " ${chroot}"
echo " ${BASEDIR}/.#ID"
exit
fi
echo "${czm} Building aurch nspawn-container."
sleep 2
mkdir "${chroot}"
if pacstrap -c "${chroot}" base base-devel git ; then
echo "${czm} Pacstrap finished nspawn-container install."
echo "${czm} Setting up container with builduser, colored shell prompts, header id's, and alias's."
sleep 5
cd "${chroot}" || { echo "[line ${LINENO}]" ; exit 1 ; }
mkdir -p "${chroot}"/var/tmp/aurch || { echo "[line ${LINENO}]" ; exit 1 ; }
systemd-nspawn -a -q useradd -m -G wheel -s /bin/bash builduser
systemd-nspawn -a -q mkdir /build
systemd-nspawn -a -q chown -R builduser:builduser /build
chmod 755 "${chroot}/build"
chown -R :alpm "${chroot}/build"
cat << 'EOF' >> "${chroot}"/etc/bash.bashrc
if [[ $(whoami) == root ]]; then
# Color bash prompt bold red
PS1="\[\033[1;91m\][\u@\h \W]\\$ \[\e[0m\]"
tput setaf 1
echo " |_____________________________________| Root Aurch Container |______________________________________|"
tput sgr0
fi
if [[ $(whoami) == builduser ]]; then
# Color bash prompt bold green
PS1="\[\033[1;92m\][\u@\h \W]\\$ \[\e[0m\]"
tput setaf 2
echo " |___________________________________| Builduser Aurch Container |___________________________________|"
tput sgr0
fi
alias sub=' su builduser; cd'
alias ls=' ls --color=auto -h --group-directories-first'
alias c=' reset; source /etc/bash.bashrc' # reset
### PACMAN ALIASES ###
alias Syy=' sudo pacman --color=always -Syy'
alias Syu=' sudo pacman --color=always -Syu'
alias p=' pacman --color=always '
alias sp=' sudo pacman --color=always '
alias Q=' pacman --color=always -Q'
alias S=' sudo pacman --color=always -S'
alias R=' sudo pacman --color=always -Rns'
alias U=' sudo pacman --color=always -U'
alias F=' pacman --color=always -F'
EOF
sed -e '/^PS1/s/^/#/g' -i "${homebuilduser}"/.bashrc
else
echo "${czm} Pacstrap failed."
fi
#----------------------------------------------------------------------------------------------
# set up container for aurch
echo "${czm} Setting up container for building AUR packages."
sleep 3
cd "${BASEDIR}" || { echo "[line ${LINENO}]" ; exit 1 ; }
if [[ -d ${chroot}/build ]] && [[ -d ${homebuilduser} ]] ; then
printf '%s\n' "%wheel ALL=(ALL) NOPASSWD: ALL" > "${chroot}/etc/sudoers.d/01-sudoers-addendum"
systemd-nspawn -a -q -D "${chroot}" -u builduser --chdir="${chrbuilduser}" --pipe \
git clone https://aur.archlinux.org/aurutils.git
systemd-nspawn -a -q -D "${chroot}" -u builduser --chdir="${chrbuilduser}"/aurutils --pipe \
makepkg -sri --noconfirm
cd "${chroot}" || { echo "[line ${LINENO}]" ; exit 1 ; }
systemd-nspawn -a -q sed -i '$a\#' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\[options]' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\CacheDir = /build' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\CleanMethod = KeepInstalled' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\[aur]' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\SigLevel = Never TrustAll' /etc/pacman.conf
systemd-nspawn -a -q sed -i '$a\Server = file:///build' /etc/pacman.conf # SC2016 We don't want expansion here!
systemd-nspawn -a -q sed -i '/CacheDir/s/^#//g' /etc/pacman.conf
systemd-nspawn -a -q sed -i '/Color/s/^#//g' /etc/pacman.conf
systemd-nspawn -a -q sed -i '/VerbosePkgLists/s/^#//g' /etc/pacman.conf
systemd-nspawn -a -q sed -i '/ParallelDownloads/s/^#//g' /etc/pacman.conf
cat <<-"EOF" > "${homebuilduser}/add-aur-repo"
#!/bin/bash
set -e
repo-add /build/aur.db.tar.gz $(find /home/builduser/aurutils/ -maxdepth 1 -type f -name aurutils*pkg.tar.zst)
chmod 646 /build/aur.db.tar.gz
chown builduser:alpm /build/aur.db.tar.gz
EOF
systemd-nspawn -a -q -D "${chroot}" --chdir="${chrbuilduser}" --pipe \
chmod +x add-aur-repo
systemd-nspawn -a -q -D "${chroot}" -u builduser --chdir="${chrbuilduser}" --pipe \
sudo ./add-aur-repo
systemd-nspawn -a -q -D "${chroot}" --pipe \
pacman -Sy
pacman -r "${chroot}" \
-b "${chroot}/var/lib/pacman/" \
--config "${chroot}/etc/pacman.conf" \
--noconfirm -Qq |
nl > "${BASEDIR}/.#orig-pkgs.log"
# Fix auto generated broken user gpg config.
systemd-nspawn -a -q -D "${chroot}" -u builduser --chdir="${chrbuilduser}" --pipe << EOF
gpg --list-keys &>/dev/null
mv /home/builduser/.gnupg/common.conf /home/builduser/.gnupg/common.conf-BU
EOF
if [[ -d /usr/local/aurch ]] && [[ ! -h /home/${SUDO_USER}/.aa-Aurch ]]; then
ln -s /usr/local/aurch "/home/${SUDO_USER}/.aa-Aurch"
fi
if [[ -d "${BASEDIR}" ]]; then
echo "To populate with variables run: 'aurch -Lv'" > "${BASEDIR}/.#aurch-vars"
fi
echo
cat <<-EOF | tee >( sed 's/\x1B\[[0-9;]*[A-Za-z]//g' >"${BASEDIR}/aurch.README")
${czm} Aurch nspawn-container setup completed.
${czm} Has base, base-devel, git, and aurutils installed.
${czm} Container AUR repo is set up in ${chroot}/build.
${czm} A symlink has been created from /usr/local/aurch to /home/${SUDO_USER}/.aa-Aurch
${czm} User builduser is set up in container, no password required for sudo.
${czm} Do not alter files proceeded with '#.' in base directory.
${czm} This info has been printed to: /usr/local/aurch/base/aurch.README
EOF
sleep 1
echo "${czm} To proceed with setting up the required local AUR repo, run: aurch-setup -Sh"
else
echo; echo "${czm} builduser setup failed in container."
fi
}
#========================================================================================================================#
setup_local_aur_repo(){
echo "${czm} Local AUR repo: ${AURREPO}"
sed -i '$a\#' /etc/pacman.conf
sed -i '$a\# Path to aurch.conf' /etc/pacman.conf
sed -i '$a\Include = /etc/aurch.conf' /etc/pacman.conf
sed -i '/CacheDir/s/^#//g' /etc/pacman.conf
echo "${czm} Following is the aurch.conf file: /etc/aurch.conf"
cat <<-EOF > /etc/aurch.conf
[options]
CacheDir = ${AURREPO}
CleanMethod = KeepInstalled
[${REPONAME}]
SigLevel = Never TrustAll
Server = file://${AURREPO}
EOF
if [[ ! -d ${AURREPO} ]]; then
mkdir -p "${AURREPO}"
fi
# repo-add "${AURREPO}/${REPONAME}".db.tar.gz
repo-add "${AURREPO}/${REPONAME}".db.tar.gz $(find "${chroot}"/build/aurutils-*-any.pkg.tar.zst)
pacsync "${REPONAME}"
chown -R :alpm "${AURREPO}"
pacman -Sy
echo; echo "${czm} Completed setting up a local pacman AUR repo in ${AURREPO}."
echo " In order to avoid an unplanned system update within the script,"
echo " your system has been left in a partial update' state."
echo "${czm} Run 'pacman -Syu' upon completion."; echo
}
#========================================================================================================================#
if [[ -z ${*} ]]; then
help
fi
while :; do
case "${1-}" in
-Sc|--setupcontainer) check_depends ; setup_chroot ;;
-Sh|--setuphost) check_depends ; setup_local_aur_repo ;;
-h|--help) help ;;
-V|--version) awk -e '/^# aurch/ {print $2,$3}' "$(which aurch-setup)" ;;
-?*) echo "${czm}${error} Input error. Running --help" ; help ;;
*) break
esac
shift
done