mynode/rootfs/standard/usr/bin/mount_drive.tcl

149 lines
4.2 KiB
Tcl
Raw Normal View History

2019-06-15 23:02:44 +00:00
#!/usr/bin/tclsh
proc checkPartitionForExistingMyNodeFs {partition} {
if [catch {runCommand mount /dev/$partition /mnt/hdd}] {
puts "Cannot mount partition ${partition}"
return 0
}
2019-12-05 01:05:52 +00:00
if { [file exists /mnt/hdd/.mynode] } {
2019-06-15 23:02:44 +00:00
puts "Found existing myNode FS on ${partition}"
runCommand echo /dev/${partition} > /tmp/.mynode_drive
return 1
}
runCommand umount /mnt/hdd
puts "No myNode filesystem on existing partition ${partition}"
return 0
}
proc checkPartitionsForExistingMyNodeFs {partitionsName} {
upvar $partitionsName partitions
runCommand mkdir -p /mnt/hdd
# Check if we are skipping the check (to reformat drive)
if { [file exists /home/bitcoin/.mynode/force_format_prompt] } {
2022-03-17 02:53:25 +00:00
puts "Forcing format prompt (/home/bitcoin/.mynode/force_format_prompt exists)"
return 0
}
# Check each partition
2019-06-15 23:02:44 +00:00
foreach partition $partitions {
if [checkPartitionForExistingMyNodeFs $partition] {
# set partitions [lsearch -all -inline -not -exact $partitions $partition]
2019-06-15 23:02:44 +00:00
return 1
}
}
return 0
}
proc findBlockDevices {hardDrivesName} {
upvar $hardDrivesName hardDrives
set devs [exec ls /sys/block/]
set hardDrives {}
foreach dev $devs {
if [regexp "sd.*|hd.*|vd.*|nvme.*" $dev] {
2020-08-26 03:54:19 +00:00
# Check if drive mounted - command will fail if not mounted and get caught
if {[catch { exec mount | grep $dev }]} {
puts "Adding possible drive $dev"
lappend hardDrives $dev
} else {
puts "Skipping drive $dev - already mounted"
}
2019-06-15 23:02:44 +00:00
}
}
}
proc findAllPartitionsForBlockDevices {blockDevices partitionsName} {
upvar $partitionsName partitions
set partitions {}
foreach dev $blockDevices {
catch {
set found [exec ls /sys/block/${dev}/ | grep ${dev}]
foreach partition $found {
lappend partitions $partition
}
}
}
}
proc createMyNodeFsOnBlockDevice {blockDevice} {
if [exec cat /sys/block/$blockDevice/ro] {
puts "Cannot create myNode partition on ${blockDevice} because it is read-only"
return 0
}
if [catch {
# Run USB check to make sure we are using a good driver
runCommand /usr/bin/mynode_usb_driver_check.sh > /dev/null
2020-03-30 02:04:42 +00:00
puts "Waiting on format confirmation..."
2020-08-21 18:31:47 +00:00
runCommand echo "drive_format_confirm" > /tmp/.mynode_status
2020-03-30 02:04:42 +00:00
while { [file exists "/tmp/format_ok"] == 0 } {
after 500
}
2019-06-15 23:02:44 +00:00
puts "Creating new partition table on ${blockDevice}"
2020-08-21 18:31:47 +00:00
runCommand echo "drive_formatting" > /tmp/.mynode_status
2019-06-15 23:02:44 +00:00
runCommand /usr/bin/format_drive.sh ${blockDevice}
after 5000
if [regexp "nvme.*" $blockDevice] {
set blockPartition ${blockDevice}p1
} else {
set blockPartition ${blockDevice}1
}
puts "Formatting new partition ${blockPartition}"
runCommand mkfs.ext4 -F -L myNode /dev/${blockPartition}
2019-06-15 23:02:44 +00:00
runCommand mount /dev/${blockPartition} /mnt/hdd -o errors=continue
2019-06-15 23:02:44 +00:00
runCommand date >/mnt/hdd/.mynode
runCommand echo /dev/${blockPartition} > /tmp/.mynode_drive
2019-06-15 23:02:44 +00:00
}] {
puts "Formatting on ${blockDevice} failed: $::errorInfo"
return 0
}
return 1
}
proc createMyNodeFsOrDie {blockDevices} {
foreach dev $blockDevices {
if [createMyNodeFsOnBlockDevice $dev] {
return
}
}
fatal "Cannot find a suitable drive for storing myNode files"
}
proc runCommand {args} {
#puts "Running: ${args}"
puts [exec -ignorestderr {*}$args]
}
proc mountFileSystems {} {
findBlockDevices hardDrives
2021-02-04 01:11:19 +00:00
set drive_count [llength $hardDrives]
puts "Found these $drive_count drives: ${hardDrives}"
2019-06-15 23:02:44 +00:00
findAllPartitionsForBlockDevices $hardDrives partitions
2021-02-04 01:11:19 +00:00
puts "Found these existing drive partitions: ${partitions}"
2019-06-15 23:02:44 +00:00
if {![checkPartitionsForExistingMyNodeFs partitions]} {
puts "No existing drive found. Creating new one."
createMyNodeFsOrDie $hardDrives
}
}
if [catch {mountFileSystems}] {
puts "No valid partition found. Try again."
exit 1
}
exit 0