net-interface-handler 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/sh -e
  2. # suppress configuration of network interface used
  3. # by iSCSI root device
  4. #
  5. # If the root filesystem is on iSCSI, then we must take care to avoid
  6. # changing the state of its network interface. To this end, the initramfs
  7. # leaves a note for us which interface was used, and we mangle
  8. # /run/network/ifstate manually to stop it being brought up or down
  9. # automatically. This is a slight layering violation, but, unfortunately,
  10. # ifupdown appears to have no way to do this without also running
  11. # /etc/network/*.d/ scripts.
  12. assert_interface() {
  13. # udev sets INTERFACE to the name of the currently-processed nic.
  14. [ -n "$INTERFACE" ] && return 0
  15. echo "environment variable INTERFACE not set." 1>&2;
  16. return 1
  17. }
  18. start() {
  19. CR="
  20. "
  21. assert_interface || return
  22. ifile=/run/initramfs/open-iscsi.interface
  23. [ -f "$ifile" ] && read iface < "$ifile" || return 0
  24. [ "$INTERFACE" = "$iface" ] || return
  25. if ! grep -qs "^$iface=" /run/network/ifstate; then
  26. mkdir -p /run/network
  27. echo "$iface=$iface" >>/run/network/ifstate
  28. if [ -f /run/net-$iface.conf ]; then
  29. conf=/run/net-$iface.conf
  30. elif [ -f /run/net6-$iface.conf ]; then
  31. conf=/run/net6-$iface.conf
  32. else
  33. conf=""
  34. fi
  35. if command -v resolvconf >/dev/null &&
  36. [ -n "$conf" ]; then
  37. . "$conf"
  38. R=""
  39. [ -n "$DOMAINSEARCH" ] && R="$R${CR}search $DOMAINSEARCH"
  40. [ -n "$IPV6DOMAINSEARCH" ] && R="$R${CR}search $IPV6DOMAINSEARCH"
  41. for ns in "$IPV4DNS0" "$IPV4DNS1" "$IPV6DNS0" "$IPV6DNS1"; do
  42. [ -n "$ns" -a "$ns" != "0.0.0.0" ] && R="$R${CR}nameserver $ns"
  43. done
  44. if [ -n "$R" ]; then
  45. # create the dir in case resolvconf did not start yet
  46. mkdir -p /run/resolvconf/interface
  47. resolvconf -a $iface.iscsi-network <<EOF
  48. ${R#${CR}}
  49. EOF
  50. fi
  51. fi
  52. fi
  53. }
  54. stop() {
  55. assert_interface || return
  56. ifile=/run/initramfs/open-iscsi.interface
  57. [ -f "$ifile" ] && read iface < "$ifile" || return 0
  58. [ "$INTERFACE" = "$iface" ] || return
  59. if grep -qs "^$iface=" /run/network/ifstate; then
  60. grep -v "^$iface=" /run/network/ifstate >/run/network/.ifstate.tmp || true
  61. mv /run/network/.ifstate.tmp /run/network/ifstate
  62. if command -v resolvconf >/dev/null; then
  63. resolvconf -d $iface.iscsi-network
  64. fi
  65. fi
  66. }
  67. case "$1" in
  68. start) start ;;
  69. stop) stop ;;
  70. *) echo "ERROR: must be called with 'start' or 'stop'" >&2; exit 1 ;;
  71. esac