NimD now correctly resolves symlinks when checking if a real filesystem is mounted. also removed extra check for target as it was unnecessary and error-prone

This commit is contained in:
Nocturn9x 2021-12-02 02:53:51 +01:00
parent b94a4c0b1c
commit 407b43ec5f
3 changed files with 18 additions and 6 deletions

View File

@ -32,7 +32,7 @@ proc mainLoop*(logger: Logger, mountDisks: bool = true, fstab: string = "/etc/fs
else:
logger.info("Skipping disk mounting, did we restart after a critical error?")
except:
logger.fatal(&"A fatal error has occurred while mounting disks, booting cannot continue. Error -> {getCurrentExceptionMsg()}")
logger.fatal(&"A fatal error has occurred while preparing filesystem, booting cannot continue. Error -> {getCurrentExceptionMsg()}")
nimDExit(logger, 131)
logger.info("Disks mounted")
logger.info("Processing boot runlevel")

View File

@ -86,6 +86,6 @@ when isMainModule:
main(logger)
except:
logger.fatal(&"A fatal unrecoverable error has occurred during startup and NimD cannot continue: {getCurrentExceptionMsg()}")
quit(131) # ENOTRECOVERABLE - State not recoverable
nimDExit(logger, 131) # ENOTRECOVERABLE - State not recoverable
# This will almost certainly cause the kernel to crash with an error the likes of "Kernel not syncing, attempted to kill init!",
# but, after all, there isn't much we can do if we can't even initialize *ourselves* is there?

View File

@ -15,6 +15,7 @@ import strutils
import sequtils
import strformat
import posix
import os
import logging
import misc
@ -56,7 +57,7 @@ proc parseFileSystemTable*(fstab: string): seq[tuple[source, target, filesystemt
# in our temporary list
temp = line.split().filterIt(it != "").join(" ").split(maxsplit=6)
if temp[0].toLowerAscii().startswith("id="):
temp[0] = &"""/dev/disk/by-id/{temp[0].split("=", maxsplit=2)[1]}"""
temp[0] = &"""disk/by-id/{temp[0].split("=", maxsplit=2)[1]}"""
if temp[0].toLowerAscii().startswith("label="):
temp[0] = &"""/dev/disk/by-label/{temp[0].split("=", maxsplit=2)[1]}"""
if temp[0].toLowerAscii().startswith("uuid="):
@ -81,12 +82,23 @@ proc umount2*(target: cstring, flags: cint): cint {.header: "sys/mount.h", impor
proc umount*(target: string): int = int(umount(cstring(target)))
proc umount2*(target: string, flags: int): int = int(umount2(cstring(target), cint(flags)))
proc exists(p: string): bool =
# Checks if a path exists. Thanks
# araq :)
try:
discard getFileInfo(p)
result = true
except OSError:
result = false
proc checkDisksIsMounted(search: tuple[source, target, filesystemtype: string, mountflags: uint64, data: string]): bool =
proc checkDisksIsMounted(search: tuple[source, target, filesystemtype: string, mountflags: uint64, data: string], expand: bool = false): bool =
## Returns true if a disk is already mounted
for entry in parseFileSystemTable(readFile("/proc/mounts")):
if entry.source == search.source and entry.target == search.target:
if expand:
if exists(entry.source) and exists(search.source) and sameFile(entry.source, search.source):
return true
elif entry.source == search.source:
return true
return false
@ -96,7 +108,7 @@ proc mountRealDisks*(logger: Logger, fstab: string = "/etc/fstab") =
try:
logger.info(&"Reading disk entries from {fstab}")
for entry in parseFileSystemTable(readFile(fstab)):
if checkDisksIsMounted(entry):
if checkDisksIsMounted(entry, expand=true):
logger.debug(&"Skipping mounting filesystem {entry.source} ({entry.filesystemtype}) at {entry.target}: already mounted")
continue
logger.debug(&"Mounting filesystem {entry.source} ({entry.filesystemtype}) at {entry.target} with mount option(s) {entry.data}")