feat: set unique hostname in QEMU VM mode
Generate hostname like "ecr-vm-<hex>" at boot time via kernel command line, leaving initramfs cacheable. Set via echo to /etc/hostname and hostname command before spawning shell.
This commit is contained in:
+15
-3
@@ -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)
|
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
|
// Build kernel command line
|
||||||
// For initramfs boot, use rdinit= instead of init=
|
// For initramfs boot, use rdinit= instead of init=
|
||||||
// No root= needed as initramfs becomes the rootfs
|
// No root= needed as initramfs becomes the rootfs
|
||||||
// 'quiet' suppresses kernel log messages for a cleaner console
|
// 'quiet' suppresses kernel log messages for a cleaner console
|
||||||
// Use setsid with redirected stdio to get proper job control
|
// Use setsid with redirected stdio to get proper job control
|
||||||
// /dev/ttyS0 is created in the initramfs for serial console
|
// /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 kernel_append = if let Some(ref cmd) = config.command {
|
||||||
let cmd_str = cmd.join(" ");
|
let cmd_str = cmd.join(" ");
|
||||||
format!(
|
format!(
|
||||||
"console=ttyS0 quiet rdinit=/bin/sh -- -c \"setsid sh -c 'exec sh </dev/ttyS0 >/dev/ttyS0 2>&1 -c {}'\"",
|
"console=ttyS0 quiet rdinit=/bin/sh -- -c \"echo {} >/etc/hostname; hostname {}; setsid sh -c 'exec sh </dev/ttyS0 >/dev/ttyS0 2>&1 -c {}'\"",
|
||||||
cmd_str
|
hostname, hostname, cmd_str
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
// Default to interactive shell with proper job control
|
// Default to interactive shell with proper job control
|
||||||
"console=ttyS0 quiet rdinit=/bin/sh -- -c \"setsid sh -c 'exec sh </dev/ttyS0 >/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 >/dev/ttyS0 2>&1'\"",
|
||||||
|
hostname, hostname
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
veprintln!("Launching QEMU: {}", qemu_bin);
|
veprintln!("Launching QEMU: {}", qemu_bin);
|
||||||
|
|||||||
Reference in New Issue
Block a user