Skip to content

Commit 77dd5be

Browse files
committed
feat(scripts): add archlinux_util.sh utility script for Arch Linux
This commit introduces a new utility script, `archlinux_util.sh`, under the `/scripts` directory. This script performs common checks for Arch-based systems and facilitates consistent usage of the pacman package manager. It also provides functions for adjusting directory permissions and installing packages. This script is intended to be used as a utility helper when setting up and managing Arch Linux systems in devcontainer features.
1 parent 08f6276 commit 77dd5be

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

scripts/archlinux_util.sh

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/bin/sh
2+
#-----------------------------------------------------------------------------------------------------------------
3+
# Copyright (c) Bart Venter.
4+
# Licensed under the MIT License. See https://github.com/bartventer/devcontainer-features for license information.
5+
#-----------------------------------------------------------------------------------------------------------------
6+
#
7+
# Maintainer: Bart Venter <https://github.com/bartventer>
8+
#
9+
# Description: This utility script, `archlinux_util.sh`, performs common checks for Arch-based systems and
10+
# facilitates consistent usage of the pacman package manager. It also provides functions for
11+
# adjusting directory permissions and installing packages. This script is intended to be used as a
12+
# utility helper when setting up and managing Arch Linux systems in devcontainer features.
13+
#
14+
#-----------------------------------------------------------------------------------------------------------------
15+
16+
17+
# Exit on error
18+
set -e
19+
20+
DIR_PERMS_CHECKED="${DIR_PERMS_CHECKED:-false}"
21+
KEYRING_CHECKED="${KEYRING_CHECKED:-false}"
22+
23+
# Echo message
24+
echo_msg() {
25+
old_message=$message
26+
message=$1
27+
echo "[devcontainer-features/scripts/archlinux] ${message}"
28+
message=$old_message
29+
}
30+
31+
# Checks if script is run as root
32+
check_root() {
33+
echo_msg "Checking if script is run as root..."
34+
if [ "$(id -u)" -ne 0 ]; then
35+
printf 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.\n'
36+
exit 1
37+
fi
38+
echo_msg "OK. Script is run as root."
39+
}
40+
41+
# Checks if we're on an Arch-based system
42+
check_system() {
43+
echo_msg "Checking Arch-based system..."
44+
if ! grep -q 'ID=arch' /etc/os-release; then
45+
echo "This script is intended for Arch-based systems. Please run this script on an Arch-based system."
46+
exit 1
47+
fi
48+
echo_msg "OK. On an Arch-based system."
49+
}
50+
51+
# Checks if pacman is installed
52+
check_pacman() {
53+
echo_msg "Checking if pacman is installed..."
54+
if ! command -v pacman > /dev/null 2>&1; then
55+
echo "Pacman could not be found. Please install pacman and try again."
56+
exit 1
57+
fi
58+
echo_msg "OK. Pacman is installed."
59+
}
60+
61+
init_pacman_keyring() {
62+
if [ "$KEYRING_CHECKED" = false ]; then
63+
echo_msg "Initializing pacman keyring (current count: $(pacman-key --list-keys | wc -l))..."
64+
if pacman-key --init && pacman-key --populate archlinux; then
65+
echo_msg "OK. Pacman keyring initialized (new count: $(pacman-key --list-keys | wc -l))."
66+
export KEYRING_CHECKED=true
67+
else
68+
echo_msg "ERROR. Pacman keyring initialization failed."
69+
exit 1
70+
fi
71+
72+
# Upgrade system
73+
echo_msg "Upgrading system..."
74+
pacman -Sy --needed --noconfirm archlinux-keyring && pacman -Su --noconfirm
75+
76+
77+
fi
78+
}
79+
80+
# Adjust directory permissions if needed
81+
adjust_dir_permissions() {
82+
if [ "$DIR_PERMS_CHECKED" = false ]; then
83+
echo_msg "Adjusting directory permissions..."
84+
if [ "$(stat -c %a /srv/ftp)" != "555" ]; then
85+
chmod 555 /srv/ftp
86+
fi
87+
if [ "$(stat -c %a /usr/share/polkit-1/rules.d/)" != "755" ]; then
88+
chmod 755 /usr/share/polkit-1/rules.d/
89+
fi
90+
export DIR_PERMS_CHECKED=true
91+
echo_msg "OK. Directory permissions adjusted."
92+
fi
93+
}
94+
95+
# Checks if packages are installed and if not, adds them to a list
96+
check_and_install_packages() {
97+
echo_msg "Checking if packages (${*}) are installed..."
98+
pkgs_to_install=""
99+
for pkg in "$@"; do
100+
if ! pacman -Q "$pkg" > /dev/null 2>&1; then
101+
pkgs_to_install="$pkgs_to_install $pkg"
102+
fi
103+
done
104+
105+
if [ -n "$pkgs_to_install" ]; then
106+
adjust_dir_permissions
107+
init_pacman_keyring
108+
109+
echo_msg "Installing packages ($pkgs_to_install)..."
110+
# shellcheck disable=SC2086
111+
if ! pacman -Syu --needed --noconfirm $pkgs_to_install; then
112+
echo "Failed to install packages. If you're getting an error about a missing secret key, you might need to manually import the key. Refer to the Arch Linux wiki for more information: https://wiki.archlinux.org/title/Pacman/Package_signing#Adding_unofficial_keys"
113+
exit 1
114+
fi
115+
fi
116+
117+
echo_msg "OK. All packages (${*}) installed or already present."
118+
}

0 commit comments

Comments
 (0)