1616Kernel shutdown subcommand implementation using reboot syscall with MULTIKERNEL_HALT command.
1717"""
1818
19- import click
20- import sys
21- import os
2219import ctypes
20+ import os
2321import platform
22+ import sys
2423from pathlib import Path
2524from typing import Optional
25+
26+ import click
27+
2628from ..models import InstanceState
2729from ..utils import get_instance_id_from_name
2830
@@ -42,18 +44,17 @@ def get_reboot_syscall():
4244 arch = platform .machine ().lower ()
4345 if arch in ('x86_64' , 'amd64' ):
4446 return SYS_REBOOT_X86_64
45- elif arch in ('aarch64' , 'arm64' ):
47+ if arch in ('aarch64' , 'arm64' ):
4648 return SYS_REBOOT_ARM64
47- elif arch .startswith ('arm' ):
49+ if arch .startswith ('arm' ):
4850 return SYS_REBOOT_ARM
49- elif arch in ('i386' , 'i686' , 'x86' ):
51+ if arch in ('i386' , 'i686' , 'x86' ):
5052 return SYS_REBOOT_X86
51- else :
52- click .echo (
53- f"Warning: Unknown architecture '{ arch } ', assuming x86_64 syscall number" ,
54- err = True
55- )
56- return SYS_REBOOT_X86_64
53+ click .echo (
54+ f"Warning: Unknown architecture '{ arch } ', assuming x86_64 syscall number" ,
55+ err = True
56+ )
57+ return SYS_REBOOT_X86_64
5758
5859
5960class MultikernelBootArgs (ctypes .Structure ):
@@ -62,6 +63,10 @@ class MultikernelBootArgs(ctypes.Structure):
6263 ("mk_id" , ctypes .c_int ),
6364 ]
6465
66+ def __init__ (self ):
67+ super ().__init__ ()
68+ self .mk_id = 0
69+
6570
6671def halt_multikernel (mk_id : int ) -> int :
6772 libc = ctypes .CDLL (None , use_errno = True )
@@ -137,7 +142,7 @@ def kill_cmd(name: Optional[str], id: Optional[int], verbose: bool):
137142 err = True
138143 )
139144 click .echo (
140- f "Check available instances in /sys/fs/multikernel/instances/" ,
145+ "Check available instances in /sys/fs/multikernel/instances/" ,
141146 err = True
142147 )
143148 sys .exit (1 )
@@ -169,7 +174,7 @@ def kill_cmd(name: Optional[str], id: Optional[int], verbose: bool):
169174 err = True
170175 )
171176 click .echo (
172- f "Check available instances in /sys/fs/multikernel/instances/" ,
177+ "Check available instances in /sys/fs/multikernel/instances/" ,
173178 err = True
174179 )
175180 sys .exit (1 )
@@ -187,7 +192,7 @@ def kill_cmd(name: Optional[str], id: Optional[int], verbose: bool):
187192 sys .exit (1 )
188193
189194 try :
190- with open (status_path , 'r' ) as f :
195+ with open (status_path , 'r' , encoding = 'utf-8' ) as f :
191196 status = f .read ().strip ()
192197
193198 status_lower = status .lower ()
@@ -249,10 +254,9 @@ def kill_cmd(name: Optional[str], id: Optional[int], verbose: bool):
249254 )
250255 sys .exit (1 )
251256
252- except Exception as e :
253- click .echo (f"Unexpected error: { e } " , err = True )
257+ except Exception as exc :
258+ click .echo (f"Unexpected error: { exc } " , err = True )
254259 if verbose :
255260 import traceback
256261 traceback .print_exc ()
257262 sys .exit (1 )
258-
0 commit comments