Skip to content

Commit

Permalink
configure dns_client SMF service
Browse files Browse the repository at this point in the history
  • Loading branch information
karencfv committed Feb 27, 2024
1 parent 9554ea2 commit 5083848
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 82 deletions.
144 changes: 77 additions & 67 deletions sled-agent/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use crate::params::{
};
use crate::profile::*;
use crate::services_migration::{AllZoneRequests, SERVICES_LEDGER_FILENAME};
use crate::smf_helper::Service;
use crate::smf_helper::SmfHelper;
use crate::zone_bundle::BundleError;
use crate::zone_bundle::ZoneBundler;
Expand Down Expand Up @@ -1339,61 +1338,61 @@ impl ServiceManager {
}

// TODO: Set up a new service for this
async fn configure_dns_client(
&self,
running_zone: &RunningZone,
dns_servers: &[IpAddr],
domain: &Option<String>,
) -> Result<(), Error> {
struct DnsClient {}

impl crate::smf_helper::Service for DnsClient {
fn service_name(&self) -> String {
"dns_client".to_string()
}
fn smf_name(&self) -> String {
"svc:/network/dns/client".to_string()
}
fn should_import(&self) -> bool {
false
}
}

let service = DnsClient {};
let smfh = SmfHelper::new(&running_zone, &service);

let etc = running_zone.root().join("etc");
let resolv_conf = etc.join("resolv.conf");
let nsswitch_conf = etc.join("nsswitch.conf");
let nsswitch_dns = etc.join("nsswitch.dns");

if dns_servers.is_empty() {
// Disable the dns/client service
smfh.disable()?;
} else {
debug!(self.inner.log, "enabling {:?}", service.service_name());
let mut config = String::new();
if let Some(d) = domain {
config.push_str(&format!("domain {d}\n"));
}
for s in dns_servers {
config.push_str(&format!("nameserver {s}\n"));
}

debug!(self.inner.log, "creating {resolv_conf}");
tokio::fs::write(&resolv_conf, config)
.await
.map_err(|err| Error::io_path(&resolv_conf, err))?;

tokio::fs::copy(&nsswitch_dns, &nsswitch_conf)
.await
.map_err(|err| Error::io_path(&nsswitch_dns, err))?;

smfh.refresh()?;
smfh.enable()?;
}
Ok(())
}
// async fn configure_dns_client(
// &self,
// running_zone: &RunningZone,
// dns_servers: &[IpAddr],
// domain: &Option<String>,
// ) -> Result<(), Error> {
// struct DnsClient {}
//
// impl crate::smf_helper::Service for DnsClient {
// fn service_name(&self) -> String {
// "dns_client".to_string()
// }
// fn smf_name(&self) -> String {
// "svc:/network/dns/client".to_string()
// }
// fn should_import(&self) -> bool {
// false
// }
// }
//
// let service = DnsClient {};
// let smfh = SmfHelper::new(&running_zone, &service);
//
// let etc = running_zone.root().join("etc");
// let resolv_conf = etc.join("resolv.conf");
// let nsswitch_conf = etc.join("nsswitch.conf");
// let nsswitch_dns = etc.join("nsswitch.dns");
//
// if dns_servers.is_empty() {
// // Disable the dns/client service
// smfh.disable()?;
// } else {
// debug!(self.inner.log, "enabling {:?}", service.service_name());
// let mut config = String::new();
// if let Some(d) = domain {
// config.push_str(&format!("domain {d}\n"));
// }
// for s in dns_servers {
// config.push_str(&format!("nameserver {s}\n"));
// }
//
// debug!(self.inner.log, "creating {resolv_conf}");
// tokio::fs::write(&resolv_conf, config)
// .await
// .map_err(|err| Error::io_path(&resolv_conf, err))?;
//
// tokio::fs::copy(&nsswitch_dns, &nsswitch_conf)
// .await
// .map_err(|err| Error::io_path(&nsswitch_dns, err))?;
//
// smfh.refresh()?;
// smfh.enable()?;
// }
// Ok(())
// }

