stormux/build/build-stormux.sh

172 lines
5.3 KiB
Bash
Raw Normal View History

2020-12-08 12:14:43 -05:00
#! /bin/bash
2020-12-08 12:26:37 -05:00
#
# Copyright 2020, Stormux, <storm_dragon@linux-a11y.org>
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
2020-12-08 12:14:43 -05:00
# keep track of mounted status for exit function
mounted=1
2020-12-08 12:14:43 -05:00
set -e # Don't want to destroy stuff if this goes majorly wrong.
trap cleanup EXIT # make sure the script cleans up after itself before closing.
cleanup() {
if [[ $mounted -eq 0 ]]; then
umount -R /mnt
2020-12-09 19:40:39 -05:00
partx -d "${loopdev}"
losetup --detach "${loopdev}"
fi
exit 0
}
help() {
echo -e "Usage:\n"
2020-12-09 08:24:34 -05:00
echo "With no arguments, build with default parameters."
for i in "${!command[@]}" ; do
echo "-${i}: ${command[${i}]}"
done | sort
exit 0
}
# Array of command line arguments
declare -A command=(
[h]="This help screen."
[n]="Image name, default is stormux-pi<3|4>-<yyyy-mm-dd>.img"
[s]="image size in GB, default is 4."
2020-12-09 08:24:34 -05:00
[v]="Version of the Raspberry Pi for which you are building. (3|4 default)"
)
# Convert the keys of the associative array to a format usable by getopts
args="${!command[*]}"
args="${args//[[:space:]]/}"
while getopts "${args}" i ; do
case "$i" in
n)
shift
imageName="$1"
shift
;;
h) help;;
s)
shift
if [[ "$1" =~ ^[[:digit:]]+$ ]]; then
imageSize="${1}G"
else
echo "Image size must be numeric."
exit 1
fi
shift
;;
v)
shift
if [[ "$1" =~ ^[34]$ ]]; then
imageVersion="$1"
else
echo "Image version must be 3 for the Raspberry Pi 3, or 4 for the Raspberry Pi 4 (default)."
exit 1
fi
shift
;;
esac
done
# make sure variables are set, or use defaults.
export imageVersion="${imageVersion:-4}"
export imageSize="${imageSize:-4G}"
export imageName="${imageName:-stormux-pi${imageVersion}-$(date '+%Y-%m-%d').img}"
# Make sure the image file doesn't exist.
if [[ -e "$imageName" ]]; then
echo "${imageName} exists, please remove or move it for this script to continue."
exit 1
2020-12-08 12:14:43 -05:00
fi
# Make sure this script is ran as root.
2020-12-08 12:14:43 -05:00
if [ "$(whoami)" != "root" ] ; then
echo "Error: This script must be run as root."
exit 1
fi
# make sure the needed tools are installed
if [[ "$(uname -m)" == "x86_64" ]]; then
if ! pacman -Q qemu-user-static &> /dev/null ; then
echo "Please install pacman -Q qemu-user-static before continuing."
exit 1
fi
2020-12-08 12:14:43 -05:00
fi
for i in parted wget ; do
if ! pacman -Q $i &> /dev/null ; then
echo "Please install $i before continuing."
exit 1
fi
done
2020-12-09 08:24:34 -05:00
# Url for the image to be downloaded.
url[3]="http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz"
url[4]="http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-aarch64-latest.tar.gz"
fallocate -l $imageSize "$imageName"
loopdev="$(losetup --find --show "${imageName}")"
2020-12-09 19:40:39 -05:00
parted --script "${loopdev}" mklabel msdos mkpart primary fat32 0% 100M mkpart primary ext4 100M 100%
mkfs.vfat -F32 "${loopdev}p1"
mkfs.ext4 -F "${loopdev}p2"
mount "${loopdev}p2" /mnt
2020-12-08 12:14:43 -05:00
mkdir /mnt/boot
2020-12-09 19:40:39 -05:00
mount "${loopdev}p1" /mnt/boot
# Things are mounted now, so set mounted to 0 (bash true)
mounted=0
2020-12-09 08:24:34 -05:00
wget "${url[$imageVersion]}" -O- | bsdtar -xpf - -C /mnt
2020-12-09 08:53:05 -05:00
arch-chroot /mnt << EOF
# set up pacman
2020-12-08 12:14:43 -05:00
pacman-key --init
pacman-key --populate archlinuxarm
2020-12-09 08:24:34 -05:00
pacman -Syu --needed --noconfirm base base-devel bash-completion cronie espeak-ng git
# Set the distribution name.
echo 'Stormux \r (\l)' > /etc/issue
echo >> /etc/issue
# Change the alarm user to be stormux
usermod -a -g users -G wheel,audio,video -m -d /home/stormux -l stormux alarm
# Grant sudo privileges to the stormux user for package installation.
echo 'stormux ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers.d/wheel
# Set the password for the stormux user
echo -e "stormux\nstormux" | passwd "stormux"
# Change to the stormux user and install some packages
sudo su - stormux
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si
cd ~
rm -rf yay
2020-12-09 08:53:05 -05:00
yay -S fenrir-git informant magic-wormhole
exit
2020-12-09 08:53:05 -05:00
# Make sure informant is up to date with news
informant read --all
2020-12-09 08:24:34 -05:00
# Configure sudo for group wheel, remove nopasswd for the stormux user
sed '/stormux/d' /etc/sudoers.d/wheel
sed 's/^# %wheel ALL=(ALL) ALL$/%wheel ALL=(ALL) ALL/' /etc/sudoers.d/wheel
2020-12-09 15:17:51 -05:00
# Set the hostname
echo stormux > /etc/hostname
2020-12-09 08:24:34 -05:00
# Enable services
systemctl enable cronie.service fenrirscreenreader.service
2020-12-09 08:53:05 -05:00
# Update fstab for Raspberry Pi 4.
[[ $imageVersion -eq 4 ]] && sed -i 's/mmcblk0/mmcblk1/g' /etc/fstab
2020-12-08 12:14:43 -05:00
EOF
exit 0