-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathdeferable_reboot.lua
57 lines (46 loc) · 1.71 KB
/
deferable_reboot.lua
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
local utils = require "lime.utils"
local config = require "lime.config"
local defreboot = {}
defreboot.DEFAULT_REBOOT_UPTIME = 60*60*27
defreboot.SLEEP_BEFORE_REBOOT_S = 30
defreboot.POSTPONE_FILE_PATH = '/tmp/deferable-reboot.defer'
function defreboot.config(min_uptime)
if min_uptime == nil then
local uci = config.get_uci_cursor()
local lime_min_uptime = config.get("system", "deferable_reboot_uptime_s", false)
local general_min_uptime = uci:get("deferable-reboot", "options", "deferable_reboot_uptime_s")
min_uptime = tonumber(lime_min_uptime or general_min_uptime or defreboot.DEFAULT_REBOOT_UPTIME)
end
assert(type(min_uptime) == "number", "min_uptime must be a number")
defreboot.min_uptime = min_uptime
end
function defreboot.should_reboot()
local uptime_s = utils.uptime_s()
local postpone_until_s = defreboot.read_postpone_file()
local min_uptime = defreboot.min_uptime
if (postpone_until_s ~= nil) and (postpone_until_s > min_uptime) then
min_uptime = postpone_until_s
end
return uptime_s > min_uptime
end
function defreboot.postpone_util_s(uptime)
assert(type(uptime) == "number", "uptime must be a number")
local f = io.open(defreboot.POSTPONE_FILE_PATH, 'w')
f:write(tostring(uptime))
f:close()
end
--! use this function to postpone the reboot, also the following command can be used
--! replacing SECONDS: # awk '{print $1 + SECONDS}' /proc/uptime > /tmp/deferable-reboot.defer
function defreboot.read_postpone_file()
local f = io.open(defreboot.POSTPONE_FILE_PATH)
if f ~= nil then
return tonumber(f:read("*l"))
end
return nil
end
function defreboot.reboot()
--! give time to sysupgrade to kill us
nixio.nanosleep(defreboot.SLEEP_BEFORE_REBOOT_S)
os.execute("reboot")
end
return defreboot