mirror of
https://github.com/kevinveenbirkenbach/raspberry-pi-tools.git
synced 2024-11-22 18:11:03 +01:00
Optimized code structure
This commit is contained in:
parent
e365b75fc2
commit
0eab534771
151
sd_setup.sh
151
sd_setup.sh
@ -2,58 +2,71 @@
|
|||||||
# @todo execute as non
|
# @todo execute as non
|
||||||
|
|
||||||
# Define constants & variables
|
# Define constants & variables
|
||||||
os_username="pi" #Default username
|
|
||||||
working_folder="/tmp/raspberry-pi-tools-$(date +%s)"; #Working folder
|
working_folder="/tmp/raspberry-pi-tools-$(date +%s)"; #Working folder
|
||||||
|
|
||||||
# Define functions
|
|
||||||
os_does_not_support_raspberry_version_error () {
|
|
||||||
echo "$1 for Raspberry Pi Version $2 is not supported!" && exit 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Setupscript for Raspberry Pi SD's"
|
echo "Setupscript for Raspberry Pi SD's"
|
||||||
echo
|
echo
|
||||||
echo "@author Kevin Veen-Birkenbach [kevin@veen.world]"
|
echo "@author Kevin Veen-Birkenbach [kevin@veen.world]"
|
||||||
echo "@since 2017-03-12"
|
echo "@since 2017-03-12"
|
||||||
echo
|
echo
|
||||||
echo
|
|
||||||
|
echo "Starting setup..."
|
||||||
|
|
||||||
echo "Create temporary working folder in $working_folder";
|
echo "Create temporary working folder in $working_folder";
|
||||||
mkdir -v "$working_folder"
|
mkdir -v "$working_folder"
|
||||||
|
|
||||||
|
echo "Checking if root..."
|
||||||
if [ "$(id -u)" != "0" ];then
|
if [ "$(id -u)" != "0" ];then
|
||||||
echo "This script must be executed as root!" && exit 1
|
echo "This script must be executed as root!" && exit 1
|
||||||
fi
|
fi
|
||||||
echo "Please type in a valid username from which the SSH-Key should be copied:"
|
|
||||||
read -r username;
|
|
||||||
image_folder="/home/$username/Images/";
|
|
||||||
ssh_key_source="/home/$username/.ssh/id_rsa.pub"
|
|
||||||
|
|
||||||
|
echo "Configure user..."
|
||||||
|
echo "Please type in a valid username from which the SSH-Key should be copied:"
|
||||||
|
read -r origin_username;
|
||||||
|
getent passwd $origin_username > /dev/null 2 && echo "User $origin_username doesn't exist. Abord program." && exit 1;
|
||||||
|
origin_user_home="/home/$username/";
|
||||||
|
|
||||||
|
# Create image folders
|
||||||
|
image_folder="$origin_user_home/Images/";
|
||||||
echo "The images will be stored in \"$image_folder\"."
|
echo "The images will be stored in \"$image_folder\"."
|
||||||
if [ ! -d "$DIR" ]; then
|
if [ ! -d "$DIR" ]; then
|
||||||
echo "Folder \"$image_folder\" doesn't exist. It will be created now."
|
echo "Folder \"$image_folder\" doesn't exist. It will be created now."
|
||||||
mkdir -v "$image_folder"
|
mkdir -v "$image_folder"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Select sd-card..."
|
||||||
echo "List of actual mounted devices:"
|
echo "List of actual mounted devices:"
|
||||||
echo
|
|
||||||
ls -lasi /dev/ | grep -E "sd|mm"
|
ls -lasi /dev/ | grep -E "sd|mm"
|
||||||
echo
|
echo
|
||||||
while [ \! -b "$of_device" ]
|
while [ \! -b "$sd_card_path" ]
|
||||||
do
|
do
|
||||||
echo "Please select the correct SD-Card."
|
echo "Please type in the name of the correct sd-card."
|
||||||
echo "/dev/:"
|
echo "/dev/:"
|
||||||
read -r device
|
read -r device
|
||||||
of_device="/dev/$device"
|
sd_card_path="/dev/$device"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Select Raspberry Pi
|
||||||
echo "Which Raspberry Pi version do you want to use?"
|
echo "Which Raspberry Pi version do you want to use?"
|
||||||
read -r version
|
read -r version
|
||||||
|
|
||||||
|
# Select Image
|
||||||
|
|
||||||
|
os_does_not_support_raspberry_version_error () {
|
||||||
|
echo "$1 for Raspberry Pi Version $2 is not supported!" && exit 1;
|
||||||
|
}
|
||||||
|
|
||||||
echo "Image for Raspberry Pi $version will be used..."
|
echo "Image for Raspberry Pi $version will be used..."
|
||||||
|
echo
|
||||||
echo "Which OS do you want to use?"
|
echo "Which OS do you want to use?"
|
||||||
echo "1) arch"
|
echo "1) arch"
|
||||||
echo "2) moode"
|
echo "2) moode"
|
||||||
echo "3) retropie"
|
echo "3) retropie"
|
||||||
|
echo
|
||||||
echo "Please type in the os:"
|
echo "Please type in the os:"
|
||||||
read -r os
|
read -r os
|
||||||
case "$os" in
|
case "$os" in
|
||||||
"arch")
|
"arch")
|
||||||
os_username="arch"
|
|
||||||
base_download_url="http://os.archlinuxarm.org/os/";
|
base_download_url="http://os.archlinuxarm.org/os/";
|
||||||
case "$version" in
|
case "$version" in
|
||||||
"1")
|
"1")
|
||||||
@ -97,10 +110,11 @@ case "$os" in
|
|||||||
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
# Download Image
|
||||||
download_url="$base_download_url$imagename"
|
download_url="$base_download_url$imagename"
|
||||||
image_path="$image_folder$image_path"
|
image_path="$image_folder$image_path"
|
||||||
root_mount_path="$working_folder""root"
|
|
||||||
ssh_key_target="$root_mount_path/home/$os_username/.ssh/authorized_keys"
|
|
||||||
if [ \! -f "$image_path" ]
|
if [ \! -f "$image_path" ]
|
||||||
then
|
then
|
||||||
echo "The selected image \"$imagename\" doesn't exist under local path \"$image_path\"."
|
echo "The selected image \"$imagename\" doesn't exist under local path \"$image_path\"."
|
||||||
@ -110,17 +124,32 @@ if [ \! -f "$image_path" ]
|
|||||||
wget "$download_url"
|
wget "$download_url"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Prepare mount paths
|
||||||
|
boot_mount_path="$working_folder""boot"
|
||||||
|
root_mount_path="$working_folder""root"
|
||||||
|
mkdir -v "$boot_mount_path"
|
||||||
|
mkdir -v "$root_mount_path"
|
||||||
|
|
||||||
|
# Define partition paths
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Copy
|
||||||
case "$os" in
|
case "$os" in
|
||||||
"arch")
|
"arch")
|
||||||
boot_path="$workingpath""boot"
|
|
||||||
if [ "${of_device:5:1}" != "s" ]
|
|
||||||
then
|
|
||||||
partion="p"
|
|
||||||
else
|
|
||||||
partion=""
|
|
||||||
fi
|
|
||||||
ofiboot=$of_device$partion"1"
|
|
||||||
ofiroot=$of_device$partion"2"
|
|
||||||
echo "fdisk wird ausgefuehrt..."
|
echo "fdisk wird ausgefuehrt..."
|
||||||
( echo "o" #Type o. This will clear out any partitions on the drive.
|
( 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 "p" #Type p to list partitions. There should be no partitions left
|
||||||
@ -137,47 +166,57 @@ case "$os" in
|
|||||||
echo "" #and then press ENTER twice to accept the default first and last sector.
|
echo "" #and then press ENTER twice to accept the default first and last sector.
|
||||||
echo ""
|
echo ""
|
||||||
echo "w" #Write the partition table and exit by typing w.
|
echo "w" #Write the partition table and exit by typing w.
|
||||||
)| fdisk "$of_device"
|
)| fdisk "$sd_card_path"
|
||||||
|
|
||||||
#Bootpartion formatieren und mounten
|
echo "Format boot partition..."
|
||||||
echo "Generate and mount boot-partition..."
|
mkfs.vfat "$boot_partition_path"
|
||||||
mkfs.vfat "$ofiboot"
|
|
||||||
mkdir -v "$boot_path"
|
|
||||||
mount "$ofiboot" "$boot_path"
|
|
||||||
|
|
||||||
#Rootpartition formatieren und mounten
|
echo "Format root partition..."
|
||||||
echo "Generate and mount root-partition..."
|
mkfs.ext4 "$root_partition_path"
|
||||||
mkfs.ext4 "$ofiroot"
|
|
||||||
mkdir -v "$root_path"
|
|
||||||
mount "$ofiroot" "$root_path"
|
|
||||||
|
|
||||||
echo "Die Root-Dateien werden auf die SD-Karte aufgespielt..."
|
mount_partitions;
|
||||||
bsdtar -xpf "$image_path" -C "$root_path"
|
|
||||||
|
echo "Root files will be transfered to sd-card..."
|
||||||
|
bsdtar -xpf "$image_path" -C "$root_mount_path"
|
||||||
sync
|
sync
|
||||||
|
|
||||||
echo "Die Boot-Dateien werden auf die SD-Karte aufgespielt..."
|
echo "Boot files will be transfered to sd-card..."
|
||||||
mv -v "$root_path/boot/"* "$boot_path"
|
mv -v "$root_mount_path/boot/"* "$boot_mount_path"
|
||||||
|
|
||||||
if [ "$username" != "" ] && [ -f "$ssh_key_source" ]
|
|
||||||
then
|
|
||||||
echo "SSH key will be copied to Raspberry Pi.."
|
|
||||||
mkdir -v "$root_path/home/$os_username/.ssh"
|
|
||||||
cat "$ssh_key_source" > "$ssh_key_target"
|
|
||||||
chown -R 1000 "$root_path/home/$os_username/.ssh"
|
|
||||||
chmod -R 400 "$root_path/home/$os_username/.ssh"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Unmount partitions..."
|
|
||||||
umount -v "$root_path" "$boot_path"
|
|
||||||
;;
|
;;
|
||||||
"moode")
|
"moode")
|
||||||
unzip -p "$image_path" | sudo dd of="$of_device" bs=4M conv=fsync
|
unzip -p "$image_path" | sudo dd of="$sd_card_path" bs=4M conv=fsync
|
||||||
|
mount_partitions;
|
||||||
;;
|
;;
|
||||||
"retropie")
|
"retropie")
|
||||||
gunzip -c "$image_path" | sudo dd of="$of_device" bs=4M conv=fsync
|
gunzip -c "$image_path" | sudo dd of="$sd_card_path" bs=4M conv=fsync
|
||||||
|
mount_partitions;
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
echo "The operation system \"$os\" is not supported yet!" && exit 1;
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
rm -r "$working_folder"
|
|
||||||
|
echo "Define target paths..."
|
||||||
|
target_home_path="$root_mount_path/home/";
|
||||||
|
target_username=$(ls $target_home_path);
|
||||||
|
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
|
||||||
|
|
||||||
|
echo "Cleaning up..."
|
||||||
|
umount -v "$root_mount_path" "$boot_mount_path"
|
||||||
|
rm -vr "$working_folder"
|
||||||
|
echo "Setup successfull :)" && exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user