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

134 lines
3.6 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
foreach partition $partitions {
if [checkPartitionForExistingMyNodeFs $partition] {
# Remove this partition from the list so we don't try to
# use it as the config drive later on.
set partitions [lsearch -all -inline -not -exact $partitions $partition]
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] {
2019-06-15 23:02:44 +00:00
lappend hardDrives $dev
}
}
}
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 {
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
puts "Found these harddrives: ${hardDrives}"
findAllPartitionsForBlockDevices $hardDrives partitions
puts "Found these existing harddrive partitions: ${partitions}"
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