raspiblitz/home.admin/config.scripts/blitz.datadrive.sh

144 lines
4.4 KiB
Bash
Raw Normal View History

2019-02-25 00:31:02 +01:00
#!/bin/bash
2019-11-27 12:29:25 +01:00
# INFO deleting all partitions of a drive: sudo wipefs -a -f /dev/sda
# using fdisk non-interactive: https://www.unix.com/shell-programming-and-scripting/207169-non-interactive-fdisk-partition-script.html
# using parted non-interactive: https://unix.stackexchange.com/questions/170258/delete-labelled-partition-non-interactively-with-one-command
2019-02-25 00:31:02 +01:00
# command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
2019-04-28 14:40:23 +02:00
echo "STILL EXPERIMENTAL - NOT FINISHED"
2019-02-25 00:31:02 +01:00
echo "managing additional data storage"
echo "blitz.datadrive.sh [on|off]"
echo "exits on 0 = needs reboot"
exit 1
fi
# check if sudo
if [ "$EUID" -ne 0 ]
then echo "Please run as root (with sudo)"
2019-02-25 04:46:32 +01:00
exit 1
2019-02-25 00:31:02 +01:00
fi
# update install sources
2019-02-25 04:46:32 +01:00
echo "make sure BTRFS is installed ..."
2019-02-25 00:31:02 +01:00
sudo apt-get install -y btrfs-tools
2019-02-25 04:46:32 +01:00
echo ""
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
# check on/off state
dataStorageNotAvailableYet=$(sudo btrfs filesystem df /mnt/data 2>&1 | grep -c "ERROR: not a btrfs filesystem")
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "Trying to switch additional data storage on ..."
if [ ${dataStorageNotAvailableYet} -eq 0 ]; then
echo "FAIL -> data storage is already on"
exit 1
fi
elif [ "$1" = "0" ] || [ "$1" = "off" ]; then
echo "Trying to switch additional data storage off ..."
if [ ${dataStorageNotAvailableYet} -eq 1 ]; then
echo "FAIL -> data storage is already off"
exit 1
fi
else
echo "FAIL -> Parameter '${$1}' not known."
exit 1
fi
###################
# SWITCH ON
###################
if [ "$1" = "1" ] || [ "$1" = "on" ]; then
# detect the two usb drives
echo "Detecting two USB sticks/drives with same size ..."
lsblk -o NAME | grep "^sd" | while read -r test1 ; do
2019-02-25 02:17:27 +01:00
size1=$(lsblk -o NAME,SIZE -b | grep "^${test1}" | awk '$1=$1' | cut -d " " -f 2)
2019-02-25 02:15:42 +01:00
echo "Checking : ${test1} size(${size1})"
lsblk -o NAME | grep "^sd" | grep -v "${test1}" | while read -r test2 ; do
2019-02-25 02:17:27 +01:00
size2=$(lsblk -o NAME,SIZE -b | grep "^${test2}" | awk '$1=$1' | cut -d " " -f 2)
2019-02-25 02:10:42 +01:00
if [ "${size1}" = "${size2}" ]; then
2019-02-25 02:24:25 +01:00
echo " MATCHING ${test2} size(${size2})"
2019-02-25 02:29:57 +01:00
echo "${test1}" > .dev1.tmp
echo "${test2}" > .dev2.tmp
2019-02-25 02:24:25 +01:00
else
echo " different ${test2} size(${size2})"
2019-02-25 02:10:42 +01:00
fi
done
2019-02-25 05:33:52 +01:00
done
dev1=$(cat .dev1.tmp)
dev2=$(cat .dev2.tmp)
rm -f .dev1.tmp
rm -f .dev2.tmp
echo "RESULTS:"
echo "dev1(${dev1})"
echo "dev2(${dev2})"
echo ""
2019-02-25 04:46:32 +01:00
2019-02-25 05:33:52 +01:00
# check that results are available
if [ ${#dev1} -eq 0 ] || [ ${#dev2} -eq 0 ]; then
echo "!! FAIL -> was not able to detect two devices with the same size"
exit 1
fi
2019-02-25 04:46:32 +01:00
2019-02-25 05:33:52 +01:00
# check size (at least 4GB minus some tolerance)
size=$(lsblk -o NAME,SIZE -b | grep "^${dev1}" | awk '$1=$1' | cut -d " " -f 2)
if [ ${size} -lt 3500000000 ]; then
echo "!! FAIL -> too small - additional storage needs to be bigger than 4GB"
exit 1
fi
2019-02-25 04:46:32 +01:00
2019-02-25 05:33:52 +01:00
# check if devices are containing old data
echo "Analysing Drives ..."
nameDev1=$(lsblk -o NAME,LABEL | grep "^${dev1}" | awk '$1=$1' | cut -d " " -f 2)
nameDev2=$(lsblk -o NAME,LABEL | grep "^${dev2}" | awk '$1=$1' | cut -d " " -f 2)
if [ "${nameDev1}" = "DATASTORE" ] || [ "${nameDev2}" = "DATASTORE" ]; then
# TODO: once implemented -> also make sure that dev1 is named "DATASTORE" and if 2nd is other -> format and add as raid
echo "!! NOT IMPLEMENTED YET -> devices seem contain old data, because name is 'DATASTORE'"
echo "if you dont care about that data: format devices devices on other computer with FAT(32) named TEST"
exit 1
fi
echo "OK drives dont contain old data."
echo ""
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
# format first drive
echo "Formatting /dev/${dev1} with BTRFS ..."
sudo mkfs.btrfs -L DATASTORE -f /dev/${dev1}
echo "OK"
echo ""
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
# mount the BTRFS drive
echo "Mounting under /mnt/data ..."
sudo mkdir -p /mnt/data
2019-05-19 02:25:06 +02:00
sudo mount barrier=1 /dev/${dev1} /mnt/data
2019-02-25 05:33:52 +01:00
echo "OK"
echo ""
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
# adding the second device
echo "Adding the second device as RAID1 .."
2019-02-25 05:40:13 +01:00
sudo btrfs device add -f /dev/${dev2} /mnt/data
2019-02-25 05:33:52 +01:00
sudo btrfs filesystem balance start -dconvert=raid1 -mconvert=raid1 /mnt/data
echo ""
exit 0
2019-02-25 00:31:02 +01:00
2019-02-28 14:56:53 +01:00
# adding the second device
uuid=$(sudo btrfs filesystem show /mnt/data | grep "uuid:" | awk '$1=$1' | cut -d " " -f 4)
2019-02-25 05:33:52 +01:00
fi
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
###################
# SWITCH OFF
###################
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
if [ "$1" = "0" ] || [ "$1" = "off" ]; then
2019-02-25 00:31:02 +01:00
2019-02-25 05:33:52 +01:00
echo "TODO -> Turn off"
sudo btrfs filesystem show /mnt/data
sudo btrfs filesystem df /mnt/data
2019-02-25 05:40:13 +01:00
2019-02-25 05:33:52 +01:00
sudo umount /mnt/data
exit 0
fi