From d52310c0f49b08f1c34a06b9117dd56518f6aa7d Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Wed, 17 Jun 2026 10:52:00 +0200 Subject: [PATCH] feat: set unique hostname in QEMU VM mode Generate hostname like "ecr-vm-" at boot time via kernel command line, leaving initramfs cacheable. Set via echo to /etc/hostname and hostname command before spawning shell. --- src/qemu_vm.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/qemu_vm.rs b/src/qemu_vm.rs index ec4d1de..2fa910f 100644 --- a/src/qemu_vm.rs +++ b/src/qemu_vm.rs @@ -54,21 +54,33 @@ pub fn launch_qemu(config: QemuConfig) -> Result<()> { qemu_bin, get_arch_package_suffix(&config.arch), get_arch_package_suffix(&config.arch), get_arch_package_suffix(&config.arch) ))?; + // Generate a unique hostname like "ecr-vm-a1b2c3" + let hostname_suffix = format!("{:x}", (std::process::id() as u64) + .wrapping_mul(std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap_or_default() + .as_nanos() as u64) % 0x1000000); // 6 hex chars + let hostname = format!("ecr-vm-{}", hostname_suffix); + // Build kernel command line // For initramfs boot, use rdinit= instead of init= // No root= needed as initramfs becomes the rootfs // 'quiet' suppresses kernel log messages for a cleaner console // Use setsid with redirected stdio to get proper job control // /dev/ttyS0 is created in the initramfs for serial console + // Set hostname before spawning shell let kernel_append = if let Some(ref cmd) = config.command { let cmd_str = cmd.join(" "); format!( - "console=ttyS0 quiet rdinit=/bin/sh -- -c \"setsid sh -c 'exec sh /dev/ttyS0 2>&1 -c {}'\"", - cmd_str + "console=ttyS0 quiet rdinit=/bin/sh -- -c \"echo {} >/etc/hostname; hostname {}; setsid sh -c 'exec sh /dev/ttyS0 2>&1 -c {}'\"", + hostname, hostname, cmd_str ) } else { // Default to interactive shell with proper job control - "console=ttyS0 quiet rdinit=/bin/sh -- -c \"setsid sh -c 'exec sh /dev/ttyS0 2>&1'\"".to_string() + format!( + "console=ttyS0 quiet rdinit=/bin/sh -- -c \"echo {} >/etc/hostname; hostname {}; setsid sh -c 'exec sh /dev/ttyS0 2>&1'\"", + hostname, hostname + ) }; veprintln!("Launching QEMU: {}", qemu_bin);