setup/clone/clone

399 lines
9.4 KiB
Bash
Executable File

#!/bin/sh
echo ">>> Loading configuration file"
sh ./clone.conf || $(echo "<<< error"; exit);
echo "<<< done";
[ ! -e ./tmp -o ! -d ./tmp ] && mkdir ./tmp;
[ ! -e ./server -o ! -d ./server ] && mkdir ./server;
[ ! -e ./server/maintenance -o ! -d ./server/maintenance ] && mkdir ./server/maintenance;
# [!] Check parameter : device file
#========================================================#
echo ">>> [!] Checking parameter : device";
# (1) Check parameter existence #
test $# -lt 1 && echo "Missing parameter : device" && exit;
# (2) Check USB and not a hard drive !!!!!!!!!! #
device_type=$(udevadm info --query=all -n $1 | grep -E "ID_BUS" | awk '{print $2}' | sed 's/ID_BUS=//');
test $device_type != "usb" && echo ">>> ERROR: device type is $device_type, \"usb\" expected." && exit;
echo "<<< done";
DEV="$1";
# [1] Init device layout (gpt table)
#========================================================#
step1(){
echo "\n>>> [1] Checking for mounted partitions ($DEV)";
# (1) List partitions of this device #
mounted_partitions=$( cat /proc/mounts | awk '{print $1}' | grep "$DEV" );
# if nothing found -> next step
test -z "$mounted_partitions" && echo "<<< done" && step2;
for mounted in $mounted_partitions; do
read -p " (!) umount $mounted (y/n) [n]" unmount;
test -n "$unmount" && test $unmount = "y" && sudo umount $mounted 2> /dev/null > /dev/null && echo " > unmounted";
done;
echo "<<< done";
step2;
}
# [2] Initialize GTP Table
#========================================================#
step2(){
echo "\n>>> [2] Formatting disk ($DEV)";
# (1) Confirmation #
read -p" (!) Erase the whole disk ? it is irreversible! (y/n) [n]" confirm_format;
test -z "$confirm_format" && echo "<<< aborting" && exit;
test $confirm_format != "y" && echo "<<< aborting" && exit;
# (2) Init gpt entry #
echo -e "g\nw" | sudo fdisk $DEV 2> /dev/null > /dev/null;
echo "<<< done";
step3;
}
# [3] Burn image to device
#========================================================#
step3(){
echo "\n>>> [3] Burning image into disk ($DEV)";
# (1) Confirmation #
read -p" (!) Burn the whole disk ? it is irreversible! (y/n) [n]" confirm_burn;
test -z "$confirm_burn" && echo "<<< aborting" && exit;
test $confirm_burn != "y" && echo "<<< aborting" && exit;
# (2) Burning image into disk #
( dd if=./original.img | pv -s $(du -bs ./original.img | awk '{print $1}') | sudo dd of=$DEV bs=4M ) \
|| $( echo "<<< error: dd command failed" && exit );
echo "<<< done";
step4;
}
# [4] Mount partition
#========================================================#
step4(){
echo "\n>>> [4] Mounting partition ${DEV}2";
# [1] Mount device partition
sudo mount ${DEV}2 /mnt || $( echo "<<< error: can't mount" && exit );
echo "<<< done";
step5;
}
# [5] Updating users
#========================================================#
step5(){
echo "\n>>> [5] Updating users";
# (1) Removing pi entry #
echo " (.) Remove pi user entry"
echo " > /etc/shadow";
sudo cat /mnt/etc/shadow | grep -vE "^pi:" | tee ./tmp/shadow > /dev/null;
# (2) Adding 'sats' user and new pi entry #
echo " (.) Adding 'sats' user and updated 'pi'";
cat ./utility/shadow_append | tee -a ./tmp/shadow > /dev/null;
# (3) Setting sats random password #
echo " (.) Set sats random password"
RANDOM_PASS=$(tr -dc A-Za-z0-9_ < /dev/urandom | head -c 150);
RANDOM_SHADOW="$(python3 -c "import crypt; print(crypt.crypt(\"${RANDOM_PASS}\", crypt.mksalt(crypt.METHOD_SHA512)))")";
RANDOM_SHADOW="$(echo $RANDOM_SHADOW | sed 's/\//\\\//g')"
cat ./tmp/shadow | sed "s/sats-passowrd/$RANDOM_SHADOW/" | tee ./tmp/shadow > /dev/null;
# (4) Update real files #
echo " (.) Update real files";
echo " > /etc/shadow";
cat ./tmp/shadow | sudo tee /mnt/etc/shadow | sudo tee /mnt/etc/shadow- > /dev/null;
rm ./tmp/shadow;
echo " > /etc/passwd";
cat ./utility/passwd_append | sudo tee -a /mnt/etc/passwd > /dev/null;
sudo cat /mnt/etc/passwd | sudo tee /mnt/etc/passwd- > /dev/null;
echo " (.) Copying pi's .bashrc to sats";
sudo mkdir -p /mnt/home/sats;
sudo cp /mnt/home/pi/.bashrc /mnt/home/sats/.bashrc;
sudo chown 666:666 /mnt/home/sats/.bashrc;
sudo chmod 544 /mnt/home/sats/.bashrc;
echo "<<< done";
read -p "next" next;
step6;
}
# [6] Updating groups
#========================================================#
step6(){
echo "\n>>> [6] Updating groups (ssh-key, sats)";
cat ./utility/group_append | sudo tee -a /mnt/etc/group > /dev/null;
cat ./utility/group_append | sudo tee -a /mnt/etc/group- > /dev/null;
echo "<<< done":
step7;
}
# [7] Manage SSH keys #
#========================================================#
step7(){
echo "\n>>> [7] Manage ssh keys";
# (1) Create ssh key pair #
echo " (.) Create ssh key [ecdsa:521]";
ssh-keygen -t ecdsa -b 521 -C "[ECDSA:521] SATS" -f ./tmp/id_ecdsa -P "" 2> /dev/null > /dev/null;
# (2) Add public key to server's `authorized_keys` file #
echo " (.) Add public key to server's list";
test ! -e ./server/authorized_keys && touch ./server/authorized_keys;
cat ./tmp/id_ecdsa.pub >> ./server/authorized_keys;
# (3) Create ssh file system #
echo " (.) Init ssh folder (/home/sats/.ssh)";
sudo mkdir -p /mnt/home/sats/.ssh;
# (4) Add both keys to sats files #
echo " (.) Add keys to ssh folder";
sudo mv ./tmp/id_ecdsa /mnt/home/sats/.ssh/id_ecdsa;
sudo mv ./tmp/id_ecdsa.pub /mnt/home/sats/.ssh/id_ecdsa.pub;
# (5) Add maintenance keys #
echo " (.) Add maintenance keys'";
sudo touch /mnt/home/sats/.ssh/authorized_keys;
cat ./server/maintenance/*.pub | sudo tee /mnt/home/sats/.ssh/authorized_keys > /dev/null;
# (6) Set up permissions #
echo " (.) Set up permissions";
sudo chown -R 666:666 /mnt/home/sats/;
sudo chmod 400 /mnt/home/sats/.ssh/*;
# (7) Restrict access to pubkey only (no password) #
echo " (.) Restrict access to pubkey";
cat ./utility/sshd_config_append | sudo tee -a /mnt/etc/ssh/sshd_config > /dev/null;
# (8) Set up automatic ssh server at start up #
echo " (.) Emulate \`systemctl enable ssh.service\`";
#sudo ln -fs /lib/systemd/system/ssh.service /mnt/etc/systemd/system/ssh.service;
# (9) Define config alias with ssh-key #
echo " (.) Define alias config";
echo "Host\tsmmp-server\n\tHostname\t$SERVER_HOSTNAME\n\tUser\t$SERVER_USERNAME\n\tIdentityFile\t~/.ssh/id_ecdsa\n" | sudo tee /mnt/home/sats/.ssh/config > /dev/null;
sudo chown 666:666 /mnt/home/sats/.ssh/config;
sudo chmod 444 /mnt/home/sats/.ssh/config;
echo "<<< done";
step8;
}
# [8] Set up sats automatic login
#========================================================#
step8(){
echo "\n>>> [8] Set up automatic login";
# (1) Create link in order to be handled #
echo " (.) Emulate \`systemctl set-default multi-user.target\`";
#sudo ln -fs /lib/systemd/system/multi-user.target /mnt/etc/systemd/system/default.target;
# (2) Link autologin to tty service #
echo " (.) Link autologin script to tty's default";
#sudo mkdir -pv /mnt/etc/systemd/system/getty@tty1.service.d;
#sudo touch /mnt/etc/systemd/system/getty@tty1.service.d/autologin.conf;
#cat ./utility/autologin_overload.conf | sudo tee /mnt/etc/systemd/system/getty@tty1.service.d/autologin.conf > /dev/null;
echo "<<< done";
step9;
}
# [9] Manage Network config
#========================================================#
step9(){
echo "\n>>> [9] Set up WiFi configuration";
echo "network={\n\tssid=\"$WIFI_SSID"\n\tpsk=\"$WIFI_PASS"\n}" | sudo tee -a /mnt/etc/wpa_supplicant/wpa_supplicant.conf > /dev/null;
echo "<<< done";
step10;
}
# [10] Set up SATS daemon
#========================================================#
step10(){
echo "\n>>> [10] Set up SATS operating folder";
# (1) Create operating folder #
echo " (.) Create operating folder";
sudo mkdir -p /mnt/home/sats/satsd/source;
sudo mkdir -p /mnt/home/sats/satsd/log;
sudo mkdir -p /mnt/home/sats/satsd/conf;
sudo touch /mnt/home/sats/satsd/first_install;
# (2) Set up SATS botting script #
echo " (.) Set up SATS booting script";
cat ./utility/booting_script | sudo tee /mnt/home/sats/satsd/boot > /dev/null;
# (3) Set up service unit #
echo " (.) Set up service unit (sats.service)";
cat ./utility/service_unit | sudo tee /mnt/lib/systemd/system/sats.service > /dev/null;
# (4) Enable startup service unit #
echo " (.) Emulate \`systemctl enable sats.service\`";
sudo mkdir -p /mnt/etc/systemd/system/multi-user.target.wants;
sudo ln -fs /lib/systemd/system/sats.service /mnt/etc/systemd/system/multi-user.target.wants/sats.service;
# (5) Adjust permissions #
echo " (.) Adjust permissions";
sudo chown -R 666:666 /mnt/home/sats/satsd;
sudo chmod -R 770 /mnt/home/sats/satsd;
echo "<<< done";
step11;
}
# [11] BLablslkfjdlkdsjfks
#========================================================#
step11(){
sudo umount /mnt;
}
# [0] Step choice
#========================================================#
echo "\nSTEPS";
echo "(1) Unmount mounted partitions";
echo "(2) Format disk (gpt table)";
echo "(3) Burn image into disk";
echo "(4) Mount / partition";
echo "(5) Update users";
echo "(6) Update groups";
echo "(7) Manage ssh keys";
echo "(8) Set up automatic sats login";
echo "(9) Set up WiFi config";
echo "(10) Set up SATS daemon";
read -p "step: " step;
case $step in
"1") step1;;
"2") step2;;
"3") step3;;
"4") step4;;
"5") step5;;
"6") step6;;
"7") step7;;
"8") step8;;
"9") step9;;
"10") step10;;
*) echo "wrong step"; exit;;
esac;