Skip to content

Commit ba485f0

Browse files
committed
Add IP configuration options to load command
- Introduced `--ip`, `--gateway`, `--netmask`, `--nic`, and `--hostname` options for the load command to support static and DHCP IP configurations. - Implemented `build_ip_param` function to construct the kernel boot parameter string based on the provided network settings. - Updated command line preparation to include the generated IP parameter if specified. Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent f1c9889 commit ba485f0

1 file changed

Lines changed: 74 additions & 1 deletion

File tree

src/kerf/load/main.py

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,50 @@ def KEXEC_MK_ID(id: int) -> int: # pylint: disable=invalid-name
4747
SYS_KEXEC_FILE_LOAD_X86 = 320
4848

4949

50+
def build_ip_param(
51+
ip_addr: Optional[str],
52+
gateway: Optional[str],
53+
netmask: str,
54+
hostname: Optional[str],
55+
nic: Optional[str],
56+
) -> Optional[str]:
57+
"""
58+
Build Linux kernel ip= boot parameter.
59+
60+
Format: ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>
61+
62+
Args:
63+
ip_addr: Client IP address or "dhcp"
64+
gateway: Gateway IP address
65+
netmask: Network mask
66+
hostname: Hostname
67+
nic: Network interface name
68+
69+
Returns:
70+
ip= parameter string, or None if no IP configuration
71+
"""
72+
if not ip_addr:
73+
return None
74+
75+
if ip_addr.lower() == "dhcp":
76+
if nic:
77+
return f"ip=:::::::{nic}:dhcp"
78+
return "ip=dhcp"
79+
80+
# Static IP configuration
81+
# Format: ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>
82+
parts = [
83+
ip_addr, # client-ip
84+
"", # server-ip (empty)
85+
gateway or "", # gw-ip
86+
netmask, # netmask
87+
hostname or "", # hostname
88+
nic or "", # device
89+
"off", # autoconf (off for static)
90+
]
91+
return "ip=" + ":".join(parts)
92+
93+
5094
def get_kexec_file_load_syscall():
5195
"""Get the kexec_file_load syscall number for current architecture."""
5296
arch = platform.machine().lower()
@@ -163,6 +207,11 @@ def kexec_file_load(
163207
@click.option("--image", help="Docker image to use as rootfs (e.g., nginx:latest)")
164208
@click.option("--entrypoint", help="Override image entrypoint for init")
165209
@click.option("--rootfs-dir", help="Use existing directory as rootfs instead of Docker image")
210+
@click.option("--ip", "ip_addr", help="IP address for spawn kernel (or 'dhcp')")
211+
@click.option("--gateway", help="Default gateway IP address")
212+
@click.option("--netmask", default="255.255.255.0", help="Network mask (default: 255.255.255.0)")
213+
@click.option("--nic", help="Network interface name (e.g., eth0)")
214+
@click.option("--hostname", help="Hostname for spawn kernel")
166215
@click.option("--verbose", "-v", is_flag=True, help="Verbose output")
167216
def load( # pylint: disable=too-many-arguments,too-many-positional-arguments,too-many-locals
168217
ctx: click.Context,
@@ -174,6 +223,11 @@ def load( # pylint: disable=too-many-arguments,too-many-positional-arguments,to
174223
image: Optional[str],
175224
entrypoint: Optional[str],
176225
rootfs_dir: Optional[str],
226+
ip_addr: Optional[str],
227+
gateway: Optional[str],
228+
netmask: str,
229+
nic: Optional[str],
230+
hostname: Optional[str],
177231
verbose: bool,
178232
):
179233
"""
@@ -203,6 +257,14 @@ def load( # pylint: disable=too-many-arguments,too-many-positional-arguments,to
203257
# Load with pre-extracted rootfs directory
204258
kerf load custom --kernel=/boot/vmlinuz --rootfs-dir=/mnt/rootfs \\
205259
--entrypoint=/sbin/init
260+
261+
# Load with static IP configuration
262+
kerf load web-server --kernel=/boot/vmlinuz --image=nginx:latest \\
263+
--ip=192.168.1.100 --gateway=192.168.1.1 --nic=eth0
264+
265+
# Load with DHCP
266+
kerf load web-server --kernel=/boot/vmlinuz --image=nginx:latest \\
267+
--ip=dhcp --nic=eth0
206268
"""
207269
try:
208270
if not name and id is None:
@@ -383,7 +445,18 @@ def load( # pylint: disable=too-many-arguments,too-many-positional-arguments,to
383445
)
384446

385447
# Prepare command line
386-
cmdline_str = cmdline if cmdline else ""
448+
cmdline_parts = []
449+
if cmdline:
450+
cmdline_parts.append(cmdline)
451+
452+
# Add IP configuration if specified
453+
ip_param = build_ip_param(ip_addr, gateway, netmask, hostname, nic)
454+
if ip_param:
455+
cmdline_parts.append(ip_param)
456+
if verbose:
457+
click.echo(f"Network config: {ip_param}")
458+
459+
cmdline_str = " ".join(cmdline_parts)
387460

388461
if verbose:
389462
click.echo(f"Command line: {cmdline_str if cmdline_str else '(empty)'}")

0 commit comments

Comments
 (0)