Separate root remounting into new function and improve killProcesses function

This commit is contained in:
2025-11-18 19:47:29 +02:00
parent 5ee42a5966
commit bcff6fecf8
2 changed files with 50 additions and 18 deletions
+6 -2
View File
@@ -195,7 +195,8 @@ func killProcesses() {
return
}
for _, process := range processes {
if process.Pid() == 1 {
sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0)
if process.Pid() == 1 || sid == 1 {
continue
}
@@ -210,7 +211,8 @@ func killProcesses() {
return
}
for _, process := range processes {
if process.Pid() == 1 {
sid, _, _ := syscall.Syscall(syscall.SYS_GETSID, uintptr(process.Pid()), 0, 0)
if process.Pid() == 1 || sid == 1 {
continue
}
@@ -270,6 +272,7 @@ func shutdownSystem() {
stopServiceManager()
killProcesses()
unmountFilesystems()
remountRootReadonly()
fmt.Print("Syncing disks... ")
syscall.Sync()
@@ -288,6 +291,7 @@ func rebootSystem() {
stopServiceManager()
killProcesses()
unmountFilesystems()
remountRootReadonly()
fmt.Print("Syncing disks... ")
syscall.Sync()
+44 -16
View File
@@ -241,11 +241,6 @@ func unmountFilesystems() {
log.Fatal(err)
}
// Reserve variables for root filesytem
rootSource := ""
rootFilesystem := ""
rootData := ""
// Unmount filesystems
entries := strings.Split(string(data), "\n")
slices.Reverse(entries)
@@ -259,17 +254,18 @@ func unmountFilesystems() {
fields := strings.Fields(entry)
mountpoint := fields[4]
filesystem := ""
source := ""
data := ""
for i := 6; i < len(fields); i++ {
if fields[i] == "-" {
filesystem = fields[i+1]
source = fields[i+2]
data = fields[i+3]
break
}
}
// Skip root filesystem
if mountpoint == "/" {
continue
}
// Skip root and ignored filesystems
ignoredFilesystems := []string{
"devtmpfs",
@@ -277,12 +273,6 @@ func unmountFilesystems() {
"sysfs",
"tmpfs",
}
if mountpoint == "/" {
rootSource = source
rootFilesystem = filesystem
_, rootData, _ = convertMountOptions(data)
continue
}
if slices.Contains(ignoredFilesystems, filesystem) {
continue
@@ -311,11 +301,49 @@ func unmountFilesystems() {
}
}
}
}
func remountRootReadonly() {
fmt.Print("Remounting root as read-only...")
data, err := os.ReadFile("/proc/self/mountinfo")
if err != nil {
log.Fatal(err)
}
filesystem := ""
source := ""
fsData := ""
// Get root filesystems
entries := strings.Split(string(data), "\n")
slices.Reverse(entries)
for _, entry := range entries {
entry = strings.TrimSpace(entry)
if len(entry) == 0 {
continue
}
// Get entry fields
fields := strings.Fields(entry)
mountpoint := fields[4]
for i := 6; i < len(fields); i++ {
if fields[i] == "-" {
filesystem = fields[i+1]
source = fields[i+2]
fsData = fields[i+3]
break
}
}
if mountpoint == "/" {
break
}
}
tries := 0
for {
err = unix.Mount(rootSource, "/", rootFilesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, rootData)
err := unix.Mount(source, "/", filesystem, syscall.MS_RDONLY|syscall.MS_REMOUNT, fsData)
if errors.Is(err, syscall.EBUSY) {
fmt.Print(".")
tries++