Skip to content

Commit

Permalink
ntp start command works except for starting daemon bit
Browse files Browse the repository at this point in the history
  • Loading branch information
karencfv committed Apr 5, 2024
1 parent 19d47b5 commit bb15ccc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 20 deletions.
24 changes: 24 additions & 0 deletions illumos-utils/src/chronyd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Utilities for interacting with chronyd.
use crate::zone::CHRONYD;
use crate::{execute, ExecutionError, PFEXEC};

/// Wraps commands for interacting with chronyd.
pub struct Chronyd {}

#[cfg_attr(any(test, feature = "testing"), mockall::automock)]
impl Chronyd {
pub fn start_daemon(file: &str) -> Result<(), ExecutionError> {
let mut cmd = std::process::Command::new(PFEXEC);
// TODO: This doesn't seem to be working. I think `execute()`
// doesn't like the "&", and it immediately exits after running.
// find a way to keep the process going.
let cmd = cmd.args(&[CHRONYD, "-d", "-f", file, "&"]);
execute(cmd)?;
Ok(())
}
}
21 changes: 21 additions & 0 deletions illumos-utils/src/svcadm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Utilities for manipulating SMF services.
use crate::zone::SVCADM;
use crate::{execute, ExecutionError, PFEXEC};

/// Wraps commands for interacting with svcadm.
pub struct Svcadm {}

#[cfg_attr(any(test, feature = "testing"), mockall::automock)]
impl Svcadm {
pub fn refresh_logadm_upgrade() -> Result<(), ExecutionError> {
let mut cmd = std::process::Command::new(PFEXEC);
let cmd = cmd.args(&[SVCADM, "refresh", "logadm-upgrade"]);
execute(cmd)?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion smf/ntp/etc/inet/chrony.conf.boundary
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

# TODO: Removeme once svc-site-ntp file is gone
pool @SERVER@ iburst maxdelay 0.1 maxsources 16
# pool @SERVER@ iburst maxdelay 0.1 maxsources 16

driftfile /var/lib/chrony/drift
ntsdumpdir /var/lib/chrony
Expand Down
2 changes: 1 addition & 1 deletion smf/ntp/etc/inet/chrony.conf.internal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#

# TODO: Removeme once svc-site-ntp file is gone
server @SERVER@ iburst minpoll 0 maxpoll 4
# server @SERVER@ iburst minpoll 0 maxpoll 4

driftfile /var/lib/chrony/drift
ntsdumpdir /var/lib/chrony
Expand Down
2 changes: 1 addition & 1 deletion smf/ntp/manifest/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<!-- TODO: Check what the /etc/logadm.d/chrony.logadm.conf file says in the previous one -->
<exec_method type="method" name="start"
exec='/opt/oxide/zone-setup-cli/bin/zone-setup ntp start -f %{config/file} -b %{config/boundary} -s %{config/server} -a %{config/allow}'
exec='/opt/oxide/zone-setup-cli/bin/zone-setup ntp start -b %{config/boundary} -s %{config/server} -a %{config/allow}'
timeout_seconds="60">
<method_context security_flags="aslr">
<method_credential user="root" group="root"
Expand Down
46 changes: 29 additions & 17 deletions zone-setup/src/bin/zone-setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! CLI to set up zone networking
//! CLI to set up zone configuration
use anyhow::anyhow;
use clap::{arg, command, value_parser, Arg, ArgMatches, Command};
use illumos_utils::chronyd::Chronyd;
use illumos_utils::ipadm::Ipadm;
use illumos_utils::route::{Gateway, Route};
use illumos_utils::svcadm::Svcadm;
use illumos_utils::chronyd::Chronyd;
use omicron_common::cmd::fatal;
use omicron_common::cmd::CmdError;
use slog::{info, Logger};
use std::fs::{read_to_string, write, OpenOptions};
use std::fs::{metadata, read_to_string, set_permissions, write, OpenOptions};
use std::io::Write;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::os::unix::fs::chown;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;

pub const HOSTS_FILE: &str = "/etc/inet/hosts";
Expand Down Expand Up @@ -66,12 +65,20 @@ fn parse_opte_iface(s: &str) -> anyhow::Result<String> {
s.parse().map_err(|_| anyhow!("ERROR: Invalid OPTE interface"))
}

fn parse_allow(s: &str) -> anyhow::Result<String> {
if s == "unknown" {
return Err(anyhow!("ERROR: Missing allowed address range"));
};

s.parse().map_err(|_| anyhow!("ERROR: Invalid allowed address range"))
}

fn parse_chrony_conf(s: &str) -> anyhow::Result<String> {
if s == "unknown" {
return Err(anyhow!("ERROR: Missing chrony configuration file"));
};

// TODO: actually check the format of the string mends with "chrony.conf"
// TODO: actually check the format of the string ends with "chrony.conf"
s.parse().map_err(|_| anyhow!("ERROR: Invalid chrony configuration file"))
}

Expand Down Expand Up @@ -176,8 +183,8 @@ async fn do_run() -> Result<(), CmdError> {
// TODO: Add some parsing to this?
)
.arg(
arg!(-a --allow <Ipv6Addr> "Allowed IPv6 address")
.value_parser(parse_ipv6),
arg!(-a --allow <String> "Allowed IPv6 range")
.value_parser(parse_allow),
),
),
)
Expand Down Expand Up @@ -216,7 +223,7 @@ async fn ntp_smf_start(
) -> Result<(), CmdError> {
let servers =
matches.get_many::<String>("servers").unwrap().collect::<Vec<_>>();
let allow: Option<&Ipv6Addr> = matches.get_one("allow");
let allow: Option<&String> = matches.get_one("allow");

let file: &String = matches.get_one("file").unwrap();
let is_boundary: &bool = matches.get_one("boundary").unwrap();
Expand Down Expand Up @@ -306,17 +313,23 @@ async fn ntp_smf_start(
// does this - system/logadm-upgrade - only processes files with mode 444 and
// root:sys ownership so we need to adjust things here (until omicron package
// supports including ownership and permissions in the generated tar files).
//
// TODO: There is an error here
// zone-setup: Could not create chrony logadm configuration file /etc/logadm.d/chrony.logadm.conf: Invalid argument (os error 22)
let mut options = OpenOptions::new();
options.mode(444).create(true).open(LOGADM_CONFIG_FILE).map_err(|err| {
CmdError::Failure(anyhow!(
"Could not create chrony logadm configuration file {}: {}",
let mut perms = metadata(LOGADM_CONFIG_FILE)
.map_err(|err| {
CmdError::Failure(anyhow!(
"Could not retrieve chrony logadm configuration file {} metadata: {}",
LOGADM_CONFIG_FILE,
err
))
})?;
})?
.permissions();
perms.set_readonly(true);
set_permissions(LOGADM_CONFIG_FILE, perms).map_err(|err| {
CmdError::Failure(anyhow!(
"Could not set 444 permissions on chrony logadm configuration file {}: {}",
LOGADM_CONFIG_FILE,
err
))
})?;

chown(LOGADM_CONFIG_FILE, Some(0), Some(3)).map_err(|err| {
CmdError::Failure(anyhow!(
Expand All @@ -330,7 +343,6 @@ async fn ntp_smf_start(
Svcadm::refresh_logadm_upgrade()
.map_err(|err| CmdError::Failure(anyhow!(err)))?;

// TODO: Start daemon
info!(&log, "Starting chronyd daemon"; "chrony config" => ?file);
Chronyd::start_daemon(file)
.map_err(|err| CmdError::Failure(anyhow!(err)))?;
Expand Down

0 comments on commit bb15ccc

Please sign in to comment.