Update blitz.datadrive.sh (#3540)

* Update blitz.datadrive.sh

Updates script for amd64 and nvme drives

* Update home.admin/config.scripts/blitz.datadrive.sh

Co-authored-by: openoms <43343391+openoms@users.noreply.github.com>

* Update blitz.datadrive.sh

Cleaned up code a bit per Openoms suggestion

Co-authored-by: openoms <43343391+openoms@users.noreply.github.com>
This commit is contained in:
allyourbankarebelongtous
2022-12-18 12:15:23 -06:00
committed by GitHub
parent 20c0da709b
commit 19cb9fdbc5

View File

@@ -105,11 +105,16 @@ if [ "$1" = "status" ]; then
fi
lsblk -o NAME,SIZE -b | grep -P "[s|vn][dv][a-z][0-9]?" > .lsblk.tmp
while read line; do
# cut line info into different informations
testname=$(echo $line | cut -d " " -f 1 | sed 's/[^a-z0-9]*//g')
if [ $(echo $line | grep -c "nvme") = 0 ]; then
testdevice=$(echo $testname | sed 's/[^a-z]*//g')
testpartition=$(echo $testname | grep -P '[a-z]{3,5}[0-9]{1}')
else
testdevice=$(echo $testname | sed 's/\([^p]*\).*/\1/')
testpartition=$(echo $testname | grep -P '[p]{1}')
fi
if [ ${#testpartition} -gt 0 ]; then
testsize=$(echo $line | sed "s/ */ /g" | cut -d " " -f 2 | sed 's/[^0-9]*//g')
else
@@ -134,9 +139,22 @@ if [ "$1" = "status" ]; then
#echo "# hdd(${hdd})"
if [ "$(uname -m)" = "x86_64" ]; then
if [ $(echo "$testpartition" | grep -c "nvme") = 0 ]; then
testParentDisk=$(echo "$testpartition" | sed 's/[^a-z]*//g')
else
testParentDisk=$(echo "$testpartition" | sed 's/\([^p]*\).*/\1/')
fi
if [ $(echo "$OSPartition" | grep -c "nvme") = 0 ]; then
OSParentDisk=$(echo "$OSPartition" | sed 's/[^a-z]*//g')
else
OSParentDisk=$(echo "$OSPartition" | sed 's/\([^p]*\).*/\1/')
fi
if [ $(echo "$bootPartition" | grep -c "nvme") = 0 ]; then
bootParentDisk=$(echo "$bootPartition" | sed 's/[^a-z]*//g')
else
bootParentDisk=$(echo "$bootPartition" | sed 's/\([^p]*\).*/\1/')
fi
if [ "$testdevice" != "$OSParentDisk" ] && [ "$testdevice" != "$bootParentDisk" ];then
sizeDataPartition=${testsize}
hddDataPartition="${testpartition}"
@@ -153,7 +171,6 @@ if [ "$1" = "status" ]; then
fi
fi
else
# default hdd set, when there is no OSpartition and there might be no partitions at all
if [ "${OSPartition}" = "root" ] && [ "${hdd}" = "" ] && [ "${testdevice}" != "" ]; then
hdd="${testdevice}"
@@ -177,7 +194,7 @@ if [ "$1" = "status" ]; then
# try to detect if its an SSD
isSMART=$(sudo smartctl -a /dev/${hdd} | grep -c "Rotation Rate:")
echo "isSMART=$(isSMART)"
echo "isSMART=${isSMART}"
if [ ${isSMART} -gt 0 ]; then
#detect using smartmontools (preferred)
isSSD=$(sudo smartctl -a /dev/${hdd} | grep 'Rotation Rate:' | grep -c "Solid State")
@@ -193,6 +210,9 @@ if [ "$1" = "status" ]; then
hddGigaBytes=0
if [ "${hdd}" != "" ]; then
hddBytes=$(fdisk -l /dev/$hdd | grep GiB | cut -d " " -f 5)
if [ "${hddBytes}" = "" ]; then
hddBytes=$(fdisk -l /dev/$hdd | grep TiB | cut -d " " -f 5)
fi
hddGigaBytes=$(echo "scale=0; ${hddBytes}/1024/1024/1024" | bc -l)
fi
echo "hddBytes=${hddBytes}"
@@ -201,7 +221,6 @@ if [ "$1" = "status" ]; then
# if positive deliver more data
if [ ${#hddDataPartition} -gt 0 ]; then
# check partition size in bytes and GBs
echo "hddDataPartitionBytes=${sizeDataPartition}"
hddDataPartitionGigaBytes=$(echo "scale=0; ${sizeDataPartition}/1024/1024/1024" | bc -l)
@@ -213,7 +232,6 @@ if [ "$1" = "status" ]; then
# if 'ext4' or 'btrfs' then temp mount and investigate content
if [ "${hddFormat}" = "ext4" ] || [ "${hddFormat}" = "btrfs" ]; then
# BTRFS is working with subvolumes for snapshots / ext4 has no SubVolumes
subVolumeDir=""
if [ "${hddFormat}" = "btrfs" ]; then
@@ -286,9 +304,14 @@ if [ "$1" = "status" ]; then
# temp storage data drive
mkdir -p /mnt/storage
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
if [ "${hddFormat}" = "btrfs" ]; then
# in btrfs setup the second partition is storage partition
mount /dev/${hdd}2 /mnt/storage 2>/dev/null
mount /dev/${hdd}${nvp}2 /mnt/storage 2>/dev/null
isTempMounted=$(df | grep /mnt/storage | grep -c ${hdd})
else
# in ext4 setup the partition is also the storage partition
@@ -317,7 +340,7 @@ if [ "$1" = "status" ]; then
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')
hdd_data_free1Kblocks=$(df -h -k /dev/${hdd}${nvp}1 | grep "/dev/${hdd}${nvp}1" | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9')
fi
if [ "${hdd_data_free1Kblocks}" != "" ]; then
hddDataFreeBytes=$((${hdd_data_free1Kblocks} * 1024))
@@ -362,7 +385,7 @@ if [ "$1" = "status" ]; then
fi
echo "hddGotMigrationData='${hddGotMigrationData}'"
# comment this line out if case to study the contect of the storage section
# comment this line out if case to study the content of the storage section
umount /mnt/storage
fi
else
@@ -391,7 +414,11 @@ if [ "$1" = "status" ]; then
# on ext4 its the whole /mnt/hdd
hddDataPartition=$(df | grep "/mnt/hdd$" | cut -d " " -f 1 | cut -d "/" -f 3)
fi
if [ $(echo "${hddDataPartition}" | grep -c "nvme") = 0 ]; then
hdd=$(echo $hddDataPartition | sed 's/[0-9]*//g')
else
hdd=$(echo "$hddDataPartition" | sed 's/\([^p]*\).*/\1/')
fi
hddFormat=$(lsblk -o FSTYPE,NAME,TYPE | grep part | grep "${hddDataPartition}" | cut -d " " -f 1)
if [ "${hddFormat}" = "ext4" ]; then
hddDataPartitionExt4=$hddDataPartition
@@ -444,12 +471,17 @@ if [ "$1" = "status" ]; then
# used space - at the moment just string info to display
if [ "${isBTRFS}" -gt 0 ]; then
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
# BTRFS 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)
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')
datadrive=$(df -h | grep "/dev/${hdd}${nvp}1" | sed -e's/ */ /g' | cut -d" " -f 5)
storageDrive=$(df -h | grep "/dev/${hdd}${nvp}2" | sed -e's/ */ /g' | cut -d" " -f 5)
hdd_data_free1Kblocks=$(df -h -k /dev/${hdd}${nvp}1 | grep "/dev/${hdd}${nvp}1" | sed -e's/ */ /g' | cut -d" " -f 4 | tr -dc '0-9')
hddUsedInfo="${datadrive} & ${storageDrive}"
elif [ "${isZFS}" -gt 0 ]; then
# ZFS calculations
@@ -470,14 +502,12 @@ if [ "$1" = "status" ]; then
echo "hddDataFreeBytes=${hddDataFreeBytes}"
echo "hddDataFreeKB=${hdd_data_free1Kblocks}"
echo "hddDataFreeGB=${hddDataFreeGB}"
fi
# HDD Adapter UASP support --> https://www.pragmaticlinux.com/2021/03/fix-for-getting-your-ssd-working-via-usb-3-on-your-raspberry-pi/
# in both cases (if mounted or not - using the hdd selection from both cases)
# only check if lsusb command is availabe
if [ ${#hdd} -gt 0 ] && [ "$(type -t lsusb | grep -c file)" -gt 0 ]; then
# determine USB HDD adapter model ID
hddAdapter=$(lsusb | grep "SATA" | head -1 | cut -d " " -f6)
if [ "${hddAdapter}" == "" ]; then
@@ -487,19 +517,16 @@ if [ "$1" = "status" ]; then
hddAdapter=$(lsusb | grep "ASMedia Technology" | head -1 | cut -d " " -f6)
fi
echo "hddAdapterUSB='${hddAdapter}'"
hddAdapterUSAP=0
# check if force UASP flag is set on sd card
if [ -f "/boot/uasp.force" ]; then
hddAdapterUSAP=1
fi
# or UASP is set by config file
if [ $(cat /mnt/hdd/raspiblitz.conf 2>/dev/null | grep -c "forceUasp=on") -eq 1 ]; then
hddAdapterUSAP=1
fi
# check if HDD ADAPTER is on UASP WHITELIST (tested devices)
if [ "${hddAdapter}" == "174c:55aa" ]; then
# UGREEN 2.5" External USB 3.0 Hard Disk Case with UASP support
@@ -521,27 +548,20 @@ if [ "$1" = "status" ]; then
# Cable Matters USB 3.1 Type-C Gen2 External SATA SSD Enclosure
hddAdapterUSAP=1
fi
echo "hddAdapterUSAP=${hddAdapterUSAP}"
fi
echo
echo "# RAID"
echo "isRaid=${isRaid}"
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=$(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)
@@ -558,16 +578,13 @@ if [ "$1" = "status" ]; then
fi
done
echo "raidCandidates=${drivecounter}"
fi
echo
echo "# SWAP"
echo "isSwapExternal=${isSwapExternal}"
if [ ${isSwapExternal} -eq 1 ]; then
echo "SwapExternalPath='${externalSwapPath}'"
fi
echo
exit 1
fi
@@ -578,7 +595,6 @@ fi
# check basics for formatting
if [ "$1" = "format" ]; then
# check valid format
if [ "$2" = "btrfs" ]; then
>&2 echo "# DATA DRIVE - FORMATTING to BTRFS layout (new)"
@@ -598,7 +614,6 @@ if [ "$1" = "format" ]; then
echo "error='missing parameter'"
exit 1
fi
if [ "$2" = "btrfs" ]; then
# check if device is existing and a disk (not a partition)
isValid=$(lsblk -o NAME,TYPE | grep disk | grep -c "${hdd}")
@@ -615,13 +630,11 @@ if [ "$1" = "format" ]; then
# get basic info on data drive
source <(/home/admin/config.scripts/blitz.datadrive.sh status)
if [ ${isSwapExternal} -eq 1 ] && [ "${hdd}" == "${datadisk}" ]; then
>&2 echo "# Switching off external SWAP of system drive"
dphys-swapfile swapoff 1>/dev/null
dphys-swapfile uninstall 1>/dev/null
fi
>&2 echo "# Unmounting all partitions of this device"
# remove device from all system mounts (also fstab)
lsblk -o UUID,NAME | grep "${hdd}" | cut -d " " -f 1 | grep "-" | while read -r uuid ; do
@@ -634,7 +647,6 @@ if [ "$1" = "format" ]; then
fi
done
mount -a
if [ "${hdd}" == "${datadisk}" ]; then
>&2 echo "# Make sure system drives are unmounted .."
umount /mnt/hdd 2>/dev/null
@@ -660,12 +672,19 @@ if [ "$1" = "format" ]; then
fi
fi
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
if [[ $hdd =~ [0-9] ]]; then
ext4IsPartition=1
else
ext4IsPartition=0
fi
else
if [[ $hdd =~ [p] ]]; then
ext4IsPartition=1
else
ext4IsPartition=0
fi
fi
wipePartitions=0
if [ "$2" = "btrfs" ]; then
wipePartitions=1
@@ -673,7 +692,6 @@ if [ "$1" = "format" ]; then
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)"
@@ -698,12 +716,9 @@ if [ "$1" = "format" ]; then
fi
# formatting old: EXT4
if [ "$2" = "ext4" ]; then
# prepare temp mount point
mkdir -p /tmp/ext4 1>/dev/null
if [ $ext4IsPartition -eq 0 ]; then
# write new EXT4 partition
>&2 echo "# Creating the one big partition"
@@ -737,7 +752,6 @@ if [ "$1" = "format" ]; then
echo "error='failed to unmount /tmp/ext4'"
exit 1
fi
>&2 echo "# Formatting"
if [ $ext4IsPartition -eq 0 ]; then
mkfs.ext4 -F -L BLOCKCHAIN /dev/${hdd}1 1>/dev/null
@@ -762,32 +776,39 @@ if [ "$1" = "format" ]; then
# setting fsk check interval to 1
# see https://github.com/rootzoll/raspiblitz/issues/360#issuecomment-467567572
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
if [ $ext4IsPartition -eq 0 ]; then
tune2fs -c 1 /dev/${hdd}1
tune2fs -c 1 /dev/${hdd}${nvp}1
else
tune2fs -c 1 /dev/${hdd}
fi
>&2 echo "# OK EXT 4 format done"
exit 0
fi
# formatting new: BTRFS layout - this consists of 3 volumes:
if [ "$2" = "btrfs" ]; then
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
# prepare temp mount point
mkdir -p /tmp/btrfs 1>/dev/null
>&2 echo "# Creating BLITZDATA (${hdd})"
parted -s -- /dev/${hdd} mkpart primary btrfs 1024KiB 30GiB 1>/dev/null
sync
sleep 6
win=$(lsblk -o NAME | grep -c ${hdd}1)
win=$(lsblk -o NAME | grep -c ${hdd}${nvp}1)
if [ ${win} -eq 0 ]; then
echo "error='partition failed'"
exit 1
fi
mkfs.btrfs -f -L BLITZDATA /dev/${hdd}1 1>/dev/null
mkfs.btrfs -f -L BLITZDATA /dev/${hdd}${nvp}1 1>/dev/null
# check result
loopdone=0
loopcount=0
@@ -807,28 +828,26 @@ if [ "$1" = "format" ]; then
fi
done
>&2 echo "# OK BLITZDATA exists now"
>&2 echo "# Creating SubVolume for Snapshots"
mount /dev/${hdd}1 /tmp/btrfs 1>/dev/null
mount /dev/${hdd}${nvp}1 /tmp/btrfs 1>/dev/null
if [ $(df | grep -c "/tmp/btrfs") -eq 0 ]; then
echo "error='mount ${hdd}1 failed'"
echo "error='mount ${hdd}${nvp}1 failed'"
exit 1
fi
cd /tmp/btrfs
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
>&2 echo "# Creating BLITZSTORAGE"
parted -s -- /dev/${hdd} mkpart primary btrfs 30GiB -34GiB 1>/dev/null
sync
sleep 6
win=$(lsblk -o NAME | grep -c ${hdd}2)
win=$(lsblk -o NAME | grep -c ${hdd}${nvp}2)
if [ ${win} -eq 0 ]; then
echo "error='partition failed'"
exit 1
fi
mkfs.btrfs -f -L BLITZSTORAGE /dev/${hdd}2 1>/dev/null
mkfs.btrfs -f -L BLITZSTORAGE /dev/${hdd}${nvp}2 1>/dev/null
# check result
loopdone=0
loopcount=0
@@ -847,28 +866,25 @@ if [ "$1" = "format" ]; then
fi
done
>&2 echo "# OK BLITZSTORAGE exists now"
>&2 echo "# Creating SubVolume for Snapshots"
mount /dev/${hdd}2 /tmp/btrfs 1>/dev/null
mount /dev/${hdd}${nvp}2 /tmp/btrfs 1>/dev/null
if [ $(df | grep -c "/tmp/btrfs") -eq 0 ]; then
echo "error='mount ${hdd}2 failed'"
echo "error='mount ${hdd}${nvp}2 failed'"
exit 1
fi
cd /tmp/btrfs
btrfs subvolume create WORKINGDIR
cd && umount /tmp/btrfs
>&2 echo "# Creating the FAT32 partition"
parted -s -- /dev/${hdd} mkpart primary fat32 -34GiB 100% 1>/dev/null
sync && sleep 3
win=$(lsblk -o NAME | grep -c ${hdd}3)
win=$(lsblk -o NAME | grep -c ${hdd}${nvp}3)
if [ ${win} -eq 0 ]; then
echo "error='partition failed'"
exit 1
fi
>&2 echo "# Creating Volume BLITZTEMP (format)"
mkfs -t vfat -n BLITZTEMP /dev/${hdd}3 1>/dev/null
mkfs -t vfat -n BLITZTEMP /dev/${hdd}${nvp}3 1>/dev/null
# check result
loopdone=0
loopcount=0
@@ -887,7 +903,6 @@ if [ "$1" = "format" ]; then
fi
done
>&2 echo "# OK BLITZTEMP exists now"
>&2 echo "# OK BTRFS format done"
exit 0
fi
@@ -898,7 +913,6 @@ fi
########################################
if [ "$1" = "fstab" ]; then
# get device to temp mount
hdd=$2
if [ ${#hdd} -eq 0 ]; then
@@ -907,9 +921,9 @@ if [ "$1" = "fstab" ]; then
echo "error='missing second parameter'"
exit 1
fi
# check if exist and which format
# if hdd is a partition (ext4)
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
if [[ $hdd =~ [0-9] ]]; then
# ext4
hddFormat=$(lsblk -o FSTYPE,NAME | grep ${hdd} | cut -d ' ' -f 1)
@@ -917,13 +931,20 @@ if [ "$1" = "fstab" ]; then
# btrfs
hddFormat=$(lsblk -o FSTYPE,NAME | grep ${hdd}1 | cut -d ' ' -f 1)
fi
else
if [[ $hdd =~ [p] ]]; then
# ext4
hddFormat=$(lsblk -o FSTYPE,NAME | grep ${hdd} | cut -d ' ' -f 1)
else
# btrfs
hddFormat=$(lsblk -o FSTYPE,NAME | grep ${hdd}p1 | cut -d ' ' -f 1)
fi
fi
if [ ${#hddFormat} -eq 0 ]; then
echo "# FAIL given device/partition not found"
echo "error='device not found'"
exit 1
fi
# unmount
if [ ${isMounted} -eq 1 ]; then
echo "# unmounting all drives"
@@ -961,7 +982,6 @@ if [ "$1" = "fstab" ]; then
echo "# updating /etc/fstab"
sed "/raspiblitz/ i UUID=${uuid1} /mnt/hdd ext4 noexec,defaults 0 2" -i /etc/fstab 1>/dev/null
fi
sync
mount -a 1>/dev/null
@@ -981,21 +1001,25 @@ if [ "$1" = "fstab" ]; then
exit 0
fi
done
echo "# OK - fstab updated for EXT4 layout"
exit 1
elif [ "${hddFormat}" = "btrfs" ]; then
### BTRFS ###
>&2 echo "# BTRFS: Updating /etc/fstab & mount"
>&2 echo "# BTRFS: Updating /etc/fstab & mount"
# get info on: Data Drive
uuidDATA=$(lsblk -o UUID,NAME,LABEL | grep "${hdd}" | grep "BLITZDATA" | cut -d " " -f 1 | grep "-")
mkdir -p /tmp/btrfs
mount /dev/${hdd}1 /tmp/btrfs 1>/dev/null
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
mount /dev/${hdd}${nvp}1 /tmp/btrfs 1>/dev/null
if [ $(df | grep -c "/tmp/btrfs") -eq 0 ]; then
echo "error='mount ${hdd}1 failed'"
echo "error='mount ${hdd}${nvp}1 failed'"
exit 1
fi
cd /tmp/btrfs
@@ -1010,9 +1034,9 @@ if [ "$1" = "fstab" ]; then
# get info on: Storage Drive
uuidSTORAGE=$(lsblk -o UUID,NAME,LABEL | grep "${hdd}" | grep "BLITZSTORAGE" | cut -d " " -f 1 | grep "-")
mount /dev/${hdd}2 /tmp/btrfs 1>/dev/null
mount /dev/${hdd}${nvp}2 /tmp/btrfs 1>/dev/null
if [ $(df | grep -c "/tmp/btrfs") -eq 0 ]; then
echo "error='mount ${hdd}2 failed'"
echo "error='mount ${hdd}${nvp}2 failed'"
exit 1
fi
cd /tmp/btrfs
@@ -1084,7 +1108,6 @@ if [ "$1" = "fstab" ]; then
echo "error='wrong hdd format'"
exit 1
fi
fi
########################################
@@ -1113,7 +1136,6 @@ if [ "$1" = "raid" ]; then
echo "error='unknown parameter'"
exit 1
fi
fi
# RAID --> ON
@@ -1184,10 +1206,8 @@ if [ "$1" = "raid" ] && [ "$2" = "on" ]; then
>&2 echo "# adding ${usbdev} as BTRFS raid1 for /mnt/hdd"
btrfs device add -f /dev/${usbdev} /mnt/hdd 1>/dev/null
btrfs filesystem balance start -dconvert=raid1 -mconvert=raid1 /mnt/hdd 1>/dev/null
>&2 echo "# OK - ${usbdev} is now part of a RAID1 for your RaspiBlitz data"
exit 0
fi
# RAID --> OFF
@@ -1219,10 +1239,8 @@ if [ "$1" = "raid" ] && [ "$2" = "off" ]; then
echo "error='fail'"
exit 1
fi
fi
########################################
# SNAPSHOTS - make and replay
########################################
@@ -1255,26 +1273,20 @@ if [ "$1" = "snapshot" ]; then
echo "error='unknown parameter'"
exit 1
fi
>&2 echo "# RASPIBLITZ SNAPSHOTS"
partition=$(df | grep "${subvolume}" | cut -d " " -f 1)
echo "subvolume='${subvolume}'"
echo "partition='${partition}'"
if [ "$3" = "create" ]; then
>&2 echo "# Preparing Snapshot ..."
# make sure backup folder exists
mkdir -p ${subvolume}/snapshots
# delete old backup if existing
oldBackupExists=$(ls ${subvolume}/snapshots | grep -c backup)
if [ ${oldBackupExists} -gt 0 ]; then
>&2 echo "# Deleting old snapshot"
btrfs subvolume delete ${subvolume}/snapshots/backup 1>/dev/null
fi
>&2 echo "# Creating Snapshot ..."
btrfs subvolume snapshot ${subvolume} ${subvolume}/snapshots/backup 1>/dev/null
if [ $(btrfs subvolume list ${subvolume} | grep -c snapshots/backup) -eq 0 ]; then
@@ -1284,16 +1296,13 @@ if [ "$1" = "snapshot" ]; then
>&2 echo "# OK - Snapshot created"
exit 0
fi
elif [ "$3" = "rollback" ]; then
# check if an old snapshot exists
oldBackupExists=$(ls ${subvolume}/snapshots | grep -c backup)
if [ ${oldBackupExists} -eq 0 ]; then
echo "error='no old snapshot found'"
exit 1
fi
>&2 echo "# Resetting state to old Snapshot ..."
umount ${subvolume}
mkdir -p /tmp/btrfs 1>/dev/null
@@ -1315,14 +1324,11 @@ if [ "$1" = "snapshot" ]; then
fi
echo "OK - Rollback done"
exit 0
else
>&2 echo "# third parameter needs to be 'create' or 'rollback'"
echo "error='unknown parameter'"
exit 1
fi
fi
###################
@@ -1357,6 +1363,7 @@ if [ "$1" = "tempmount" ]; then
fi
# if hdd is a partition
if [ $(echo "${hdd}" | grep -c "nvme") = 0 ]; then
if [[ $hdd =~ [0-9] ]]; then
hddDataPartition=$hdd
hddDataPartitionExt4=$hddDataPartition
@@ -1365,7 +1372,16 @@ if [ "$1" = "tempmount" ]; then
hddBTRFS=$hdd
hddFormat=$(lsblk -o FSTYPE,NAME | grep ${hddBTRFS}1 | cut -d ' ' -f 1)
fi
else
if [[ $hdd =~ [p] ]]; 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}p1 | cut -d ' ' -f 1)
fi
fi
if [ ${#hddFormat} -eq 0 ]; then
>&2 echo "# FAIL given device not found"
echo "error='device not found'"
@@ -1373,7 +1389,6 @@ if [ "$1" = "tempmount" ]; then
fi
if [ "${hddFormat}" == "ext4" ]; then
if [ "${hddDataPartitionExt4}" == "" ]; then
echo "error='parameter is no partition'"
exit 1
@@ -1383,7 +1398,6 @@ if [ "$1" = "tempmount" ]; then
echo "# temp mount /dev/${hddDataPartitionExt4} --> /mnt/hdd"
mkdir -p /mnt/hdd 1>/dev/null
mount /dev/${hddDataPartitionExt4} /mnt/hdd
# check result
isMounted=$(df | grep -c "/mnt/hdd")
if [ ${isMounted} -eq 0 ]; then
@@ -1401,12 +1415,17 @@ if [ "$1" = "tempmount" ]; then
bitcoinGID=$(id -g bitcoin)
# do BTRFS temp mount
if [ $(echo "${hddBTRFS}" | grep -c "nvme") = 0 ]; then
nvp=""
else
nvp="p"
fi
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
mount -t btrfs -o degraded -o subvol=WORKINGDIR /dev/${hddBTRFS}${nvp}1 /mnt/hdd
mount -t btrfs -o subvol=WORKINGDIR /dev/${hddBTRFS}${nvp}2 /mnt/storage
mount -o umask=0000,uid=${bitcoinUID},gid=${bitcoinGID} /dev/${hddBTRFS}${nvp}3 /mnt/temp
# check result
isMountedA=$(df | grep -c "/mnt/hdd")
@@ -1429,7 +1448,6 @@ if [ "$1" = "tempmount" ]; then
echo "isMounted=${isMounted}"
echo "isBTRFS=${isBTRFS}"
exit 1
fi
if [ "$1" = "unmount" ]; then
@@ -1445,7 +1463,6 @@ fi
########################################
if [ "$1" = "link" ]; then
if [ ${isMounted} -eq 0 ] ; then
echo "error='no data drive mounted'"
exit 1
@@ -1465,7 +1482,6 @@ if [ "$1" = "link" ]; then
if [ ${isBTRFS} -eq 1 ]; then
>&2 echo "# Creating BTRFS setup links"
>&2 echo "# - linking blockchains into /mnt/hdd"
if [ $(ls -F /mnt/hdd/bitcoin | grep -c '/mnt/hdd/bitcoin@') -eq 0 ]; then
mkdir -p /mnt/storage/bitcoin
@@ -1475,47 +1491,36 @@ if [ "$1" = "link" ]; then
ln -s /mnt/storage/bitcoin /mnt/hdd/bitcoin
rm /mnt/storage/bitcoin/bitcoin 2>/dev/null
fi
>&2 echo "# linking lnd for user bitcoin"
rm /home/bitcoin/.lnd 2>/dev/null
ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
>&2 echo "# - linking blockchain for user bitcoin"
ln -s /mnt/storage/bitcoin /home/bitcoin/.bitcoin
>&2 echo "# - linking storage into /mnt/hdd"
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
>&2 echo "# - linking temp into /mnt/hdd"
rm /mnt/hdd/temp 2>/dev/null
ln -s /mnt/temp /mnt/hdd/temp
chown -R bitcoin:bitcoin /mnt/temp
>&2 echo "# - creating snapshots folder"
mkdir -p /mnt/hdd/snapshots
mkdir -p /mnt/storage/snapshots
else
>&2 echo "# Creating EXT4 setup links"
>&2 echo "# opening blockchain into /mnt/hdd"
mkdir -p /mnt/hdd/bitcoin
>&2 echo "# linking blockchain for user bitcoin"
rm /home/bitcoin/.bitcoin 2>/dev/null
ln -s /mnt/hdd/bitcoin /home/bitcoin/.bitcoin
>&2 echo "# linking lnd for user bitcoin"
rm /home/bitcoin/.lnd 2>/dev/null
ln -s /mnt/hdd/lnd /home/bitcoin/.lnd
>&2 echo "# creating default storage & temp folders"
mkdir -p /mnt/hdd/app-storage
mkdir -p /mnt/hdd/temp
fi
# fix ownership of linked files
@@ -1542,7 +1547,6 @@ if [ "$1" = "link" ]; then
>&2 echo "# OK - all symbolic links are built"
exit 0
fi
########################################
@@ -1550,55 +1554,40 @@ fi
########################################
if [ "$1" = "swap" ]; then
>&2 echo "# RASPIBLITZ DATA DRIVES - SWAP FILE"
if [ ${isMounted} -eq 0 ]; then
echo "error='no data drive mounted'"
exit 1
fi
if [ "$2" = "on" ]; then
if [ ${isSwapExternal} -eq 1 ]; then
>&2 echo "# OK - already ON"
exit 1
fi
>&2 echo "# Switch off/uninstall old SWAP"
dphys-swapfile swapoff 1>/dev/null
dphys-swapfile uninstall 1>/dev/null
if [ ${isBTRFS} -eq 1 ]; then
>&2 echo "# Rewrite external SWAP config for BTRFS setup"
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
else
>&2 echo "# Rewrite external SWAP config for EXT4 setup"
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
fi
sed -i "s/^CONF_SWAPSIZE=/#CONF_SWAPSIZE=/g" /etc/dphys-swapfile
sed -i "s/^#CONF_MAXSWAP=.*/CONF_MAXSWAP=3072/g" /etc/dphys-swapfile
>&2 echo "# Creating SWAP file .."
dd if=/dev/zero of=$externalSwapPath count=3072 bs=1MiB 1>/dev/null
chmod 0600 $externalSwapPath 1>/dev/null
>&2 echo "# Activating new SWAP"
mkswap $externalSwapPath
dphys-swapfile setup
dphys-swapfile swapon
>&2 echo "# OK - Swap is now ON external"
exit 0
elif [ "$2" = "off" ]; then
if [ ${isSwapExternal} -eq 0 ]; then
>&2 echo "# OK - already OFF"
exit 1
@@ -1621,13 +1610,11 @@ if [ "$1" = "swap" ]; then
>&2 echo "# OK - Swap is now OFF external"
exit 0
else
>&2 echo "# FAIL unknown second parameter - try 'on' or 'off'"
echo "error='unknown parameter'"
exit 1
fi
fi
########################################
@@ -1640,16 +1627,13 @@ if [ "$1" = "clean" ]; then
# get HDD status
source <(/home/admin/config.scripts/blitz.datadrive.sh status)
if [ ${isMounted} -eq 0 ]; then
>&2 echo "# FAIL: cannot clean - the drive is not mounted'"
echo "error='not mounted'"
exit 1
fi
>&2 echo "# Making sure 'secure-delete' is installed ..."
apt-get install -y secure-delete 1>/dev/null
>&2 echo
>&2 echo "# IMPORTANT: No 100% guarantee that sensitive data is completely deleted!"
# see: https://www.davescomputers.com/securely-deleting-files-solid-state-drive/"
@@ -1659,9 +1643,7 @@ if [ "$1" = "clean" ]; then
# DELETE ALL DATA (with option to keep blockchain)
if [ "$2" = "all" ]; then
if [ "$3" = "-total" ] || [ "$3" = "-keepblockchain" ]; then
>&2 echo "# Deleting personal Data .."
# make sure swap is off
@@ -1672,23 +1654,19 @@ if [ "$1" = "clean" ]; then
# for all other data shred files selectively
for entry in $(ls -A1 /mnt/hdd)
do
delete=1
whenDeleteSchredd=1
# 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
# deactivate delete if a blockchain directory (if -keepblockchain)
if [ "$3" = "-keepblockchain" ]; then
if [ "${entry}" = "bitcoin" ]; then
delete=0
fi
fi
# decide when to shred or just delete - just delete nonsensitive data
if [ "${entry}" = "torrent" ] || [ "${entry}" = "app-storage" ]; then
whenDeleteSchredd=0
@@ -1705,10 +1683,8 @@ if [ "$1" = "clean" ]; then
if [ "${isSSD}" == "1" ]; then
whenDeleteSchredd=0
fi
# delete or keep
if [ ${delete} -eq 1 ]; then
if [ -d "/mnt/hdd/$entry" ]; then
if [ ${whenDeleteSchredd} -eq 1 ]; then
>&2 echo "# shredding DIR : ${entry}"
@@ -1726,11 +1702,9 @@ if [ "$1" = "clean" ]; then
rm /mnt/hdd/$entry
fi
fi
else
>&2 echo "# keeping: ${entry}"
fi
done
# KEEP BLOCKCHAIN means just blocks & chainstate - delete the rest
@@ -1739,10 +1713,8 @@ if [ "$1" = "clean" ]; then
for chain in "${chains[@]}"
do
echo "Cleaning Blockchain: ${chain}"
# take extra care if wallet.db exists
srm -v /mnt/hdd/${chain}/wallet.db 2>/dev/null
# the rest just delete (keep blocks and chainstate and testnet3)
for entry in $(ls -A1 /mnt/hdd/${chain} 2>/dev/null)
do
@@ -1791,10 +1763,8 @@ if [ "$1" = "clean" ]; then
fi
done
fi
>&2 echo "# OK cleaning done."
exit 1
else
>&2 echo "# FAIL unknown third parameter try '-total' or '-keepblockchain'"
echo "error='unknown parameter'"
@@ -1803,29 +1773,23 @@ if [ "$1" = "clean" ]; then
# RESET BLOCKCHAIN (e.g to rebuilt blockchain )
elif [ "$2" = "blockchain" ]; then
# here is no secure delete needed - because not sensitive data
>&2 echo "# Deleting all Blockchain Data (blocks/chainstate) from storage .."
# set path based on EXT4/BTRFS
basePath="/mnt/hdd"
if [ ${isBTRFS} -eq 1 ]; then
basePath="/mnt/storage"
fi
# deleting the blocks and chainstate
rm -R ${basePath}/bitcoin/blocks 1>/dev/null 2>/dev/null
rm -R ${basePath}/bitcoin/chainstate 1>/dev/null 2>/dev/null
>&2 echo "# OK cleaning done."
exit 1
# RESET TEMP (keep swapfile)
elif [ "$2" = "temp" ]; then
>&2 echo "# Deleting the temp folder/drive (keeping SWAP file) .."
tempPath="/mnt/hdd/temp"
for entry in $(ls -A1 ${tempPath} 2>/dev/null)
do
# sorting file
@@ -1835,7 +1799,6 @@ if [ "$1" = "clean" ]; then
fi
# delete or keep
if [ ${delete} -eq 1 ]; then
if [ -d "${tempPath}/$entry" ]; then
>&2 echo "# shredding DIR : ${entry}"
rm -r ${tempPath}/$entry
@@ -1843,21 +1806,17 @@ if [ "$1" = "clean" ]; then
>&2 echo "# shredding FILE : ${entry}"
rm ${tempPath}/$entry
fi
else
>&2 echo "# keeping: ${entry}"
fi
done
>&2 echo "# OK cleaning done."
exit 1
else
>&2 echo "# FAIL unknown second parameter - try 'all','blockchain' or 'temp'"
echo "error='unknown parameter'"
exit 1
fi
fi
########################################
@@ -1894,7 +1853,6 @@ if [ "$1" = "uasp-fix" ]; then
echo "# Skipping UASP deactivation ... cmdlineExists(${cmdlineExists}) hddAdapterUSB(${hddAdapterUSB}) hddAdapterUSAP(${hddAdapterUSAP})"
echo "neededReboot=0"
fi
exit 0
fi