244 lines
5.9 KiB
Bash
244 lines
5.9 KiB
Bash
#!/usr/bin/bash
|
|
|
|
# Yes, this is a disaster of a bash script.
|
|
# I'm really hacking things together here.
|
|
|
|
# Fail on command failure.
|
|
# This acts funky, and prevents failure on reserved words like:
|
|
#
|
|
# "The -e setting shall be ignored when executing the compound list following the
|
|
# while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word,
|
|
# or any command of an AND-OR list other than the last." - set man page
|
|
#set -e
|
|
|
|
RED='\e[31m'
|
|
GREEN='\e[32m'
|
|
|
|
assert() {
|
|
if ! $1; then
|
|
echo "Script depends on $2 to function, please resolve manually."
|
|
fi
|
|
|
|
# Yes, this is always true, but anyway.
|
|
return $1
|
|
}
|
|
|
|
is_installed() {
|
|
return ! which "$1" > /dev/null
|
|
}
|
|
|
|
run_unprivileged() {
|
|
sudo -u "$USERNAME" "$@"
|
|
}
|
|
|
|
log() {
|
|
if "$1"; then
|
|
echo -e "$2...${GREEN}yes $3"
|
|
else
|
|
echo -e "$2...${RED}no"
|
|
fi
|
|
}
|
|
|
|
# Checks
|
|
|
|
[ $(id -u) = 0 ]
|
|
if ! $?; then
|
|
echo "Script must be run with elevated permissions."
|
|
exit 1
|
|
fi
|
|
|
|
# As much as I highly doubt that pacman, curl, lspci, and mhwd-kernel will be missing,
|
|
# might as well do the easy check.
|
|
#
|
|
# Also, if sudo cannot be called, the user might be using doas, in which case they'll need to fix
|
|
# their aliases or patch the privilege drop for run_unprivileged.
|
|
|
|
SUDO=$(is_installed sudo)
|
|
PACMAN=$(is_installed pacman)
|
|
YAY=$(is_installed yay)
|
|
CURL=$(is_installed curl)
|
|
LSPCI=$(is_installed lspci)
|
|
REFLECTOR=$(is_installed reflector)
|
|
MHWD_KERNEL=$(is_installed mhwd-kernel)
|
|
DKMS=$(is_installed dkms)
|
|
|
|
log $(assert $SUDO "sudo") "sudo"
|
|
log $(assert $PACMAN "pacman") "pacman"
|
|
log $YAY "yay"
|
|
log $CURL "curl"
|
|
log $(assert $LSPCI "lspci") "lspci"
|
|
log $REFLECTOR "reflector"
|
|
log $(assert $MHWD_KERNEL "mhwd-kernel") "mhwd-kernel"
|
|
log $DKMS "dkms"
|
|
|
|
pacman -Qs pamac
|
|
PAMAC=$?
|
|
|
|
USERNAME=$(logname)
|
|
[ $#USERNAME > 0]
|
|
log $? "unprivileged_user" "$USERNAME"
|
|
|
|
[ $(lspci | grep "VGA" | grep "nvidia") > 0 ]
|
|
NVIDIA_GPU=$?
|
|
log $NVIDIA_GPU "nvidia_gpu"
|
|
|
|
# Identify installed Manjaro kernels
|
|
mhwd_dump=$(mhwd-kernel -li | grep "linux")
|
|
|
|
re="linux\d+"
|
|
|
|
ACTIVE_KERNEL=$(echo "$mhwd_dump" | grep "running" | grep -Po "$re" )
|
|
ALTERNATE_KERNELS=$(echo "$mhwd_dump" | grep -Po "$re" | grep -v "$ACTIVE_KERNEL")
|
|
|
|
[ $#ACTIVE_KERNEL > 0]
|
|
log $? "active_kernel" "$ACTIVE_KERNEL"
|
|
|
|
[ $#ALTERNATE_KERNELS > 0 ]
|
|
log $? "alternate_kernels"
|
|
|
|
# no dkms, no propagation
|
|
# dkms, no nvidia, guaranteed propagation
|
|
# dkms, w/ nvidia, possible propagation
|
|
PROPAGATE_DKMS=$DKMS
|
|
if $DKMS && $NVIDIA_GPU; then
|
|
[ $(dkms status | grep -v "nvidia") > 0 ]
|
|
PROPAGATE_DKMS=$?
|
|
fi
|
|
|
|
log "$PROPAGATE_DKMS" "propagate_dkms"
|
|
|
|
# Prep
|
|
pacman -Syy
|
|
|
|
if $PAMAC; then
|
|
echo "Pamac is installed, removing..."
|
|
# TODO: Uninstall pamac if present
|
|
fi
|
|
|
|
if ! $YAY; then
|
|
echo "Yay is not installed, calling: pacman -S yay"
|
|
pacman -S yay
|
|
|
|
assert $(is_installed yay) "yay"
|
|
|
|
run_unprivileged yay -Syy
|
|
fi
|
|
|
|
if ! $CURL; then
|
|
echo "Curl is not installed, calling: pacman -S curl"
|
|
pacman -S curl
|
|
|
|
assert $(is_installed curl) "curl"
|
|
fi
|
|
|
|
if ! $REFLECTOR; then
|
|
echo "Reflector is not installed, calling: yay -S reflector"
|
|
run_unprivileged yay -S reflector
|
|
|
|
assert $(is_installed reflector) "reflector"
|
|
fi
|
|
|
|
# Configure new mirrorlists
|
|
|
|
# CachyOS
|
|
# See: https://wiki.cachyos.org/features/optimized_repos/#adding-our-repositories-to-an-existing-arch-linux-install
|
|
|
|
cd $(mktemp -d)
|
|
curl https://mirror.cachyos.org/cachyos-repo.tar.xz -o cachyos-repo.tar.xz
|
|
tar xvf cachyos-repo.tar.xz
|
|
cd cachyos-repo
|
|
|
|
# Just in case, ensure the script can be executed.
|
|
if [[ ! -x "cachyos-repo.sh" ]]; then
|
|
chmod +x cachyos-repo.sh
|
|
fi
|
|
|
|
./cachyos-repo.sh
|
|
|
|
# Arch
|
|
# Backup the old Manjaro mirrorlist
|
|
DEFAULT_MIRRORLIST='/etc/pacman.d/mirrorlist'
|
|
|
|
mv $DEFAULT_MIRRORLIST "$DEFAULT_MIRRORLIST.bak"
|
|
curl -o "$DEFAULT_MIRRORLIST" https://archlinux.org/mirrorlist/all/
|
|
|
|
reflector\
|
|
--fastest 32\
|
|
--protocol https\
|
|
--sort rate\
|
|
--country CA,NZ,DE,NL,PL,SE\
|
|
--save /etc/pacman.d/mirrorlist
|
|
|
|
pacman -Syy
|
|
|
|
# Install new kernel
|
|
|
|
DEFAULT_KERNEL="linux-cachyos"
|
|
PROPAGATE_MANJARO="Keep all currently installed Manjaro kernels"
|
|
|
|
# yes, they're hard-coded. For now at least.
|
|
KNOWN_KERNELS=(
|
|
"$PROPAGATE_MANJARO",
|
|
"linux",
|
|
"linux-lts",
|
|
"linux-hardened",
|
|
"linux-rt",
|
|
"linux-cachyos",
|
|
"linux-cachyos-lts",
|
|
"linux-cachyos-hardened",
|
|
"linux-cachyos-bore",
|
|
"linux-cachyos-eevdf",
|
|
"linux-cachyos-bmq",
|
|
"linux-cachyos-deckify",
|
|
"linux-cachyos-rc",
|
|
"linux-cachyos-rt-bore",
|
|
"linux-cachyos-server",
|
|
"linux-zen"
|
|
#"linux-cachyos-cacule",
|
|
#"linux-cachyos-debug",
|
|
#"linux-cachyos-gcc",
|
|
)
|
|
|
|
echo "INSTALL NEW KERNEL"
|
|
echo "Warning: All options except 0 will remove all Manjaro kernels except the active one."
|
|
echo " If you have no additional kernels installed, then this will do nothing."
|
|
echo "Note: Multiple options can be chosen with comma separation."
|
|
echo
|
|
|
|
# List KNOWN_KERNELS
|
|
for idx in "${!KNOWN_KERNELS[@]}"; do
|
|
printf "%s\t%s\n" "$idx" "${KNOWN_KERNELS[$idx]}"
|
|
done
|
|
|
|
# Print default kernel selection
|
|
for idx in "${!KNOWN_KERNELS[@]}"; do
|
|
if [[ "${KNOWN_KERNELS[$idx]}" == "$DEFAULT_KERNEL" ]]; then
|
|
printf "Default: %s (%s)" "$idx" "$DEFAULT_KERNEL"
|
|
kernels="$idx"
|
|
fi
|
|
done
|
|
|
|
echo -n "Kernel(s): "
|
|
read -r tmp
|
|
|
|
if [[ ${#tmp} > 0 ]]; then
|
|
# Credit to bgoldst on StackOverflow for this solution.
|
|
# https://stackoverflow.com/questions/10586153/how-to-split-a-string-into-an-array-in-bash
|
|
kernels=$(readarray -td, a <<< "$tmp,"; unset 'a[-1]'; declare -p a)
|
|
fi
|
|
|
|
if [[ $ALTERNATE_KERNELS > 0 ]]; then
|
|
# Determine if the user wants to maintain the installed kernel(s).
|
|
for idx in "${kernels}"; do
|
|
if [[ "${KNOWN_KERNELS[$idx]}" == "$PROPAGATE_MANJARO" ]]; then
|
|
run_unprivileged yay -R "${ALTERNATE_KERNELS[@]}"
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# TODO: Install kernels
|
|
# TODO: dkms module propagation
|
|
# TODO: Remove DKMS if only present for nvidia-dkms [ ! $PROPAGATE_DKMS ]
|
|
# TODO: Kill Orphans
|