Skip to content

Commit

Permalink
Move the modification of network-scripts configuration to distro-inde…
Browse files Browse the repository at this point in the history
…pendent config
  • Loading branch information
nullpo-head committed Oct 27, 2021
1 parent da031d6 commit 80f4031
Showing 1 changed file with 14 additions and 39 deletions.
53 changes: 14 additions & 39 deletions distrod/libs/src/distro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,20 @@ fn disable_incompatible_systemd_network_configuration(
let path = path?;
fs::remove_file(&path).with_context(|| format!("Failed to remove '{:?}'.", &path))?;
}
// Remove network-scripts configurations
let path_to_network =
ContainerPath::new("/etc/sysconfig/network-scripts/ifcfg-eth0")?.to_host_path(rootfs);
if path_to_network.exists() {
let backup_name =
ContainerPath::new("/etc/sysconfig/network-scripts/disabled-by-distrod.ifcfg-eth0")?
.to_host_path(rootfs);
fs::rename(&path_to_network, &backup_name).with_context(|| {
format!(
"Failed to move {:?} to {:?}",
&path_to_network, &backup_name
)
})?;
}
// Remove /etc/resolv.conf so that systemd knows it shouldn't touch resolv.conf
if overwrites_potential_userfiles {
let resolv_conf_path = &ContainerPath::new("/etc/resolv.conf")?.to_host_path(rootfs);
Expand Down Expand Up @@ -645,21 +659,13 @@ fn do_distro_specific_initialization(
match detect_distro(rootfs).with_context(|| "Failed to detect distro.")? {
Debian | Kali => initialize_debian_rootfs(rootfs, overwrites_potential_userfiles)
.with_context(|| "Failed to do initialization for debian-based distros."),
Centos | Oracle | AlmaLinux | RockyLinux => {
initialize_centos_rootfs(rootfs, overwrites_potential_userfiles)
.with_context(|| "Failed to do initialization for centos-based distros.")
}
_ => Ok(()),
}
}

enum DistroName {
Debian,
Kali,
Centos,
AlmaLinux,
Oracle,
RockyLinux,
Undetected,
}

Expand All @@ -676,10 +682,6 @@ fn detect_distro(rootfs: &HostPath) -> Result<DistroName> {
match os_release?.get_env("ID").map(strip_quotes) {
Some("debian") => Ok(DistroName::Debian),
Some("kali") => Ok(DistroName::Kali),
Some("centos") => Ok(DistroName::Centos),
Some("almalinux") => Ok(DistroName::AlmaLinux),
Some("ol") => Ok(DistroName::Oracle),
Some("rocky") => Ok(DistroName::RockyLinux),
_ => Ok(DistroName::Undetected),
}
}
Expand Down Expand Up @@ -733,33 +735,6 @@ fn put_readenv_in_sudo_pam(rootfs: &HostPath) -> Result<()> {
Ok(())
}

fn initialize_centos_rootfs(
rootfs: &HostPath,
_overwrites_potential_userfiles: bool,
) -> Result<()> {
disable_centos_network_initialization(rootfs)
.with_context(|| "Failed to disable CentOS-based network initialization.")?;
Ok(())
}

/// Prevent the network initialization of CentOS-based distros from resetting WSL's network interfaces
fn disable_centos_network_initialization(rootfs: &HostPath) -> Result<()> {
let path_to_network =
ContainerPath::new("/etc/sysconfig/network-scripts/ifcfg-eth0")?.to_host_path(rootfs);
if path_to_network.exists() {
let backup_name =
ContainerPath::new("/etc/sysconfig/network-scripts/disabled-by-distrod.ifcfg-eth0")?
.to_host_path(rootfs);
fs::rename(&path_to_network, &backup_name).with_context(|| {
format!(
"Failed to move {:?} to {:?}",
&path_to_network, &backup_name
)
})?;
}
Ok(())
}

fn export_distro_run_info(rootfs: &Path, init_pid: u32) -> Result<()> {
if let Ok(Some(_)) = get_distro_run_info_file(false, false) {
fs::remove_file(&get_distro_run_info_path()?)
Expand Down

0 comments on commit 80f4031

Please sign in to comment.