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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@@ -78,42 +79,42 @@ func mountVirtualFilesystems() {
|
|||||||
|
|
||||||
// Mount /proc
|
// Mount /proc
|
||||||
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("proc", "/proc", "proc", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /proc: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /sys
|
// Mount /sys
|
||||||
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
if err := mount("sys", "/sys", "sysfs", commonOptions+",nodev,noexec", false); err != nil {
|
||||||
panic(err)
|
printErrorAndReboot("Error: could not mount /sys: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount /dev
|
// Mount /dev
|
||||||
if err := mount("dev", "/dev", "devtmpfs", commonOptions+",mode=755,inode64", false); err != nil {
|
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
|
// Mount /run
|
||||||
if err := mount("run", "/run", "tmpfs", commonOptions+",nodev,mode=755,inode64", false); err != nil {
|
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
|
// Mount /dev/pts
|
||||||
if err := mount("devpts", "/dev/pts", "devpts", commonOptions+",gid=5,mode=620,ptmxmode=000", true); err != nil {
|
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
|
// Mount /dev/shm
|
||||||
if err := mount("shm", "/dev/shm", "tmpfs", commonOptions+",nodev,inode64", true); err != nil {
|
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
|
// Mount securityfs
|
||||||
if err := mount("securityfs", "/sys/kernel/security", "securityfs", commonOptions, false); err != nil {
|
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
|
// Mount cgroups v2
|
||||||
if err := mount("cgroup2", "/sys/fs/cgroup", "cgroup2", commonOptions+",noexec,nsdelegate,memory_recursiveprot", false); err != nil {
|
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.")
|
fmt.Println("Done.")
|
||||||
@@ -122,9 +123,8 @@ func mountVirtualFilesystems() {
|
|||||||
func mountFilesystems() {
|
func mountFilesystems() {
|
||||||
fmt.Print("Mounting fstab entries... ")
|
fmt.Print("Mounting fstab entries... ")
|
||||||
|
|
||||||
if err := mountFstabEntries(); err != nil {
|
if err, line := mountFstabEntries(); err != nil {
|
||||||
log.Println("Could not mount fstab entries!")
|
printErrorAndReboot("Error: could not mount fstab entry on line %d: %s", line, err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Done.")
|
fmt.Println("Done.")
|
||||||
@@ -138,8 +138,7 @@ func startServiceManager() {
|
|||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
err := cmd.Start()
|
err := cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Could not initialize service manager!")
|
printErrorAndReboot("Error: could not initialize service manager: %s", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
serviceManagerPid = cmd.Process.Pid
|
serviceManagerPid = cmd.Process.Pid
|
||||||
|
|
||||||
@@ -258,3 +257,10 @@ func rebootSystem() {
|
|||||||
panic(err)
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -28,12 +29,14 @@ var flagsEquivalence = map[string]uintptr{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Split string flags to mount flags and mount data
|
// 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, ",") {
|
for _, flag := range strings.Split(options, ",") {
|
||||||
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
if unixFlag, ok := flagsEquivalence[flag]; ok {
|
||||||
flags = append(flags, unixFlag)
|
flags = append(flags, unixFlag)
|
||||||
} else {
|
} else {
|
||||||
if data == "" {
|
if flag == "noauto" || flag == "nofail" {
|
||||||
|
extra = append(extra, flag)
|
||||||
|
} else if data == "" {
|
||||||
data = flag
|
data = flag
|
||||||
} else {
|
} else {
|
||||||
data += "," + flag
|
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
|
// 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 {
|
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) {
|
if isMountpoint(target) && !slices.Contains(flags, unix.MS_REMOUNT) {
|
||||||
flags = append(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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mountFstabEntries() error {
|
func mountFstabEntries() (error, int) {
|
||||||
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
if _, err := os.Stat("/etc/fstab"); os.IsNotExist(err) {
|
||||||
return nil
|
return nil, 0
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes, err := os.ReadFile("/etc/fstab")
|
bytes, err := os.ReadFile("/etc/fstab")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
swapPriority := -2
|
swapPriority := -2
|
||||||
|
|
||||||
for _, line := range strings.Split(string(bytes), "\n") {
|
for i, line := range strings.Split(string(bytes), "\n") {
|
||||||
line = strings.TrimSpace(line)
|
line = strings.TrimSpace(line)
|
||||||
if strings.HasPrefix(line, "#") || line == "" {
|
if strings.HasPrefix(line, "#") || line == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
source := strings.Split(line, " ")[0]
|
// Get fields from line
|
||||||
target := strings.Split(line, " ")[1]
|
fields := []string{}
|
||||||
fstype := strings.Split(line, " ")[2]
|
sb := &strings.Builder{}
|
||||||
options := strings.Split(line, " ")[3]
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +179,11 @@ func mountFstabEntries() error {
|
|||||||
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
_, _, err := unix.Syscall(unix.SYS_SWAPON, uintptr(unsafe.Pointer(&b[0])), uintptr((swapPriority<<SwapFlagPrioShift)&SwapFlagPrioMask), 0)
|
||||||
swapPriority--
|
swapPriority--
|
||||||
if err != 0 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
@@ -152,9 +193,13 @@ func mountFstabEntries() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := unix.Mount(source, target, fstype, combineUnixFlags(flags), data); err != nil {
|
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