-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patharch-freeze-kernel-modules
executable file
·63 lines (50 loc) · 1.72 KB
/
arch-freeze-kernel-modules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
#
# Freeze Kernel Modules (Arch Boot Maintenance Script) by epitron
#
# Purpose: When you upgrade the kernel in Arch, this script will ensure that the old kernel modules
# don't get deleted, allowing the system to keep fuctioning normally until the next reboot,
# at which time the old modules will be removed.
#
# Usage: Run this script as root when the machine starts.
#
#
# Verbosely run a shell command
# (Prints the command it runs, and if there's an error, it tells the user and terminates the script.)
#
def cmd(*args)
puts " => #{args.join(" ")}"
print " "
unless system(*args)
$stderr.puts "[!] Command exited with error (may not be fatal)"
end
end
# Make sure the script is running as root
if Process.uid != 0
$stderr.puts "[!] Error: This script must be run as the root user."
exit 1
end
Dir.chdir("/lib/modules")
# Detect new and old kernel module directories
everything = Dir["*"]
extra_modules = Dir["extramodules-*"]
current_modules = Dir["extramodules-*/version"].map { |v| File.read(v).strip }
old_modules = everything - (current_modules + extra_modules)
# Make sure the current kernel modules directories are present!
current_modules.each do |k|
unless File.directory? k
$stderr.puts "[!] Error: The modules directory /lib/modules/#{k} is missing. Something has gone awry; please investigate."
exit 1
end
end
# Unprotect and remove the old module dirs
if old_modules.any?
puts "[*] Removing old kernel modules:"
cmd("chattr", "-R", "-i", *old_modules)
cmd("rm", "-rf", *old_modules)
end
# Protect the new module dirs
if current_modules.any?
puts "[*] Protecting new kernel modules:"
cmd("chattr", "-R", "+i", *current_modules)
end