Skip to content

Instantly share code, notes, and snippets.

@FlyingFathead
Last active December 6, 2021 03:42
Show Gist options
  • Save FlyingFathead/3055b243d0ad0f33ce7847fab89974ac to your computer and use it in GitHub Desktop.
Save FlyingFathead/3055b243d0ad0f33ce7847fab89974ac to your computer and use it in GitHub Desktop.
Ubuntu Impish (21.10) Raspberry Pi4B distribution check and 'linux-modules-extra-raspi' "auto-installer" (fingers crossed!)
#!/bin/bash
# this script will download and install the 'linux-modules-extra-raspi' package if:
#
# 1. the Linux distribution is detected as Ubuntu, and
# 2. the mentioned package is not installed on the system according to dpkg.
#
# The packages are:
# On the Raspberry Pi: apt package 'linux-modules-extra[-raspi]', or:
# on x86_64 (amd64) environments, named according to the kernel that's in use.
# is required starting from Ubuntu Impish to install zram-swap
#
# For more info on the purpose of this script, please see:
# https://github.com/foundObjects/zram-swap (I'm not affiliated with the project.)
# or, more specifically: https://github.com/foundObjects/zram-swap/issues/8
# First, we check if we're running Ubuntu.
if [ -n "$(uname -a | grep Ubuntu)" ]; then
echo "INFO: Your distribution was detected as Ubuntu." &&
echo "INFO: This might require extra apt packages to be installed."
# get the kernel version to be used for suffixes on generic packages
kernelver=$(uname -r)
export kernelver
# check for aarch64 (Raspberry Pi, etc.)
checkcpu="aarch64"
export checkcpu
if [ $(uname -p | grep -c "$checkcpu") -eq 1 ];
then
echo "INFO: Detected your processor as $checkcpu."
raspicheck=$(cat /proc/device-tree/model | grep -c "Raspberry Pi")
export raspicheck
if [ $raspicheck -eq 1 ];
then
aptrequired="linux-modules-extra-raspi"
export aptrequired
echo "INFO: Your computer was detected as Raspberry Pi."
echo "INFO: Will be requiring $aptrequired to be installed."
else
aptrequired="linux-modules-extra-$kernelver"
export aptrequired
echo "INFO: You will (likely) need the generic $aptrequired for your kernel."
echo "INFO: This is however completely untested on other aarch64 platforms than the RPi4B as for this script."
fi
fi
# check for x86_64
checkcpu="x86_64"
export checkcpu
if [ $(uname -p | grep -c "$checkcpu") -eq 1 ];
then
echo "INFO: Detected your processor as $checkcpu."
aptrequired="linux-modules-extra-$kernelver"
export aptrequired
echo "INFO: Will be requiring $aptrequired to be installed."
fi
# Look for the required apt package.
if [ $(dpkg-query -W -f='${Status}' $aptrequired 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
# Install it if it's not found.
echo "INFO: Package $aptrequired not installed!" &&
echo "INFO: Getting it via apt-get now." &&
echo "Running apt-get update & upgrade first..."
sudo apt-get update &&
sudo apt-get upgrade &&
echo "Installing the $aptrequired package via apt-get..."
sudo apt-get install $aptrequired &&
# Double-checking that the package IS indeed installed! Rather be safe than sorry! :o)
echo "Making sure the package is now installed."
if [ $(dpkg-query -W -f='${Status}' $aptrequired 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
echo "ERROR! Package wasn't installed successfully!"
echo "ERROR! Please either re-run the installer or try reconfiguring/fixing your dpkg/apt-get so that $aptrequired is properly installed on this system."
exit 1
fi
else
echo "INFO: The required apt package $aptrequired is installed."
fi
echo "OK!"
fi
@FlyingFathead
Copy link
Author

Feel free to comment if this needs improvements or doesn't work out for you! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment