From 407b43ec5fc1a4c5c1ca7a01aa191bc2b48150a4 Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Thu, 2 Dec 2021 02:53:51 +0100 Subject: [PATCH] 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 --- src/core/mainloop.nim | 2 +- src/main.nim | 2 +- src/util/disks.nim | 20 ++++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/core/mainloop.nim b/src/core/mainloop.nim index f37781d..e00aafe 100644 --- a/src/core/mainloop.nim +++ b/src/core/mainloop.nim @@ -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") diff --git a/src/main.nim b/src/main.nim index 0759f17..ed470de 100644 --- a/src/main.nim +++ b/src/main.nim @@ -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? \ No newline at end of file diff --git a/src/util/disks.nim b/src/util/disks.nim index 3d2e5fe..cc597fa 100644 --- a/src/util/disks.nim +++ b/src/util/disks.nim @@ -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}")