/
hibernate.sls
199 lines (165 loc) · 5.86 KB
/
hibernate.sls
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{% from "framework-laptop/map.jinja" import framework with context %}
{% set swapfile = "/swapfile" %}
{% set swapfile_exists = salt['file.file_exists'](swapfile) %}
{% set mem_size = salt['cmd.shell']("echo $(($(getconf _PHYS_PAGES) * $(getconf PAGE_SIZE) / (1024 * 1024 * 1024)))") | int %}
{% if swapfile_exists %}
{% set swap_size = salt['cmd.shell']("echo $((($(swapon -s | grep '/swapfile ' | tr -s '[:blank:]' ',' | cut -d ',' -f 3) / 1024 + 1) / 1024))") | int %}
{% if swap_size <= mem_size %}
hibernate_swap_off:
cmd.run:
- name: swapoff {{ swapfile }}
hibernate_kill_swap:
file.absent:
- name: {{ swapfile }}
{% set swapfile_exists = false %}
{% endif %}
{% endif %}
{% set swap_size = mem_size + 1 %}
{% if swapfile_exists %}
{% set resume_offset = salt['cmd.shell']("filefrag -v " ~ swapfile ~ " | grep '^ *0:' | tr -s '[:blank:]' ',' | cut -d',' -f5 | tr -d '.'") %}
{% set resume_uud = salt['cmd.shell']("findmnt -no UUID -T " ~ swapfile) %}
{% endif %}
hibernate_create_swap_file:
cmd.run:
- name: fallocate -l {{swap_size}}G {{swapfile}}
- unless: '[ -f {{swapfile}} ]'
hibernate_chmod_swap:
file.managed:
- name: {{swapfile}}
- mode: 0600
- require:
- cmd: hibernate_create_swap_file
hibernate_make_swap:
cmd.run:
- name: mkswap {{swapfile}}
- require:
- file: hibernate_chmod_swap
- onchanges:
- cmd: hibernate_create_swap_file
hibernate_swap_on:
cmd.run:
- name: swapon {{swapfile}}
- unless: "swapon -s | grep -q '{{swapfile}} '"
- require:
- cmd: hibernate_make_swap
- file: hibernate_chmod_swap
hibernate_swap_fstab:
file.append:
- name: /etc/fstab
- unless: grep -q '{{swapfile}} ' /etc/fstab
- text: '{{swapfile}} none swap sw 0 0'
- require:
- cmd: hibernate_create_swap_file
- cmd: hibernate_swap_on
{% if swapfile_exists %}
# Cleanup old-style config.
hibernate_grub_resume_old_config_line:
file.replace:
- name: /etc/default/grub
- pattern: '^GRUB_CMDLINE_LINUX_DEFAULT="\${GRUB_CMDLINE_LINUX_DEFAULT} resume=UUID=.* resume_offset=.*"$'
- repl: ''
hibernate_grub_resume_old_config:
file.replace:
- name: /etc/default/grub
- pattern: '^(.*)(resume=UUID=[-|\w]+)([\s|"].*)$'
- repl: '\g<1>\g<3>'
- require:
- file: hibernate_grub_resume_old_config_line
hibernate_grub_resume_offset_old_config:
file.replace:
- name: /etc/default/grub
- pattern: '^(.*)(resume_offset=\w+)([\s|"].*)$'
- repl: '\g<1>\g<3>'
- require:
- file: hibernate_grub_resume_old_config_line
# Now do the config
hibernate_grub_resume:
file.managed:
- name: /etc/default/grub.d/hibernate.cfg
- contents: 'GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_DEFAULT} resume=UUID={{resume_uud}} resume_offset={{resume_offset}}"'
- require:
- cmd: hibernate_swap_on
hibernate_update_grub:
cmd.run:
- name: update-grub
- onchanges:
- file: hibernate_grub_resume
- file: hibernate_grub_resume_offset_old_config
- file: hibernate_grub_resume_old_config
- file: hibernate_grub_resume_old_config_line
hibernate_polkit_enabled:
file.managed:
- name: /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
- source: salt://framework-laptop/files/com.ubuntu.enable-hibernate.pkla
{% if framework.hibernate.mode == 'hybrid_sleep' %}
{% set suspend_state = "disk" %}
{% set handle_suspend_key = "suspend" %}
{% set handle_lid_switch = "suspend" %}
{% set handle_lid_switch_external_power = "suspend" %}
{% elif framework.hibernate.mode == 'suspend_then_hibernate' %}
{% set suspend_state = "mem standby freeze" %}
{% set handle_suspend_key = "suspend-then-hibernate" %}
{% set handle_lid_switch = "suspend-then-hibernate" %}
{% set handle_lid_switch_external_power = "suspend-then-hibernate" %}
hibernate-hibernate-delay-sec:
file.replace:
- name: /etc/systemd/sleep.conf
- pattern: '^HibernateDelaySec=.*'
- repl: HibernateDelaySec={{ framework.hibernate.suspend_then_hibernate.hibernate_delay_sec }}
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
{% elif framework.hibernate.mode == 'hibernate' %}
# Reset everything to default in case we've touched it before.
{% set suspend_state = "mem standby freeze" %}
{% set handle_suspend_key = "suspend" %}
{% set handle_lid_switch = "suspend" %}
{% set handle_lid_switch_external_power = "suspend" %}
{% endif %}
hibernate-suspend-mode:
file.replace:
- name: /etc/systemd/sleep.conf
- pattern: '^SuspendMode=.*'
- repl: SuspendMode=suspend
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
hibernate-suspend-state:
file.replace:
- name: /etc/systemd/sleep.conf
- pattern: '^SuspendState=.*'
- repl: SuspendState={{ suspend_state }}
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
hibernate-handle-suspend-key:
file.replace:
- name: /etc/systemd/logind.conf
- pattern: '^HandleSuspendKey=.*'
- repl: HandleSuspendKey={{ handle_suspend_key }}
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
hibernate-handle-lid-switch:
file.replace:
- name: /etc/systemd/logind.conf
- pattern: '^HandleLidSwitch=.*'
- repl: HandleLidSwitch={{ handle_lid_switch }}
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
hibernate-handle-lid-switch-external-power:
file.replace:
- name: /etc/systemd/logind.conf
- pattern: '^HandleLidSwitchExternalPower=.*'
- repl: HandleLidSwitchExternalPower={{ handle_lid_switch_external_power }}
- append_if_not_found: True
- require:
- file: hibernate_grub_resume
- cmd: hibernate_update_grub
{% endif %}