mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 10:36:12 +00:00
Separate root remounting into new function and improve killProcesses function
This commit is contained in:
+6
-2
@@ -195,7 +195,8 @@ func killProcesses() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, process := range processes {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +211,8 @@ func killProcesses() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, process := range processes {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,6 +272,7 @@ func shutdownSystem() {
|
|||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
killProcesses()
|
killProcesses()
|
||||||
unmountFilesystems()
|
unmountFilesystems()
|
||||||
|
remountRootReadonly()
|
||||||
|
|
||||||
fmt.Print("Syncing disks... ")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
@@ -288,6 +291,7 @@ func rebootSystem() {
|
|||||||
stopServiceManager()
|
stopServiceManager()
|
||||||
killProcesses()
|
killProcesses()
|
||||||
unmountFilesystems()
|
unmountFilesystems()
|
||||||
|
remountRootReadonly()
|
||||||
|
|
||||||
fmt.Print("Syncing disks... ")
|
fmt.Print("Syncing disks... ")
|
||||||
syscall.Sync()
|
syscall.Sync()
|
||||||
|
|||||||
+44
-16
@@ -241,11 +241,6 @@ func unmountFilesystems() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reserve variables for root filesytem
|
|
||||||
rootSource := ""
|
|
||||||
rootFilesystem := ""
|
|
||||||
rootData := ""
|
|
||||||
|
|
||||||
// Unmount filesystems
|
// Unmount filesystems
|
||||||
entries := strings.Split(string(data), "\n")
|
entries := strings.Split(string(data), "\n")
|
||||||
slices.Reverse(entries)
|
slices.Reverse(entries)
|
||||||
@@ -259,17 +254,18 @@ func unmountFilesystems() {
|
|||||||
fields := strings.Fields(entry)
|
fields := strings.Fields(entry)
|
||||||
mountpoint := fields[4]
|
mountpoint := fields[4]
|
||||||
filesystem := ""
|
filesystem := ""
|
||||||
source := ""
|
|
||||||
data := ""
|
|
||||||
for i := 6; i < len(fields); i++ {
|
for i := 6; i < len(fields); i++ {
|
||||||
if fields[i] == "-" {
|
if fields[i] == "-" {
|
||||||
filesystem = fields[i+1]
|
filesystem = fields[i+1]
|
||||||
source = fields[i+2]
|
|
||||||
data = fields[i+3]
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip root filesystem
|
||||||
|
if mountpoint == "/" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Skip root and ignored filesystems
|
// Skip root and ignored filesystems
|
||||||
ignoredFilesystems := []string{
|
ignoredFilesystems := []string{
|
||||||
"devtmpfs",
|
"devtmpfs",
|
||||||
@@ -277,12 +273,6 @@ func unmountFilesystems() {
|
|||||||
"sysfs",
|
"sysfs",
|
||||||
"tmpfs",
|
"tmpfs",
|
||||||
}
|
}
|
||||||
if mountpoint == "/" {
|
|
||||||
rootSource = source
|
|
||||||
rootFilesystem = filesystem
|
|
||||||
_, rootData, _ = convertMountOptions(data)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if slices.Contains(ignoredFilesystems, filesystem) {
|
if slices.Contains(ignoredFilesystems, filesystem) {
|
||||||
continue
|
continue
|
||||||
@@ -311,11 +301,49 @@ func unmountFilesystems() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func remountRootReadonly() {
|
||||||
fmt.Print("Remounting root as read-only...")
|
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
|
tries := 0
|
||||||
for {
|
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) {
|
if errors.Is(err, syscall.EBUSY) {
|
||||||
fmt.Print(".")
|
fmt.Print(".")
|
||||||
tries++
|
tries++
|
||||||
|
|||||||
Reference in New Issue
Block a user