raspberry-pi-tools/sd_setup.sh

371 lines
12 KiB
Bash
Raw Normal View History

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
2020-04-30 11:09:01 +02:00
echo "Setupscript for Raspberry Pi devices"
2020-04-29 14:21:46 +02:00
echo
echo "@author Kevin Veen-Birkenbach [kevin@veen.world]"
echo "@since 2017-03-12"
echo
2020-04-29 18:21:18 +02:00
2020-04-30 11:36:42 +02:00
# Define colors
2020-04-30 11:38:45 +02:00
red_color=$(tput setaf 1)
green_color=$(tput setaf 2)
yellow_color=$(tput setaf 3)
blue_color=$(tput setaf 4)
magenta_color=$(tput setaf 5)
reset_color=$(tput sgr0)
2020-04-30 11:36:42 +02:00
message(){
echo "$1[$2]:${reset_color} $3 ";
}
2020-04-30 11:15:41 +02:00
question(){
2020-04-30 11:36:42 +02:00
message "${magenta_color}" "QUESTION" "$1";
2020-04-30 11:15:41 +02:00
}
2020-04-30 11:09:01 +02:00
info(){
2020-04-30 11:36:42 +02:00
message "${blue_color}" "INFO" "$1";
2020-04-30 11:09:01 +02:00
}
2020-04-29 23:15:06 +02:00
warning(){
2020-04-30 11:36:42 +02:00
message "${yellow_color}" "WARNING" "$1";
}
success(){
message "${green_color}" "SUCCESS" "$1";
2020-04-29 23:15:06 +02:00
}
destructor(){
2020-04-30 15:32:52 +02:00
info "Cleaning up..."
2020-04-30 16:50:51 +02:00
sed -i 's/^#CHROOT //g' "$root_mount_path/etc/ld.so.preload"
umount -v "$chroot_dev_pts_mount_path" || warning "Umounting $chroot_dev_pts_mount_path failed!"
umount -v "$chroot_dev_mount_path" || warning "Umounting $chroot_dev_mount_path failed!"
umount -v "$chroot_proc_mount_path" || warning "Umounting $chroot_proc_mount_path failed!"
umount -v "$chroot_sys_mount_path" || warning "Umounting $chroot_sys_mount_path failed!"
umount -v "$root_mount_path""/boot/" || warning "Umounting $root_mount_path/boot/ failed!"
umount -v "$root_mount_path" || warning "Umounting $root_mount_path failed!"
umount -v "$boot_mount_path" || warning "Umounting $boot_mount_path failed!"
2020-04-30 15:32:52 +02:00
rmdir -v "$root_mount_path" || warning "Removing $root_mount_path failed!"
rmdir -v "$boot_mount_path" || warning "Removing $boot_mount_path failed!"
rmdir -v "$working_folder" || warning "Removing $working_folder failed!"
2020-04-29 23:15:06 +02:00
}
2020-04-29 22:26:43 +02:00
error(){
2020-04-30 11:36:42 +02:00
message "${red_color}" "ERROR" "$1 -> Leaving program."
2020-04-30 11:55:33 +02:00
if [ "$2" != "no_destructor" ]
then
destructor
fi
2020-04-29 23:15:06 +02:00
exit 1;
2020-04-29 22:26:43 +02:00
}
2020-04-29 18:35:35 +02:00
2020-04-30 11:09:01 +02:00
info "Starting setup..."
info "Define variables..."
working_folder="/tmp/raspberry-pi-tools-$(date +%s)/";
info "Define functions..."
info "Create temporary working folder in $working_folder";
mkdir -v "$working_folder"
2020-04-29 18:21:18 +02:00
2020-04-30 11:09:01 +02:00
info "Checking if root..."
if [ "$(id -u)" != "0" ];then
2020-04-29 22:26:43 +02:00
error "This script must be executed as root!"
2020-04-29 14:21:46 +02:00
fi
2020-04-29 18:21:18 +02:00
2020-04-30 11:09:01 +02:00
info "Configure user..."
2020-04-30 11:15:41 +02:00
question "Please type in a valid username from which the SSH-Key should be copied:" && read -r origin_username;
2020-04-29 22:26:43 +02:00
getent passwd "$origin_username" > /dev/null 2 || error "User $origin_username doesn't exist.";
2020-04-29 18:35:35 +02:00
origin_user_home="/home/$origin_username/";
2020-04-29 16:56:31 +02:00
2020-04-30 11:09:01 +02:00
info "Image routine starts..."
2020-04-29 19:06:39 +02:00
image_folder="$origin_user_home""Images/";
2020-04-30 11:09:01 +02:00
info "The images will be stored in \"$image_folder\"."
2020-04-29 19:06:39 +02:00
if [ ! -d "$image_folder" ]; then
2020-04-30 11:09:01 +02:00
info "Folder \"$image_folder\" doesn't exist. It will be created now."
2020-04-29 16:39:19 +02:00
mkdir -v "$image_folder"
fi
2020-04-29 18:21:18 +02:00
2020-04-30 11:15:41 +02:00
info "Selecting sd-card..."
info "Show available devices..."
2020-04-29 14:21:46 +02:00
ls -lasi /dev/ | grep -E "sd|mm"
2020-04-30 11:15:41 +02:00
question "Please type in the name of the correct device: /dev/" && read -r device
2020-04-30 10:52:30 +02:00
sd_card_path="/dev/$device"
if [ ! -b "$sd_card_path" ]
then
error "$sd_card_path is not valid device."
fi
if mount | grep -q "$sd_card_path"
then
error "Device $sd_card_path is allready mounted. Umount with \"umount $sd_card_path*\"."
fi
2020-04-29 18:21:18 +02:00
2020-04-30 11:15:41 +02:00
question "Select which Raspberry Pi version should be used:" && read -r version
2020-04-29 18:21:18 +02:00
2020-04-30 11:15:41 +02:00
info "Selecting operating system should be used..."
info "Available systems:"
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-30 11:15:41 +02:00
question "Please type in the os:" && read -r os
2020-04-29 18:56:07 +02:00
os_does_not_support_raspberry_version_error () {
2020-04-29 22:26:43 +02:00
error "$os for Raspberry Pi Version $version is not supported!";
2020-04-29 18:56:07 +02:00
}
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 21:44:50 +02:00
os_does_not_support_raspberry_version_error
2020-04-29 14:21:46 +02:00
;;
esac
;;
"moode")
2020-04-29 21:01:53 +02:00
image_checksum="185cbc9a4994534bb7a4bc2744c78197"
2020-04-29 14:21:46 +02:00
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")
2020-04-29 21:01:53 +02:00
image_checksum="98b4205ad0248d378c6776e20c54e487"
2020-04-29 14:21:46 +02:00
imagename="retropie-buster-4.6-rpi1_zero.img.gz"
;;
"2" | "3")
2020-04-29 21:01:53 +02:00
image_checksum="2e082ef5fc2d7cf7d910494cf0f7185b"
2020-04-29 14:21:46 +02:00
imagename="retropie-buster-4.6-rpi2_rpi3.img.gz"
;;
"4")
2020-04-29 21:01:53 +02:00
image_checksum="9154d998cba5219ddf23de46d8845f6c"
2020-04-29 14:21:46 +02:00
imagename="retropie-buster-4.6-rpi4.img.gz"
;;
*)
2020-04-29 21:44:50 +02:00
os_does_not_support_raspberry_version_error
2020-04-29 14:21:46 +02:00
;;
esac
;;
*)
2020-04-29 22:26:43 +02:00
error "The operation system \"$os\" is not supported yet!"
2020-04-29 14:21:46 +02:00
;;
esac
2020-04-29 18:21:18 +02:00
2020-04-30 11:09:01 +02:00
info "Generating os-image..."
2020-04-29 14:21:46 +02:00
download_url="$base_download_url$imagename"
2020-04-29 19:24:43 +02:00
image_path="$image_folder$imagename"
2020-04-29 18:21:18 +02:00
2020-04-30 11:15:41 +02:00
question "Should the image download be forced?(y/n)" && read -r force_image_download
2020-04-30 10:16:53 +02:00
if [ "$force_image_download" = "y" ]
2020-04-30 10:15:26 +02:00
then
if [ -f "$image_path" ]
then
2020-04-30 11:15:41 +02:00
info "Removing image $image_path."
2020-04-30 10:16:53 +02:00
rm "$image_path" || error "Removing image \"$image_path\" failed."
2020-04-30 10:15:26 +02:00
else
2020-04-30 11:15:41 +02:00
info "Forcing download wasn't neccessary. File $image_path doesn't exist."
2020-04-30 10:15:26 +02:00
fi
fi
2020-04-30 11:09:01 +02:00
info "Start Download procedure..."
2020-04-30 10:15:26 +02:00
if [ -f "$image_path" ]
2020-04-29 14:21:46 +02:00
then
2020-04-30 11:09:01 +02:00
info "Image exist local. Download skipped."
2020-04-30 10:15:26 +02:00
else
2020-04-30 11:09:01 +02:00
info "Image \"$imagename\" doesn't exist under local path \"$image_path\"."
info "Image \"$imagename\" gets downloaded from \"$download_url\"..."
2020-04-30 10:15:26 +02:00
wget "$download_url" -P "$image_folder" || error "Download from \"$download_url\" failed."
2020-04-29 14:21:46 +02:00
fi
2020-04-29 18:21:18 +02:00
2020-04-30 11:09:01 +02:00
info "Verifying image..."
2020-04-29 21:01:53 +02:00
if [[ -v image_checksum ]]
then
2020-04-30 10:15:26 +02:00
echo "$image_checksum $image_path"| md5sum -c -|| error "Verification failed. HINT: Force the download of the image."
2020-04-29 21:01:53 +02:00
else
2020-04-30 09:51:12 +02:00
warning "Verification is not possible. No checksum is defined."
2020-04-29 21:01:53 +02:00
fi
2020-04-29 21:33:26 +02:00
2020-04-30 11:09:01 +02:00
info "Preparing mount paths..."
2020-04-30 11:55:33 +02:00
boot_mount_path="$working_folder""boot/"
root_mount_path="$working_folder""root/"
2020-04-29 18:21:18 +02:00
mkdir -v "$boot_mount_path"
mkdir -v "$root_mount_path"
2020-04-30 11:09:01 +02:00
info "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(){
2020-04-30 11:09:01 +02:00
info "Mount boot and root partition..."
2020-04-29 22:26:43 +02:00
mount "$boot_partition_path" "$boot_mount_path" || error "Mount from $boot_partition_path to $boot_mount_path failed..."
mount "$root_partition_path" "$root_mount_path" || error "Mount from $root_partition_path to $root_mount_path failed..."
2020-04-30 11:09:01 +02:00
info "The following mounts refering this setup exist:" && mount | grep "$working_folder"
2020-04-29 18:21:18 +02:00
}
2020-04-30 11:15:41 +02:00
question "Should the image be transfered to $sd_card_path?(y/n)" && read -r transfer_image
2020-04-30 09:29:57 +02:00
if [ "$transfer_image" = "y" ]
then
2020-04-30 09:49:55 +02:00
2020-04-30 11:15:41 +02:00
question "Should $sd_card_path be overwritten with zeros before copying?(y/n)" && read -r copy_zeros_to_device
2020-04-30 09:49:55 +02:00
if [ "$copy_zeros_to_device" = "y" ]
then
2020-04-30 11:09:01 +02:00
info "Overwritting..."
2020-04-30 09:49:55 +02:00
dd if=/dev/zero of="$sd_card_path" bs=1M || error "Overwritting $sd_card_path failed."
else
2020-04-30 11:09:01 +02:00
info "Skipping Overwritting..."
2020-04-30 09:49:55 +02:00
fi
2020-04-30 11:09:01 +02:00
info "Starting image transfer..."
2020-04-30 09:29:57 +02:00
case "$os" in
"arch")
2020-04-30 11:09:01 +02:00
info "Execute fdisk..."
2020-04-30 09:29:57 +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-30 09:49:55 +02:00
)| fdisk "$sd_card_path" || error "Creating partitions failed. Try to execute this script with the overwritting parameter."
2020-04-30 09:29:57 +02:00
2020-04-30 11:09:01 +02:00
info "Format boot partition..."
2020-04-30 09:29:57 +02:00
mkfs.vfat "$boot_partition_path" || error "Format boot is not possible."
2020-04-30 11:09:01 +02:00
info "Format root partition..."
2020-04-30 09:29:57 +02:00
mkfs.ext4 "$root_partition_path" || error "Format root is not possible."
mount_partitions;
2020-04-30 11:09:01 +02:00
info "Root files will be transfered to device..."
2020-04-30 09:29:57 +02:00
bsdtar -xpf "$image_path" -C "$root_mount_path"
sync
2020-04-30 11:09:01 +02:00
info "Boot files will be transfered to device..."
2020-04-30 09:29:57 +02:00
mv -v "$root_mount_path/boot/"* "$boot_mount_path"
2020-04-29 20:20:26 +02:00
2020-04-30 09:29:57 +02:00
;;
"moode")
2020-04-30 10:15:26 +02:00
unzip -p "$image_path" | sudo dd of="$sd_card_path" bs=1M conv=fsync || error "DD $image_path to $sd_card_path failed."
2020-04-30 09:29:57 +02:00
sync
;;
"retropie")
gunzip -c "$image_path" | sudo dd of="$sd_card_path" bs=1M conv=fsync
sync
;;
*)
error "Image transfer for operation system \"$os\" is not supported yet!";
;;
esac
else
2020-04-30 11:09:01 +02:00
info "Skipping image transfer..."
2020-04-30 09:29:57 +02:00
fi
2020-04-29 20:20:26 +02:00
2020-04-30 11:09:01 +02:00
info "Start regular mounting procedure..."
2020-04-30 10:52:30 +02:00
if mount | grep -q "$boot_mount_path" && mount | grep -q "$root_mount_path"
2020-04-30 09:29:57 +02:00
then
2020-04-30 11:09:01 +02:00
info "Everything allready mounted. Skipping..."
2020-04-30 09:29:57 +02:00
else
mount_partitions
fi
2020-04-29 18:21:18 +02:00
2020-04-30 11:09:01 +02:00
info "Define target paths..."
2020-04-30 15:32:52 +02:00
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/";
2020-04-30 11:09:01 +02:00
info "Copy ssh key to target..."
2020-04-29 18:21:18 +02:00
target_user_ssh_folder_path="$target_user_home_folder_path"".ssh/"
2020-04-29 23:15:06 +02:00
target_authorized_keys="$target_user_ssh_folder_path""authorized_keys"
2020-04-30 15:32:52 +02:00
origin_user_rsa_pub="$origin_user_home"".ssh/id_rsa.pub";
2020-04-29 18:21:18 +02:00
if [ -f "$origin_user_rsa_pub" ]
then
mkdir -v "$target_user_ssh_folder_path"
cat "$origin_user_rsa_pub" > "$target_authorized_keys"
2020-04-29 20:20:26 +02:00
target_authorized_keys_content=$(cat "$target_authorized_keys")
2020-04-30 11:09:01 +02:00
info "$target_authorized_keys contains the following: $target_authorized_keys_content"
2020-04-29 20:20:26 +02:00
chown -vR 1000 "$target_user_ssh_folder_path"
2020-04-29 23:33:37 +02:00
chmod -v 700 "$target_user_ssh_folder_path"
chmod -v 600 "$target_authorized_keys"
2020-04-29 18:21:18 +02:00
else
2020-04-30 11:09:01 +02:00
warning "The ssh key \"$origin_user_rsa_pub\" can't be copied to \"$target_authorized_keys\" because it doesn't exist."
2020-04-29 18:21:18 +02:00
fi
2020-04-30 11:55:33 +02:00
info "Start chroot procedures..."
info "Mount chroot environments..."
2020-04-30 15:32:52 +02:00
chroot_sys_mount_path="$root_mount_path""sys/"
chroot_proc_mount_path="$root_mount_path""proc/"
2020-04-30 16:50:51 +02:00
chroot_dev_mount_path="$root_mount_path""dev/"
chroot_dev_pts_mount_path="$root_mount_path""dev/pts"
mount --bind "$boot_mount_path" "$root_mount_path""/boot" || error "Mounting $chroot_dev_mount_path failed."
mount --bind /dev "$chroot_dev_mount_path" || error "Mounting $chroot_dev_mount_path failed."
mount --bind /sys "$chroot_sys_mount_path" || error "Mounting $chroot_sys_mount_path failed."
mount --bind /proc "$chroot_proc_mount_path" || error "Mounting $chroot_proc_mount_path failed."
mount --bind /dev/pts "$chroot_dev_pts_mount_path" || error "Mounting $chroot_dev_pts_mount_path failed."
sed -i 's/^/#CHROOT /g' /mnt/raspbian/etc/ld.so.preload
cp -v /usr/bin/qemu-arm-static "$root_mount_path""/usr/bin/" || error "Copy qemu-arm-static failed. The following packages are neccessary: qemu qemu-user-static binfmt-support."
info "Changing passwords on target system..."
question "Type in new password: " && read -r password_1
question "Repeat new password\"$target_username\"" && read -r password_2
if [ "$password_1" == "$password_2" ]
then
(
echo "(
echo '$password_1'
echo '$password_1'
) | passwd $target_username"
echo "(
echo '$password_1'
echo '$password_1'
) | passwd"
) | chroot "$root_mount_path" /bin/bash || error "Password change failed."
else
error "Passwords didn't match."
fi
2020-04-29 18:56:07 +02:00
2020-04-30 16:50:51 +02:00
question "Do you want to copy all Wifi passwords to the device?(y/n)" && read -r copy_wifi
2020-04-29 18:56:07 +02:00
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-30 15:32:52 +02:00
info "The first level folder structure on $root_mount_path is:" && tree -laL 1 "$root_mount_path"
info "The first level folder structure on $boot_mount_path is:" && tree -laL 1 "$boot_mount_path"
2020-04-29 21:33:26 +02:00
2020-04-29 23:15:06 +02:00
destructor
2020-04-30 11:36:42 +02:00
success "Setup successfull :)" && exit 0