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
2021-12-14 23:34:35 +01:00
echo "error='run as root'"
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 ..."
2022-02-07 10:29:53 +01:00
apt-get install -y btrfs-progs 1>/dev/null
2019-12-10 20:45:34 +01:00
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
2022-01-25 12:07:11 +01:00
isMounted = $( df | grep -c /mnt/hdd)
isBTRFS = $( 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" )
2022-01-25 12:07:11 +01:00
isZFS = $( zfs list 2>/dev/null | grep -c "/mnt/hdd" )
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
2021-11-30 11:58:06 +00:00
# optional second parameter can be 'bitcoin'
2019-12-13 18:21:56 +01:00
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
2022-01-25 12:07:11 +01:00
OSPartition = $( 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
2022-01-25 12:07:11 +01:00
bootPartition = $( 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
2022-01-25 12:07:11 +01:00
bootPartition = $( 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
2021-11-30 11:58:06 +00:00
testsize = $( echo $line | sed "s/ */ /g" | cut -d " " -f 2 | sed 's/[^0-9]*//g' )
2020-10-07 12:43:53 +02:00
else
2021-11-30 11:58:06 +00:00
testsize = 0
2020-10-07 12:43:53 +02:00
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
2022-01-25 12:07:11 +01:00
testpartitioncount = $( fdisk -l | grep /dev/$testdevice | wc -l)
2020-11-30 14:14:49 +01:00
# 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
2021-11-30 11:58:06 +00:00
if [ " $( uname -m) " = "x86_64" ] ; then
testParentDisk = $( echo " $testpartition " | sed 's/[^a-z]*//g' )
OSParentDisk = $( echo " $OSPartition " | sed 's/[^a-z]*//g' )
bootParentDisk = $( echo " $bootPartition " | sed 's/[^a-z]*//g' )
if [ " $testdevice " != " $OSParentDisk " ] && [ " $testdevice " != " $bootParentDisk " ] ; then
sizeDataPartition = ${ testsize }
hddDataPartition = " ${ testpartition } "
hdd = " ${ testdevice } "
fi
elif [ $testpartitioncount -gt 0 ] ; then
# if a partition was found - make sure to skip the OS and boot partitions
if [ " ${ testpartition } " != " ${ OSPartition } " ] && [ " ${ testpartition } " != " ${ bootPartition } " ] ; then
# make sure to use the biggest
if [ ${ testsize } -gt ${ sizeDataPartition } ] ; then
sizeDataPartition = ${ testsize }
hddDataPartition = " ${ testpartition } "
hdd = " ${ testdevice } "
fi
fi
2020-10-07 12:43:53 +02:00
else
2020-11-30 14:14:49 +01:00
2021-11-30 11:58:06 +00:00
# default hdd set, when there is no OSpartition and there might be no partitions at all
if [ " ${ OSPartition } " = "root" ] && [ " ${ hdd } " = "" ] && [ " ${ testdevice } " != "" ] ; then
2020-11-30 14:14:49 +01:00
hdd = " ${ testdevice } "
2021-11-30 11:58:06 +00:00
fi
# make sure to use the biggest
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)
2022-01-25 12:07:11 +01:00
sizeDataPartition = $( fdisk -l /dev/$testdevice | grep GiB | cut -d " " -f 5)
2021-11-30 11:58:06 +00:00
hddDataPartition = " ${ testdevice } 1 "
hdd = " ${ testdevice } "
fi
2019-12-11 01:19:13 +01:00
fi
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
2022-01-25 12:07:11 +01:00
isSSD = $( 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
2022-01-25 12:07:11 +01:00
hddBytes = $( fdisk -l /dev/$hdd | grep GiB | cut -d " " -f 5)
2020-11-30 14:14:49 +01:00
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 = ""
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
mountError = $( mount /dev/${ hddDataPartitionExt4 } /mnt/hdd 2>& 1)
2020-09-21 21:59:37 +02:00
isTempMounted = $( df | grep /mnt/hdd | grep -c ${ hddDataPartitionExt4 } )
2020-01-21 01:02:32 +01:00
fi
if [ " ${ hddFormat } " = "btrfs" ] ; then
2022-01-25 12:07:11 +01:00
mountError = $( 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-12-14 23:34:35 +01:00
# make copy of raspiblitz.conf &
2021-05-04 14:28:42 +02:00
# check for recoverable RaspiBlitz data (if config file exists) and raid
2021-12-14 23:34:35 +01:00
hddRaspiData = $( ls -l /mnt/hdd${ subVolumeDir } 2>/dev/null | grep -c raspiblitz.conf)
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
2021-12-14 23:34:35 +01:00
# output version data from raspiblitz.conf
2021-05-04 14:37:23 +02:00
source /mnt/hdd${ subVolumeDir } /raspiblitz.conf
2021-12-14 23:34:35 +01:00
echo " hddRaspiVersion=' ${ raspiBlitzVersion } ' "
# create hdd-inspect data dir on RAMDISK
2022-01-17 11:38:21 +01:00
mkdir /var/cache/raspiblitz/hdd-inspect 2>/dev/null
2021-12-14 23:34:35 +01:00
# make copy of raspiblitz.conf to RAMDISK
2021-12-20 01:06:29 +01:00
cp -a /mnt/hdd${ subVolumeDir } /raspiblitz.conf /var/cache/raspiblitz/hdd-inspect/raspiblitz.conf
2021-05-04 14:28:42 +02:00
2021-12-14 23:34:35 +01:00
# make copy of WIFI config to RAMDISK (if available)
2021-12-20 01:06:29 +01:00
cp -a /mnt/hdd${ subVolumeDir } /app-data/wpa_supplicant.conf /var/cache/raspiblitz/hdd-inspect/wpa_supplicant.conf 2>/dev/null
2021-12-14 23:34:35 +01:00
2021-12-18 19:18:57 +01:00
# Convert old ssh backup data structure (if needed)
2022-02-07 20:59:11 +01:00
oldDataExists = $( sudo ls /mnt/hdd${ subVolumeDir } /ssh/ssh_host_rsa_key 2>/dev/null | grep -c "ssh_host_rsa_key" )
2022-02-07 19:53:08 +01:00
if [ " ${ oldDataExists } " != "0" ] ; then
2021-12-18 19:18:57 +01:00
# make a complete backup of directory
2022-02-07 20:59:11 +01:00
cp -a /mnt/hdd${ subVolumeDir } /ssh /mnt/hdd${ subVolumeDir } /app-storage/ssh-old-backup
2021-12-18 19:18:57 +01:00
# delete old false sub directory (if exists)
2022-02-07 20:59:11 +01:00
rm -r /mnt/hdd${ subVolumeDir } /ssh/ssh 2>/dev/null
2021-12-18 19:18:57 +01:00
# move ssh root keys into new directory (if exists)
2022-02-07 20:59:11 +01:00
mv /mnt/hdd${ subVolumeDir } /ssh/root_backup /mnt/hdd${ subVolumeDir } /app-data/ssh-root 2>/dev/null
2021-12-18 19:18:57 +01:00
# move sshd keys into new directory
2022-02-07 20:59:11 +01:00
mkdir -p /mnt/hdd${ subVolumeDir } /app-data/sshd 2>/dev/null
mv /mnt/hdd${ subVolumeDir } /ssh /mnt/hdd${ subVolumeDir } /app-data/sshd/ssh
2021-12-18 19:18:57 +01:00
fi
2021-12-14 23:34:35 +01:00
2021-12-18 19:18:57 +01:00
# make copy of SSH keys to RAMDISK (if available)
2022-01-28 14:32:07 +01:00
cp -a /mnt/hdd${ subVolumeDir } /app-data/sshd /var/cache/raspiblitz/hdd-inspect 2>/dev/null
cp -a /mnt/hdd${ subVolumeDir } /app-data/ssh-root /var/cache/raspiblitz/hdd-inspect 2>/dev/null
2021-12-14 23:34:35 +01:00
fi
2021-05-04 14:37:23 +02:00
# comment this line out if case to study the contect of the data section
2022-01-25 12:07:11 +01:00
umount /mnt/hdd
2019-12-11 01:19:13 +01:00
fi
# temp storage data drive
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
mount /dev/${ hddDataPartitionExt4 } /mnt/storage 2>/dev/null
2020-09-21 21:59:37 +02:00
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
2022-01-25 12:07:11 +01:00
hddBlocksBitcoin = $( 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-13 18:21:56 +01:00
if [ " ${ blockchainType } " = "bitcoin" ] && [ ${ hddBlocksBitcoin } -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
2022-01-17 11:36:52 +01:00
if [ " ${ hdd_data_free1Kblocks } " != "" ] ; then
hddDataFreeBytes = $(( ${ hdd_data_free1Kblocks } * 1024 ))
hddDataFreeGB = $(( ${ hdd_data_free1Kblocks } / ( 1024 * 1024 )) )
echo " hddDataFreeBytes= ${ hddDataFreeBytes } "
echo " hddDataFreeKB= ${ hdd_data_free1Kblocks } "
echo " hddDataFreeGB= ${ hddDataFreeGB } "
else
echo "# ERROR: Was not able to determine hddDataFree space"
fi
2021-04-05 13:14:56 +02:00
# check if its another fullnode implementation data disk
2021-05-04 14:39:03 +02:00
hddGotMigrationData = ""
2022-01-12 14:03:18 +01:00
hddGotMigrationDataExtra = ""
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
2022-01-25 12:07:11 +01:00
isUmbrelHDD = $( ls /mnt/storage/umbrel/info.json 2>/dev/null | grep -c '.json' )
isCitadelHDD = $( ls /mnt/storage/citadel/info.json 2>/dev/null | grep -c '.json' )
isMyNodeHDD = $( 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
2022-06-09 17:51:42 +02:00
# sudo cat /mnt/hdd/umbrel/app-data/bitcoin/umbrel-app.yml | grep "version:" | cut -d ":" -f2 | tr -d \" | xargs
2021-04-05 13:14:56 +02:00
hddGotMigrationData = "umbrel"
2022-06-09 17:51:42 +02:00
btcVersion = $( grep "lncm/bitcoind" /mnt/storage/umbrel/app-data/bitcoin/docker-compose.yml 2>/dev/null | sed 's/.*bitcoind://' | sed 's/@.*//' )
clnVersion = $( grep "lncm/clightning" /mnt/storage/umbrel/app-data/core-lightning/docker-compose.yml 2>/dev/null | sed 's/.*clightning://' | sed 's/@.*//' )
lndVersion = $( grep "lightninglabs/lnd" /mnt/storage/umbrel/app-data/lightning/docker-compose.yml 2>/dev/null | sed 's/.*lnd://' | sed 's/@.*//' )
# umbrel <0.5.0 (old structure)
if [ " ${ lndVersion } " = = "" ] ; then
lndVersion = $( grep "lightninglabs/lnd" /mnt/storage/umbrel/docker-compose.yml 2>/dev/null | sed 's/.*lnd://' | sed 's/@.*//' )
fi
echo " hddVersionBTC=' ${ btcVersion } ' "
echo " hddVersionLND=' ${ clnVersion } ' "
echo " hddVersionCLN=' ${ lndVersion } ' "
2021-10-07 11:50:25 +02:00
elif [ ${ isMyNodeHDD } -gt 0 ] ; then
2021-04-05 13:14:56 +02:00
hddGotMigrationData = "mynode"
2021-11-30 18:27:04 +01:00
elif [ ${ isCitadelHDD } -gt 0 ] ; then
hddGotMigrationData = "citadel"
2022-01-12 14:03:18 +01:00
lndVersion = $( grep "lightninglabs/lnd" /mnt/storage/citadel/docker-compose.yml 2>/dev/null | sed 's/.*lnd://' | sed 's/@.*//' )
echo " hddVersionLND=' ${ lndVersion } ' "
2021-04-05 13:14:56 +02:00
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
2022-01-25 12:07:11 +01:00
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"
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
2022-01-25 12:07:11 +01:00
if [ " ${ isBTRFS } " -gt 0 ] ; then
2020-11-10 23:40:54 +01:00
# on btrfs date the storage partition as the data partition
hddDataPartition = $( df | grep " /mnt/storage $" | cut -d " " -f 1 | cut -d "/" -f 3)
2022-01-25 12:07:11 +01:00
elif [ " ${ isZFS } " -gt 0 ] ; then
# a ZFS pool has no leading /
hddDataPartition = $( df | grep " /mnt/hdd $" | cut -d " " -f 1 | cut -d "/" -f 2)
if [ ${# hddDataPartition } -eq 0 ] ; then
# just a pool, no filesystem
hddDataPartition = $( df | grep " /mnt/hdd $" | cut -d " " -f 1 | cut -d "/" -f 1)
fi
2020-11-10 23:40:54 +01:00
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
2022-01-25 12:07:11 +01:00
hddRaspiData = $( 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
2022-01-25 12:07:11 +01:00
isSSD = $( 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 } ' "
2021-12-16 22:36:53 +01:00
echo " hddCandidate=' ${ hdd } ' "
echo " hddPartitionCandidate=' ${ hddDataPartition } ' "
2019-12-13 18:29:20 +01:00
# check if blockchain data is available
2022-01-25 12:07:11 +01:00
hddBlocksBitcoin = $( ls /mnt/hdd/bitcoin/blocks/blk00000.dat 2>/dev/null | grep -c '.dat' )
2019-12-13 18:29:20 +01:00
echo " hddBlocksBitcoin= ${ hddBlocksBitcoin } "
if [ " ${ blockchainType } " = "bitcoin" ] && [ ${ hddBlocksBitcoin } -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
2022-01-25 12:07:11 +01:00
if [ " ${ isZFS } " -gt 0 ] ; then
sizeDataPartition = $( zpool list -pH | awk '{print $2}' )
hddGigaBytes = $( echo " scale=0; ${ sizeDataPartition } /1024/1024/1024 " | bc -l)
else
sizeDataPartition = $( lsblk -o NAME,SIZE -b | grep " ${ hddDataPartition } " | awk '$1=$1' | cut -d " " -f 2)
hddGigaBytes = $( echo " scale=0; ${ sizeDataPartition } /1024/1024/1024 " | bc -l)
fi
2020-09-21 21:59:37 +02:00
echo " hddBytes= ${ sizeDataPartition } "
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
2022-01-25 12:07:11 +01:00
if [ " ${ isBTRFS } " -gt 0 ] ; then
2021-11-30 11:58:06 +00:00
# BTRFS calculations
2019-12-16 13:24:32 +01:00
# 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 } "
2022-01-25 12:07:11 +01:00
elif [ " ${ isZFS } " -gt 0 ] ; then
# ZFS calculations
2022-06-20 16:45:39 +01:00
hdd_used_space = $(( $( zpool list -pH | awk '{print $3}' ) / 1024 / 1024 / 1024 ))
hdd_used_ratio = $(( 100 * hdd_used_space / hddGigaBytes))
2022-01-25 12:07:11 +01:00
hdd_data_free1Kblocks = $(( $( zpool list -pH | awk '{print $4}' ) / 1024 ))
hddUsedInfo = " ${ hdd_used_space } ( ${ hdd_used_ratio } %) "
else
# EXT4 calculations
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)
hdd_data_free1Kblocks = $( df -h -k /dev/${ hddDataPartitionExt4 } | grep " /dev/ ${ hddDataPartitionExt4 } " | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9' )
hddUsedInfo = " ${ hdd_used_space } ( ${ hdd_used_ratio } %) "
2019-12-16 13:24:32 +01:00
fi
echo " hddUsedInfo=' ${ hddUsedInfo } ' "
2021-12-14 23:34:35 +01:00
hddDataFreeBytes = $(( ${ hdd_data_free1Kblocks } * 1024 ))
hddDataFreeGB = $(( ${ hdd_data_free1Kblocks } / ( 1024 * 1024 )) )
echo " hddDataFreeBytes= ${ hddDataFreeBytes } "
2021-04-05 13:14:56 +02:00
echo " hddDataFreeKB= ${ hdd_data_free1Kblocks } "
2021-12-14 23:34:35 +01:00
echo " hddDataFreeGB= ${ hddDataFreeGB } "
2019-12-16 13:24:32 +01:00
2019-12-11 01:19:13 +01:00
fi
2021-12-19 14:37:48 +00:00
# HDD Adapter 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-12-09 03:23:47 +01:00
# only check if lsusb command is availabe
if [ ${# hdd } -gt 0 ] && [ " $( type -t lsusb | grep -c file) " -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
2021-12-14 23:34:35 +01:00
# check if force UASP flag is set on sd card
2021-09-09 11:33:31 +01:00
if [ -f "/boot/uasp.force" ] ; then
hddAdapterUSAP = 1
fi
2021-12-14 23:34:35 +01:00
# or UASP is set by config file
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
2022-02-12 11:14:31 +01:00
if [ " ${ hddAdapter } " = = "174c:1153" ] ; then
# UGREEN 2.5" External USB 3.0 Hard Disk Case with UASP support, 2021+ version
hddAdapterUSAP = 1
fi
2021-04-03 12:24:50 +02:00
if [ " ${ hddAdapter } " = = "0825:0001" ] || [ " ${ hddAdapter } " = = "174c:0825" ] ; then
# SupTronics 2.5" SATA HDD Shield X825 v1.5
hddAdapterUSAP = 1
fi
2022-05-17 18:55:20 +02:00
if [ " ${ hddAdapter } " = = "2109:0715" ] ; then
# ICY BOX IB-247-C31 Type-C Enclosure for 2.5inch SATA Drives
hddAdapterUSAP = 1
fi
if [ " ${ hddAdapter } " = = "174c:235c" ] ; then
# Cable Matters USB 3.1 Type-C Gen2 External SATA SSD Enclosure
hddAdapterUSAP = 1
fi
2021-12-14 23:34:35 +01:00
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' )
2022-01-25 12:07:11 +01:00
raidUsbDev = $( btrfs filesystem show /mnt/hdd | grep -F -v " ${ raidHddDev } " | grep "/dev/" | cut -d "/" --f 3)
2019-12-13 14:53:32 +01:00
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
2022-01-25 12:07:11 +01:00
source <( /home/admin/config.scripts/blitz.datadrive.sh status)
2020-02-24 22:44:59 +01:00
if [ ${ isSwapExternal } -eq 1 ] && [ " ${ hdd } " = = " ${ datadisk } " ] ; then
>& 2 echo "# Switching off external SWAP of system drive"
2022-01-25 12:07:11 +01:00
dphys-swapfile swapoff 1>/dev/null
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 } "
2022-01-25 12:07:11 +01:00
sed -i " /UUID= ${ uuid } /d " /etc/fstab
2020-09-23 17:15:09 +02:00
sync
else
>& 2 echo "# skipping empty result"
fi
2019-12-10 20:45:34 +01:00
done
2022-01-25 12:07:11 +01:00
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 .."
2022-01-25 12:07:11 +01:00
umount /mnt/hdd 2>/dev/null
umount /mnt/temp 2>/dev/null
umount /mnt/storage 2>/dev/null
2020-02-24 22:44:59 +01:00
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)"
2022-01-25 12:07:11 +01:00
sfdisk --delete /dev/${ hdd }
2020-11-01 11:46:31 +01:00
sleep 4
2022-01-25 12:07:11 +01:00
wipefs -a /dev/${ hdd }
2020-11-01 11:46:31 +01:00
sleep 4
partitions = $( lsblk | grep -c " ─ ${ hdd } " )
if [ ${ partitions } -gt 0 ] ; then
>& 2 echo "# WARNING: partitions are still not clean - try Quick & Dirty"
2022-01-25 12:07:11 +01:00
dd if = /dev/zero of = /dev/${ hdd } bs = 512 count = 1
2020-11-01 11:46:31 +01:00
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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
mkdir -p /tmp/ext4 1>/dev/null
2020-09-21 21:59:37 +02:00
2020-11-01 11:46:31 +01:00
if [ $ext4IsPartition -eq 0 ] ; then
# write new EXT4 partition
>& 2 echo "# Creating the one big partition"
2022-01-25 12:07:11 +01:00
parted -s /dev/${ hdd } mkpart primary ext4 1024KiB 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
2022-01-25 12:07:11 +01:00
umount -f /tmp/ext4 2>/dev/null
2020-09-21 21:59:37 +02:00
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
2022-01-25 12:07:11 +01:00
mkfs.ext4 -F -L BLOCKCHAIN /dev/${ hdd } 1 1>/dev/null
2020-11-01 11:46:31 +01:00
else
2022-01-25 12:07:11 +01:00
mkfs.ext4 -F -L BLOCKCHAIN /dev/${ hdd } 1>/dev/null
2020-11-01 11:46:31 +01:00
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
2022-01-25 12:07:11 +01:00
tune2fs -c 1 /dev/${ hdd } 1
2020-11-01 11:46:31 +01:00
else
2022-01-25 12:07:11 +01:00
tune2fs -c 1 /dev/${ hdd }
2020-11-01 11:46:31 +01:00
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
2022-01-25 12:07:11 +01:00
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 } ) "
2022-01-25 12:07:11 +01:00
parted -s -- /dev/${ hdd } mkpart primary btrfs 1024KiB 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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
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"
2022-01-25 12:07:11 +01:00
mount /dev/${ hdd } 1 /tmp/btrfs 1>/dev/null
2020-09-21 21:59:37 +02:00
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 1 failed' "
exit 1
fi
cd /tmp/btrfs
2022-01-25 12:07:11 +01:00
btrfs subvolume create WORKINGDIR
subVolDATA = $( btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && umount /tmp/btrfs
2020-09-21 21:59:37 +02:00
>& 2 echo "# Creating BLITZSTORAGE"
2022-01-25 12:07:11 +01:00
parted -s -- /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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
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"
2022-01-25 12:07:11 +01:00
mount /dev/${ hdd } 2 /tmp/btrfs 1>/dev/null
2020-09-21 21:59:37 +02:00
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 2 failed' "
exit 1
fi
cd /tmp/btrfs
2022-01-25 12:07:11 +01:00
btrfs subvolume create WORKINGDIR
cd && umount /tmp/btrfs
2020-09-21 21:59:37 +02:00
2021-08-27 03:59:21 -04:00
>& 2 echo "# Creating the FAT32 partition"
2022-01-25 12:07:11 +01:00
parted -s -- /dev/${ hdd } mkpart primary fat32 -34GiB 100% 1>/dev/null
2020-09-21 21:59:37 +02:00
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)"
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
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"
2022-01-25 12:07:11 +01:00
umount /mnt/hdd > /dev/null 2>& 1
umount /mnt/storage > /dev/null 2>& 1
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"
2022-01-25 12:07:11 +01:00
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"
2022-01-25 12:07:11 +01:00
sed " /raspiblitz/ i UUID= ${ uuid1 } /mnt/hdd ext4 noexec,defaults 0 2 " -i /etc/fstab 1>/dev/null
2020-09-21 21:59:37 +02:00
fi
2019-12-10 20:45:34 +01:00
sync
2022-01-25 12:07:11 +01:00
mount -a 1>/dev/null
2019-12-12 12:45:22 +01:00
# 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 "-" )
2022-01-25 12:07:11 +01:00
mkdir -p /tmp/btrfs
mount /dev/${ hdd } 1 /tmp/btrfs 1>/dev/null
2019-12-12 12:45:22 +01:00
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 1 failed' "
exit 1
fi
cd /tmp/btrfs
2022-01-25 12:07:11 +01:00
subVolDATA = $( btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && umount /tmp/btrfs
2019-12-12 12:45:22 +01:00
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 "-" )
2022-01-25 12:07:11 +01:00
mount /dev/${ hdd } 2 /tmp/btrfs 1>/dev/null
2019-12-12 12:45:22 +01:00
if [ $( df | grep -c "/tmp/btrfs" ) -eq 0 ] ; then
echo " error='mount ${ hdd } 2 failed' "
exit 1
fi
cd /tmp/btrfs
2022-01-25 12:07:11 +01:00
subVolSTORAGE = $( btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
cd && umount /tmp/btrfs
2019-12-12 12:45:22 +01:00
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 } "
2022-01-25 12:07:11 +01:00
sed -i " /UUID= ${ uuid } /d " /etc/fstab
2019-12-12 12:45:22 +01:00
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
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd 1>/dev/null
2019-12-12 12:45:22 +01:00
fstabAdd1 = " UUID= ${ uuidDATA } /mnt/hdd btrfs noexec,defaults,subvolid= ${ subVolDATA } 0 2 "
2022-01-25 12:07:11 +01:00
sed " 3 a ${ fstabAdd1 } " -i /etc/fstab 1>/dev/null
mkdir -p /mnt/storage 1>/dev/null
2019-12-12 12:45:22 +01:00
fstabAdd2 = " UUID= ${ uuidSTORAGE } /mnt/storage btrfs noexec,defaults,subvolid= ${ subVolSTORAGE } 0 2 "
2022-01-25 12:07:11 +01:00
sed " 4 a ${ fstabAdd2 } " -i /etc/fstab 1>/dev/null
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 "
2022-01-25 12:07:11 +01:00
sed " 5 a ${ fstabAdd3 } " -i /etc/fstab 1>/dev/null
sync && mount -a 1>/dev/null
2019-12-12 12:45:22 +01:00
# 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
2022-07-18 21:07:14 +01:00
>& 2 echo "# ERROR: # NOT IMPLEMENTED YET -> devices seem contain old data"
2019-12-13 16:38:59 +01:00
>& 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
2022-01-25 12:07:11 +01:00
parted -s /dev/${ usbdev } rm ${ v_partition }
2019-12-10 20:45:34 +01:00
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 "
2022-01-25 12:07:11 +01:00
btrfs device add -f /dev/${ usbdev } /mnt/hdd 1>/dev/null
btrfs filesystem balance start -dconvert= raid1 -mconvert= raid1 /mnt/hdd 1>/dev/null
2019-12-10 20:45:34 +01:00
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
2022-01-25 12:07:11 +01:00
isBTRFS = $( 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"
2022-01-25 12:07:11 +01:00
btrfs balance start -mconvert= dup -dconvert= single /mnt/hdd 1>/dev/null
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
2022-01-25 12:07:11 +01:00
mkdir -p ${ subvolume } /snapshots
2019-12-10 20:45:34 +01:00
# delete old backup if existing
2022-01-25 12:07:11 +01:00
oldBackupExists = $( ls ${ subvolume } /snapshots | grep -c backup)
2019-12-10 20:45:34 +01:00
if [ ${ oldBackupExists } -gt 0 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Deleting old snapshot"
2022-01-25 12:07:11 +01:00
btrfs subvolume delete ${ subvolume } /snapshots/backup 1>/dev/null
2019-12-10 20:45:34 +01:00
fi
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating Snapshot ..."
2022-01-25 12:07:11 +01:00
btrfs subvolume snapshot ${ subvolume } ${ subvolume } /snapshots/backup 1>/dev/null
if [ $( btrfs subvolume list ${ subvolume } | grep -c snapshots/backup) -eq 0 ] ; then
2019-12-10 20:45:34 +01:00
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
2022-01-25 12:07:11 +01:00
oldBackupExists = $( ls ${ subvolume } /snapshots | grep -c backup)
2019-12-10 20:45:34 +01:00
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 ..."
2022-01-25 12:07:11 +01:00
umount ${ subvolume }
mkdir -p /tmp/btrfs 1>/dev/null
mount ${ partition } /tmp/btrfs
mv /tmp/btrfs/WORKINGDIR/snapshots/backup /tmp/btrfs/backup
btrfs subvolume delete /tmp/btrfs/WORKINGDIR
mv /tmp/btrfs/backup /tmp/btrfs/WORKINGDIR
subVolID = $( btrfs subvolume show /tmp/btrfs/WORKINGDIR | grep "Subvolume ID:" | awk '$1=$1' | cut -d " " -f 3)
sed -i " / ${ subvolumeESC } /d " /etc/fstab
2019-12-10 20:45:34 +01:00
fstabAdd = " UUID= ${ uuid } ${ subvolume } btrfs noexec,defaults,subvolid= ${ subVolID } 0 2 "
2022-01-25 12:07:11 +01:00
sed " 4 a ${ fstabAdd } " -i /etc/fstab 1>/dev/null
umount /tmp/btrfs
mount -a
2019-12-10 20:45:34 +01:00
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
2022-05-05 10:59:50 +02:00
if [ " ${ hddFormat } " = = "ext4" ] ; then
2019-12-11 15:08:08 +01:00
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 "
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd 1>/dev/null
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
2022-05-05 10:59:50 +02:00
elif [ " ${ hddFormat } " = = "btrfs" ] ; then
2019-12-11 15:08:08 +01:00
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
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd 1>/dev/null
mkdir -p /mnt/storage 1>/dev/null
mkdir -p /mnt/temp 1>/dev/null
mount -t btrfs -o degraded -o subvol = WORKINGDIR /dev/${ hddBTRFS } 1 /mnt/hdd
mount -t btrfs -o subvol = WORKINGDIR /dev/${ hddBTRFS } 2 /mnt/storage
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
2022-01-25 12:07:11 +01:00
umount /mnt/hdd 2>/dev/null
umount /mnt/storage 2>/dev/null
umount /mnt/temp 2>/dev/null
2021-05-23 10:37:57 -05:00
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
2022-01-25 12:07:11 +01:00
if [ $( 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"
2022-01-25 12:07:11 +01:00
rm -r /home/bitcoin/.bitcoin 2>/dev/null
2019-12-11 18:18:16 +01:00
else
2022-01-25 12:07:11 +01:00
rm /home/bitcoin/.bitcoin 2>/dev/null
2019-12-11 18:18:16 +01:00
fi
2019-12-15 12:53:42 +01:00
# make sure common base directory exits
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd/lnd
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
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/storage/bitcoin
cp -R /mnt/hdd/bitcoin/* /mnt/storage/bitcoin 2>/dev/null
chown -R bitcoin:bitcoin /mnt/storage/bitcoin
rm -r /mnt/hdd/bitcoin
ln -s /mnt/storage/bitcoin /mnt/hdd/bitcoin
rm /mnt/storage/bitcoin/bitcoin 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"
2022-01-25 12:07:11 +01:00
rm /home/bitcoin/.lnd 2>/dev/null
ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
2019-12-12 14:50:11 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking blockchain for user bitcoin"
2022-01-25 12:07:11 +01:00
ln -s /mnt/storage/bitcoin /home/bitcoin/.bitcoin
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking storage into /mnt/hdd"
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/storage/app-storage
chown -R bitcoin:bitcoin /mnt/storage/app-storage
rm /mnt/hdd/app-storage 2>/dev/null
ln -s /mnt/storage/app-storage /mnt/hdd/app-storage
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - linking temp into /mnt/hdd"
2022-01-25 12:07:11 +01:00
rm /mnt/hdd/temp 2>/dev/null
ln -s /mnt/temp /mnt/hdd/temp
2022-07-19 12:43:27 +01:00
chown -R bitcoin:bitcoin
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# - creating snapshots folder"
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd/snapshots
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"
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd/bitcoin
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# linking blockchain for user bitcoin"
2022-01-25 12:07:11 +01:00
rm /home/bitcoin/.bitcoin 2>/dev/null
ln -s /mnt/hdd/bitcoin /home/bitcoin/.bitcoin
2019-12-15 12:53:42 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# linking lnd for user bitcoin"
2022-01-25 12:07:11 +01:00
rm /home/bitcoin/.lnd 2>/dev/null
ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
2019-12-12 14:50:11 +01:00
2019-12-15 12:53:42 +01:00
>& 2 echo "# creating default storage & temp folders"
2022-01-25 12:07:11 +01:00
mkdir -p /mnt/hdd/app-storage
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
2022-01-25 12:07:11 +01:00
chown -R bitcoin:bitcoin /mnt/hdd/bitcoin
chown -R bitcoin:bitcoin /mnt/hdd/lnd
chown -R bitcoin:bitcoin /home/bitcoin/.lnd
chown -R bitcoin:bitcoin /home/bitcoin/.bitcoin
chown bitcoin:bitcoin /mnt/hdd/app-storage
chown bitcoin:bitcoin /mnt/hdd/app-data
chown -R bitcoin:bitcoin /mnt/hdd/temp 2>/dev/null
chmod -R 777 /mnt/temp 2>/dev/null
chmod -R 777 /mnt/hdd/temp 2>/dev/null
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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
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
2022-01-25 12:07:11 +01:00
mv ./README.txt /mnt/hdd/app-storage/README.txt 2>/dev/null
2019-12-15 12:53:42 +01:00
2022-01-17 20:54:22 +00:00
>& 2 echo "# OK - all symbolic links are built"
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"
2022-01-25 12:07:11 +01:00
dphys-swapfile swapoff 1>/dev/null
dphys-swapfile uninstall 1>/dev/null
2019-12-10 20:45:34 +01:00
if [ ${ isBTRFS } -eq 1 ] ; then
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite external SWAP config for BTRFS setup"
2022-01-25 12:07:11 +01:00
sed -i "s/^#CONF_SWAPFILE=/CONF_SWAPFILE=/g" /etc/dphys-swapfile
sed -i "s/^CONF_SWAPFILE=.*/CONF_SWAPFILE=\/mnt\/temp\/swapfile/g" /etc/dphys-swapfile
2022-01-08 00:08:38 +00:00
2019-12-10 20:45:34 +01:00
else
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite external SWAP config for EXT4 setup"
2022-01-25 12:07:11 +01:00
sed -i "s/^#CONF_SWAPFILE=/CONF_SWAPFILE=/g" /etc/dphys-swapfile
sed -i "s/^CONF_SWAPFILE=.*/CONF_SWAPFILE=\/mnt\/hdd\/swapfile/g" /etc/dphys-swapfile
2019-12-10 20:45:34 +01:00
fi
2022-01-25 12:07:11 +01:00
sed -i "s/^CONF_SWAPSIZE=/#CONF_SWAPSIZE=/g" /etc/dphys-swapfile
2022-07-19 12:43:27 +01:00
sed -i "s/^#CONF_MAXSWAP=.*/CONF_MAXSWAP=3072/g" /etc/dphys-swapfile
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# Creating SWAP file .."
2022-07-19 12:43:27 +01:00
dd if = /dev/zero of = $externalSwapPath count = 3072 bs = 1MiB 1>/dev/null
2022-01-25 12:07:11 +01:00
chmod 0600 $externalSwapPath 1>/dev/null
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# Activating new SWAP"
2022-01-25 12:07:11 +01:00
mkswap $externalSwapPath
dphys-swapfile setup
dphys-swapfile swapon
2019-12-10 20:45:34 +01:00
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"
2022-01-25 12:07:11 +01:00
dphys-swapfile swapoff 1>/dev/null
dphys-swapfile uninstall 1>/dev/null
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# Rewrite SWAP config"
2022-01-25 12:07:11 +01:00
sed -i "12s/.*/CONF_SWAPFILE=\/var\/swap/" /etc/dphys-swapfile
sed -i "16s/.*/#CONF_SWAPSIZE=/" /etc/dphys-swapfile
dd if = /dev/zero of = /var/swap count = 256 bs = 1MiB 1>/dev/null
chmod 0600 /var/swap
2019-12-10 20:45:34 +01:00
2019-12-13 16:38:59 +01:00
>& 2 echo "# Create and switch on new SWAP"
2022-01-25 12:07:11 +01:00
mkswap /var/swap 1>/dev/null
dphys-swapfile setup 1>/dev/null
dphys-swapfile swapon 1>/dev/null
2019-12-10 20:45:34 +01:00
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 ..."
2022-01-25 12:07:11 +01:00
apt-get install -y secure-delete 1>/dev/null
2019-12-10 20:45:34 +01:00
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
2022-01-25 12:07:11 +01:00
dphys-swapfile swapoff 1>/dev/null
dphys-swapfile uninstall 1>/dev/null
2019-12-10 20:45:34 +01:00
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
2022-05-05 10:59:50 +02:00
# dont delete temp - will be deleted on every boot anyway
# but keep in case during setup a migration file was uploaded there
if [ " ${ entry } " = "temp" ] ; then
delete = 0
fi
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
2021-11-30 11:58:06 +00:00
if [ " ${ entry } " = "bitcoin" ] ; then
2019-12-15 18:05:12 +01:00
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
2021-11-30 11:58:06 +00:00
if [ " ${ entry } " = "bitcoin" ] ; 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 } "
2022-01-25 12:07:11 +01:00
srm -lr /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
else
>& 2 echo " # deleting DIR : ${ entry } "
2022-01-25 12:07:11 +01:00
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 } "
2022-01-25 12:07:11 +01:00
srm -l /mnt/hdd/$entry
2019-12-15 16:48:10 +01:00
else
>& 2 echo " # deleting FILE : ${ entry } "
2022-01-25 12:07:11 +01:00
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
2021-11-30 11:58:06 +00:00
chains = ( bitcoin)
2019-12-10 20:45:34 +01:00
for chain in " ${ chains [@] } "
do
echo " Cleaning Blockchain: ${ chain } "
2019-12-15 18:05:12 +01:00
# take extra care if wallet.db exists
2022-01-25 12:07:11 +01:00
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 } "
2022-01-25 12:07:11 +01:00
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 } "
2022-01-25 12:07:11 +01:00
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 } "
2022-01-25 12:07:11 +01:00
rm -r /mnt/hdd/bitcoin/testnet3/$entry
2021-07-07 11:06:12 +01:00
else
>& 2 echo " # deleting FILE : /mnt/hdd/bitcoin/testnet3/ ${ entry } "
2022-01-25 12:07:11 +01:00
rm /mnt/hdd/bitcoin/testnet3/$entry
2021-07-07 11:06:12 +01:00
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
2022-01-25 12:07:11 +01:00
rm -R ${ basePath } /bitcoin/blocks 1>/dev/null 2>/dev/null
rm -R ${ basePath } /bitcoin/chainstate 1>/dev/null 2>/dev/null
2019-12-10 20:45:34 +01:00
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 } "
2022-01-25 12:07:11 +01:00
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 } "
2022-01-25 12:07:11 +01:00
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/
2022-01-25 12:07:11 +01:00
cmdlineExists = $( ls /boot/cmdline.txt 2>/dev/null | grep -c "cmdline.txt" )
2021-05-03 21:04:53 +02:00
if [ ${ cmdlineExists } -eq 1 ] && [ ${# hddAdapterUSB } -gt 0 ] && [ ${ hddAdapterUSAP } -eq 0 ] ; then
echo "# Checking for UASP deactivation ..."
2022-01-25 12:07:11 +01:00
usbQuirkActive = $( cat /boot/cmdline.txt | grep -c "usb-storage.quirks=" )
usbQuirkDone = $( cat /boot/cmdline.txt | grep -c " usb-storage.quirks= ${ hddAdapterUSB } :u " )
2021-05-03 21:04:53 +02:00
if [ ${ usbQuirkActive } -gt 0 ] && [ ${ usbQuirkDone } -eq 0 ] ; then
# remove old usb-storage.quirks
2022-01-25 12:07:11 +01:00
sed -i "s/usb-storage.quirks=[^ ]* //g" /boot/cmdline.txt
2021-05-03 21:04:53 +02:00
fi
if [ ${ usbQuirkDone } -eq 0 ] ; then
# add new usb-storage.quirks
2022-01-25 12:07:11 +01:00
sed -i " s/^/usb-storage.quirks= ${ hddAdapterUSB } :u / " /boot/cmdline.txt
2021-05-03 21:04:53 +02:00
# 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