2019-02-25 00:31:02 +01:00
#!/bin/bash
if [ $# -eq 0 ] || [ " $1 " = "-h" ] || [ " $1 " = "-help" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# managing the data drive(s) with old EXT4 or new BTRFS"
2021-05-23 10:37:57 -05:00
>& 2 echo "# blitz.datadrive.sh [status|tempmount|unmount|format|fstab|raid|link|swap|clean|snapshot|uasp-fix]"
2019-12-13 16:38:59 +01:00
echo "error='missing parameters'"
2019-02-25 00:31:02 +01:00
exit 1
fi
2019-12-12 12:45:22 +01:00
###################
# BASICS
###################
# TO UNDERSTAND THE BTFS HDD LAYOUT:
####################################
2021-08-27 03:59:21 -04:00
# 1) BLITZDATA - a BTRFS partition for all RaspiBlitz data - 30GB
2019-12-12 12:45:22 +01:00
# here put all files of LND, app, etc that need backup
2021-08-27 03:59:21 -04:00
# 2) BLITZSTORAGE - a BTFRS partition for mostly Blockchain data
2019-12-12 12:45:22 +01:00
# all data here can get lost and rebuild if needed (Blockchain, Indexes, etc)
# 3) BLITZTEMP - a FAT partition just for SWAP & Exchange - 34GB
# used for SWAP file and easy to read from Win32/MacOS for exchange
# this directory should get cleaned on every start (except from swap)
2019-12-10 20:45:34 +01:00
# check if started with sudo
if [ " $EUID " -ne 0 ] ; then
2020-01-15 10:57:10 +01:00
echo "error='missing sudo'"
2019-12-10 20:45:34 +01:00
exit 1
fi
# install BTRFS if needed
btrfsInstalled = $( btrfs --version 2>/dev/null | grep -c "btrfs-progs" )
if [ ${ btrfsInstalled } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Installing BTRFS ..."
2019-12-10 20:45:34 +01:00
sudo apt-get install -y btrfs-tools 1>/dev/null
fi
btrfsInstalled = $( btrfs --version 2>/dev/null | grep -c "btrfs-progs" )
if [ ${ btrfsInstalled } -eq 0 ] ; then
echo "error='missing btrfs package'"
2019-02-25 04:46:32 +01:00
exit 1
2019-02-25 00:31:02 +01:00
fi
2019-12-10 20:45:34 +01:00
###################
# STATUS
###################
# gathering system info
# is global so that also other parts of this script can use this
# basics
isMounted = $( sudo df | grep -c /mnt/hdd)
2020-01-15 18:19:15 +01:00
isBTRFS = $( sudo btrfs filesystem show 2>/dev/null| grep -c 'BLITZSTORAGE' )
2019-12-10 20:45:34 +01:00
isRaid = $( btrfs filesystem df /mnt/hdd 2>/dev/null | grep -c "Data, RAID1" )
2021-05-23 10:53:37 -05:00
isSSD = "0"
2019-12-10 20:45:34 +01:00
# determine if swap is external on or not
externalSwapPath = "/mnt/hdd/swapfile"
if [ ${ isBTRFS } -eq 1 ] ; then
externalSwapPath = "/mnt/temp/swapfile"
fi
isSwapExternal = $( swapon -s | grep -c " ${ externalSwapPath } " )
# output and exit if just status action
if [ " $1 " = "status" ] ; then
2019-12-13 18:21:56 +01:00
# optional second parameter can be 'bitcoin' or 'litecoin'
blockchainType = $2
2019-12-13 16:55:29 +01:00
echo "# RASPIBLITZ DATA DRIVE Status"
2019-12-10 20:45:34 +01:00
echo
2019-12-13 16:55:29 +01:00
echo "# BASICS"
2019-12-10 20:45:34 +01:00
echo " isMounted= ${ isMounted } "
echo " isBTRFS= ${ isBTRFS } "
2021-05-03 23:53:34 +02:00
# if HDD is not mounted system then it is in the pre-setup phase
2019-12-11 01:19:13 +01:00
# deliver all the detailes needed about the data drive
# and it content for the setup dialogs
if [ ${ isMounted } -eq 0 ] ; then
2019-12-12 14:34:47 +01:00
echo
2019-12-13 16:55:29 +01:00
echo "# SETUP INFO"
2019-12-11 01:19:13 +01:00
2020-09-21 21:59:37 +02:00
# find the HDD (biggest single partition)
2021-05-04 14:18:55 +02:00
# will then be used to offer formatting and permanent mounting
2020-09-30 13:55:04 +02:00
hdd = ""
2020-09-21 21:59:37 +02:00
sizeDataPartition = 0
2021-09-29 11:54:01 +02:00
OSPartition = $( sudo df /usr 2>/dev/null | grep dev | cut -d " " -f 1 | sed "s#/dev/##g" )
2021-09-14 10:43:03 +01:00
# detect boot partition on UEFI systems
2021-09-29 11:54:01 +02:00
bootPartition = $( sudo df /boot/efi 2>/dev/null | grep dev | cut -d " " -f 1 | sed "s#/dev/##g" )
2021-09-14 10:43:03 +01:00
if [ ${# bootPartition } -eq 0 ] ; then
# for non UEFI
2021-09-29 11:54:01 +02:00
bootPartition = $( sudo df /boot 2>/dev/null | grep dev | cut -d " " -f 1 | sed "s#/dev/##g" )
2021-09-14 10:43:03 +01:00
fi
2021-07-20 19:19:34 +02:00
lsblk -o NAME,SIZE -b | grep -P "[s|vn][dv][a-z][0-9]?" > .lsblk.tmp
2020-09-21 21:59:37 +02:00
while read line; do
2020-09-30 13:55:04 +02:00
# cut line info into different informations
testname = $( echo $line | cut -d " " -f 1 | sed 's/[^a-z0-9]*//g' )
testdevice = $( echo $testname | sed 's/[^a-z]*//g' )
testpartition = $( echo $testname | grep -P '[a-z]{3,5}[0-9]{1}' )
2020-10-07 12:43:53 +02:00
if [ ${# testpartition } -gt 0 ] ; then
testsize = $( echo $line | sed "s/ */ /g" | cut -d " " -f 2 | sed 's/[^0-9]*//g' )
else
testsize = 0
fi
2020-11-30 14:14:49 +01:00
2020-09-30 13:55:04 +02:00
#echo "# line($line)"
#echo "# testname(${testname}) testdevice(${testdevice}) testpartition(${testpartition}) testsize(${testsize})"
2020-11-30 14:14:49 +01:00
2020-10-07 12:43:53 +02:00
# count partitions
2020-11-30 14:14:49 +01:00
testpartitioncount = 0
if [ ${# testdevice } -gt 0 ] ; then
testpartitioncount = $( sudo fdisk -l | grep /dev/$testdevice | wc -l)
# do not count line with disk info
testpartitioncount = $(( testpartitioncount-1))
fi
#echo "# testpartitioncount($testpartitioncount)"
#echo "# testpartitioncount(${testpartitioncount})"
#echo "# OSPartition(${OSPartition})"
2021-09-14 10:43:03 +01:00
#echo "# bootPartition(${bootPartition})"
2020-11-30 14:14:49 +01:00
#echo "# hdd(${hdd})"
2020-10-07 12:43:53 +02:00
if [ $testpartitioncount -gt 0 ] ; then
2021-09-14 10:43:03 +01:00
# if a partition was found - make sure to skip the OS and boot partitions
2021-09-30 21:32:53 +02:00
if [ " ${ testpartition } " != " ${ OSPartition } " ] && [ " ${ testpartition } " != " ${ bootPartition } " ] ; then
2020-10-07 12:43:53 +02:00
# make sure to use the biggest
if [ ${ testsize } -gt ${ sizeDataPartition } ] ; then
sizeDataPartition = ${ testsize }
hddDataPartition = " ${ testpartition } "
hdd = " ${ testdevice } "
fi
fi
else
2020-11-30 14:14:49 +01:00
2021-08-27 03:59:21 -04:00
# default hdd set, when there is no OSpartition and there might be no partitions at all
2020-11-30 14:14:49 +01:00
if [ " ${ OSPartition } " = "root" ] && [ " ${ hdd } " = "" ] && [ " ${ testdevice } " != "" ] ; then
hdd = " ${ testdevice } "
fi
# make sure to use the biggest
2020-11-01 11:24:12 +01:00
if [ ${ testsize } -gt ${ sizeDataPartition } ] ; then
2021-08-27 03:59:21 -04:00
# Partition to be created is smaller than disk so this is not correct (but close)
2020-11-01 11:24:12 +01:00
sizeDataPartition = $( sudo fdisk -l /dev/$testdevice | grep GiB | cut -d " " -f 5)
hddDataPartition = " ${ testdevice } 1 "
hdd = " ${ testdevice } "
2020-11-30 14:14:49 +01:00
fi
2019-12-11 01:19:13 +01:00
fi
2020-09-30 13:55:04 +02:00
2020-09-21 21:59:37 +02:00
done < .lsblk.tmp
rm -f .lsblk.tmp 1>/dev/null 2>/dev/null
2020-10-07 12:43:53 +02:00
2021-05-04 14:18:55 +02:00
# display possible warnings from hdd partition detection
2020-11-30 14:14:49 +01:00
if [ " ${ hddPartitionCandidate } " != "" ] && [ ${# hddDataPartition } -lt 4 ] ; then
echo " # WARNING: found invalid partition ( ${ hddDataPartition } ) - redacting "
2020-09-30 13:55:04 +02:00
hddDataPartition = ""
fi
2020-11-01 19:58:50 +01:00
2021-05-04 14:18:55 +02:00
# try to detect if its an SSD
2020-09-21 21:59:37 +02:00
isSSD = $( sudo cat /sys/block/${ hdd } /queue/rotational 2>/dev/null | grep -c 0)
2020-11-01 19:58:50 +01:00
echo " isSSD= ${ isSSD } "
2020-09-21 21:59:37 +02:00
2021-05-04 14:18:55 +02:00
# display results from hdd & partition detection
2019-12-11 01:19:13 +01:00
echo " hddCandidate=' ${ hdd } ' "
2020-11-30 14:14:49 +01:00
hddBytes = 0
hddGigaBytes = 0
if [ " ${ hdd } " != "" ] ; then
hddBytes = $( sudo fdisk -l /dev/$hdd | grep GiB | cut -d " " -f 5)
hddGigaBytes = $( echo " scale=0; ${ hddBytes } /1024/1024/1024 " | bc -l)
fi
2020-11-01 19:58:50 +01:00
echo " hddBytes= ${ hddBytes } "
echo " hddGigaBytes= ${ hddGigaBytes } "
echo " hddPartitionCandidate=' ${ hddDataPartition } ' "
2021-05-04 14:18:55 +02:00
# if positive deliver more data
2020-09-21 21:59:37 +02:00
if [ ${# hddDataPartition } -gt 0 ] ; then
2019-12-11 01:19:13 +01:00
2020-09-21 21:59:37 +02:00
# check partition size in bytes and GBs
echo " hddDataPartitionBytes= ${ sizeDataPartition } "
hddDataPartitionGigaBytes = $( echo " scale=0; ${ sizeDataPartition } /1024/1024/1024 " | bc -l)
2020-11-01 19:58:50 +01:00
echo " hddPartitionGigaBytes= ${ hddDataPartitionGigaBytes } "
2019-12-11 01:19:13 +01:00
2020-09-21 21:59:37 +02:00
# check format of devices partition
hddFormat = $( lsblk -o FSTYPE,NAME,TYPE | grep part | grep " ${ hddDataPartition } " | cut -d " " -f 1)
2019-12-11 01:19:13 +01:00
echo " hddFormat=' ${ hddFormat } ' "
# if 'ext4' or 'btrfs' then temp mount and investigate content
if [ " ${ hddFormat } " = "ext4" ] || [ " ${ hddFormat } " = "btrfs" ] ; then
2019-12-11 04:09:31 +01:00
2021-05-04 14:18:55 +02:00
# BTRFS is working with subvolumes for snapshots / ext4 has no SubVolumes
2019-12-11 04:09:31 +01:00
subVolumeDir = ""
if [ " ${ hddFormat } " = "btrfs" ] ; then
subVolumeDir = "/WORKINGDIR"
fi
2019-12-11 01:19:13 +01:00
# temp mount data drive
2020-01-21 01:02:32 +01:00
mountError = ""
2019-12-11 01:19:13 +01:00
sudo mkdir -p /mnt/hdd
2020-01-21 01:02:32 +01:00
if [ " ${ hddFormat } " = "ext4" ] ; then
2020-09-30 13:55:04 +02:00
hddDataPartitionExt4 = $hddDataPartition
2020-09-21 21:59:37 +02:00
mountError = $( sudo mount /dev/${ hddDataPartitionExt4 } /mnt/hdd 2>& 1)
isTempMounted = $( df | grep /mnt/hdd | grep -c ${ hddDataPartitionExt4 } )
2020-01-21 01:02:32 +01:00
fi
if [ " ${ hddFormat } " = "btrfs" ] ; then
mountError = $( sudo mount -o degraded /dev/${ hdd } 1 /mnt/hdd 2>& 1)
2020-09-21 21:59:37 +02:00
isTempMounted = $( df | grep /mnt/hdd | grep -c ${ hdd } )
2020-01-21 01:02:32 +01:00
fi
2020-01-15 18:04:10 +01:00
# check for mount error
if [ ${# mountError } -gt 0 ] || [ ${ isTempMounted } -eq 0 ] ; then
echo "hddError='data mount failed'"
2019-12-11 01:19:13 +01:00
else
2020-09-21 21:59:37 +02:00
2021-05-04 14:28:42 +02:00
#####################################
2021-06-20 20:59:05 +02:00
# Pre-Setup Investigation of DATA-PART
2021-05-04 14:28:42 +02:00
# check for recoverable RaspiBlitz data (if config file exists) and raid
hddRaspiData = $( sudo ls -l /mnt/hdd${ subVolumeDir } 2>/dev/null | grep -c raspiblitz.conf)
2021-05-21 11:08:33 -05:00
#isRaid=$(btrfs filesystem df /mnt/hdd 2>/dev/null | grep -c "Data, RAID1")
2021-05-04 14:28:42 +02:00
echo " hddRaspiData= ${ hddRaspiData } "
2021-05-04 14:37:23 +02:00
hddRaspiVersion = ""
if [ ${ hddRaspiData } -eq 1 ] ; then
source /mnt/hdd${ subVolumeDir } /raspiblitz.conf
hddRaspiVersion = " ${ raspiBlitzVersion } "
fi
2021-05-04 14:38:16 +02:00
echo " hddRaspiVersion=' ${ hddRaspiVersion } ' "
2021-05-04 14:37:23 +02:00
# check if there is a wifi configuration as backup
hddGotWifiConf = $( ls /mnt/hdd${ subVolumeDir } /app-data/wpa_supplicant.conf 2>/dev/null | grep -c "wpa_supplicant.conf" )
if [ ${ hddGotWifiConf } -eq 1 ] ; then
# make a copy to the mem cache drive (so that Wifi can be connected before setup & final HDD mount)
sudo cp /mnt/hdd${ subVolumeDir } /app-data/wpa_supplicant.conf /var/cache/raspiblitz/wpa_supplicant.conf
echo "wifiBackupConfigCopy='/var/cache/raspiblitz/wpa_supplicant.conf'"
fi
2021-05-04 14:28:42 +02:00
2021-05-04 14:37:23 +02:00
# comment this line out if case to study the contect of the data section
sudo umount /mnt/hdd
2019-12-11 01:19:13 +01:00
fi
# temp storage data drive
sudo mkdir -p /mnt/storage
2019-12-11 03:19:35 +01:00
if [ " ${ hddFormat } " = "btrfs" ] ; then
2019-12-11 04:15:34 +01:00
# in btrfs setup the second partition is storage partition
2020-01-15 20:43:42 +01:00
sudo mount /dev/${ hdd } 2 /mnt/storage 2>/dev/null
2020-09-21 21:59:37 +02:00
isTempMounted = $( df | grep /mnt/storage | grep -c ${ hdd } )
2019-12-11 01:19:13 +01:00
else
2020-09-21 21:59:37 +02:00
# in ext4 setup the partition is also the storage partition
sudo mount /dev/${ hddDataPartitionExt4 } /mnt/storage 2>/dev/null
isTempMounted = $( df | grep /mnt/storage | grep -c ${ hddDataPartitionExt4 } )
2019-12-11 01:19:13 +01:00
fi
if [ ${ isTempMounted } -eq 0 ] ; then
echo "hddError='storage mount failed'"
else
2021-04-05 13:14:56 +02:00
2021-05-04 14:28:42 +02:00
########################################
# Pre-Setup Invetigation of STORAGE-PART
2021-05-04 14:18:55 +02:00
2019-12-11 01:19:13 +01:00
# check for blockchain data on storage
2019-12-11 04:15:34 +01:00
hddBlocksBitcoin = $( sudo ls /mnt/storage${ subVolumeDir } /bitcoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat' )
2019-12-11 04:23:32 +01:00
echo " hddBlocksBitcoin= ${ hddBlocksBitcoin } "
2019-12-11 04:15:34 +01:00
hddBlocksLitecoin = $( sudo ls /mnt/storage${ subVolumeDir } /litecoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat' )
2019-12-11 04:23:32 +01:00
echo " hddBlocksLitecoin= ${ hddBlocksLitecoin } "
2019-12-13 18:21:56 +01:00
if [ " ${ blockchainType } " = "bitcoin" ] && [ ${ hddBlocksBitcoin } -eq 1 ] ; then
echo "hddGotBlockchain=1"
elif [ " ${ blockchainType } " = "litecoin" ] && [ ${ hddBlocksLitecoin } -eq 1 ] ; then
echo "hddGotBlockchain=1"
elif [ ${# blockchainType } -gt 0 ] ; then
echo "hddGotBlockchain=0"
fi
2021-04-05 13:14:56 +02:00
# check free space on data drive
if [ ${ isBTRFS } -eq 0 ] ; then
# EXT4
hdd_data_free1Kblocks = $( df -h -k /dev/${ hddDataPartitionExt4 } | grep " /dev/ ${ hddDataPartitionExt4 } " | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9' )
else
# BRTS
hdd_data_free1Kblocks = $( df -h -k /dev/${ hdd } 1 | grep " /dev/ ${ hdd } 1 " | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9' )
fi
echo " hddDataFreeKB= ${ hdd_data_free1Kblocks } "
# check if its another fullnode implementation data disk
2021-05-04 14:39:03 +02:00
hddGotMigrationData = ""
2021-04-05 13:14:56 +02:00
if [ " ${ hddFormat } " = "ext4" ] ; then
2021-10-07 11:50:25 +02:00
# check for other node implementations
2021-04-05 13:14:56 +02:00
isUmbrelHDD = $( sudo ls /mnt/storage/umbrel/info.json 2>/dev/null | grep -c '.json' )
2021-10-07 11:50:25 +02:00
isMyNodeHDD = $( sudo ls /mnt/storage/mynode/bitcoin/bitcoin.conf 2>/dev/null | grep -c '.conf' )
2021-04-05 13:14:56 +02:00
if [ ${ isUmbrelHDD } -gt 0 ] ; then
hddGotMigrationData = "umbrel"
2021-10-07 11:50:25 +02:00
elif [ ${ isMyNodeHDD } -gt 0 ] ; then
2021-04-05 13:14:56 +02:00
hddGotMigrationData = "mynode"
fi
else
echo "# not an ext4 drive - all known fullnode packages use ext4 at the moment"
fi
echo " hddGotMigrationData=' ${ hddGotMigrationData } ' "
2021-05-04 14:37:23 +02:00
# comment this line out if case to study the contect of the storage section
sudo umount /mnt/storage
2019-12-11 01:19:13 +01:00
fi
2019-12-11 04:46:02 +01:00
else
# if not ext4 or btrfs - there is no usable data
echo "hddRaspiData=0"
echo "hddBlocksBitcoin=0"
echo "hddBlocksLitecoin=0"
2019-12-13 18:21:56 +01:00
echo "hddGotBlockchain=0"
2019-12-11 01:19:13 +01:00
fi
fi
2019-12-12 14:34:47 +01:00
else
2019-12-13 18:29:20 +01:00
# STATUS INFO WHEN MOUNTED
2019-12-12 14:34:47 +01:00
# output data drive
2020-11-10 23:40:54 +01:00
if [ ${ isBTRFS } -eq 1 ] ; then
# on btrfs date the storage partition as the data partition
hddDataPartition = $( df | grep " /mnt/storage $" | cut -d " " -f 1 | cut -d "/" -f 3)
else
# on ext4 its the whole /mnt/hdd
hddDataPartition = $( df | grep " /mnt/hdd $" | cut -d " " -f 1 | cut -d "/" -f 3)
fi
2020-09-21 21:59:37 +02:00
hdd = $( echo $hddDataPartition | sed 's/[0-9]*//g' )
hddFormat = $( lsblk -o FSTYPE,NAME,TYPE | grep part | grep " ${ hddDataPartition } " | cut -d " " -f 1)
if [ " ${ hddFormat } " = "ext4" ] ; then
hddDataPartitionExt4 = $hddDataPartition
fi
2020-10-06 18:49:11 +00:00
hddRaspiData = $( sudo ls -l /mnt/hdd | grep -c raspiblitz.conf)
2020-10-05 23:04:59 +02:00
echo " hddRaspiData= ${ hddRaspiData } "
2021-05-21 11:08:33 -05:00
hddRaspiVersion = ""
if [ ${ hddRaspiData } -eq 1 ] ; then
source /mnt/hdd/raspiblitz.conf
hddRaspiVersion = " ${ raspiBlitzVersion } "
fi
echo " hddRaspiVersion=' ${ hddRaspiVersion } ' "
2020-09-21 21:59:37 +02:00
2020-10-05 23:04:59 +02:00
isSSD = $( sudo cat /sys/block/${ hdd } /queue/rotational 2>/dev/null | grep -c 0)
2020-09-21 21:59:37 +02:00
echo " isSSD= ${ isSSD } "
2020-10-05 23:04:59 +02:00
2019-12-13 14:57:07 +01:00
echo " datadisk=' ${ hdd } ' "
2020-09-21 21:59:37 +02:00
echo " datapartition=' ${ hddDataPartition } ' "
2019-12-13 18:29:20 +01:00
# check if blockchain data is available
hddBlocksBitcoin = $( sudo ls /mnt/hdd/bitcoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat' )
echo " hddBlocksBitcoin= ${ hddBlocksBitcoin } "
hddBlocksLitecoin = $( sudo ls /mnt/hdd/litecoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat' )
echo " hddBlocksLitecoin= ${ hddBlocksLitecoin } "
if [ " ${ blockchainType } " = "bitcoin" ] && [ ${ hddBlocksBitcoin } -eq 1 ] ; then
echo "hddGotBlockchain=1"
elif [ " ${ blockchainType } " = "litecoin" ] && [ ${ hddBlocksLitecoin } -eq 1 ] ; then
echo "hddGotBlockchain=1"
elif [ ${# blockchainType } -gt 0 ] ; then
echo "hddGotBlockchain=0"
fi
2019-12-13 18:33:25 +01:00
2020-02-24 18:30:34 +01:00
# check size in bytes and GBs
2020-09-21 21:59:37 +02:00
sizeDataPartition = $( lsblk -o NAME,SIZE -b | grep " ${ hddDataPartition } " | awk '$1=$1' | cut -d " " -f 2)
echo " hddBytes= ${ sizeDataPartition } "
hddGigaBytes = $( echo " scale=0; ${ sizeDataPartition } /1024/1024/1024 " | bc -l)
2020-02-24 18:30:34 +01:00
echo " hddGigaBytes= ${ hddGigaBytes } "
2019-12-16 13:24:32 +01:00
# used space - at the moment just string info to display
if [ ${ isBTRFS } -eq 0 ] ; then
# EXT4 calculations
2020-09-21 21:59:37 +02:00
hdd_used_space = $( df -h | grep " /dev/ ${ hddDataPartitionExt4 } " | sed -e's/ */ /g' | cut -d" " -f 3 2>/dev/null)
hdd_used_ratio = $( df -h | grep " /dev/ ${ hddDataPartitionExt4 } " | sed -e's/ */ /g' | cut -d" " -f 5 | tr -dc '0-9' 2>/dev/null)
2021-04-05 13:14:56 +02:00
hdd_data_free1Kblocks = $( df -h -k /dev/${ hddDataPartitionExt4 } | grep " /dev/ ${ hddDataPartitionExt4 } " | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9' )
2019-12-16 13:24:32 +01:00
hddUsedInfo = " ${ hdd_used_space } ( ${ hdd_used_ratio } %) "
else
# BRTS calculations
# TODO: this is the final/correct way - make better later
# https://askubuntu.com/questions/170044/btrfs-and-missing-free-space
datadrive = $( df -h | grep " /dev/ ${ hdd } 1 " | sed -e's/ */ /g' | cut -d" " -f 5)
storageDrive = $( df -h | grep " /dev/ ${ hdd } 2 " | sed -e's/ */ /g' | cut -d" " -f 5)
2021-04-05 13:14:56 +02:00
hdd_data_free1Kblocks = $( df -h -k /dev/${ hdd } 1 | grep " /dev/ ${ hdd } 1 " | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9' )
2019-12-16 13:24:32 +01:00
hddUsedInfo = " ${ datadrive } & ${ storageDrive } "
fi
echo " hddUsedInfo=' ${ hddUsedInfo } ' "
2021-04-05 13:14:56 +02:00
echo " hddDataFreeKB= ${ hdd_data_free1Kblocks } "
2019-12-16 13:24:32 +01:00
2019-12-11 01:19:13 +01:00
fi
2021-04-03 12:24:50 +02:00
# HDD Adpater UASP support --> https://www.pragmaticlinux.com/2021/03/fix-for-getting-your-ssd-working-via-usb-3-on-your-raspberry-pi/
2021-05-04 14:18:55 +02:00
# in both cases (if mounted or not - using the hdd selection from both cases)
2021-03-26 23:26:29 +01:00
if [ ${# hdd } -gt 0 ] ; then
2021-04-03 12:24:50 +02:00
# determine USB HDD adapter model ID
2021-03-26 23:26:29 +01:00
hddAdapter = $( lsusb | grep "SATA" | head -1 | cut -d " " -f6)
2021-04-03 12:24:50 +02:00
if [ " ${ hddAdapter } " = = "" ] ; then
hddAdapter = $( lsusb | grep "GC Protronics" | head -1 | cut -d " " -f6)
fi
if [ " ${ hddAdapter } " = = "" ] ; then
hddAdapter = $( lsusb | grep "ASMedia Technology" | head -1 | cut -d " " -f6)
fi
2021-03-26 23:26:29 +01:00
echo " hddAdapterUSB=' ${ hddAdapter } ' "
2021-04-03 12:24:50 +02:00
hddAdapterUSAP = 0
2021-09-09 11:33:31 +01:00
# check if user wants to force UASP on
if [ -f "/boot/uasp.force" ] ; then
hddAdapterUSAP = 1
echo "uaspForced=1"
fi
2021-09-09 12:51:50 +02:00
if [ $( cat /mnt/hdd/raspiblitz.conf 2>/dev/null | grep -c "forceUasp=on" ) -eq 1 ] ; then
2021-09-09 11:33:31 +01:00
hddAdapterUSAP = 1
fi
# check if HDD ADAPTER is on UASP WHITELIST (tested devices)
2021-04-03 12:24:50 +02:00
if [ " ${ hddAdapter } " = = "174c:55aa" ] ; then
# UGREEN 2.5" External USB 3.0 Hard Disk Case with UASP support
hddAdapterUSAP = 1
fi
if [ " ${ hddAdapter } " = = "0825:0001" ] || [ " ${ hddAdapter } " = = "174c:0825" ] ; then
# SupTronics 2.5" SATA HDD Shield X825 v1.5
hddAdapterUSAP = 1
fi
2021-04-05 13:14:56 +02:00
echo " hddAdapterUSAP= ${ hddAdapterUSAP } "
2021-03-26 23:26:29 +01:00
fi
2019-12-12 14:34:47 +01:00
echo
2019-12-13 16:55:29 +01:00
echo "# RAID"
2019-12-10 20:45:34 +01:00
echo " isRaid= ${ isRaid } "
2019-12-13 14:53:32 +01:00
if [ ${ isRaid } -eq 1 ] && [ ${ isMounted } -eq 1 ] && [ ${ isBTRFS } -eq 1 ] ; then
# RAID is ON - give information about running raid setup
# show devices used for raid
raidHddDev = $( lsblk -o NAME,MOUNTPOINT | grep "/mnt/hdd" | awk '$1=$1' | cut -d " " -f 1 | sed 's/[^0-9a-z]*//g' )
raidUsbDev = $( sudo btrfs filesystem show /mnt/hdd | grep -F -v " ${ raidHddDev } " | grep "/dev/" | cut -d "/" --f 3)
echo " raidHddDev=' ${ raidHddDev } ' "
echo " raidUsbDev=' ${ raidUsbDev } ' "
else
# RAID is OFF - give information about possible drives to activate
# find the possible drives that can be used as
drivecounter = 0
for disk in $( lsblk -o NAME,TYPE | grep "disk" | awk '$1=$1' | cut -d " " -f 1)
do
devMounted = $( lsblk -o MOUNTPOINT,NAME | grep " $disk " | grep -c "^/" )
2021-08-27 03:59:21 -04:00
# is raid candidate when not mounted and not the data drive candidate (hdd/ssd)
2021-09-07 16:36:16 +02:00
if [ ${ devMounted } -eq 0 ] && [ " ${ disk } " != " ${ hdd } " ] && [ " ${ hdd } " != "" ] && [ " ${ disk } " != "" ] ; then
2019-12-13 15:20:10 +01:00
sizeBytes = $( lsblk -o NAME,SIZE -b | grep " ^ ${ disk } " | awk '$1=$1' | cut -d " " -f 2)
sizeGigaBytes = $( echo " scale=0; ${ sizeBytes } /1024/1024/1024 " | bc -l)
2021-09-13 20:31:39 +02:00
vedorname = $( lsblk -o NAME,VENDOR | grep " ^ ${ disk } " | awk '$1=$1' | cut -d " " -f 2 | sed 's/[^a-zA-Z0-9]//g' )
2019-12-13 15:20:10 +01:00
mountoption = " ${ disk } ${ sizeGigaBytes } GB ${ vedorname } "
2019-12-13 14:53:32 +01:00
echo " raidCandidate[ ${ drivecounter } ]=' ${ mountoption } ' "
drivecounter = $(( $drivecounter + 1 ))
fi
done
echo " raidCandidates= ${ drivecounter } "
2019-12-10 20:45:34 +01:00
fi
echo
2019-12-13 16:55:29 +01:00
echo "# SWAP"
2019-12-10 20:45:34 +01:00
echo " isSwapExternal= ${ isSwapExternal } "
if [ ${ isSwapExternal } -eq 1 ] ; then
echo " SwapExternalPath=' ${ externalSwapPath } ' "
fi
echo
exit 1
fi
######################
# FORMAT EXT4 or BTRFS
######################
2021-08-27 03:59:21 -04:00
# check basics for formatting
2019-12-10 20:45:34 +01:00
if [ " $1 " = "format" ] ; then
# check valid format
if [ " $2 " = "btrfs" ] ; then
2020-09-21 21:59:37 +02:00
>& 2 echo "# DATA DRIVE - FORMATTING to BTRFS layout (new)"
2019-12-10 20:45:34 +01:00
elif [ " $2 " = "ext4" ] ; then
2020-09-21 21:59:37 +02:00
>& 2 echo "# DATA DRIVE - FORMATTING to EXT4 layout (old)"
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# missing valid second parameter: 'btrfs' or 'ext4'"
2019-12-10 20:45:34 +01:00
echo "error='missing parameter'"
exit 1
fi
2019-12-11 01:19:13 +01:00
# get device name to format
hdd = $3
2019-12-10 20:45:34 +01:00
if [ ${# hdd } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# missing valid third parameter as the device (like 'sda')"
2021-08-27 03:59:21 -04:00
>& 2 echo "# run 'status' to see candidate devices"
2019-12-11 01:19:13 +01:00
echo "error='missing parameter'"
2019-02-25 05:33:52 +01:00
exit 1
2019-12-10 20:45:34 +01:00
fi
2020-11-01 11:46:31 +01:00
if [ " $2 " = "btrfs" ] ; then
2020-11-01 13:42:03 +01:00
# check if device is existing and a disk (not a partition)
2020-11-01 11:46:31 +01:00
isValid = $( lsblk -o NAME,TYPE | grep disk | grep -c " ${ hdd } " )
else
2020-11-01 13:42:03 +01:00
# check if device is existing (its OK when its a partition)
isValid = $( lsblk -o NAME,TYPE | grep -c " ${ hdd } " )
2020-11-01 11:46:31 +01:00
fi
2019-12-11 01:19:13 +01:00
if [ ${ isValid } -eq 0 ] ; then
2020-11-01 13:42:03 +01:00
>& 2 echo "# given device was not found"
2019-12-13 16:38:59 +01:00
>& 2 echo "# or is not of type disk - see 'lsblk'"
2019-12-11 01:19:13 +01:00
echo "error='device not valid'"
exit 1
2019-12-10 20:45:34 +01:00
fi
2020-09-21 21:59:37 +02:00
# get basic info on data drive
2020-02-24 22:44:59 +01:00
source <( sudo /home/admin/config.scripts/blitz.datadrive.sh status)
if [ ${ isSwapExternal } -eq 1 ] && [ " ${ hdd } " = = " ${ datadisk } " ] ; then
>& 2 echo "# Switching off external SWAP of system drive"
2019-12-10 20:45:34 +01:00
sudo dphys-swapfile swapoff 1>/dev/null
sudo dphys-swapfile uninstall 1>/dev/null
2019-02-25 05:33:52 +01:00
fi
2019-12-10 20:45:34 +01:00
2020-02-24 22:44:59 +01:00
>& 2 echo "# Unmounting all partitions of this device"
2019-12-10 20:45:34 +01:00
# remove device from all system mounts (also fstab)
2020-09-23 17:15:09 +02:00
lsblk -o UUID,NAME | grep " ${ hdd } " | cut -d " " -f 1 | grep "-" | while read -r uuid ; do
if [ ${# uuid } -gt 0 ] ; then
>& 2 echo " # Cleaning /etc/fstab from ${ uuid } "
sudo sed -i " /UUID= ${ uuid } /d " /etc/fstab
sync
else
>& 2 echo "# skipping empty result"
fi
2019-12-10 20:45:34 +01:00
done
sudo mount -a
2019-12-11 01:19:13 +01:00
2020-02-24 22:44:59 +01:00
if [ " ${ hdd } " = = " ${ datadisk } " ] ; then
>& 2 echo "# Make sure system drives are unmounted .."
sudo umount /mnt/hdd 2>/dev/null
sudo umount /mnt/temp 2>/dev/null
sudo umount /mnt/storage 2>/dev/null
unmounted1 = $( df | grep -c "/mnt/hdd" )
if [ ${ unmounted1 } -gt 0 ] ; then
2020-09-23 17:15:09 +02:00
>& 2 echo "# ERROR: failed to unmount /mnt/hdd"
2020-02-24 22:44:59 +01:00
echo "error='failed to unmount /mnt/hdd'"
exit 1
fi
unmounted2 = $( df | grep -c "/mnt/temp" )
if [ ${ unmounted2 } -gt 0 ] ; then
2020-09-23 17:15:09 +02:00
>& 2 echo "# ERROR: failed to unmount /mnt/temp"
2020-02-24 22:44:59 +01:00
echo "error='failed to unmount /mnt/temp'"
exit 1
fi
unmounted3 = $( df | grep -c "/mnt/storage" )
if [ ${ unmounted3 } -gt 0 ] ; then
2020-09-23 17:15:09 +02:00
>& 2 echo "# ERROR: failed to unmount /mnt/storage"
2020-02-24 22:44:59 +01:00
echo "error='failed to unmount /mnt/storage'"
exit 1
fi
2019-12-10 20:45:34 +01:00
fi
2020-11-01 11:46:31 +01:00
if [ [ $hdd = ~ [ 0-9] ] ] ; then
ext4IsPartition = 1
else
ext4IsPartition = 0
fi
wipePartitions = 0
if [ " $2 " = "btrfs" ] ; then
wipePartitions = 1
2020-06-12 17:23:26 +02:00
fi
2020-11-01 11:46:31 +01:00
if [ " $2 " = "ext4" ] && [ $ext4IsPartition -eq 0 ] ; then
wipePartitions = 1
fi
if [ $wipePartitions -eq 1 ] ; then
# wipe all partitions and write fresh GPT
>& 2 echo "# Wiping all partitions (sfdisk/wipefs)"
sudo sfdisk --delete /dev/${ hdd }
sleep 4
sudo wipefs -a /dev/${ hdd }
sleep 4
partitions = $( lsblk | grep -c " ─ ${ hdd } " )
if [ ${ partitions } -gt 0 ] ; then
>& 2 echo "# WARNING: partitions are still not clean - try Quick & Dirty"
sudo dd if = /dev/zero of = /dev/${ hdd } bs = 512 count = 1
fi
partitions = $( lsblk | grep -c " ─ ${ hdd } " )
if [ ${ partitions } -gt 0 ] ; then
>& 2 echo "# ERROR: partition cleaning failed"
echo "error='partition cleaning failed'"
exit 1
fi
2021-09-30 10:52:20 +02:00
sudo parted -s /dev/${ hdd } mklabel gpt 1>/dev/null 1>& 2
2020-11-01 11:46:31 +01:00
sleep 2
sync
2019-12-10 20:45:34 +01:00
fi
2020-09-21 21:59:37 +02:00
# formatting old: EXT4
2020-11-01 11:46:31 +01:00
2020-09-21 21:59:37 +02:00
if [ " $2 " = "ext4" ] ; then
# prepare temp mount point
sudo mkdir -p /tmp/ext4 1>/dev/null
2020-11-01 11:46:31 +01:00
if [ $ext4IsPartition -eq 0 ] ; then
# write new EXT4 partition
>& 2 echo "# Creating the one big partition"
2021-09-30 10:52:20 +02:00
sudo parted /dev/${ hdd } mkpart primary ext4 0% 100% 1>& 2
2020-11-01 11:46:31 +01:00
sleep 6
sync
2021-03-08 19:53:33 +01:00
# loop until the partition gets available
2020-11-01 11:46:31 +01:00
loopdone = 0
loopcount = 0
while [ ${ loopdone } -eq 0 ]
do
2021-03-08 19:53:33 +01:00
>& 2 echo "# waiting until the partition gets available"
2020-11-01 11:46:31 +01:00
sleep 2
sync
loopdone = $( lsblk -o NAME | grep -c ${ hdd } 1)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 10 ] ; then
2021-03-08 19:53:33 +01:00
>& 2 echo "# partition failed"
2020-11-01 11:46:31 +01:00
echo "error='partition failed'"
exit 1
fi
done
2021-03-08 19:53:33 +01:00
>& 2 echo "# partition available"
2020-11-01 11:46:31 +01:00
fi
2020-09-21 21:59:37 +02:00
# make sure /mnt/hdd is unmounted before formatting
sudo umount -f /tmp/ext4 2>/dev/null
unmounted = $( df | grep -c "/tmp/ext4" )
if [ ${ unmounted } -gt 0 ] ; then
2020-09-23 17:15:09 +02:00
>& 2 echo "# ERROR: failed to unmount /tmp/ext4"
2020-09-21 21:59:37 +02:00
echo "error='failed to unmount /tmp/ext4'"
exit 1
fi
2019-12-10 20:45:34 +01:00
2020-09-21 21:59:37 +02:00
>& 2 echo "# Formatting"
2020-11-01 11:46:31 +01:00
if [ $ext4IsPartition -eq 0 ] ; then
sudo mkfs.ext4 -F -L BLOCKCHAIN /dev/${ hdd } 1 1>/dev/null
else
sudo mkfs.ext4 -F -L BLOCKCHAIN /dev/${ hdd } 1>/dev/null
fi
2020-09-21 21:59:37 +02:00
loopdone = 0
loopcount = 0
while [ ${ loopdone } -eq 0 ]
do
>& 2 echo "# waiting until formatted drives gets available"
sleep 2
sync
loopdone = $( lsblk -o NAME,LABEL | grep -c BLOCKCHAIN)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 10 ] ; then
2020-09-23 17:15:09 +02:00
>& 2 echo "# ERROR: formatting ext4 failed"
2020-09-21 21:59:37 +02:00
echo "error='formatting ext4 failed'"
exit 1
fi
done
2021-08-27 03:59:21 -04:00
# setting fsk check interval to 1
2020-09-21 21:59:37 +02:00
# see https://github.com/rootzoll/raspiblitz/issues/360#issuecomment-467567572
2020-11-01 11:46:31 +01:00
if [ $ext4IsPartition -eq 0 ] ; then
sudo tune2fs -c 1 /dev/${ hdd } 1
else
sudo tune2fs -c 1 /dev/${ hdd }
fi
2020-09-21 21:59:37 +02:00
>& 2 echo "# OK EXT 4 format done"
exit 0
fi
2021-08-27 03:59:21 -04:00
# formatting new: BTRFS layout - this consists of 3 volumes:
2020-09-21 21:59:37 +02:00
if [ " $2 " = "btrfs" ] ; then
2019-12-10 20:45:34 +01:00
2020-11-01 19:58:50 +01:00
# prepare temp mount point
2020-09-21 21:59:37 +02:00
sudo mkdir -p /tmp/btrfs 1>/dev/null
2019-12-10 20:45:34 +01:00
2020-11-01 19:58:50 +01:00
>& 2 echo " # Creating BLITZDATA ( ${ hdd } ) "
2020-09-21 21:59:37 +02:00
sudo parted -s -a optimal -- /dev/${ hdd } mkpart primary btrfs 0% 30GiB 1>/dev/null
2020-11-01 19:58:50 +01:00
sync
sleep 6
2020-09-21 21:59:37 +02:00
win = $( lsblk -o NAME | grep -c ${ hdd } 1)
if [ ${ win } -eq 0 ] ; then
echo "error='partition failed'"
exit 1
fi
sudo mkfs.btrfs -f -L BLITZDATA /dev/${ hdd } 1 1>/dev/null
2020-11-01 19:58:50 +01:00
# check result
loopdone = 0
loopcount = 0
while [ ${ loopdone } -eq 0 ]
do
>& 2 echo "# waiting until formatted drives gets available"
sleep 2
sync
2021-08-17 11:38:26 +02:00
sudo parted -l
2020-11-01 19:58:50 +01:00
loopdone = $( lsblk -o NAME,LABEL | grep -c BLITZDATA)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 60 ] ; then
>& 2 echo "# ERROR: formatting BTRFS failed (BLITZDATA)"
>& 2 echo "# check with: lsblk -o NAME,LABEL | grep -c BLITZDATA"
echo "error='formatting failed'"
exit 1
fi
done
2020-09-21 21:59:37 +02:00
>& 2 echo "# OK BLITZDATA exists now"
2019-12-10 20:45:34 +01:00
2020-09-21 21:59:37 +02:00
>& 2 echo "# Creating SubVolume for Snapshots"
sudo mount /dev/${ hdd } 1 /tmp/btrfs 1>/dev/null
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 1 failed' "
exit 1
fi
cd /tmp/btrfs
sudo btrfs subvolume create WORKINGDIR
subVolDATA = $( sudo btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && sudo umount /tmp/btrfs
>& 2 echo "# Creating BLITZSTORAGE"
sudo parted -s -a optimal -- /dev/${ hdd } mkpart primary btrfs 30GiB -34GiB 1>/dev/null
2020-11-01 19:58:50 +01:00
sync
sleep 6
2020-09-21 21:59:37 +02:00
win = $( lsblk -o NAME | grep -c ${ hdd } 2)
if [ ${ win } -eq 0 ] ; then
echo "error='partition failed'"
exit 1
fi
sudo mkfs.btrfs -f -L BLITZSTORAGE /dev/${ hdd } 2 1>/dev/null
2020-11-01 19:58:50 +01:00
# check result
loopdone = 0
loopcount = 0
while [ ${ loopdone } -eq 0 ]
do
>& 2 echo "# waiting until formatted drives gets available"
sleep 2
sync
2021-08-17 11:38:26 +02:00
sudo parted -l
2020-11-01 19:58:50 +01:00
loopdone = $( lsblk -o NAME,LABEL | grep -c BLITZSTORAGE)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 60 ] ; then
>& 2 echo "# ERROR: formatting BTRFS failed (BLITZSTORAGE)"
echo "error='formatting failed'"
exit 1
fi
done
2020-09-21 21:59:37 +02:00
>& 2 echo "# OK BLITZSTORAGE exists now"
2019-12-10 20:45:34 +01:00
2020-09-21 21:59:37 +02:00
>& 2 echo "# Creating SubVolume for Snapshots"
sudo mount /dev/${ hdd } 2 /tmp/btrfs 1>/dev/null
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 2 failed' "
exit 1
fi
cd /tmp/btrfs
sudo btrfs subvolume create WORKINGDIR
cd && sudo umount /tmp/btrfs
2021-08-27 03:59:21 -04:00
>& 2 echo "# Creating the FAT32 partition"
2020-09-21 21:59:37 +02:00
sudo parted -s -a optimal -- /dev/${ hdd } mkpart primary fat32 -34GiB 100% 1>/dev/null
sync && sleep 3
win = $( lsblk -o NAME | grep -c ${ hdd } 3)
if [ ${ win } -eq 0 ] ; then
echo "error='partition failed'"
exit 1
fi
2019-12-10 20:45:34 +01:00
2020-09-21 21:59:37 +02:00
>& 2 echo "# Creating Volume BLITZTEMP (format)"
sudo mkfs -t vfat -n BLITZTEMP /dev/${ hdd } 3 1>/dev/null
2020-11-01 19:58:50 +01:00
# check result
loopdone = 0
loopcount = 0
while [ ${ loopdone } -eq 0 ]
do
>& 2 echo "# waiting until formatted drives gets available"
sleep 2
sync
2021-08-17 11:38:26 +02:00
sudo parted -l
2020-11-01 19:58:50 +01:00
loopdone = $( lsblk -o NAME,LABEL | grep -c BLITZTEMP)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 60 ] ; then
>& 2 echo "# ERROR: formatting vfat failed (BLITZTEMP)"
echo "error='formatting failed'"
exit 1
fi
done
2020-09-21 21:59:37 +02:00
>& 2 echo "# OK BLITZTEMP exists now"
2019-12-12 12:45:22 +01:00
2020-09-21 21:59:37 +02:00
>& 2 echo "# OK BTRFS format done"
exit 0
fi
2019-12-12 12:45:22 +01:00
fi
########################################
# Refresh FSTAB for permanent mount
########################################
if [ " $1 " = "fstab" ] ; then
# get device to temp mount
hdd = $2
if [ ${# hdd } -eq 0 ] ; then
2020-09-21 21:59:37 +02:00
echo "# FAIL which device/partition should be temp mounted (e.g. sda)"
echo "# run 'status' to see device candidates"
2019-12-12 12:45:22 +01:00
echo "error='missing second parameter'"
exit 1
fi
2020-09-21 21:59:37 +02:00
# check if exist and which format
2020-10-18 18:22:32 +02:00
# if hdd is a partition (ext4)
if [ [ $hdd = ~ [ 0-9] ] ] ; then
# ext4
hddFormat = $( lsblk -o FSTYPE,NAME | grep ${ hdd } | cut -d ' ' -f 1)
else
# btrfs
hddFormat = $( lsblk -o FSTYPE,NAME | grep ${ hdd } 1 | cut -d ' ' -f 1)
fi
2019-12-12 12:45:22 +01:00
if [ ${# hddFormat } -eq 0 ] ; then
2020-09-21 21:59:37 +02:00
echo "# FAIL given device/partition not found"
2019-12-12 12:45:22 +01:00
echo "error='device not found'"
exit 1
fi
# unmount
if [ ${ isMounted } -eq 1 ] ; then
2020-09-21 21:59:37 +02:00
echo "# unmounting all drives"
sudo umount /mnt/hdd > /dev/null 2>& 1
sudo umount /mnt/storage > /dev/null 2>& 1
sudo umount /mnt/temp > /dev/null 2>& 1
2019-12-12 12:45:22 +01:00
fi
if [ " ${ hddFormat } " = "ext4" ] ; then
### EXT4 ###
2020-09-21 21:59:37 +02:00
hddDataPartitionExt4 = $hdd
2019-12-12 12:45:22 +01:00
# loop until the uuids are available
uuid1 = ""
loopcount = 0
while [ ${# uuid1 } -eq 0 ]
do
2020-09-21 21:59:37 +02:00
echo "# waiting until uuid gets available"
2019-12-12 12:45:22 +01:00
sleep 2
sync
2020-09-21 21:59:37 +02:00
uuid1 = $( lsblk -o NAME,UUID | grep " ${ hddDataPartitionExt4 } " | awk '$1=$1' | cut -d " " -f 2 | grep "-" )
2019-12-12 12:45:22 +01:00
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 10 ] ; then
echo "error='no uuid'"
exit 1
fi
done
# write new /etc/fstab & mount
2020-09-21 21:59:37 +02:00
echo "# mount /mnt/hdd"
2020-07-04 13:00:35 +02:00
sudo mkdir -p /mnt/hdd 1>/dev/null
2020-09-21 21:59:37 +02:00
updated = $( cat /etc/fstab | grep -c "/mnt/hdd" )
if [ $updated -eq 0 ] ; then
echo "# updating /etc/fstab"
sudo sed " /raspiblitz/ i UUID= ${ uuid1 } /mnt/hdd ext4 noexec,defaults 0 2 " -i /etc/fstab 1>/dev/null
fi
2019-12-10 20:45:34 +01:00
sync
2019-12-12 12:45:22 +01:00
sudo mount -a 1>/dev/null
# loop mounts are available
mountactive1 = 0
loopcount = 0
while [ ${ mountactive1 } -eq 0 ]
do
2020-09-21 21:59:37 +02:00
echo "# waiting until mounting is active"
2019-12-12 12:45:22 +01:00
sleep 2
sync
mountactive1 = $( df | grep -c /mnt/hdd)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 10 ] ; then
2020-09-21 21:59:37 +02:00
echo "# WARNING was not able freshly mount new devices - might need reboot or check /etc/fstab"
2019-12-12 12:45:22 +01:00
echo "needsReboot=1"
exit 0
fi
done
2020-09-21 21:59:37 +02:00
echo "# OK - fstab updated for EXT4 layout"
2019-12-13 23:26:32 +01:00
exit 1
2019-12-12 12:45:22 +01:00
elif [ " ${ hddFormat } " = "btrfs" ] ; then
### BTRFS ###
2019-12-13 16:38:59 +01:00
>& 2 echo "# BTRFS: Updating /etc/fstab & mount"
2019-12-12 12:45:22 +01:00
# get info on: Data Drive
uuidDATA = $( lsblk -o UUID,NAME,LABEL | grep " ${ hdd } " | grep "BLITZDATA" | cut -d " " -f 1 | grep "-" )
sudo mkdir -p /tmp/btrfs
sudo mount /dev/${ hdd } 1 /tmp/btrfs 1>/dev/null
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 1 failed' "
exit 1
fi
cd /tmp/btrfs
subVolDATA = $( sudo btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && sudo umount /tmp/btrfs
echo " uuidDATA=' ${ uuidDATA } ' "
echo " subVolDATA=' ${ subVolDATA } ' "
if [ ${# uuidDATA } -eq 0 ] || [ ${# subVolDATA } -eq 0 ] ; then
echo "error='no datadrive uuids'"
exit 1
fi
# get info on: Storage Drive
uuidSTORAGE = $( lsblk -o UUID,NAME,LABEL | grep " ${ hdd } " | grep "BLITZSTORAGE" | cut -d " " -f 1 | grep "-" )
sudo mount /dev/${ hdd } 2 /tmp/btrfs 1>/dev/null
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 2 failed' "
exit 1
fi
cd /tmp/btrfs
subVolSTORAGE = $( sudo btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && sudo umount /tmp/btrfs
echo " uuidSTORAGE=' ${ uuidSTORAGE } ' "
echo " subVolSTORAGE=' ${ subVolSTORAGE } ' "
if [ ${# uuidSTORAGE } -eq 0 ] || [ ${# subVolSTORAGE } -eq 0 ] ; then
echo "error='no storagedrive uuids'"
2019-12-10 20:45:34 +01:00
exit 1
fi
2019-02-25 04:46:32 +01:00
2019-12-12 12:45:22 +01:00
# get info on: Temp Drive
uuidTEMP = $( lsblk -o LABEL,UUID | grep "BLITZTEMP" | awk '$1=$1' | cut -d " " -f 2 | grep "-" )
echo " uuidTEMP=' ${ uuidTEMP } ' "
if [ ${# uuidTEMP } -eq 0 ] ; then
echo "error='no tempdrive uuids'"
exit 1
fi
# remove old entries from fstab
2020-09-23 17:15:09 +02:00
lsblk -o UUID,NAME | grep " ${ hdd } " | cut -d " " -f 1 | grep "-" | while read -r uuid ; do
2019-12-13 16:38:59 +01:00
>& 2 echo " # Cleaning /etc/fstab from ${ uuid } "
2019-12-12 12:45:22 +01:00
sudo sed -i " /UUID= ${ uuid } /d " /etc/fstab
sync
done
2021-08-27 03:59:21 -04:00
# get user and groupid if usr/group bitcoin
2019-12-15 19:15:47 +01:00
bitcoinUID = $( id -u bitcoin)
bitcoinGID = $( id -g bitcoin)
2019-12-12 12:45:22 +01:00
# modifying /etc/fstab & mount
sudo mkdir -p /mnt/hdd 1>/dev/null
fstabAdd1 = " UUID= ${ uuidDATA } /mnt/hdd btrfs noexec,defaults,subvolid= ${ subVolDATA } 0 2 "
sudo sed " 3 a ${ fstabAdd1 } " -i /etc/fstab 1>/dev/null
sudo mkdir -p /mnt/storage 1>/dev/null
fstabAdd2 = " UUID= ${ uuidSTORAGE } /mnt/storage btrfs noexec,defaults,subvolid= ${ subVolSTORAGE } 0 2 "
sudo sed " 4 a ${ fstabAdd2 } " -i /etc/fstab 1>/dev/null
sudo mkdir -p /mnt/temp 1>/dev/null
2019-12-15 19:15:47 +01:00
fstabAdd3 = " UUID= ${ uuidTEMP } /mnt/temp vfat noexec,defaults,uid= ${ bitcoinUID } ,gid= ${ bitcoinGID } 0 2 "
2019-12-12 12:45:22 +01:00
sudo sed " 5 a ${ fstabAdd3 } " -i /etc/fstab 1>/dev/null
sync && sudo mount -a 1>/dev/null
# test mount
mountactive1 = 0
mountactive2 = 0
mountactive3 = 0
loopcount = 0
while [ ${ mountactive1 } -eq 0 ] || [ ${ mountactive2 } -eq 0 ] || [ ${ mountactive3 } -eq 0 ]
do
2019-12-13 16:38:59 +01:00
>& 2 echo "# waiting until mountings are active"
2019-12-12 12:45:22 +01:00
sleep 2
sync
mountactive1 = $( df | grep -c /mnt/hdd)
mountactive2 = $( df | grep -c /mnt/temp)
mountactive3 = $( df | grep -c /mnt/storage)
loopcount = $(( $loopcount + 1 ))
if [ ${ loopcount } -gt 10 ] ; then
2020-09-21 21:59:37 +02:00
>& 2 echo "# WARNING was not able freshly mount new devices - might need reboot or check /etc/fstab"
2019-12-12 12:45:22 +01:00
echo "needsReboot=1"
exit 1
fi
done
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - fstab updated for BTRFS layout"
2019-12-13 23:26:32 +01:00
exit 1
2019-12-12 12:45:22 +01:00
else
echo "error='wrong hdd format'"
exit 1
fi
2019-12-10 20:45:34 +01:00
fi
########################################
# RAID with USB Stick for data partition
########################################
if [ " $1 " = "raid" ] ; then
# checking if BTRFS mode
if [ ${ isBTRFS } -eq 0 ] ; then
echo "error='raid only BTRFS'"
2019-02-25 05:33:52 +01:00
exit 1
fi
2019-02-25 04:46:32 +01:00
2019-12-10 20:45:34 +01:00
# checking parameter
if [ " $2 " = "on" ] ; then
if [ ${ isRaid } -eq 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - already ON"
2019-12-10 20:45:34 +01:00
exit
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# RAID - Adding raid drive to RaspiBlitz data drive"
2019-12-10 20:45:34 +01:00
elif [ " $2 " = "off" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# RAID - Removing raid drive to RaspiBlitz data drive"
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# possible 2nd parameter is 'on' or 'off'"
2021-08-27 03:59:21 -04:00
echo "error='unknown parameter'"
2019-12-10 20:45:34 +01:00
exit 1
fi
fi
# RAID --> ON
if [ " $1 " = "raid" ] && [ " $2 " = "on" ] ; then
# todo - how to determine which device is the usb raid to add
# maybe give all options with "status" and have this as
# second parameter - like its named: lsblk
usbdev = $3
if [ ${# usbdev } -eq 0 ] ; then
2021-08-27 03:59:21 -04:00
>& 2 echo "# FAIL third parameter is missing with the name of the USB device to add"
2019-12-10 20:45:34 +01:00
echo "error='missing parameter'"
exit 1
fi
# check that dev exists and is unique
usbdevexists = $( lsblk -o NAME,UUID | grep -c " ^ ${ usbdev } " )
if [ ${ usbdevexists } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo " # FAIL not found: ${ usbdev } "
2019-12-10 20:45:34 +01:00
echo "error='dev not found'"
exit 1
fi
if [ ${ usbdevexists } -gt 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo " # FAIL multiple matches: ${ usbdev } "
2019-12-10 20:45:34 +01:00
echo "error='dev not unique'"
exit 1
fi
# check that device is a disk and not a partition
isDisk = $( lsblk -o NAME,TYPE | grep " ^ ${ usbdev } " | grep -c disk)
if [ ${ isDisk } -eq 0 ] ; then
echo "error='dev is not disk'"
2019-02-25 05:33:52 +01:00
exit 1
fi
2019-02-25 04:46:32 +01:00
2019-12-10 20:45:34 +01:00
# check that device is not mounted
if [ $( df | cut -d " " -f 1 | grep -c " / ${ usbdev } " ) -gt 0 ] ; then
echo "error='dev is in use'"
exit 1
fi
# check if old BTRFS filesystem
usbdevBTRFS = $( lsblk -o NAME,UUID,FSTYPE | grep " ^ ${ usbdev } " | grep -c "btrfs" )
if [ ${ usbdevBTRFS } -eq 1 ] ; then
# edge case: already contains BTRFS data
2019-02-25 05:33:52 +01:00
# TODO: once implemented -> also make sure that dev1 is named "DATASTORE" and if 2nd is other -> format and add as raid
2019-12-13 16:38:59 +01:00
>& 2 echo "# ERROR: !! NOT IMPLEMENTED YET -> devices seem contain old data"
>& 2 echo "# if you dont care about that data: format on other computer with FAT"
2019-12-10 20:45:34 +01:00
echo "error='old data on dev'"
exit 1
fi
2021-08-27 03:59:21 -04:00
# remove all partitions from device
2019-12-10 20:45:34 +01:00
for v_partition in $( parted -s /dev/${ usbdev } print| awk '/^ / {print $1}' )
do
sudo parted -s /dev/${ usbdev } rm ${ v_partition }
done
2020-10-18 18:22:32 +02:00
# check if usb device is at least 30GB big
2019-12-10 20:45:34 +01:00
usbdevsize = $( lsblk -o NAME,SIZE -b | grep " ^ ${ usbdev } " | awk '$1=$1' | cut -d " " -f 2)
if [ ${ usbdevsize } -lt 30000000000 ] ; then
2020-10-18 18:22:32 +02:00
>& 2 echo " # FAIL ${ usbdev } is smaller than the minimum 30GB "
2019-12-10 20:45:34 +01:00
echo "error='dev too small'"
exit 1
fi
# add usb device as raid for data
2019-12-13 16:38:59 +01:00
>& 2 echo " # adding ${ usbdev } as BTRFS raid1 for /mnt/hdd "
2019-12-10 20:45:34 +01:00
sudo btrfs device add -f /dev/${ usbdev } /mnt/hdd 1>/dev/null
sudo btrfs filesystem balance start -dconvert= raid1 -mconvert= raid1 /mnt/hdd 1>/dev/null
2019-12-13 16:38:59 +01:00
>& 2 echo " # OK - ${ usbdev } is now part of a RAID1 for your RaspiBlitz data "
2019-02-25 05:33:52 +01:00
exit 0
2019-02-25 00:31:02 +01:00
2019-12-10 20:45:34 +01:00
fi
# RAID --> OFF
if [ " $1 " = "raid" ] && [ " $2 " = "off" ] ; then
# checking if BTRFS mode
2020-01-15 18:23:55 +01:00
isBTRFS = $( sudo btrfs filesystem show 2>/dev/null| grep -c 'BLITZSTORAGE' )
2019-12-10 20:45:34 +01:00
if [ ${ isBTRFS } -eq 0 ] ; then
echo "error='raid only BTRFS'"
exit 1
fi
2020-01-15 11:48:41 +01:00
deviceToBeRemoved = " /dev/ ${ raidUsbDev } "
# just in case be able to remove missing drive
if [ ${# raidUsbDev } -eq 0 ] ; then
deviceToBeRemoved = "missing"
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# removing USB DEV from RAID"
2019-12-10 20:45:34 +01:00
sudo btrfs balance start -mconvert= dup -dconvert= single /mnt/hdd 1>/dev/null
2020-01-15 11:48:41 +01:00
sudo btrfs device remove ${ deviceToBeRemoved } /mnt/hdd 1>/dev/null
2019-12-10 20:45:34 +01:00
2020-01-15 12:05:51 +01:00
isRaid = $( btrfs filesystem df /mnt/hdd 2>/dev/null | grep -c "Data, RAID1" )
if [ ${ isRaid } -eq 0 ] ; then
>& 2 echo "# OK - RaspiBlitz data is not running in RAID1 anymore"
exit 0
else
>& 2 echo "# FAIL - was not able to remove RAID device"
echo "error='fail'"
exit 1
fi
2019-02-25 05:33:52 +01:00
fi
2019-02-25 00:31:02 +01:00
2019-12-10 20:45:34 +01:00
########################################
# SNAPSHOTS - make and replay
########################################
if [ " $1 " = "snapshot" ] ; then
# check if data drive is mounted
if [ ${ isMounted } -eq 0 ] ; then
echo "error='no data drive mounted'"
exit 1
fi
# check if BTRFS
if [ ${ isBTRFS } -eq 0 ] ; then
echo "error='no BTRFS'"
exit 1
fi
# SECOND PARAMETER: 'data' or 'storage'
if [ " $2 " = "data" ] ; then
subvolume = "/mnt/hdd"
subvolumeESC = "\/mnt\/hdd"
uuid = $( lsblk -o LABEL,UUID | grep "BLITZDATA" | awk '$1=$1' | cut -d " " -f 2 | grep "-" )
elif [ " $2 " = "storage" ] ; then
subvolume = "/mnt/storage"
subvolumeESC = "\/mnt\/storage"
uuid = $( lsblk -o LABEL,UUID | grep "BLITZSTORAGE" | awk '$1=$1' | cut -d " " -f 2 | grep "-" )
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# second parameter needs to be 'data' or 'storage'"
2019-12-10 20:45:34 +01:00
echo "error='unknown parameter'"
exit 1
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# RASPIBLITZ SNAPSHOTS"
2019-12-10 20:45:34 +01:00
partition = $( df | grep " ${ subvolume } " | cut -d " " -f 1)
echo " subvolume=' ${ subvolume } ' "
echo " partition=' ${ partition } ' "
if [ " $3 " = "create" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Preparing Snapshot ..."
2019-12-10 20:45:34 +01:00
# make sure backup folder exists
sudo mkdir -p ${ subvolume } /snapshots
# delete old backup if existing
oldBackupExists = $( sudo ls ${ subvolume } /snapshots | grep -c backup)
if [ ${ oldBackupExists } -gt 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Deleting old snapshot"
2019-12-10 20:45:34 +01:00
sudo btrfs subvolume delete ${ subvolume } /snapshots/backup 1>/dev/null
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating Snapshot ..."
2019-12-10 20:45:34 +01:00
sudo btrfs subvolume snapshot ${ subvolume } ${ subvolume } /snapshots/backup 1>/dev/null
if [ $( sudo btrfs subvolume list ${ subvolume } | grep -c snapshots/backup) -eq 0 ] ; then
echo "error='not created'"
exit 1
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - Snapshot created"
2019-12-10 20:45:34 +01:00
exit 0
fi
2019-02-25 00:31:02 +01:00
2019-12-10 20:45:34 +01:00
elif [ " $3 " = "rollback" ] ; then
2019-02-25 05:33:52 +01:00
2019-12-10 20:45:34 +01:00
# check if an old snapshot exists
oldBackupExists = $( sudo ls ${ subvolume } /snapshots | grep -c backup)
if [ ${ oldBackupExists } -eq 0 ] ; then
echo "error='no old snapshot found'"
exit 1
fi
2019-02-25 05:40:13 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# Resetting state to old Snapshot ..."
2019-12-10 20:45:34 +01:00
sudo umount ${ subvolume }
sudo mkdir -p /tmp/btrfs 1>/dev/null
sudo mount ${ partition } /tmp/btrfs
sudo mv /tmp/btrfs/WORKINGDIR/snapshots/backup /tmp/btrfs/backup
sudo btrfs subvolume delete /tmp/btrfs/WORKINGDIR
sudo mv /tmp/btrfs/backup /tmp/btrfs/WORKINGDIR
subVolID = $( sudo btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
sudo sed -i " / ${ subvolumeESC } /d " /etc/fstab
fstabAdd = " UUID= ${ uuid } ${ subvolume } btrfs noexec,defaults,subvolid= ${ subVolID } 0 2 "
sudo sed " 4 a ${ fstabAdd } " -i /etc/fstab 1>/dev/null
sudo umount /tmp/btrfs
sudo mount -a
sync
if [ $( df | grep -c " ${ subvolume } " ) -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# check drive setting ... rollback seemed to "
2019-12-10 20:45:34 +01:00
echo "error='failed rollback'"
exit 1
fi
echo "OK - Rollback done"
exit 0
2019-02-25 05:33:52 +01:00
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# third parameter needs to be 'create' or 'rollback'"
2019-12-10 20:45:34 +01:00
echo "error='unknown parameter'"
exit 1
fi
fi
2019-12-11 15:08:08 +01:00
###################
# TEMP MOUNT
###################
if [ " $1 " = "tempmount" ] ; then
2021-04-15 19:51:29 +02:00
2021-05-23 10:41:06 -05:00
# get HDD status and candidates
source <( /home/admin/config.scripts/blitz.datadrive.sh status)
2019-12-11 15:08:08 +01:00
if [ ${ isMounted } -eq 1 ] ; then
echo "error='already mounted'"
exit 1
fi
2021-05-23 10:37:57 -05:00
# get device to temp mount from parameter (optional)
2020-10-18 18:22:32 +02:00
hdd = $2
2021-05-23 10:37:57 -05:00
# automount if no parameter the hddcandinate
if [ " ${ hdd } " = = "" ] ; then
if [ " ${ hddFormat } " != "btrfs" ] ; then
hdd = " ${ hddPartitionCandidate } "
else
hdd = " ${ hddCandidate } "
fi
fi
# if still no hdd .. throw error
if [ " ${ hdd } " = = "" ] ; then
>& 2 echo "# FAIL there is no detected hdd candidate to tempmount"
echo "error='hdd not found'"
2019-12-11 16:19:03 +01:00
exit 1
fi
2020-10-18 18:22:32 +02:00
# if hdd is a partition
if [ [ $hdd = ~ [ 0-9] ] ] ; then
hddDataPartition = $hdd
hddDataPartitionExt4 = $hddDataPartition
hddFormat = $( lsblk -o FSTYPE,NAME | grep ${ hddDataPartitionExt4 } | cut -d ' ' -f 1)
else
hddBTRFS = $hdd
hddFormat = $( lsblk -o FSTYPE,NAME | grep ${ hddBTRFS } 1 | cut -d ' ' -f 1)
fi
2019-12-11 16:19:03 +01:00
if [ ${# hddFormat } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# FAIL given device not found"
2019-12-11 16:19:03 +01:00
echo "error='device not found'"
2019-12-11 15:08:08 +01:00
exit 1
fi
if [ " ${ hddFormat } " = "ext4" ] ; then
2021-04-15 19:51:29 +02:00
if [ " ${ hddDataPartitionExt4 } " = = "" ] ; then
echo "error='parameter is no partition'"
exit 1
fi
2019-12-11 15:08:08 +01:00
# do EXT4 temp mount
2021-04-15 19:51:29 +02:00
echo " # temp mount /dev/ ${ hddDataPartitionExt4 } --> /mnt/hdd "
2019-12-11 15:08:08 +01:00
sudo mkdir -p /mnt/hdd 1>/dev/null
2020-09-21 21:59:37 +02:00
sudo mount /dev/${ hddDataPartitionExt4 } /mnt/hdd
2019-12-11 15:08:08 +01:00
# check result
isMounted = $( df | grep -c "/mnt/hdd" )
2019-12-11 15:58:58 +01:00
if [ ${ isMounted } -eq 0 ] ; then
2019-12-11 15:08:08 +01:00
echo "error='temp mount failed'"
2019-12-11 16:33:53 +01:00
exit 1
2019-12-11 15:08:08 +01:00
else
2019-12-11 17:25:28 +01:00
isMounted = 1
isBTRFS = 0
2019-12-11 15:08:08 +01:00
fi
elif [ " ${ hddFormat } " = "btrfs" ] ; then
2021-08-27 03:59:21 -04:00
# get user and groupid if usr/group bitcoin
2019-12-15 19:25:29 +01:00
bitcoinUID = $( id -u bitcoin)
bitcoinGID = $( id -g bitcoin)
2019-12-11 16:33:53 +01:00
# do BTRFS temp mount
2019-12-11 15:08:08 +01:00
sudo mkdir -p /mnt/hdd 1>/dev/null
sudo mkdir -p /mnt/storage 1>/dev/null
sudo mkdir -p /mnt/temp 1>/dev/null
2020-10-18 18:22:32 +02:00
sudo mount -t btrfs -o degraded -o subvol = WORKINGDIR /dev/${ hddBTRFS } 1 /mnt/hdd
sudo mount -t btrfs -o subvol = WORKINGDIR /dev/${ hddBTRFS } 2 /mnt/storage
2020-11-30 14:14:49 +01:00
sudo mount -o umask = 0000,uid= ${ bitcoinUID } ,gid= ${ bitcoinGID } /dev/${ hddBTRFS } 3 /mnt/temp
2019-12-11 15:08:08 +01:00
# check result
2019-12-11 15:58:58 +01:00
isMountedA = $( df | grep -c "/mnt/hdd" )
isMountedB = $( df | grep -c "/mnt/storage" )
isMountedC = $( df | grep -c "/mnt/temp" )
if [ ${ isMountedA } -eq 0 ] && [ ${ isMountedB } -eq 0 ] && [ ${ isMountedC } -eq 0 ] ; then
2019-12-11 15:08:08 +01:00
echo "error='temp mount failed'"
2019-12-11 16:33:53 +01:00
exit 1
2019-12-11 15:08:08 +01:00
else
2019-12-11 17:25:28 +01:00
isMounted = 1
isBTRFS = 1
2019-12-11 15:08:08 +01:00
fi
else
echo "error='no supported hdd format'"
exit 1
fi
2019-12-11 17:25:28 +01:00
# outputting change state
echo " isMounted= ${ isMounted } "
echo " isBTRFS= ${ isBTRFS } "
2019-12-13 18:33:25 +01:00
exit 1
2019-12-11 17:25:28 +01:00
2019-12-11 15:08:08 +01:00
fi
2021-05-23 10:37:57 -05:00
if [ " $1 " = "unmount" ] ; then
sudo umount /mnt/hdd 2>/dev/null
sudo umount /mnt/storage 2>/dev/null
sudo umount /mnt/temp 2>/dev/null
echo "# OK done unmount"
exit 1
fi
2019-12-10 20:45:34 +01:00
########################################
# LINKING all directories with ln
########################################
2019-12-12 12:45:22 +01:00
if [ " $1 " = "link" ] ; then
2019-12-10 20:45:34 +01:00
2019-12-11 17:25:28 +01:00
if [ ${ isMounted } -eq 0 ] ; then
2019-12-10 20:45:34 +01:00
echo "error='no data drive mounted'"
exit 1
fi
2019-12-11 18:18:16 +01:00
# cleanups
if [ $( sudo ls -la /home/bitcoin/ | grep -c "bitcoin ->" ) -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# - /home/bitcoin/.bitcoin -> is not a link, cleaning"
2019-12-11 18:18:16 +01:00
sudo rm -r /home/bitcoin/.bitcoin 2>/dev/null
else
sudo rm /home/bitcoin/.bitcoin 2>/dev/null
fi
if [ $( sudo ls -la /home/bitcoin/ | grep -c "litecoin ->" ) -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# - /home/bitcoin/.litecoin -> is not a link, cleaning"
2019-12-11 18:18:16 +01:00
sudo rm -r /home/bitcoin/.litecoin 2>/dev/null
else
sudo rm /home/bitcoin/.litecoin 2>/dev/null
fi
2019-12-15 12:53:42 +01:00
# make sure common base directory exits
sudo mkdir -p /mnt/hdd/lnd
sudo mkdir -p /mnt/hdd/app-data
2019-12-13 19:21:59 +01:00
2019-12-10 20:45:34 +01:00
if [ ${ isBTRFS } -eq 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating BTRFS setup links"
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking blockchains into /mnt/hdd"
2020-11-10 23:40:54 +01:00
if [ $( ls -F /mnt/hdd/bitcoin | grep -c '/mnt/hdd/bitcoin@' ) -eq 0 ] ; then
2019-12-11 17:44:07 +01:00
sudo mkdir -p /mnt/storage/bitcoin
2020-11-10 23:40:54 +01:00
sudo cp -R /mnt/hdd/bitcoin/* /mnt/storage/bitcoin 2>/dev/null
2019-12-11 17:44:07 +01:00
sudo chown -R bitcoin:bitcoin /mnt/storage/bitcoin
2020-11-10 23:40:54 +01:00
sudo rm -r /mnt/hdd/bitcoin
2019-12-11 17:44:07 +01:00
sudo ln -s /mnt/storage/bitcoin /mnt/hdd/bitcoin
2019-12-11 18:18:16 +01:00
sudo rm /mnt/storage/bitcoin/bitcoin 2>/dev/null
2019-12-11 17:44:07 +01:00
fi
2020-11-10 23:40:54 +01:00
if [ $( ls -F /mnt/hdd/litecoin | grep -c '/mnt/hdd/litecoin@' ) -eq 0 ] ; then
2019-12-11 17:44:07 +01:00
sudo mkdir -p /mnt/storage/litecoin
2020-11-10 23:40:54 +01:00
sudo cp -R /mnt/hdd/litecoin/* /mnt/storage/litecoin 2>/dev/null
2019-12-11 17:44:07 +01:00
sudo chown -R bitcoin:bitcoin /mnt/storage/litecoin
2020-11-10 23:40:54 +01:00
sudo rm -r /mnt/hdd/litecoin
2019-12-11 17:44:07 +01:00
sudo ln -s /mnt/storage/litecoin /mnt/hdd/litecoin
2019-12-11 18:18:16 +01:00
sudo rm /mnt/storage/litecoin/litecoin 2>/dev/null
2019-12-11 17:44:07 +01:00
fi
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# linking lnd for user bitcoin"
2019-12-12 14:50:11 +01:00
sudo rm /home/bitcoin/.lnd 2>/dev/null
sudo ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking blockchain for user bitcoin"
2019-12-10 20:45:34 +01:00
sudo ln -s /mnt/storage/bitcoin /home/bitcoin/.bitcoin
sudo ln -s /mnt/storage/litecoin /home/bitcoin/.litecoin
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking storage into /mnt/hdd"
2019-12-10 20:45:34 +01:00
sudo mkdir -p /mnt/storage/app-storage
sudo chown -R bitcoin:bitcoin /mnt/storage/app-storage
2019-12-11 17:14:08 +01:00
sudo rm /mnt/hdd/app-storage 2>/dev/null
2019-12-10 20:45:34 +01:00
sudo ln -s /mnt/storage/app-storage /mnt/hdd/app-storage
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking temp into /mnt/hdd"
2019-12-11 17:14:08 +01:00
sudo rm /mnt/hdd/temp 2>/dev/null
2019-12-10 20:45:34 +01:00
sudo ln -s /mnt/temp /mnt/hdd/temp
2019-12-15 19:15:47 +01:00
sudo chown -R bitcoin:bitcoin /mnt/temp
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - creating snapshots folder"
2019-12-11 17:44:07 +01:00
sudo mkdir -p /mnt/hdd/snapshots
sudo mkdir -p /mnt/storage/snapshots
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating EXT4 setup links"
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# opening blockchain into /mnt/hdd"
2019-12-10 20:45:34 +01:00
sudo mkdir -p /mnt/hdd/bitcoin
sudo mkdir -p /mnt/hdd/litecoin
2019-12-13 16:38:59 +01:00
>& 2 echo "# linking blockchain for user bitcoin"
2019-12-10 20:45:34 +01:00
sudo rm /home/bitcoin/.bitcoin 2>/dev/null
sudo ln -s /mnt/hdd/bitcoin /home/bitcoin/.bitcoin
2019-12-15 12:53:42 +01:00
2019-12-10 20:45:34 +01:00
sudo rm /home/bitcoin/.litecoin 2>/dev/null
sudo ln -s /mnt/hdd/litecoin /home/bitcoin/.litecoin
2019-12-13 16:38:59 +01:00
>& 2 echo "# linking lnd for user bitcoin"
2019-12-12 14:50:11 +01:00
sudo rm /home/bitcoin/.lnd 2>/dev/null
sudo ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
2019-12-15 12:53:42 +01:00
>& 2 echo "# creating default storage & temp folders"
2019-12-10 20:45:34 +01:00
sudo mkdir -p /mnt/hdd/app-storage
sudo mkdir -p /mnt/hdd/temp
2019-12-15 01:34:51 +01:00
2019-12-10 20:45:34 +01:00
fi
2019-12-12 14:50:11 +01:00
# fix ownership of linked files
2019-12-15 12:53:42 +01:00
sudo chown -R bitcoin:bitcoin /mnt/hdd/bitcoin
sudo chown -R bitcoin:bitcoin /mnt/hdd/litecoin
sudo chown -R bitcoin:bitcoin /mnt/hdd/lnd
sudo chown -R bitcoin:bitcoin /home/bitcoin/.lnd
sudo chown -R bitcoin:bitcoin /home/bitcoin/.litecoin
sudo chown -R bitcoin:bitcoin /home/bitcoin/.bitcoin
sudo chown -R bitcoin:bitcoin /mnt/hdd/app-storage
sudo chown -R bitcoin:bitcoin /mnt/hdd/app-data
sudo chown -R bitcoin:bitcoin /mnt/hdd/temp
2020-11-30 14:14:49 +01:00
sudo chmod -R 777 /mnt/temp
sudo chmod -R 777 /mnt/hdd/temp
sudo chmod 777 /mnt
2019-12-12 14:50:11 +01:00
2019-12-15 12:53:42 +01:00
# write info files about what directories are for
2020-04-28 15:31:00 +02:00
echo "The /mnt/hdd/temp directory is for short time data and will get cleaned up on very start. Dont work with data here thats bigger then 25GB - because on BTRFS hdd layout this is a own partition with limited space. Also on BTRFS hdd layout the temp partition is an FAT format - so it can be easily mounted on Windows and OSx laptops by just connecting it to such laptops. Use this for easy export data. To import data make sure to work with the data before bootstrap is deleting the directory on startup." > ./README.txt
2019-12-15 19:29:16 +01:00
sudo mv ./README.txt /mnt/hdd/temp/README.txt 2>/dev/null
2019-12-15 12:53:42 +01:00
2020-08-24 15:04:40 +02:00
echo "The /mnt/hdd/app-data directory should be used by additional/optional apps and services installed to the RaspiBlitz for their data that should survive an import/export/backup. Data that can be reproduced (indexes, etc.) should be stored in app-storage." > ./README.txt
2019-12-15 19:29:16 +01:00
sudo mv ./README.txt /mnt/hdd/app-data/README.txt 2>/dev/null
2019-12-15 12:53:42 +01:00
2021-08-27 03:59:21 -04:00
echo "The /mnt/hdd/app-storage directory should be used by additional/optional apps and services installed to the RaspiBlitz for their non-critical and reproducible data (indexes, public blockchain, etc.) that does not need to survive an an import/export/backup. Data is critical should be in app-data." > ./README.txt
2019-12-15 19:29:16 +01:00
sudo mv ./README.txt /mnt/hdd/app-storage/README.txt 2>/dev/null
2019-12-15 12:53:42 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - all symbolic links build"
2019-02-25 05:33:52 +01:00
exit 0
fi
2019-12-10 20:45:34 +01:00
########################################
# SWAP on data drive
########################################
if [ " $1 " = "swap" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# RASPIBLITZ DATA DRIVES - SWAP FILE"
2019-12-10 20:45:34 +01:00
if [ ${ isMounted } -eq 0 ] ; then
echo "error='no data drive mounted'"
exit 1
fi
if [ " $2 " = "on" ] ; then
if [ ${ isSwapExternal } -eq 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - already ON"
2019-12-10 20:45:34 +01:00
exit 1
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Switch off/uninstall old SWAP"
2019-12-10 20:45:34 +01:00
sudo dphys-swapfile swapoff 1>/dev/null
sudo dphys-swapfile uninstall 1>/dev/null
if [ ${ isBTRFS } -eq 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite external SWAP config for BTRFS setup"
2019-12-10 20:45:34 +01:00
sudo sed -i "12s/.*/CONF_SWAPFILE=\/mnt\/temp\/swapfile/" /etc/dphys-swapfile
sudo sed -i "16s/.*/#CONF_SWAPSIZE=/" /etc/dphys-swapfile
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite external SWAP config for EXT4 setup"
2019-12-10 20:45:34 +01:00
sudo sed -i "12s/.*/CONF_SWAPFILE=\/mnt\/hdd\/swapfile/" /etc/dphys-swapfile
sudo sed -i "16s/.*/#CONF_SWAPSIZE=/" /etc/dphys-swapfile
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating SWAP file .."
2019-12-10 20:45:34 +01:00
sudo dd if = /dev/zero of = $externalSwapPath count = 2048 bs = 1MiB 1>/dev/null
sudo chmod 0600 $externalSwapPath 1>/dev/null
2019-12-13 16:38:59 +01:00
>& 2 echo "# Activating new SWAP"
2019-12-10 20:45:34 +01:00
sudo mkswap $externalSwapPath
sudo dphys-swapfile setup
sudo dphys-swapfile swapon
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - Swap is now ON external"
2019-12-10 20:45:34 +01:00
exit 0
elif [ " $2 " = "off" ] ; then
if [ ${ isSwapExternal } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - already OFF"
2019-12-10 20:45:34 +01:00
exit 1
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Switch off/uninstall old SWAP"
2019-12-10 20:45:34 +01:00
sudo dphys-swapfile swapoff 1>/dev/null
sudo dphys-swapfile uninstall 1>/dev/null
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite SWAP config"
2019-12-10 20:45:34 +01:00
sudo sed -i "12s/.*/CONF_SWAPFILE=\/var\/swap/" /etc/dphys-swapfile
sudo sed -i "16s/.*/#CONF_SWAPSIZE=/" /etc/dphys-swapfile
sudo dd if = /dev/zero of = /var/swap count = 256 bs = 1MiB 1>/dev/null
sudo chmod 0600 /var/swap
2019-12-13 16:38:59 +01:00
>& 2 echo "# Create and switch on new SWAP"
2019-12-10 20:45:34 +01:00
sudo mkswap /var/swap 1>/dev/null
sudo dphys-swapfile setup 1>/dev/null
sudo dphys-swapfile swapon 1>/dev/null
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK - Swap is now OFF external"
2019-12-10 20:45:34 +01:00
exit 0
else
2021-08-27 03:59:21 -04:00
>& 2 echo "# FAIL unknown second parameter - try 'on' or 'off'"
echo "error='unknown parameter'"
2019-12-10 20:45:34 +01:00
exit 1
fi
fi
########################################
# CLEAN data drives
########################################
if [ " $1 " = "clean" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# RASPIBLITZ DATA DRIVES - CLEANING"
2019-12-10 20:45:34 +01:00
2021-05-23 10:53:37 -05:00
# get HDD status
source <( /home/admin/config.scripts/blitz.datadrive.sh status)
2019-12-11 04:33:58 +01:00
if [ ${ isMounted } -eq 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# FAIL: cannot clean - the drive is not mounted'"
2019-12-11 04:33:58 +01:00
echo "error='not mounted'"
exit 1
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Making sure 'secure-delete' is installed ..."
2019-12-10 20:45:34 +01:00
sudo apt-get install -y secure-delete 1>/dev/null
2019-12-15 18:07:00 +01:00
>& 2 echo
2021-05-23 10:53:37 -05:00
>& 2 echo "# IMPORTANT: No 100% guarantee that sensitive data is completely deleted!"
# see: https://www.davescomputers.com/securely-deleting-files-solid-state-drive/"
# see: https://unix.stackexchange.com/questions/62345/securely-delete-files-on-btrfs-filesystem"
2019-12-15 18:05:12 +01:00
>& 2 echo "# --> Dont resell or gift data drive. Destroy physically if needed."
2019-12-15 18:07:00 +01:00
>& 2 echo
2019-12-10 20:45:34 +01:00
# DELETE ALL DATA (with option to keep blockchain)
if [ " $2 " = "all" ] ; then
if [ " $3 " = "-total" ] || [ " $3 " = "-keepblockchain" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Deleting personal Data .."
2019-12-10 20:45:34 +01:00
# make sure swap is off
sudo dphys-swapfile swapoff 1>/dev/null
sudo dphys-swapfile uninstall 1>/dev/null
sync
2021-08-27 03:59:21 -04:00
# for all other data shred files selectively
2019-12-10 20:45:34 +01:00
for entry in $( ls -A1 /mnt/hdd)
do
delete = 1
2019-12-15 16:48:10 +01:00
whenDeleteSchredd = 1
2019-12-15 18:05:12 +01:00
# deactivate delete if a blockchain directory (if -keepblockchain)
2019-12-10 20:45:34 +01:00
if [ " $3 " = "-keepblockchain" ] ; then
2019-12-15 18:05:12 +01:00
if [ " ${ entry } " = "bitcoin" ] || [ " ${ entry } " = "litecoin" ] ; then
delete = 0
2019-12-10 20:45:34 +01:00
fi
2019-12-15 16:48:10 +01:00
fi
2019-12-15 18:05:12 +01:00
2021-08-27 03:59:21 -04:00
# decide when to shred or just delete - just delete nonsensitive data
2019-12-15 18:05:12 +01:00
if [ " ${ entry } " = "torrent" ] || [ " ${ entry } " = "app-storage" ] ; then
whenDeleteSchredd = 0
fi
2019-12-15 16:48:10 +01:00
if [ " ${ entry } " = "bitcoin" ] || [ " ${ entry } " = "litecoin" ] ; then
2019-12-15 18:05:12 +01:00
whenDeleteSchredd = 0
fi
# if BTRFS just shred stuff in /mnt/hdd/temp (because thats EXT4)
if [ ${ isBTRFS } -eq 1 ] && [ " ${ entry } " != "temp" ] ; then
whenDeleteSchredd = 0
fi
2021-08-27 03:59:21 -04:00
# on SSDs never shred
2019-12-15 18:05:12 +01:00
# https://www.davescomputers.com/securely-deleting-files-solid-state-drive/
2021-05-23 10:53:37 -05:00
if [ " ${ isSSD } " = = "1" ] ; then
2019-12-15 18:05:12 +01:00
whenDeleteSchredd = 0
2019-12-15 16:48:10 +01:00
fi
2019-12-10 20:45:34 +01:00
# delete or keep
if [ ${ delete } -eq 1 ] ; then
if [ -d " /mnt/hdd/ $entry " ] ; then
2019-12-15 16:48:10 +01:00
if [ ${ whenDeleteSchredd } -eq 1 ] ; then
>& 2 echo " # shredding DIR : ${ entry } "
2021-09-07 14:11:32 +01:00
sudo srm -lr /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
else
>& 2 echo " # deleting DIR : ${ entry } "
2021-09-07 14:11:32 +01:00
sudo rm -r /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
fi
2019-12-10 20:45:34 +01:00
else
2019-12-15 16:48:10 +01:00
if [ ${ whenDeleteSchredd } -eq 1 ] ; then
>& 2 echo " # shredding FILE : ${ entry } "
2021-09-07 14:11:32 +01:00
sudo srm -l /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
else
>& 2 echo " # deleting FILE : ${ entry } "
2021-09-07 14:11:32 +01:00
sudo rm /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
fi
2019-12-10 20:45:34 +01:00
fi
else
2019-12-13 16:38:59 +01:00
>& 2 echo " # keeping: ${ entry } "
2019-12-10 20:45:34 +01:00
fi
done
# KEEP BLOCKCHAIN means just blocks & chainstate - delete the rest
if [ " $3 " = "-keepblockchain" ] ; then
chains = ( bitcoin litecoin)
for chain in " ${ chains [@] } "
do
echo " Cleaning Blockchain: ${ chain } "
2019-12-15 18:05:12 +01:00
# take extra care if wallet.db exists
2021-09-07 13:29:51 +01:00
sudo srm -v /mnt/hdd/${ chain } /wallet.db 2>/dev/null
2019-12-15 18:05:12 +01:00
2021-07-07 11:06:12 +01:00
# the rest just delete (keep blocks and chainstate and testnet3)
2019-12-10 20:45:34 +01:00
for entry in $( ls -A1 /mnt/hdd/${ chain } 2>/dev/null)
do
# sorting file
delete = 1
2021-07-07 11:06:12 +01:00
if [ " ${ entry } " = "blocks" ] || [ " ${ entry } " = "chainstate" ] \
|| [ " ${ entry } " = "testnet3" ] ; then
2019-12-10 20:45:34 +01:00
delete = 0
fi
# delete or keep
if [ ${ delete } -eq 1 ] ; then
if [ -d " /mnt/hdd/ ${ chain } / $entry " ] ; then
2019-12-15 18:05:12 +01:00
>& 2 echo " # Deleting DIR : /mnt/hdd/ ${ chain } / ${ entry } "
sudo rm -r /mnt/hdd/${ chain } /$entry
2019-12-10 20:45:34 +01:00
else
2019-12-15 18:05:12 +01:00
>& 2 echo " # deleting FILE : /mnt/hdd/ ${ chain } / ${ entry } "
sudo rm /mnt/hdd/${ chain } /$entry
2019-12-10 20:45:34 +01:00
fi
else
2019-12-13 16:38:59 +01:00
>& 2 echo " # keeping: ${ entry } "
2019-12-10 20:45:34 +01:00
fi
done
2021-07-07 11:06:12 +01:00
# keep blocks and chainstate in testnet3 if exists
if [ -d /mnt/hdd/bitcoin/testnet3 ] ; then
for entry in $( ls -A1 /mnt/hdd/bitcoin/testnet3 2>/dev/null)
do
# sorting file
delete = 1
if [ " ${ entry } " = "blocks" ] || [ " ${ entry } " = "chainstate" ] ; then
delete = 0
fi
# delete or keep
if [ ${ delete } -eq 1 ] ; then
if [ -d " /mnt/hdd/bitcoin/testnet3/ $entry " ] ; then
>& 2 echo " # Deleting DIR : /mnt/hdd/bitcoin/testnet3/ ${ entry } "
sudo rm -r /mnt/hdd/bitcoin/testnet3/$entry
else
>& 2 echo " # deleting FILE : /mnt/hdd/bitcoin/testnet3/ ${ entry } "
sudo rm /mnt/hdd/bitcoin/testnet3/$entry
fi
else
>& 2 echo " # keeping: ${ entry } "
fi
done
fi
2019-12-10 20:45:34 +01:00
done
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK cleaning done."
2019-12-10 20:45:34 +01:00
exit 1
else
2021-08-27 03:59:21 -04:00
>& 2 echo "# FAIL unknown third parameter try '-total' or '-keepblockchain'"
echo "error='unknown parameter'"
2019-12-10 20:45:34 +01:00
exit 1
fi
# RESET BLOCKCHAIN (e.g to rebuilt blockchain )
elif [ " $2 " = "blockchain" ] ; then
# here is no secure delete needed - because not sensitive data
2019-12-13 16:38:59 +01:00
>& 2 echo "# Deleting all Blockchain Data (blocks/chainstate) from storage .."
2019-12-10 20:45:34 +01:00
# set path based on EXT4/BTRFS
basePath = "/mnt/hdd"
if [ ${ isBTRFS } -eq 1 ] ; then
basePath = "/mnt/storage"
fi
# deleting the blocks and chainstate
sudo rm -R ${ basePath } /bitcoin/blocks 1>/dev/null 2>/dev/null
sudo rm -R ${ basePath } /bitcoin/chainstate 1>/dev/null 2>/dev/null
sudo rm -R ${ basePath } /litecoin/blocks 1>/dev/null 2>/dev/null
sudo rm -R ${ basePath } /litecoin/chainstate 1>/dev/null 2>/dev/null
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK cleaning done."
2019-12-10 20:45:34 +01:00
exit 1
# RESET TEMP (keep swapfile)
elif [ " $2 " = "temp" ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Deleting the temp folder/drive (keeping SWAP file) .."
2019-12-10 20:45:34 +01:00
tempPath = "/mnt/hdd/temp"
for entry in $( ls -A1 ${ tempPath } 2>/dev/null)
do
# sorting file
delete = 1
if [ " ${ entry } " = "swapfile" ] ; then
delete = 0
fi
# delete or keep
if [ ${ delete } -eq 1 ] ; then
if [ -d " ${ tempPath } / $entry " ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo " # shredding DIR : ${ entry } "
2020-07-26 04:20:14 +02:00
sudo rm -r ${ tempPath } /$entry
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo " # shredding FILE : ${ entry } "
2020-07-26 04:20:14 +02:00
sudo rm ${ tempPath } /$entry
2019-12-10 20:45:34 +01:00
fi
else
2019-12-13 16:38:59 +01:00
>& 2 echo " # keeping: ${ entry } "
2019-12-10 20:45:34 +01:00
fi
done
2019-12-13 16:38:59 +01:00
>& 2 echo "# OK cleaning done."
2019-12-10 20:45:34 +01:00
exit 1
else
2021-08-27 03:59:21 -04:00
>& 2 echo "# FAIL unknown second parameter - try 'all','blockchain' or 'temp'"
echo "error='unknown parameter'"
2019-12-10 20:45:34 +01:00
exit 1
fi
fi
2021-05-03 21:04:53 +02:00
########################################
# UASP-fix
########################################
if [ " $1 " = "uasp-fix" ] ; then
# get HDD status and if the connected adapter is supports UASP
source <( /home/admin/config.scripts/blitz.datadrive.sh status)
# check if UASP is already deactivated (on RaspiOS)
# https://www.pragmaticlinux.com/2021/03/fix-for-getting-your-ssd-working-via-usb-3-on-your-raspberry-pi/
cmdlineExists = $( sudo ls /boot/cmdline.txt 2>/dev/null | grep -c "cmdline.txt" )
if [ ${ cmdlineExists } -eq 1 ] && [ ${# hddAdapterUSB } -gt 0 ] && [ ${ hddAdapterUSAP } -eq 0 ] ; then
echo "# Checking for UASP deactivation ..."
usbQuirkActive = $( sudo cat /boot/cmdline.txt | grep -c "usb-storage.quirks=" )
usbQuirkDone = $( sudo cat /boot/cmdline.txt | grep -c " usb-storage.quirks= ${ hddAdapterUSB } :u " )
if [ ${ usbQuirkActive } -gt 0 ] && [ ${ usbQuirkDone } -eq 0 ] ; then
# remove old usb-storage.quirks
sudo sed -i "s/usb-storage.quirks=[^ ]* //g" /boot/cmdline.txt
fi
if [ ${ usbQuirkDone } -eq 0 ] ; then
# add new usb-storage.quirks
sudo sed -i " 1s/^/usb-storage.quirks= ${ hddAdapterUSB } :u / " /boot/cmdline.txt
# go into reboot to activate new setting
echo " # DONE deactivating UASP for ${ hddAdapterUSB } ... reboot needed "
echo "neededReboot=1"
else
echo " # Already UASP deactivated for ${ hddAdapterUSB } "
echo "neededReboot=0"
fi
2021-09-30 21:32:53 +02:00
else
2021-05-03 21:04:53 +02:00
echo " # Skipping UASP deactivation ... cmdlineExists( ${ cmdlineExists } ) hddAdapterUSB( ${ hddAdapterUSB } ) hddAdapterUSAP( ${ hddAdapterUSAP } ) "
echo "neededReboot=0"
fi
exit 0
fi
2019-12-10 20:45:34 +01:00
echo "error='unkown command'"
2020-01-06 22:55:08 +01:00
exit 1