Permalink
Browse files

Added systemd support for static and dhcp configruations

TODO for systemd:
- Add default gateway support
- Add wireless support
- Add route support per device
  • Loading branch information...
1 parent b1f03fc commit 1edad584dde7c3fd9102ec899f862ba4d160d60f @Ratler committed Jun 26, 2012
Showing with 96 additions and 4 deletions.
  1. +96 −4 lnet/lnet
View
100 lnet/lnet
@@ -65,6 +65,28 @@ get_dev_status() {
fi
}
+function systemd_reload() {
+ systemctl daemon-reload > /dev/null 2>&1
+}
+
+function systemd_enable() {
+ systemctl -q enable $1
+}
+
+function systemd_disable() {
+ systemctl -q disable $1
+}
+
+function systemd_reload_enable() {
+ systemd_reload
+ systemd_enable $1
+}
+
+function systemd_reload_disable() {
+ systemd_reload
+ systemd_disable $1
+}
+
main ()
{
unset PROMPT
@@ -205,7 +227,17 @@ function ethernet_menu() {
case $COMMAND in
C) ethernet_config $1;;
M) ethernet_manage_device $1;;
- D) if confirm "Are you sure you wish to delete $1?" "--defaultno"; then rm -f "$CONFIG_DIR/$1"; fi;;
+ D)
+ if confirm "Are you sure you wish to delete $1?" "--defaultno"; then
+ rm -f "$CONFIG_DIR/$1"
+ if [ -d /run/systemd ]; then
+ local SYSTEMDUNITDIR=$(pkg-config systemd --variable=systemdsystemunitdir)
+ systemd_disable ${1}.service
+ rm -f $SYSTEMDUNITDIR/${1}.service
+ systemd_reload
+ fi
+ fi
+ ;;
esac
}
@@ -279,8 +311,28 @@ function ethernet_manage_device() {
$DIALOG --infobox "$DHCP_TIMEOUT" 0 0
case $COMMAND in
- S) /etc/init.d/network `echo $TOGGLE | tr 'S' 's'` "$1" ;;
- R) /etc/init.d/network restart "$1" ;;
+ S)
+ # Assume systemd is running if /run/systemd exists
+ if [ -d /run/systemd ]; then
+ local SYSTEMDUNITDIR=$(pkg-config systemd --variable=systemdsystemunitdir)
+ if [ -f $SYSTEMDUNITDIR/${1}.service ]; then
+ systemctl ${TOGGLE/S/s} ${1}.service
+ fi
+ else
+ /etc/init.d/network ${TOGGLE/S/s} "$1"
+ fi
+ ;;
+ R)
+ # Assume systemd is running if /run/systemd exists
+ if [ -d /run/systemd ]; then
+ local SYSTEMDUNITDIR=$(pkg-config systemd --variable=systemdsystemunitdir)
+ if [ -f $SYSTEMDUNITDIR/${1}.service ]; then
+ systemctl restart ${1}.service
+ fi
+ else
+ /etc/init.d/network restart "$1"
+ fi
+ ;;
esac
if [[ "$(/sbin/ifconfig | grep $1 | wc -l)" -ge "1" ]]; then
@@ -302,7 +354,7 @@ unset NETMASK BROADCAST GATEWAY DHCP_CLIENT DHCP_OPTIONS
unset WIRELESS_KEY WIRELESS_RATE WIRELESS_MODE WIRELESS_ESSID
if_template() {
- cat > $CONFIG_DIR/$DEVICE << EOF
+ cat > $CONFIG_DIR/$DEVICE << EOF
#
# configuration for "$DEVICE"
# automatically generated by lnet - do not edit, run 'lnet' instead
@@ -333,6 +385,46 @@ ROUTE_2="$ROUTE_2"
ROUTE_3="$ROUTE_3"
ROUTE_4="$ROUTE_4"
EOF
+
+ # Assume systemd is running if /run/systemd exists
+ # Create ethX.service file
+ if [ -d /run/systemd ]; then
+ local SYSTEMDUNITDIR=$(pkg-config systemd --variable=systemdsystemunitdir)
+ if [ "$ADDRESS" = "dhcp" ]; then
+ cat > $SYSTEMDUNITDIR/${DEVICE}.service <<EOF
+[Unit]
+Description=DHCPCD client daemon $DEVICE
+Wants=network.target
+Before=network.target
+
+[Service]
+ExecStart=/usr/sbin/dhcpcd -q --nobackground $DEVICE
+
+[Install]
+WantedBy=multi-user.target
+EOF
+ else
+ cat > $SYSTEMDUNITDIR/${DEVICE}.service <<EOF
+[Unit]
+Description=Network connecitivity $DEVICE
+Wants=network.target
+Before=network.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+EnvironmentFile=/etc/config.d/network/${DEVICE}
+ExecStart=/usr/sbin/ip link set dev ${DEVICE} up
+ExecStart=/usr/sbin/ip addr add \${ADDRESS}/\${NETMASK} broadcast \${BROADCAST} dev ${DEVICE}
+ExecStop=/usr/sbin/ip addr flush dev ${DEVICE}
+ExecStop=/usr/sbin/ip link set dev ${DEVICE} down
+
+[Install]
+WantedBy=multi-user.target
+EOF
+ fi
+ [ "$AUTO" = "Y" ] && systemd_reload_enable ${DEVICE}.service || systemd_reload_disable ${DEVICE}.service
+ fi
}
if [ -n "$1" ]; then

0 comments on commit 1edad58

Please sign in to comment.