2020-04-29 14:21:46 +02:00
|
|
|
#!/bin/bash
|
2020-04-29 18:35:35 +02:00
|
|
|
# shellcheck disable=SC2010 # ls | grep allowed
|
2020-04-29 14:21:46 +02:00
|
|
|
|
|
|
|
echo "Setupscript for Raspberry Pi SD's"
|
|
|
|
echo
|
|
|
|
echo "@author Kevin Veen-Birkenbach [kevin@veen.world]"
|
|
|
|
echo "@since 2017-03-12"
|
|
|
|
echo
|
2020-04-29 18:21:18 +02:00
|
|
|
|
|
|
|
echo "Starting setup..."
|
|
|
|
|
2020-04-29 18:35:35 +02:00
|
|
|
echo "Define working folder..."
|
|
|
|
working_folder="/tmp/raspberry-pi-tools-$(date +%s)"; #Working folder
|
|
|
|
|
|
|
|
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "Create temporary working folder in $working_folder";
|
2020-04-29 15:57:42 +02:00
|
|
|
mkdir -v "$working_folder"
|
2020-04-29 18:21:18 +02:00
|
|
|
|
|
|
|
echo "Checking if root..."
|
2020-04-29 15:57:42 +02:00
|
|
|
if [ "$(id -u)" != "0" ];then
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "This script must be executed as root!" && exit 1
|
|
|
|
fi
|
2020-04-29 18:21:18 +02:00
|
|
|
|
|
|
|
echo "Configure user..."
|
2020-04-29 16:39:19 +02:00
|
|
|
echo "Please type in a valid username from which the SSH-Key should be copied:"
|
2020-04-29 18:21:18 +02:00
|
|
|
read -r origin_username;
|
2020-04-29 18:35:35 +02:00
|
|
|
getent passwd "$origin_username" > /dev/null 2 && echo "User $origin_username doesn't exist. Abord program." && exit 1;
|
|
|
|
origin_user_home="/home/$origin_username/";
|
2020-04-29 16:56:31 +02:00
|
|
|
|
2020-04-29 18:21:18 +02:00
|
|
|
# Create image folders
|
|
|
|
image_folder="$origin_user_home/Images/";
|
2020-04-29 16:39:19 +02:00
|
|
|
echo "The images will be stored in \"$image_folder\"."
|
|
|
|
if [ ! -d "$DIR" ]; then
|
|
|
|
echo "Folder \"$image_folder\" doesn't exist. It will be created now."
|
|
|
|
mkdir -v "$image_folder"
|
|
|
|
fi
|
2020-04-29 18:21:18 +02:00
|
|
|
|
|
|
|
echo "Select sd-card..."
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "List of actual mounted devices:"
|
|
|
|
ls -lasi /dev/ | grep -E "sd|mm"
|
|
|
|
echo
|
2020-04-29 18:35:35 +02:00
|
|
|
while [ ! -b "$sd_card_path" ]
|
2020-04-29 14:21:46 +02:00
|
|
|
do
|
2020-04-29 18:21:18 +02:00
|
|
|
echo "Please type in the name of the correct sd-card."
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "/dev/:"
|
|
|
|
read -r device
|
2020-04-29 18:21:18 +02:00
|
|
|
sd_card_path="/dev/$device"
|
2020-04-29 14:21:46 +02:00
|
|
|
done
|
2020-04-29 18:21:18 +02:00
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Select which Raspberry Pi version should be used"
|
2020-04-29 14:21:46 +02:00
|
|
|
read -r version
|
2020-04-29 18:21:18 +02:00
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Select which operation system should be used..."
|
2020-04-29 18:21:18 +02:00
|
|
|
echo
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "1) arch"
|
|
|
|
echo "2) moode"
|
|
|
|
echo "3) retropie"
|
2020-04-29 18:21:18 +02:00
|
|
|
echo
|
2020-04-29 14:21:46 +02:00
|
|
|
echo "Please type in the os:"
|
|
|
|
read -r os
|
2020-04-29 18:56:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
os_does_not_support_raspberry_version_error () {
|
|
|
|
echo "$1 for Raspberry Pi Version $2 is not supported!" && exit 1;
|
|
|
|
}
|
|
|
|
|
2020-04-29 14:21:46 +02:00
|
|
|
case "$os" in
|
|
|
|
"arch")
|
|
|
|
base_download_url="http://os.archlinuxarm.org/os/";
|
|
|
|
case "$version" in
|
|
|
|
"1")
|
|
|
|
imagename="ArchLinuxARM-rpi-latest.tar.gz"
|
|
|
|
;;
|
|
|
|
"2" | "3")
|
|
|
|
imagename="ArchLinuxARM-rpi-2-latest.tar.gz"
|
|
|
|
;;
|
|
|
|
"4")
|
|
|
|
imagename="ArchLinuxARM-rpi-4-latest.tar.gz"
|
|
|
|
;;
|
|
|
|
*)
|
2020-04-29 15:57:42 +02:00
|
|
|
os_does_not_support_raspberry_version_error "$os" "$version"
|
2020-04-29 14:21:46 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
"moode")
|
|
|
|
base_download_url="https://github.com/moode-player/moode/releases/download/r651prod/"
|
|
|
|
imagename="moode-r651-iso.zip";
|
|
|
|
;;
|
|
|
|
"retropie")
|
|
|
|
base_download_url="https://github.com/RetroPie/RetroPie-Setup/releases/download/4.6/";
|
|
|
|
case "$version" in
|
|
|
|
"1")
|
|
|
|
imagename="retropie-buster-4.6-rpi1_zero.img.gz"
|
|
|
|
;;
|
|
|
|
|
|
|
|
"2" | "3")
|
|
|
|
imagename="retropie-buster-4.6-rpi2_rpi3.img.gz"
|
|
|
|
;;
|
|
|
|
|
|
|
|
"4")
|
|
|
|
imagename="retropie-buster-4.6-rpi4.img.gz"
|
|
|
|
;;
|
|
|
|
*)
|
2020-04-29 15:57:42 +02:00
|
|
|
os_does_not_support_raspberry_version_error "$os" "$version"
|
2020-04-29 14:21:46 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
|
|
|
;;
|
|
|
|
esac
|
2020-04-29 18:21:18 +02:00
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Download os-image..."
|
2020-04-29 14:21:46 +02:00
|
|
|
download_url="$base_download_url$imagename"
|
2020-04-29 16:56:31 +02:00
|
|
|
image_path="$image_folder$image_path"
|
2020-04-29 18:21:18 +02:00
|
|
|
|
2020-04-29 18:35:35 +02:00
|
|
|
if [ ! -f "$image_path" ]
|
2020-04-29 14:21:46 +02:00
|
|
|
then
|
2020-04-29 16:56:31 +02:00
|
|
|
echo "The selected image \"$imagename\" doesn't exist under local path \"$image_path\"."
|
2020-04-29 18:35:35 +02:00
|
|
|
if [ ! -f "$image_path" ]
|
2020-04-29 14:21:46 +02:00
|
|
|
then
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Image \"$imagename\" gets downloaded from \"$download_url\"..."
|
2020-04-29 15:57:42 +02:00
|
|
|
wget "$download_url"
|
2020-04-29 14:21:46 +02:00
|
|
|
fi
|
|
|
|
fi
|
2020-04-29 18:21:18 +02:00
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Preparing mount paths..."
|
2020-04-29 18:21:18 +02:00
|
|
|
boot_mount_path="$working_folder""boot"
|
|
|
|
root_mount_path="$working_folder""root"
|
|
|
|
mkdir -v "$boot_mount_path"
|
|
|
|
mkdir -v "$root_mount_path"
|
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Defining partition paths..."
|
2020-04-29 18:21:18 +02:00
|
|
|
if [ "${sd_card_path:5:1}" != "s" ]
|
|
|
|
then
|
|
|
|
partion="p"
|
|
|
|
else
|
|
|
|
partion=""
|
|
|
|
fi
|
|
|
|
boot_partition_path=$sd_card_path$partion"1"
|
|
|
|
root_partition_path=$sd_card_path$partion"2"
|
|
|
|
|
|
|
|
mount_partitions(){
|
|
|
|
echo "Mount boot and root partition..."
|
|
|
|
mount "$boot_partition_path" "$boot_mount_path"
|
|
|
|
mount "$root_partition_path" "$root_mount_path"
|
|
|
|
}
|
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Copy data to sd-card..."
|
2020-04-29 14:21:46 +02:00
|
|
|
case "$os" in
|
|
|
|
"arch")
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "fdisk is executedman fd"
|
2020-04-29 14:21:46 +02:00
|
|
|
( echo "o" #Type o. This will clear out any partitions on the drive.
|
|
|
|
echo "p" #Type p to list partitions. There should be no partitions left
|
|
|
|
echo "n" #Type n,
|
|
|
|
echo "p" #then p for primary,
|
|
|
|
echo "1" #1 for the first partition on the drive,
|
|
|
|
echo "" #press ENTER to accept the default first sector,
|
|
|
|
echo "+100M" #then type +100M for the last sector.
|
|
|
|
echo "t" #Type t,
|
|
|
|
echo "c" #then c to set the first partition to type W95 FAT32 (LBA).
|
|
|
|
echo "n" #Type n,
|
|
|
|
echo "p" #then p for primary,
|
|
|
|
echo "2" #2 for the second partition on the drive,
|
|
|
|
echo "" #and then press ENTER twice to accept the default first and last sector.
|
|
|
|
echo ""
|
|
|
|
echo "w" #Write the partition table and exit by typing w.
|
2020-04-29 18:21:18 +02:00
|
|
|
)| fdisk "$sd_card_path"
|
|
|
|
|
|
|
|
echo "Format boot partition..."
|
|
|
|
mkfs.vfat "$boot_partition_path"
|
|
|
|
|
|
|
|
echo "Format root partition..."
|
|
|
|
mkfs.ext4 "$root_partition_path"
|
2020-04-29 14:21:46 +02:00
|
|
|
|
2020-04-29 18:21:18 +02:00
|
|
|
mount_partitions;
|
|
|
|
|
|
|
|
echo "Root files will be transfered to sd-card..."
|
|
|
|
bsdtar -xpf "$image_path" -C "$root_mount_path"
|
|
|
|
sync
|
2020-04-29 14:21:46 +02:00
|
|
|
|
2020-04-29 18:21:18 +02:00
|
|
|
echo "Boot files will be transfered to sd-card..."
|
|
|
|
mv -v "$root_mount_path/boot/"* "$boot_mount_path"
|
2020-04-29 14:21:46 +02:00
|
|
|
|
2020-04-29 15:57:42 +02:00
|
|
|
;;
|
2020-04-29 16:09:53 +02:00
|
|
|
"moode")
|
2020-04-29 18:21:18 +02:00
|
|
|
unzip -p "$image_path" | sudo dd of="$sd_card_path" bs=4M conv=fsync
|
|
|
|
mount_partitions;
|
2020-04-29 15:57:42 +02:00
|
|
|
;;
|
2020-04-29 16:09:53 +02:00
|
|
|
"retropie")
|
2020-04-29 18:21:18 +02:00
|
|
|
gunzip -c "$image_path" | sudo dd of="$sd_card_path" bs=4M conv=fsync
|
|
|
|
mount_partitions;
|
2020-04-29 16:09:53 +02:00
|
|
|
;;
|
2020-04-29 14:21:46 +02:00
|
|
|
*)
|
2020-04-29 15:57:42 +02:00
|
|
|
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
|
|
|
;;
|
2020-04-29 14:21:46 +02:00
|
|
|
esac
|
2020-04-29 18:21:18 +02:00
|
|
|
|
|
|
|
echo "Define target paths..."
|
|
|
|
target_home_path="$root_mount_path/home/";
|
2020-04-29 18:35:35 +02:00
|
|
|
target_username=$(ls "$target_home_path");
|
2020-04-29 18:21:18 +02:00
|
|
|
target_user_home_folder_path="$target_home_path$target_username/";
|
|
|
|
|
|
|
|
echo "Copy ssh key to target..."
|
|
|
|
target_user_ssh_folder_path="$target_user_home_folder_path"".ssh/"
|
|
|
|
target_authorized_keys="$target_user_ssh_folder_path/authorized_keys"
|
|
|
|
origin_user_rsa_pub="$origin_user_home/.ssh/id_rsa.pub";
|
|
|
|
if [ -f "$origin_user_rsa_pub" ]
|
|
|
|
then
|
|
|
|
mkdir -v "$target_user_ssh_folder_path"
|
|
|
|
cat "$origin_user_rsa_pub" > "$target_authorized_keys"
|
|
|
|
chown -R 1000 "$target_user_ssh_folder_path"
|
|
|
|
chmod -R 400 "$target_user_ssh_folder_path"
|
|
|
|
else
|
|
|
|
echo "The ssh key \"$origin_user_rsa_pub\" can't be copied to \"$target_authorized_keys\" because it doesn't exist."
|
|
|
|
fi
|
|
|
|
|
2020-04-29 18:56:07 +02:00
|
|
|
echo "Change password of user \"$target_username\"..."
|
|
|
|
chroot "$root_mount_path" "passwd $target_username"
|
|
|
|
|
|
|
|
echo "Change password of root user..."
|
|
|
|
chroot "$root_mount_path" "passwd root"
|
|
|
|
|
|
|
|
echo "Do you want to copy all Wifi passwords to the sd?(y/n)"
|
|
|
|
read -r copy_wifi
|
|
|
|
if [ "$copy_wifi" = "y" ]
|
|
|
|
then
|
|
|
|
origin_wifi_config_path="/etc/NetworkManager/system-connections/"
|
|
|
|
target_wifi_config_path="$root_mount_path$origin_wifi_config_path"
|
|
|
|
rsync -av "$origin_wifi_config_path" "$target_wifi_config_path"
|
|
|
|
fi
|
|
|
|
|
2020-04-29 18:21:18 +02:00
|
|
|
echo "Cleaning up..."
|
|
|
|
umount -v "$root_mount_path" "$boot_mount_path"
|
|
|
|
rm -vr "$working_folder"
|
|
|
|
echo "Setup successfull :)" && exit 0
|