Optimized code structure

This commit is contained in:
Kevin Veen-Birkenbach 2020-04-29 23:15:06 +02:00
parent 555d9ecfe5
commit ae60d9ae75
2 changed files with 26 additions and 12 deletions

View File

@ -16,6 +16,9 @@ To pase the configuration to the program use this syntax:
echo "mmcblk1" #The device echo "mmcblk1" #The device
echo "3" #The raspberry pi number echo "3" #The raspberry pi number
echo "arch" #The operation system echo "arch" #The operation system
echo "user_password" #The user password
echo "root_password" #The root password
echo "y" #Copy wifi configurations to target system
)| sudo bash ./sd_setup.sh )| sudo bash ./sd_setup.sh
``` ```
## Backup ## Backup

View File

@ -13,8 +13,21 @@ echo "Define variables..."
working_folder="/tmp/raspberry-pi-tools-$(date +%s)/"; working_folder="/tmp/raspberry-pi-tools-$(date +%s)/";
echo "Define functions..." echo "Define functions..."
warning(){
echo "WARNING: $1";
}
destructor(){
echo "Cleaning up..."
umount -v "$root_mount_path" "$boot_mount_path" || warning "Umounting $root_mount_path and/or $boot_mount_path failed!"
rm -vr "$working_folder" || warning "Removing $working_folder failed!"
}
error(){ error(){
echo "ERROR: $1. Leaving program." && exit 1; echo "ERROR: $1 Leaving program."
destructor
exit 1;
} }
echo "Create temporary working folder in $working_folder"; echo "Create temporary working folder in $working_folder";
@ -137,7 +150,7 @@ if [[ -v image_checksum ]]
then then
echo "$image_checksum $image_path"| md5sum -c -|| error "Verification failed." echo "$image_checksum $image_path"| md5sum -c -|| error "Verification failed."
else else
echo "WARNING: Verification is not possible. No checksum is define." warning "Verification is not possible. No checksum is define."
fi fi
echo "Preparing mount paths..." echo "Preparing mount paths..."
@ -225,7 +238,7 @@ target_user_home_folder_path="$target_home_path$target_username/";
echo "Copy ssh key to target..." echo "Copy ssh key to target..."
target_user_ssh_folder_path="$target_user_home_folder_path"".ssh/" target_user_ssh_folder_path="$target_user_home_folder_path"".ssh/"
target_authorized_keys="$target_user_ssh_folder_path/authorized_keys" target_authorized_keys="$target_user_ssh_folder_path""authorized_keys"
origin_user_rsa_pub="$origin_user_home/.ssh/id_rsa.pub"; origin_user_rsa_pub="$origin_user_home/.ssh/id_rsa.pub";
if [ -f "$origin_user_rsa_pub" ] if [ -f "$origin_user_rsa_pub" ]
then then
@ -240,10 +253,10 @@ if [ -f "$origin_user_rsa_pub" ]
fi fi
echo "Change password of user \"$target_username\"..." echo "Change password of user \"$target_username\"..."
chroot "$root_mount_path" /bin/passwd "$target_username" (chroot "$root_mount_path" /bin/passwd "$target_username") || error "Password change for \"$target_username\" wasn't possible."
echo "Change password of root user..." echo "Change password of root user..."
chroot "$root_mount_path" /bin/passwd root (chroot "$root_mount_path" /bin/passwd root) || error "Password change for \"root\" wasn't possible."
echo "Do you want to copy all Wifi passwords to the sd-card?(y/n)" echo "Do you want to copy all Wifi passwords to the sd-card?(y/n)"
read -r copy_wifi read -r copy_wifi
@ -256,7 +269,5 @@ fi
echo "The first level folder structure on $root_mount_path is now:" && tree -laL 1 "$root_mount_path" echo "The first level folder structure on $root_mount_path is now:" && tree -laL 1 "$root_mount_path"
echo "The first level folder structure on $boot_mount_path is now:" && tree -laL 1 "$boot_mount_path" echo "The first level folder structure on $boot_mount_path is now:" && tree -laL 1 "$boot_mount_path"
echo "Cleaning up..." destructor
umount -v "$root_mount_path" "$boot_mount_path" || error "Umounting $root_mount_path and/or $boot_mount_path failed!."
rm -vr "$working_folder"
echo "Setup successfull :)" && exit 0 echo "Setup successfull :)" && exit 0