mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Improve error handling and fstab parsing
This commit is contained in:
+19
-13
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -78,42 +79,42 @@ func mountVirtualFilesystems() {
|
||||
|
||||
// Mount /proc
|
||||
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /proc: %s", err)
|
||||
}
|
||||
|
||||
// Mount /sys
|
||||
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /sys: %s", err)
|
||||
}
|
||||
|
||||
// Mount /dev
|
||||
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /dev: %s", err)
|
||||
}
|
||||
|
||||
// Mount /run
|
||||
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /run: %s", err)
|
||||
}
|
||||
|
||||
// Mount /dev/pts
|
||||
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /dev/pts: %s", err)
|
||||
}
|
||||
|
||||
// Mount /dev/shm
|
||||
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /dev/shm: %s", err)
|
||||
}
|
||||
|
||||
// Mount securityfs
|
||||
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /sys/kernel/security: %s", err)
|
||||
}
|
||||
|
||||
// Mount cgroups v2
|
||||
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not mount /sys/fs/cgroup: %s", err)
|
||||
}
|
||||
|
||||
fmt.Println("Done.")
|
||||
@@ -122,9 +123,8 @@ func mountVirtualFilesystems() {
|
||||
func mountFilesystems() {
|
||||
fmt.Print("Mounting fstab entries... ")
|
||||
|
||||
if err := mountFstabEntries(); err != nil {
|
||||
log.Println("Could not mount fstab entries!")
|
||||
panic(err)
|
||||
if err, line := mountFstabEntries(); err != nil {
|
||||
printErrorAndReboot("Error: could not mount fstab entry on line %d: %s", line, err)
|
||||
}
|
||||
|
||||
fmt.Println("Done.")
|
||||
@@ -138,8 +138,7 @@ func startServiceManager() {
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Start()
|
||||
if err != nil {
|
||||
log.Println("Could not initialize service manager!")
|
||||
panic(err)
|
||||
printErrorAndReboot("Error: could not initialize service manager: %s", err)
|
||||
}
|
||||
serviceManagerPid = cmd.Process.Pid
|
||||
|
||||
@@ -258,3 +257,10 @@ func rebootSystem() {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func printErrorAndReboot(format string, v ...any) {
|
||||
log.Printf(format, v...)
|
||||
fmt.Println("Press 'Enter' to reboot...")
|
||||
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
||||
rebootSystem()
|
||||
}
|
||||
|
||||
+63
-18
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -28,12 +29,14 @@ var flagsEquivalence = map[string]uintptr{
|
||||
}
|
||||
|
||||
// Split string flags to mount flags and mount data
|
||||
func convertMountOptions(options string) (flags []uintptr, data string) {
|
||||
func convertMountOptions(options string) (flags []uintptr, data string, extra []string) {
|
||||
for _, flag := range strings.Split(options, ",") {
|
||||
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
||||
flags = append(flags, unixFlag)
|
||||
} else {
|
||||
if data == "" {
|
||||
if flag == "noauto" || flag == "nofail" {
|
||||
extra = append(extra, flag)
|
||||
} else if data == "" {
|
||||
data = flag
|
||||
} else {
|
||||
data += "," + flag
|
||||
@@ -41,7 +44,7 @@ func convertMountOptions(options string) (flags []uintptr, data string) {
|
||||
}
|
||||
}
|
||||
|
||||
return flags, data
|
||||
return flags, data, extra
|
||||
}
|
||||
|
||||
// Combine a unix flag slice or array into a single uintptr
|
||||
@@ -84,7 +87,7 @@ func isMountpoint(mountpoint string) bool {
|
||||
}
|
||||
|
||||
func mount(source, target, fstype string, options string, mkdir bool) error {
|
||||
flags, data := convertMountOptions(options)
|
||||
flags, data, _ := convertMountOptions(options)
|
||||
|
||||
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
||||
flags = append(flags, unix.MS_REMOUNT)
|
||||
@@ -104,34 +107,68 @@ func mount(source, target, fstype string, options string, mkdir bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func mountFstabEntries() error {
|
||||
func mountFstabEntries() (error, int) {
|
||||
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
||||
return nil
|
||||
return nil, 0
|
||||
} else if err != nil {
|
||||
return err
|
||||
return err, 0
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile("/etc/fstab")
|
||||
if err != nil {
|
||||
return err
|
||||
return err, 0
|
||||
}
|
||||
|
||||
swapPriority := -2
|
||||
|
||||
for _, line := range strings.Split(string(bytes), "\n") {
|
||||
for i, line := range strings.Split(string(bytes), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "#") || line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
source := strings.Split(line, " ")[0]
|
||||
target := strings.Split(line, " ")[1]
|
||||
fstype := strings.Split(line, " ")[2]
|
||||
options := strings.Split(line, " ")[3]
|
||||
// Get fields from line
|
||||
fields := []string{}
|
||||
sb := &strings.Builder{}
|
||||
quoted := false
|
||||
for _, r := range line {
|
||||
if r == '"' {
|
||||
quoted = !quoted
|
||||
} else if !quoted && r == ' ' {
|
||||
str := sb.String()
|
||||
if len(strings.TrimSpace(str)) > 0 {
|
||||
fields = append(fields, sb.String())
|
||||
}
|
||||
sb.Reset()
|
||||
} else {
|
||||
sb.WriteRune(r)
|
||||
}
|
||||
}
|
||||
if sb.Len() > 0 {
|
||||
fields = append(fields, sb.String())
|
||||
}
|
||||
if len(fields) < 4 {
|
||||
return fmt.Errorf("Not enough fields"), i + 1
|
||||
}
|
||||
source := fields[0]
|
||||
target := fields[1]
|
||||
fstype := fields[2]
|
||||
options := fields[3]
|
||||
|
||||
flags, data := convertMountOptions(options)
|
||||
// Replace device prefixes
|
||||
if cutSource, ok := strings.CutPrefix(source, "LABEL="); ok {
|
||||
source = "/dev/disk/by-label/" + strings.ReplaceAll(cutSource, " ", "\\x20")
|
||||
} else if cutSource, ok := strings.CutPrefix(source, "UUID="); ok {
|
||||
source = "/dev/disk/by-uuid/" + cutSource
|
||||
} else if cutSource, ok := strings.CutPrefix(source, "PARTLABEL="); ok {
|
||||
source = "/dev/disk/by-partlabel/" + cutSource
|
||||
} else if cutSource, ok := strings.CutPrefix(source, "PARTUUID="); ok {
|
||||
source = "/dev/disk/by-partuuid/" + cutSource
|
||||
}
|
||||
|
||||
if slices.Contains(strings.Split(data, ","), "noauto") {
|
||||
flags, data, extra := convertMountOptions(options)
|
||||
|
||||
if slices.Contains(extra, "noauto") {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -142,7 +179,11 @@ func mountFstabEntries() error {
|
||||
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
||||
swapPriority--
|
||||
if err != 0 {
|
||||
return fmt.Errorf("swapon syscall returned none-zero error code: %d", err)
|
||||
if slices.Contains(extra, "nofail") {
|
||||
fmt.Printf("Warning: could not mount fstab entry on line %d: swapon syscall returned non-zero exit code: %d\n", i+1, err)
|
||||
} else {
|
||||
return fmt.Errorf("swapon syscall returned non-zero exit code: %d", err), i + 1
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -152,9 +193,13 @@ func mountFstabEntries() error {
|
||||
}
|
||||
|
||||
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
||||
return err
|
||||
if slices.Contains(extra, "nofail") {
|
||||
log.Printf("Warning: could not mount fstab entry on line %d: %s\n", i+1, err)
|
||||
} else {
|
||||
return err, i + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user