Skip to content

Commit

Permalink
Add pd daemon service: bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsong-cnyn committed Nov 5, 2024
1 parent 225b634 commit cb53771
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
36 changes: 27 additions & 9 deletions script/_dhcpv6_pd_ref
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,32 @@ PD_DAEMON_PATH="${PD_DAEMON_DIR}/dhcp6_pd_daemon.py"
PD_DAEMON_SERVICE_NAME="dhcp6_pd_daemon.service"
PD_DAEMON_SERVICE_PATH="/etc/systemd/system/${PD_DAEMON_SERVICE_NAME}"

create_pd_dhcpcd_conf()
DHCP_CONFIG_FILE="/etc/dhcpcd.conf.orig"
DHCP_CONFIG_ORIG_FILE="/etc/dhcpcd.conf.orig"
DHCP_CONFIG_WITH_PD_FILE="/etc/dhcpcd.conf.with-pd"
DHCP_CONFIG_NO_PD_FILE="/etc/dhcpcd.conf.no-pd"

# Create dhcpcd configuration file with ipv6 prefix request.
create_dhcpcd_conf_with_pd()
{
sudo tee /etc/dhcpcd.conf.pd >/dev/null <<EOF
sudo tee ${DHCP_CONFIG_WITH_PD_FILE} >/dev/null <<EOF
noipv6rs # disable router solicitation
interface eth0
iaid 1
ia_pd 2/::/64 -
release
noipv6rs
EOF
}

# Create dhcpcd configuration file with no prefix request.
create_dhcpcd_conf_no_pd()
{
sudo tee ${DHCP_CONFIG_NO_PD_FILE} >/dev/null <<EOF
noipv6rs # disable router solicitation
interface eth0
iaid 1
release
noipv6rs
EOF
}
Expand Down Expand Up @@ -78,8 +97,8 @@ dhcpv6_pd_ref_uninstall()
sudo rm -f ${PD_DAEMON_SERVICE_PATH} || true
fi

if [[ -f "/etc/dhcpcd.conf.orig" ]]; then
sudo mv /etc/dhcpcd.conf.orig /etc/dhcpcd.conf
if [[ -f ${DHCP_CONFIG_ORIG_FILE} ]]; then
sudo mv ${DHCP_CONFIG_ORIG_FILE} ${DHCP_CONFIG_FILE}
fi

sudo rm -f ${DHCPCD_ENTER_HOOK} ${DHCPCD_EXIT_HOOK}
Expand All @@ -98,10 +117,8 @@ dhcpv6_pd_ref_install()
{
with DHCPV6_PD_REF || return 0

if [[ -f "/etc/dhcpcd.conf" ]]; then
sudo mv /etc/dhcpcd.conf /etc/dhcpcd.conf.orig
else
sudo touch /etc/dhcpcd.conf.orig
if [[ -f ${DHCP_CONFIG_FILE} ]]; then
sudo mv ${DHCP_CONFIG_FILE} ${DHCP_CONFIG_ORIG_FILE}
fi

# Add dhcpcd.hooks
Expand All @@ -110,7 +127,8 @@ dhcpv6_pd_ref_install()
sudo mkdir -p ${PD_DAEMON_DIR}
sudo install -m 755 "$(dirname "$0")"/reference-device/dhcp6_pd_daemon.py ${PD_DAEMON_PATH}

create_pd_dhcpcd_conf
create_dhcpcd_conf_with_pd
create_dhcpcd_conf_no_pd
create_dhcp6_pd_daemon_service

if have systemctl; then
Expand Down
26 changes: 13 additions & 13 deletions script/reference-device/dhcp6_pd_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,36 @@
intended_dhcp6pd_state = None

DHCP_CONFIG_FILE = "/etc/dhcpcd.conf"
DHCP_CONFIG_PD_FILE = "/etc/dhcpcd.conf.pd"
DHCP_CONFIG_ORIG_FILE = "/etc/dhcpcd.conf.orig"
DHCP_CONFIG_WITH_PD_FILE = "/etc/dhcpcd.conf.with-pd"
DHCP_CONFIG_NO_PD_FILE = "/etc/dhcpcd.conf.no-pd"

def restart_dhcpcd_with_pd_config():
global intended_dhcp6pd_state
if os.path.isfile(DHCP_CONFIG_PD_FILE):
if os.path.isfile(DHCP_CONFIG_WITH_PD_FILE):
try:
subprocess.run(["sudo", "cp", DHCP_CONFIG_PD_FILE, DHCP_CONFIG_FILE], check=True)
subprocess.run(["sudo", "cp", DHCP_CONFIG_WITH_PD_FILE, DHCP_CONFIG_FILE], check=True)
subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
subprocess.run(["sudo", "service", "dhcpcd", "restart"], check=True)
logging.info("Successfully restarted dhcpcd service.")
logging.info("Successfully restarted dhcpcd service with pd enabled.")
intended_dhcp6pd_state = None
except subprocess.CalledProcessError as e:
logging.error(f"Error restarting dhcpcd service: {e}")
intended_dhcp6pd_state = None
else:
logging.error(f"{DHCP_CONFIG_PD_FILE} not found. Cannot apply configuration with prefix delegation.")
logging.error(f"{DHCP_CONFIG_WITH_PD_FILE} not found. Cannot apply configuration with pd enabled.")
intended_dhcp6pd_state = None

def restore_default_config():
if os.path.isfile(DHCP_CONFIG_ORIG_FILE):
if os.path.isfile(DHCP_CONFIG_NO_PD_FILE):
try:
subprocess.run(["sudo", "cp", DHCP_CONFIG_ORIG_FILE, DHCP_CONFIG_FILE], check=True)
subprocess.run(["sudo", "cp", DHCP_CONFIG_NO_PD_FILE, DHCP_CONFIG_FILE], check=True)
subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
subprocess.run(["sudo", "dhcpcd", "--release"], check=True)
subprocess.run(["sudo", "service", "dhcpcd", "restart"], check=True)
logging.info("Successfully restored default dhcpcd config.")
logging.info("Successfully restarted dhcpcd service with pd disabled.")
except subprocess.CalledProcessError as e:
logging.error(f"Error restoring dhcpcd config: {e}")
logging.error(f"Error restarting dhcpcd service: {e}")
else:
logging.error(f"{DHCP_CONFIG_ORIG_FILE} not found. Cannot restore default configuration.")
logging.error(f"{DHCP_CONFIG_NO_PD_FILE} not found. Cannot apply configuration with pd disabled.")

def properties_changed_handler(interface_name, changed_properties, invalidated_properties):
global intended_dhcp6pd_state
Expand Down Expand Up @@ -103,7 +102,8 @@ def check_and_reconnect(dbus_obj, properties_dbus_iface):
dbus_obj, properties_dbus_iface = connect_to_signal()

def main():
# always restart dhcpcd to begin with a previous saved state
# Ensure dhcpcd is running in its last known state. In case it was killed or crashed previously, or
# radvd is not yet enabled because of inactive network.target
try:
subprocess.run(["sudo", "systemctl", "reload-or-restart", "dhcpcd"], check=True)
logging.info("Successfully restarting dhcpcd service.")
Expand Down

0 comments on commit cb53771

Please sign in to comment.