guest: set hostname from launch config

This commit is contained in:
Alex Zenla 2024-03-31 03:18:56 +00:00
parent 6cd8cc12db
commit 377b837db9
No known key found for this signature in database
GPG Key ID: 067B238899B51269
3 changed files with 20 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use ipnetwork::IpNetwork;
use krata::ethtool::EthtoolHandle;
use krata::idm::client::IdmClient;
use krata::launchcfg::{LaunchInfo, LaunchNetwork};
use libc::{setsid, TIOCSCTTY};
use libc::{sethostname, setsid, TIOCSCTTY};
use log::{trace, warn};
use nix::ioctl_write_int_bad;
use nix::unistd::{dup2, execve, fork, ForkResult, Pid};
@ -91,6 +91,18 @@ impl GuestInit {
self.nuke_initrd().await?;
self.bind_new_root().await?;
if let Some(hostname) = launch.hostname.clone() {
let result = unsafe {
sethostname(
hostname.as_bytes().as_ptr() as *mut libc::c_char,
hostname.len(),
)
};
if result != 0 {
warn!("failed to set hostname: {}", result);
}
}
if let Some(network) = &launch.network {
trace!("initializing network");
if let Err(error) = self.network_setup(network).await {

View File

@ -29,6 +29,7 @@ pub struct LaunchNetwork {
#[derive(Serialize, Deserialize, Debug)]
pub struct LaunchInfo {
pub hostname: Option<String>,
pub network: Option<LaunchNetwork>,
pub env: HashMap<String, String>,
pub run: Option<Vec<String>>,

View File

@ -64,6 +64,12 @@ impl GuestLauncher {
let ipv6_network_mask: u32 = 10;
let launch_config = LaunchInfo {
hostname: Some(
request
.name
.map(|x| x.to_string())
.unwrap_or_else(|| format!("krata-{}", uuid)),
),
network: Some(LaunchNetwork {
link: "eth0".to_string(),
ipv4: LaunchNetworkIpv4 {