blib/lib/App/FuguVM/State.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 192 CC 7 SCAR 12.5
CC
7
Cov
0%
was_unclean_shutdown
12.0
new
6.0
is_vm_running
6.0
CRAP 3.5
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2024 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 use v5.36;
19  
20 package App::FuguVM::State;
21 our $VERSION = '0.3.1';
22  
23 use Fugu::File;
24 use Fugu::Log;
25 use Fugu::Pidfile;
26 use Fugu::StateFile;
27  
28 # App::FuguVM::State - what FuguVM remembers about one VM between runs.
29 #
30 # The JSON blob rides on Fugu::StateFile. The two process IDs ride on
31 # Fugu::Pidfile, which locks before it truncates and reaps a zombie
32 # before it answers "running".
33 #
34 # The module is persistence only. It starts nothing and stops nothing:
35 # the proxy lifecycle belongs to App::FuguVM::Guest, which is what removed the
36 # require cycle between this module and App::FuguVM::Proxy.
37  
38 sub new ( $class, $state_dir, $vm_name, %opts )
39 {
40 # The name becomes a directory under the state directory. A
41 # name with a separator in it would put that directory
42 # somewhere else.
43 unless ( Fugu::File->valid_name($vm_name) ) {
44 Fugu::Log->default->error( 'Not a usable VM name: %s',
45 $vm_name // '(none)' );
46 return;
47 }
48  
49 my $vm_state_dir = "$state_dir/$vm_name";
50  
51 my $self = bless {
52 state_dir => $state_dir,
53 vm_name => $vm_name,
54 vm_state_dir => $vm_state_dir,
55 disk_path => "$vm_state_dir/disk.qcow2",
56 vm_pid => Fugu::Pidfile->new( path => "$vm_state_dir/vm.pid" ),
57 proxy_pid =>
58 Fugu::Pidfile->new( path => "$vm_state_dir/proxy.pid" ),
59 autoinstall_pid => Fugu::Pidfile->new(
60 path => "$vm_state_dir/autoinstall.pid"
61 ),
62 store => Fugu::StateFile->new(
63 path => "$vm_state_dir/status",
64 mode => 0600,
65 ),
66 }, $class;
67  
68 Fugu::File->ensure_dir($vm_state_dir) or return;
69 $self->load;
70  
71 return $self;
72 }
73  
74 sub load ($self)
75 {
76 $self->{store}->load;
77 return $self;
78 }
79  
80 sub save ($self)
81 {
82 $self->{store}->save;
83 return $self;
84 }
85  
86 # $self->store:
87 # Return the state store. App::FuguVM::Guest gives it to the proxy, which
88 # keeps its port there.
89 sub store ($self)
90 {
91 return $self->{store};
92 }
93  
94 # $self->state_dir:
95 # Return the directory that holds every VM's state, not this
96 # VM's own. App::FuguVM::Disk keys its paths by VM name under it.
97 sub state_dir ($self)
98 {
99 return $self->{state_dir};
100 }
101  
102 # $self->vm_pidfile:
103 # Return the PID file of the QEMU process. QEMU writes it itself,
104 # through its -pidfile option.
105 sub vm_pidfile ($self)
106 {
107 return $self->{vm_pid};
108 }
109  
110 # $self->proxy_pidfile:
111 # Return the PID file of the proxy child. App::FuguVM::Guest gives it to
112 # the proxy supervisor.
113 sub proxy_pidfile ($self)
114 {
115 return $self->{proxy_pid};
116 }
117  
118 # $self->autoinstall_pidfile:
119 # Return the PID file of the autoinstall responder child.
120 # App::FuguVM::Guest gives it to the responder supervisor. The
121 # file lives in the state directory of the guest, so an
122 # interrupted run leaves no listener that nothing owns.
123 sub autoinstall_pidfile ($self)
124 {
125 return $self->{autoinstall_pid};
126 }
127  
128 # VM PID management. QEMU writes the pid file itself, so the module
129 # only reads and clears it.
130 sub get_vm_pid ($self)
131 {
132 return $self->{vm_pid}->read_pid;
133 }
134  
135 sub clear_vm_pid ($self)
136 {
137 $self->{vm_pid}->remove;
138 return $self;
139 }
140  
141 # $self->is_vm_running:
142 # Report if the QEMU process is alive. A QEMU that became a
143 # zombie is not running: the check reaps it and says so.
144 sub is_vm_running ($self)
145 {
146 return $self->{vm_pid}->is_running ? 1 : 0;
147 }
148  
149 # Disk state
150 sub disk_path ($self)
151 {
152 return $self->{disk_path};
153 }
154  
155 sub disk_exists ($self)
156 {
157 return -f $self->{disk_path};
158 }
159  
160 # Installation state
161 sub is_installed ($self)
162 {
163 return $self->{store}->get('installed') ? 1 : 0;
164 }
165  
166 # $self->mark_installed($arch):
167 # Record the install mark and the architecture of the installed
168 # disk. The argument is required, so no path can forget it.
169 sub mark_installed ( $self, $arch )
170 {
171 my $data = $self->{store}->data;
172 $data->{installed} = 1;
173 $data->{installed_at} = time;
174 $data->{installed_arch} = $arch;
175 $self->{store}->save;
176  
177 return $self;
178 }
179  
180 # $self->get_installed_arch:
181 # Return the recorded architecture of the installed disk, or
182 # undef.
183 sub get_installed_arch ($self)
184 {
185 return $self->{store}->get('installed_arch');
186 }
187  
188 # The runtime record: the facts of one run of one guest. The record
189 # holds the selected accelerator and the two resolved host ports.
190 # App::FuguVM::Guest writes it before it spawns QEMU, and the stop
191 # verbs clear it beside clear_vm_pid.
192  
193 # $self->set_runtime(%facts):
194 # Record accel, ssh_port and console_port, and save the store.
195 sub set_runtime ( $self, %facts )
196 {
197 $self->{store}->set( runtime => \%facts );
198  
199 return $self;
200 }
201  
202 # $self->get_runtime:
203 # Return the recorded facts as a hash reference. Return an empty
204 # hash reference when the guest never ran.
205 sub get_runtime ($self)
206 {
207 return $self->{store}->get('runtime') // {};
208 }
209  
210 # $self->clear_runtime:
211 # Delete the record, and save the store.
212 sub clear_runtime ($self)
213 {
214 $self->{store}->delete('runtime');
215  
216 return $self;
217 }
218  
219 # Root password management. The state stores the password for the
220 # initial setup. The store writes at mode 0600.
221 sub set_root_password ( $self, $password )
222 {
223 $self->{store}->set( root_password => $password );
224 return $self;
225 }
226  
227 sub get_root_password ($self)
228 {
229 return $self->{store}->get('root_password');
230 }
231  
232 # SSH key installation state
233 # The state tracks which SSH key is installed. Thus the system can
234 # detect when the configured SSH key changed. Then it automatically
235 # installs the new key.
236 sub mark_ssh_key_installed ( $self, $ssh_pubkey = undef )
237 {
238 my $data = $self->{store}->data;
239 $data->{ssh_key_installed} = 1;
240 $data->{ssh_key_installed_at} = time;
241 $data->{installed_ssh_pubkey} = $ssh_pubkey if defined $ssh_pubkey;
242 $self->{store}->save;
243  
244 return $self;
245 }
246  
247 sub get_installed_ssh_pubkey ($self)
248 {
249 return $self->{store}->get('installed_ssh_pubkey');
250 }
251  
252 sub vm_state_dir ($self)
253 {
254 return $self->{vm_state_dir};
255 }
256  
257 sub vm_name ($self)
258 {
259 return $self->{vm_name};
260 }
261  
262 sub data ($self)
263 {
264 return $self->{store}->data;
265 }
266  
267 # Shutdown state tracking
268 # The state records whether the VM was shut down cleanly. This detects
269 # the risk of filesystem corruption.
270  
271 sub mark_clean_shutdown ($self)
272 {
273 my $data = $self->{store}->data;
274 $data->{shutdown_clean} = 1;
275 $data->{shutdown_at} = time;
276 delete $data->{running};
277 $self->{store}->save;
278  
279 return $self;
280 }
281  
282 sub mark_unclean_shutdown ($self)
283 {
284 my $data = $self->{store}->data;
285 $data->{shutdown_clean} = 0;
286 $data->{shutdown_at} = time;
287 delete $data->{running};
288 $self->{store}->save;
289  
290 return $self;
291 }
292  
293 sub mark_running ($self)
294 {
295 my $data = $self->{store}->data;
296 $data->{running} = 1;
297 $data->{started_at} = time;
298 delete $data->{shutdown_clean};
299 $self->{store}->save;
300  
301 return $self;
302 }
303  
304 sub was_unclean_shutdown ($self)
305 {
306 my $data = $self->{store}->data;
307  
308 # The state explicitly marks the shutdown as unclean
309 if ( exists $data->{shutdown_clean} && !$data->{shutdown_clean} ) {
310 return 1;
311 }
312  
313 # The state says running, but the VM process is dead. Thus the
314 # VM crashed or was killed.
315 if ( $data->{running} && !$self->is_vm_running ) {
316 return 1;
317 }
318  
319 return 0;
320 }
321  
322 sub clear_shutdown_state ($self)
323 {
324 my $data = $self->{store}->data;
325 delete $data->{shutdown_clean};
326 delete $data->{running};
327 $self->{store}->save;
328  
329 return $self;
330 }
331  
332 1;