-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibvirtipmi.py
executable file
·1171 lines (1007 loc) · 44 KB
/
libvirtipmi.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright 2024 s3rj1k
# SPDX-License-Identifier: Apache-2.0
# pylint: disable=too-many-lines
"""IPMI BMC implementation for managing Libvirt domains."""
# https://www.intel.com/content/dam/www/public/us/en/documents/product-briefs/ipmi-second-gen-interface-spec-v2-rev1-1.pdf
# * boot flags - Page 396
import argparse
import inspect
import logging
import os
import secrets
import string
import sys
import threading
import time
import xml.etree.ElementTree as ET
from collections import defaultdict
from enum import Enum, IntEnum, IntFlag
from functools import wraps
from socket import socket
from threading import Lock
from typing import Optional, Dict, Any, Tuple
from uuid import UUID
import libvirt
import pyghmi.ipmi.private.session as ipmisession
from pyghmi.ipmi.command import power_states
from pyghmi.ipmi.private import constants
from pyghmi.ipmi.private.serversession import IpmiServer
from pyghmi.ipmi.private.serversession import ServerSession
DEFAULT_LISTEN_TIMEOUT = 30
domain_locks: Dict[str, Lock] = defaultdict(Lock)
def setup_logger(log_level: str) -> logging.Logger:
"""Configure and return a logger with the specified log level."""
logger = logging.getLogger("LibvirtIPMI")
handler = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(getattr(logging, log_level.upper()))
return logger
def domain_lock(func):
"""Decorator to ensure concurency-safe domain operations."""
@wraps(func)
def wrapper(self: "LibvirtIPMI", *args, **kwargs) -> Any:
session = self.find_session(args, kwargs)
if not session or not session.domain:
self.logger.debug(
f"No session or domain found for {func.__name__}, executing without lock"
)
return func(self, *args, **kwargs)
self.logger.debug(
f"Attempting to acquire lock for domain '{session.domain}' in {func.__name__}"
)
lock = domain_locks[session.domain]
acquired = lock.acquire(timeout=5)
if not acquired:
self.logger.debug(
f"Lock acquisition failed for domain '{session.domain}' in {func.__name__} (timeout)"
)
return IpmiResponseCode.NODE_BUSY
self.logger.debug(
f"Lock acquired for domain '{session.domain}' in {func.__name__}"
)
try:
return func(self, *args, **kwargs)
finally:
lock.release()
self.logger.debug(
f"Lock released for domain '{session.domain}' in {func.__name__}"
)
return wrapper
# https://github.com/ipmitool/ipmitool/blob/master/lib/ipmi_strings.c#L1229
class IpmiResponseCode(IntEnum):
"""Standard IPMI response codes used to indicate command execution status."""
SUCCESS = 0x00
NODE_BUSY = 0xC0
INVALID_COMMAND = 0xC1
INVALID_DATA_FIELD = 0xCC
DESTINATION_UNAVAILABLE = 0xD3
COMMAND_NOT_SUPPORTED_IN_PRESENT_STATE = 0xD5
COMMAND_DISABLED = 0xD6
UNSPECIFIED_ERROR = 0xFF
class IpmiNetFn(IntEnum):
"""IPMI Network Function codes defining categories of commands."""
CHASSIS = 0x00
APP = 0x06
class IpmiCommand(IntEnum):
"""IPMI command codes for specific operations within each Network Function."""
GET_DEVICE_ID = 0x01
GET_SYSTEM_GUID = 0x37
GET_CHASSIS_STATUS = 0x01
CHASSIS_CONTROL = 0x02
SET_SYSTEM_BOOT_OPTIONS = 0x08
GET_SYSTEM_BOOT_OPTIONS = 0x09
class BootDevice(Enum):
"""Mapping between IPMI boot device codes and Libvirt boot device names."""
NETWORK = (0b0001, "network")
HD = (0b0010, "hd")
CDROM = (0b0101, "cdrom")
FD = (0b1111, "fd")
def __init__(self, code: int, name: str):
self.ipmi_code = code
self.libvirt_name = name
@classmethod
def from_ipmi(cls, code: int) -> Optional["BootDevice"]:
"""Convert IPMI boot device code to BootDevice enum value."""
for device in cls:
if device.ipmi_code == code:
return device
return None
@classmethod
def from_libvirt(cls, name: str) -> Optional["BootDevice"]:
"""Convert Libvirt boot device name to BootDevice enum value."""
for device in cls:
if device.libvirt_name == name:
return device
return None
class BootOptionParameters(IntEnum):
"""IPMI parameters for configuring system boot options."""
SET_IN_PROGRESS = 0
# SERVICE_PARTITION_SELECTOR = 1
# SERVICE_PARTITION_SCAN = 2
BOOT_FLAG_VALID_BIT_CLEARING = 3
BOOT_INFO_ACKNOWLEDGE = 4
BOOT_FLAGS = 5
class BootFlags(IntFlag):
"""IPMI boot flags for controlling boot behavior and state."""
DEFAULT = 0b00000000
VALID = 0b10000000
PERSISTENT = 0b01000000
EFI = 0b00100000
LEGACY_BIOS = 0b00000000
class EnhancedServerSession(ServerSession):
"""Extended server session class that adds domain-aware authentication and management."""
# pylint: disable=too-many-arguments
def __init__(
self,
authdata: Dict[str, str],
kg: Optional[str],
clientaddr: tuple[str, int],
netsocket: socket,
request: bytes,
uuid: Optional[UUID],
bmc: "LibvirtIPMI",
) -> None:
self._domain: Optional[str] = None
self._bmc = bmc
super().__init__(authdata, kg, clientaddr, netsocket, request, uuid, bmc)
@property
def domain(self) -> Optional[str]:
"""Get the domain name associated with the session."""
if username := getattr(self, "username", None):
return username.decode("utf-8")
return None
def get_sockaddr(self) -> Optional[Tuple[str, int, int, int]]:
"""Get the socket address tuple."""
return getattr(self, "sockaddr", None)
def close_server_session(self) -> None:
"""Override server session closure with error payload."""
if hasattr(self, "clientsessionid"):
self.send_payload(
bytearray([0x00, 0x02, 0x00, 0x00]) + self.clientsessionid,
constants.payload_types["rakp2"],
retry=False,
)
super().close_server_session()
def _got_rakp1(self, data: Any) -> None:
"""Override RAKP1 handling for faster domain validation."""
if not hasattr(self, "_bmc") or not hasattr(self._bmc, "hypervisor"):
self._bmc.logger.debug("BMC or hypervisor not properly configured")
self.close_server_session()
return
try:
domain_name = bytes(data[28:]).decode("utf-8")
if not domain_name:
self._bmc.logger.debug("Empty domain name in RAKP1")
self.close_server_session()
return
with libvirt.openReadOnly(self._bmc.hypervisor) as conn:
try:
conn.lookupByName(domain_name)
self.authdata = {domain_name: self._bmc.password}
except libvirt.libvirtError:
self._bmc.logger.debug(f"Domain '{domain_name}' not found")
self.close_server_session()
return
super()._got_rakp1(data)
# pylint: disable=broad-exception-caught
except (UnicodeDecodeError, libvirt.libvirtError, Exception) as e:
self._bmc.logger.debug(f"Error in RAKP1 handling: {e}")
self.close_server_session()
return
# pylint: disable=too-many-public-methods,too-many-instance-attributes
class LibvirtIPMI(IpmiServer):
"""Baseboard Management Controller implementation for Libvirt domains using IPMI protocol."""
# pylint: disable=too-many-arguments
def __init__(
self,
address: str,
port: int,
password: str,
hypervisor: str,
logger: logging.Logger,
) -> None:
random_username = "".join(
secrets.choice(string.ascii_letters + string.digits) for _ in range(12)
)
super().__init__(
authdata={random_username: password}, address=address, port=port
)
self.logger = logger
self.password = password
self.hypervisor = hypervisor
self.lib_major_ver: int = 0
self.lib_minor_ver: int = 0
self.major_ver: int = 0
self.minor_ver: int = 0
self.conn = None
self.get_connection()
def format_bytes(self, data: Any) -> str:
"""Format byte data consistently for logging."""
match data:
case bytes() | bytearray():
return f"[{', '.join(f'0x{b:02x}' for b in data)}]"
case list():
return f"[{', '.join(f'0x{b:02x}' for b in data)}]"
case None:
return "[]"
case _:
return str(data)
def format_ipmi_message(
self, prefix: str, session: Any, code: Optional[int] = None, data: Any = None
) -> str:
"""Format IPMI message consistently for logging."""
parts = [
f"{prefix} -",
f"Domain[{session.domain if hasattr(session, 'domain') else 'Unknown'}]",
]
if prefix == "Request" and hasattr(session, "get_sockaddr"):
if clientaddr := session.get_sockaddr():
parts.extend([f"RAddr[{clientaddr[0]}]", f"RPort[{clientaddr[1]}]"])
if prefix == "Request" and isinstance(data, dict):
parts.extend(
[
f"NetFn[0x{data.get('netfn', 0):02x}]",
f"Cmd[0x{data.get('command', 0):02x}]",
f"Data{self.format_bytes(data.get('data', []))}",
]
)
if prefix == "Response":
parts.append(f"Code[0x{code:02x}]")
if data is not None:
parts.append(f"Data{self.format_bytes(data)}")
return " ".join(parts)
def send_ipmi_response(self, session: Any, code: int, data: Any) -> None:
"""Send an IPMI response to the session."""
try:
self.logger.info(self.format_ipmi_message("Response", session, code, data))
if data is not None:
session.send_ipmi_response(code=code, data=data)
else:
session.send_ipmi_response(code=code)
# pylint: disable=broad-exception-caught
except Exception as e:
if hasattr(session, "domain") and session.domain:
self.logger.error(
f"Failed to send IPMI response for domain {session.domain}: {e}"
)
else:
self.logger.error(f"Failed to send IPMI response: {e}")
def find_session(self, args: tuple, kwargs: dict) -> Optional[Any]:
"""Find and return the session from args or kwargs."""
if session := kwargs.get("session"):
if session.domain:
return session
return None
for arg in args:
if hasattr(arg, "domain"):
if arg.domain:
return arg
return None
return None
def free_connection(self) -> None:
"""Free the Libvirt connection."""
try:
if hasattr(self, "conn") and self.conn:
try:
self.logger.debug("Attempting to close Libvirt connection")
self.conn.close()
self.conn = None
self.logger.debug("Successfully closed Libvirt connection")
# pylint: disable=broad-exception-caught
except Exception as e:
self.logger.error(f"Failed to close connection: {e}")
# pylint: disable=broad-exception-caught
except Exception as e:
self.logger.error(f"Error during cleanup: {e}")
def __del__(self):
self.free_connection()
def parse_versions(self, version: int, libversion: int) -> None:
"""Parse and store version information."""
self.logger.debug(
f"Parsing versions - Raw Version: {version}, Raw Library: {libversion}"
)
if not version or not libversion:
self.logger.debug("Version or libversion is empty/zero")
raise libvirt.libvirtError("Failed to get version information")
self.lib_major_ver = libversion // 1_000_000
self.lib_minor_ver = (libversion // 1_000) % 1_000
if version < 1_000_000:
self.major_ver = version
self.minor_ver = 0
self.logger.warning(
f"Version number {version} is smaller than expected format. "
"Treating entire number as major version."
)
else:
self.major_ver = version // 1_000_000
self.minor_ver = (version // 1_000) % 1_000
self.logger.debug(
f"Parsed versions - Version: {self.major_ver}.{self.minor_ver}, "
f"Library: {self.lib_major_ver}.{self.lib_minor_ver}"
)
def get_connection(self) -> Optional[libvirt.virConnect]:
"""Get or establish a Libvirt connection."""
try:
if self.conn and self.conn.isAlive():
self.logger.debug("Reusing existing live connection")
return self.conn
if self.conn:
self.logger.debug(
"Existing connection found but not alive, attempting to close"
)
try:
self.conn.close()
# pylint: disable=broad-exception-caught
except Exception:
self.logger.debug(
"Failed to close existing connection, ignoring error"
)
# pylint: disable=unnecessary-pass
pass
self.logger.debug(
f"Attempting to open new connection to hypervisor: {self.hypervisor}"
)
self.conn = libvirt.open(self.hypervisor)
if not self.conn:
self.logger.error("Failed to get connection: undefined object")
self.conn = None
return None
self.parse_versions(self.conn.getVersion(), self.conn.getLibVersion())
self.logger.debug("Successfully established new connection")
return self.conn
except libvirt.libvirtError as e:
self.logger.error(f"Failed to get connection: {e}")
self.conn = None
return None
def get_domain(self, session: Any) -> Optional[libvirt.virDomain]:
"""Get the Libvirt domain associated with the session."""
if not hasattr(session, "domain") or not session.domain:
self.logger.debug("No domain specified in session")
return None
try:
self.logger.debug(f"Looking up domain: {session.domain}")
if conn := self.get_connection():
self.logger.debug(f"Successfully found domain: {session.domain}")
return conn.lookupByName(session.domain)
self.logger.error(f"Failed to lookup domain: {session.domain}")
return None
except libvirt.libvirtError as e:
self.logger.error(f"Failed to lookup domain: {e}")
return None
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password mc info
def get_device_id(self, session: Any) -> None:
"""Handle IPMI Get Device ID command."""
data = [
# Byte 1: Device ID
self.lib_major_ver & 0xFF,
# Byte 2: Device Revision
self.lib_minor_ver & 0xFF,
# Bytes 3-4: Firmware Revision
self.major_ver & 0b01111111,
self.minor_ver,
# Byte 5: IPMI Version
0b00000010,
# Byte 6: Additional Device Support
0b10000000,
# Bytes 7-9: Manufacturer ID
# (Libvirt https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers)
0x5D,
0x90,
0x00,
# Bytes 10-11: Product ID
0xAF,
0x10,
]
self.send_ipmi_response(session, IpmiResponseCode.SUCCESS, data)
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password mc guid
@domain_lock
def get_system_guid(self, session: Any) -> None:
"""Handle IPMI Get System GUID command."""
self.logger.debug("Getting domain GUID")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for GUID retrieval")
self.send_ipmi_response(
session, IpmiResponseCode.DESTINATION_UNAVAILABLE, None
)
return
try:
guid = UUID(domain.UUIDString())
self.logger.debug(
f"Successfully retrieved domain '{session.domain}' GUID: {guid}"
)
self.send_ipmi_response(session, IpmiResponseCode.SUCCESS, guid.bytes_le)
except (libvirt.libvirtError, ValueError) as e:
self.logger.error(f"Failed to get domain '{session.domain}' UUID: {e}")
self.send_ipmi_response(session, IpmiResponseCode.NODE_BUSY, None)
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password power off
@domain_lock
def power_off(self, session: Any) -> IpmiResponseCode:
"""Power off the virtual machine."""
self.logger.debug("Attempting to power off domain")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for power off")
return IpmiResponseCode.DESTINATION_UNAVAILABLE
try:
if not domain.isActive():
self.logger.debug(f"Domain '{session.domain}' is already powered off")
return IpmiResponseCode.SUCCESS
self.logger.debug(
f"Domain '{session.domain}' is active, sending destroy command"
)
domain.destroy()
self.logger.debug(f"Successfully powered off domain '{session.domain}'")
return IpmiResponseCode.SUCCESS
except libvirt.libvirtError as e:
self.logger.error(f"Failed to power off domain '{session.domain}': {e}")
return IpmiResponseCode.NODE_BUSY
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password power on
@domain_lock
def power_on(self, session: Any) -> IpmiResponseCode:
"""Power on the virtual machine."""
self.logger.debug("Attempting to power on domain")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for power on")
return IpmiResponseCode.DESTINATION_UNAVAILABLE
try:
if domain.isActive():
self.logger.debug(f"Domain '{session.domain}' is already powered on")
return IpmiResponseCode.SUCCESS
self.logger.debug(
f"Domain '{session.domain}' is inactive, sending create command"
)
domain.create()
self.logger.debug(f"Successfully powered on domain '{session.domain}'")
return IpmiResponseCode.SUCCESS
except libvirt.libvirtError as e:
self.logger.error(f"Failed to power on domain '{session.domain}': {e}")
return IpmiResponseCode.NODE_BUSY
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis power cycle
def power_cycle(self, session: Any) -> IpmiResponseCode:
"""Perform power cycle operation on the virtual machine."""
self.logger.debug("Starting power cycle operation")
rc = self.power_off(session)
if rc != IpmiResponseCode.SUCCESS:
self.logger.debug("Power off failed during cycle")
return rc
self.logger.debug("Power off successful, waiting before power on")
time.sleep(1)
self.logger.debug("Attempting power on after cycle delay")
return self.power_on(session)
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis power reset
@domain_lock
def power_reset(self, session: Any) -> IpmiResponseCode:
"""Perform hard reset on the virtual machine."""
self.logger.debug("Attempting domain reset")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for reset")
return IpmiResponseCode.DESTINATION_UNAVAILABLE
try:
if not domain.isActive():
self.logger.debug(f"Cannot reset inactive domain '{session.domain}'")
return IpmiResponseCode.COMMAND_NOT_SUPPORTED_IN_PRESENT_STATE
self.logger.debug(
f"Domain '{session.domain}' is active, sending reset command"
)
domain.reset()
self.logger.debug(f"Successfully reset domain '{session.domain}'")
return IpmiResponseCode.SUCCESS
except libvirt.libvirtError as e:
self.logger.error(f"Failed to reset domain '{session.domain}': {e}")
return IpmiResponseCode.NODE_BUSY
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis power diag
@domain_lock
def pulse_diag(self, session: Any) -> IpmiResponseCode:
"""Inject NMI (diagnostic interrupt) into the virtual machine."""
self.logger.debug("Attempting to inject NMI (diagnostic interrupt)")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for NMI injection")
return IpmiResponseCode.DESTINATION_UNAVAILABLE
try:
if not domain.isActive():
self.logger.debug(
f"Cannot inject NMI into inactive domain '{session.domain}'"
)
return IpmiResponseCode.COMMAND_NOT_SUPPORTED_IN_PRESENT_STATE
self.logger.debug(f"Domain '{session.domain}' is active, injecting NMI")
domain.injectNMI()
self.logger.debug(
f"Successfully injected NMI into domain '{session.domain}'"
)
return IpmiResponseCode.SUCCESS
except libvirt.libvirtError as e:
self.logger.error(
f"Failed to inject NMI into domain '{session.domain}': {e}"
)
return IpmiResponseCode.NODE_BUSY
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis power soft
@domain_lock
def power_shutdown(self, session: Any) -> IpmiResponseCode:
"""Perform soft shutdown of the virtual machine."""
self.logger.debug("Attempting soft shutdown of domain")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for shutdown")
return IpmiResponseCode.DESTINATION_UNAVAILABLE
try:
if not domain.isActive():
self.logger.debug(f"Cannot shutdown inactive domain '{session.domain}'")
return IpmiResponseCode.COMMAND_NOT_SUPPORTED_IN_PRESENT_STATE
self.logger.debug(
f"Domain '{session.domain}' is active, sending shutdown command"
)
domain.shutdown()
self.logger.debug(
f"Successfully initiated domain '{session.domain}' shutdown"
)
return IpmiResponseCode.SUCCESS
except libvirt.libvirtError as e:
self.logger.error(f"Failed to shutdown domain '{session.domain}': {e}")
return IpmiResponseCode.NODE_BUSY
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis power status
@domain_lock
def get_power_state(self, session: Any) -> int:
"""Get current power state of the virtual machine."""
self.logger.debug("Getting domain power state")
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for power state check")
raise ConnectionError("Unable to get domain")
try:
is_active = domain.isActive()
state = power_states["on"] if is_active else power_states["off"]
self.logger.debug(
f"Domain '{session.domain}' power state: {'on' if is_active else 'off'}"
)
return state
except libvirt.libvirtError as e:
self.logger.error(
f"Failed to get domain '{session.domain}' power state: {e}"
)
raise
def get_chassis_status(self, session: Any) -> None:
"""Handle IPMI Get Chassis Status command."""
self.logger.debug("Getting chassis status")
try:
power_state = self.get_power_state(session)
self.logger.debug(f"Retrieved power state: {power_state}")
match power_state:
case state if state not in (0, 1):
self.logger.debug(f"Invalid power state value: {state}")
return self.send_ipmi_response(
session, IpmiResponseCode.UNSPECIFIED_ERROR, None
)
case state:
self.logger.debug(
f"Sending chassis status response with power state: {state}"
)
self.send_ipmi_response(
session, IpmiResponseCode.SUCCESS, [state, 0, 0]
)
except ConnectionError:
self.logger.debug("Domain unavailable for chassis status")
return self.send_ipmi_response(
session, IpmiResponseCode.DESTINATION_UNAVAILABLE, None
)
except libvirt.libvirtError as e:
self.logger.debug(f"Libvirt error while getting chassis status: {e}")
return self.send_ipmi_response(session, IpmiResponseCode.NODE_BUSY, None)
except (KeyError, IndexError, TypeError, AttributeError) as e:
self.logger.debug(f"Invalid data field in chassis status request: {e}")
return self.send_ipmi_response(
session, IpmiResponseCode.INVALID_DATA_FIELD, None
)
except NotImplementedError:
self.logger.debug("Command not supported in current state")
return self.send_ipmi_response(
session, IpmiResponseCode.COMMAND_NOT_SUPPORTED_IN_PRESENT_STATE, None
)
def control_chassis(self, session: Any, request: dict[str, Any]) -> None:
"""Handle IPMI Chassis Control commands."""
try:
command = request["data"][0]
self.logger.debug(f"Processing chassis control command: {command}")
match command:
case 0:
rc = self.power_off(session)
case 1:
rc = self.power_on(session)
case 2:
rc = self.power_cycle(session)
case 3:
rc = self.power_reset(session)
case 4:
rc = self.pulse_diag(session)
case 5:
rc = self.power_shutdown(session)
case _:
self.logger.debug(
f"Received unknown chassis control command: {command}"
)
rc = IpmiResponseCode.UNSPECIFIED_ERROR
self.logger.debug(
f"Chassis control command completed with response code: {rc}"
)
self.send_ipmi_response(session, rc, None)
except (
KeyError,
IndexError,
TypeError,
AttributeError,
NotImplementedError,
) as e:
self.logger.debug(f"Invalid data in chassis control request: {e}")
self.send_ipmi_response(session, IpmiResponseCode.INVALID_DATA_FIELD, None)
def get_boot_device_name(self, domain_xml: ET.Element) -> Optional[str]:
"""Extract boot device name from domain XML."""
try:
boot_element = domain_xml.find(".//os/boot")
if boot_element is None:
return None
return boot_element.attrib.get("dev")
except ET.ParseError as e:
self.logger.error(f"Failed to parse boot device: {e}")
return None
# pylint: disable=broad-exception-caught
except Exception:
return None
def is_efi_boot(self, domain_xml: ET.Element) -> bool:
"""Check if domain is configured for EFI boot."""
try:
os_element = domain_xml.find(".//os")
if os_element is None:
return False
if os_element.get("firmware") == "efi":
return True
loader = os_element.find("loader")
if loader is None:
return False
secure_boot = loader.get("secure") == "yes"
is_ovmf_pflash = (
loader.get("type") == "pflash"
and loader.text is not None
and "OVMF" in loader.text
)
return secure_boot or is_ovmf_pflash
# pylint: disable=broad-exception-caught
except Exception:
return False
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis bootparam get 5
@domain_lock
def get_boot_options(self, session: Any, request: Dict[str, Any]) -> None:
"""Handle IPMI Get System Boot Options command."""
self.logger.debug("Processing get boot options request")
if request["data"][0] != BootOptionParameters.BOOT_FLAGS:
self.logger.debug(
f"Unsupported boot parameter requested: {request['data'][0]}"
)
self.send_ipmi_response(session, IpmiResponseCode.COMMAND_DISABLED, None)
return
try:
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for boot options")
self.send_ipmi_response(
session, IpmiResponseCode.DESTINATION_UNAVAILABLE, None
)
return
try:
self.logger.debug(f"Getting domain '{session.domain}' XML description")
domain_xml = ET.fromstring(
domain.XMLDesc(libvirt.VIR_DOMAIN_XML_INACTIVE)
)
name = self.get_boot_device_name(domain_xml)
if not name:
self.logger.debug(
f"No boot device name found in domain '{session.domain}'"
)
raise NotImplementedError
self.logger.debug(
f"Converting Libvirt boot device '{name}' for domain '{session.domain}' to IPMI format"
)
raw = BootDevice.from_libvirt(name)
if not raw:
self.logger.debug(
f"Failed to convert boot device for domain '{session.domain}' to IPMI format"
)
raise NotImplementedError
boot_flags = BootFlags.VALID | BootFlags.PERSISTENT
self.logger.debug(f"Checking for EFI boot in domain '{session.domain}'")
if self.is_efi_boot(domain_xml):
self.logger.debug(f"EFI boot detected in domain '{session.domain}'")
boot_flags |= BootFlags.EFI
else:
self.logger.debug(
f"Legacy BIOS boot detected in domain '{session.domain}'"
)
boot_flags |= BootFlags.LEGACY_BIOS
self.logger.debug(
f"Sending boot options response with flags: {boot_flags:08b}, device: {raw.ipmi_code}, domain '{session.domain}'"
)
self.send_ipmi_response(
session,
IpmiResponseCode.SUCCESS,
[
1, # Version
BootOptionParameters.BOOT_FLAGS,
boot_flags,
raw.ipmi_code << 2,
0, # Reserved
0, # Reserved
0, # Reserved
],
)
except (libvirt.libvirtError, ET.ParseError) as e:
self.logger.error(
f"Failed to get domain '{session.domain}' boot options: {e}"
)
self.send_ipmi_response(session, IpmiResponseCode.NODE_BUSY, None)
return
except NotImplementedError:
self.logger.debug("Sending default boot options response")
self.send_ipmi_response(
session,
IpmiResponseCode.SUCCESS,
[
1, # Version
BootOptionParameters.BOOT_FLAGS,
BootFlags.DEFAULT,
0, # Default
0, # Reserved
0, # Reserved
0, # Reserved
],
)
# pylint: disable=broad-exception-caught
except Exception as e:
self.logger.error(f"Failed to get domain boot options: {e}")
self.send_ipmi_response(session, IpmiResponseCode.UNSPECIFIED_ERROR, None)
def update_boot_device(self, tree: ET.Element, boot_device_name: str) -> None:
"""Update boot device in domain XML configuration."""
if tree is None or not isinstance(boot_device_name, str):
raise ValueError("Invalid arguments for update_boot_device")
for device_element in tree.findall("devices/*"):
for boot_element in device_element.findall("boot"):
device_element.remove(boot_element)
os_element = tree.find("os")
if os_element is not None:
for boot_element in os_element.findall("boot"):
os_element.remove(boot_element)
boot_element = ET.SubElement(os_element, "boot")
boot_element.set("dev", boot_device_name)
# ipmitool -I lanplus -H 127.0.0.1 -U vm -P password chassis bootdev pxe|disk|cdrom|floppy
@domain_lock
def set_boot_options(self, session: Any, request: Dict[str, Any]) -> None:
"""Handle IPMI Set System Boot Options command."""
self.logger.debug("Processing set boot options request")
if request["data"][0] in (
BootOptionParameters.SET_IN_PROGRESS,
BootOptionParameters.BOOT_FLAG_VALID_BIT_CLEARING,
BootOptionParameters.BOOT_INFO_ACKNOWLEDGE,
):
self.logger.debug(
f"Ignoring unsupported boot parameter: {request['data'][0]}"
)
self.send_ipmi_response(session, IpmiResponseCode.SUCCESS, None)
return
if request["data"][0] != BootOptionParameters.BOOT_FLAGS:
self.logger.debug(f"Invalid boot parameter requested: {request['data'][0]}")
self.send_ipmi_response(session, IpmiResponseCode.INVALID_DATA_FIELD, None)
return
# Extract boot device from request data
raw = (request["data"][2] >> 2) & 0b1111
self.logger.debug(f"Extracted raw boot device code: {raw}")
try:
boot_device = BootDevice.from_ipmi(raw)
if not boot_device:
self.logger.debug(f"Invalid IPMI boot device code: {raw}")
self.send_ipmi_response(
session, IpmiResponseCode.INVALID_DATA_FIELD, None
)
return
self.logger.debug(
f"Converted IPMI boot device to Libvirt name: {boot_device.libvirt_name}"
)
domain = self.get_domain(session)
if not domain:
self.logger.debug("Domain unavailable for setting boot device")
self.send_ipmi_response(
session, IpmiResponseCode.DESTINATION_UNAVAILABLE, None
)
return
self.logger.debug(f"Getting domain '{session.domain}' XML description")
tree = ET.fromstring(
domain.XMLDesc(
flags=libvirt.VIR_DOMAIN_XML_SECURE
| libvirt.VIR_DOMAIN_XML_INACTIVE
)
)
self.logger.debug(
f"Updating domain '{session.domain}' boot device in XML to: {boot_device.libvirt_name}"
)
self.update_boot_device(tree, boot_device.libvirt_name)
try:
if conn := self.get_connection():
self.logger.debug(
f"Applying new domain '{session.domain}' XML configuration"
)
xml_str = ET.tostring(tree, encoding="unicode", method="xml")
conn.defineXML(xml_str)
self.logger.debug(
f"Successfully updated domain '{session.domain}' boot device"
)
self.send_ipmi_response(session, IpmiResponseCode.SUCCESS, None)
return
except libvirt.libvirtError as e:
self.logger.error(
f"Failed to define new domain '{session.domain}' XML configuration: {e}"
)
self.send_ipmi_response(session, IpmiResponseCode.NODE_BUSY, None)
return
except libvirt.libvirtError as e:
self.logger.error(f"Failed to set domain boot device: {e}")
self.send_ipmi_response(session, IpmiResponseCode.NODE_BUSY, None)
# pylint: disable=broad-exception-caught
except Exception as e:
self.logger.error(f"Failed to set domain boot device: {e}")
self.send_ipmi_response(session, IpmiResponseCode.UNSPECIFIED_ERROR, None)
# pylint: disable=too-many-return-statements