async fn dns_install(
info: &SledAgentInfo,
Expand Down Expand Up @@ -1944,7 +1943,9 @@ impl ServiceManager {
zone:
OmicronZoneConfig {
zone_type:
OmicronZoneType::BoundaryNtp { ntp_servers, .. },
OmicronZoneType::BoundaryNtp {
ntp_servers, domain, ..
},
underlay_address,
..
},
Expand All @@ -1954,7 +1955,9 @@ impl ServiceManager {
zone:
OmicronZoneConfig {
zone_type:
OmicronZoneType::InternalNtp { ntp_servers, .. },
OmicronZoneType::InternalNtp {
ntp_servers, domain, ..
},
underlay_address,
..
},
Expand Down Expand Up @@ -1991,8 +1994,11 @@ impl ServiceManager {
)
.to_string();

let domain = if let Some(d) = domain { d } else { "unknown" };

let ntp_config = PropertyGroupBuilder::new("config")
.add_property("allow", "astring", &rack_net)
.add_property("domain", "astring", domain)
.add_property("boundary", "boolean", &is_boundary);

for server in ntp_servers.clone() {
Expand All @@ -2001,8 +2007,18 @@ impl ServiceManager {
.add_property("server", "astring", &server);
}

let disabled_dns_client_service = ServiceBuilder::new("network/dns/client")
.add_instance(ServiceInstanceBuilder::new("default").disable());
let dns_client_service;
if ntp_servers.is_empty() {
dns_client_service =
ServiceBuilder::new("network/dns/client").add_instance(
ServiceInstanceBuilder::new("default").disable(),
);
} else {
dns_client_service = ServiceBuilder::new(
"network/dns/client",
)
.add_instance(ServiceInstanceBuilder::new("default"));
}

let ntp_service = ServiceBuilder::new("oxide/ntp")
.add_instance(
Expand All @@ -2016,15 +2032,9 @@ impl ServiceManager {
// But then I can't make the service depend on it (≖_≖ )
.add_service(opte_interface_setup)
.add_service(disabled_ssh_service)
.add_service(dns_client_service)
.add_service(ntp_service);

if ntp_servers.is_empty() {
profile.clone().add_service(disabled_dns_client_service);
} else {
// Enable dns client service
todo!()
}

profile
.add_to_zone(&self.inner.log, &installed_zone)
.await
Expand Down
28 changes: 14 additions & 14 deletions sled-agent/src/smf_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,18 @@ impl<'t> SmfHelper<'t> {
Ok(())
}

pub fn disable(&self) -> Result<(), Error> {
self.running_zone
.run_cmd(&[
illumos_utils::zone::SVCADM,
"disable",
"-t",
&self.default_smf_name,
])
.map_err(|err| Error::ZoneCommand {
intent: format!("Disable {} service", self.default_smf_name),
err,
})?;
Ok(())
}
// pub fn disable(&self) -> Result<(), Error> {
// self.running_zone
// .run_cmd(&[
// illumos_utils::zone::SVCADM,
// "disable",
// "-t",
// &self.default_smf_name,
// ])
// .map_err(|err| Error::ZoneCommand {
// intent: format!("Disable {} service", self.default_smf_name),
// err,
// })?;
// Ok(())
// }
}
1 change: 1 addition & 0 deletions smf/ntp/manifest/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<propval name="boundary" type="boolean" value="false" />
<propval name="server" type="astring" value="" />
<propval name="allow" type="astring" value="" />
<propval name="domain" type="astring" value="unknown" />
</property_group>

<stability value="Unstable" />
Expand Down
23 changes: 22 additions & 1 deletion smf/ntp/method/svc-site-ntp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function config { die $SMF_EXIT_ERR_CONFIG "$@"; }
typeset -r action=${1:?action parameter not specified}
typeset -r contract=$2 # For the refresh and stop methods

for var in file boundary server allow; do
for var in file boundary server allow domain; do
nameref _var=$var

typeset _var=`svcprop -p config/$var $SMF_FMRI`
Expand Down Expand Up @@ -106,13 +106,34 @@ function update_logadm {
svcadm refresh logadm-upgrade
}

function configure_dns_client {
if [[ $boundary == true ]]; then
echo "* Generating resolv.conf file"
if [[ $domain != "unknown" ]]; then
echo "domain $domain" >> /etc/resolv.conf
fi

for s in $server; do
echo "nameserver $s" >> /etc/resolv.conf
done

echo "* Copying nsswitch.dns to nsswitch.conf"
cp /etc/nsswitch.dns /etc/nsswitch.conf

svcadm refresh -t svc:/network/dns/client
svcadm enable -t svc:/network/dns/client
done
}

case $action in
start)
configure_dns_client
generate_config_file
update_logadm
start_daemon
;;
refresh)
configure_dns_client
generate_config_file && stop_daemon
# SMF will restart the service since the contract is now empty.
;;
Expand Down

0 comments on commit 5083848

Please sign in to comment.