#!/bin/sh -e

chrooted() {
  if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ];
  then
    # the devicenumber/inode pair of / is the same as that of /sbin/init's
    # root, so we're *not* in a chroot and hence return false.
    return 1
  fi
  echo "A chroot environment has been detected, udev not started."
  return 0
}

in_debootstrap() {
  # debootstrap --second-stage may be run in an emulator instead of a chroot,
  # we need to check for this special case because start-stop-daemon would
  # not be available. (#520742)
  if [ -d /debootstrap/ ]; then
    echo "Being installed by debootstrap, udev not started."
    return 0
  fi
  return 1
} 

can_start_udevd() {
  if [ ! -d /sys/class/ ]; then
    echo "udev requires a mounted sysfs, not started."
    return 1
  fi
  return 0
}

enable_udev() {
  can_start_udevd || return 0
  invoke-rc.d udev start
}

update_initramfs() {
  [ -x /usr/sbin/update-initramfs -a -e /etc/initramfs-tools/initramfs.conf ] \
    || return 0
  update-initramfs -u
}

upgrade_fixes() {
  if dpkg --compare-versions "$2" lt "204-1"; then
    # We dropped udev-mtab with udev 204.
    update-rc.d udev-mtab remove
  fi

  if dpkg --compare-versions "$2" lt "226-1"; then
    update-rc.d udev-finish remove
  fi

  # we enabled net.ifnames in 220-7 by default; don't change iface names in
  # virtualized envs (where 75-persistent-net-generator.rules didn't work)
  if dpkg --compare-versions "$2" lt-nl "220-7~" &&
     [ ! -e /etc/udev/rules.d/70-persistent-net.rules ] &&
     [ ! -e /etc/udev/rules.d/80-net-setup-link.rules ] &&
     ! grep -q net.ifnames /proc/cmdline ; then
    mkdir -p /etc/udev/rules.d
    cat <<EOF > /etc/udev/rules.d/80-net-setup-link.rules
# This machine is most likely a virtualized guest, where the old persistent
# network interface mechanism (75-persistent-net-generator.rules) did not work.
# This file disables /lib/udev/rules.d/80-net-setup-link.rules to avoid
# changing network interface names on upgrade. Please read
# /usr/share/doc/udev/README.Debian.gz about how to migrate to the currently
# supported mechanism.
EOF
  fi

  # 226 introduced predictable interface names for virtio
  # (https://github.com/systemd/systemd/pull/1119); disable for upgrades
  if dpkg --compare-versions "$2" lt-nl "226-2~" &&
      [ ! -e /etc/systemd/network/50-virtio-kernel-names.link ] &&
      ls -d /sys/bus/virtio/drivers/virtio_net/virt* >/dev/null 2>&1; then
    echo "virtio network devices detected, disabling predictable interface names in /etc/systemd/network/50-virtio-kernel-names.link"
    mkdir -p /etc/systemd/network/
    cat <<EOF > /etc/systemd/network/50-virtio-kernel-names.link
# udev 226 introduced predictable interface names for virtio;
# disable this for upgrades. You can remove this file if you update your
# network configuration to move to the ens* names instead.
# See /usr/share/doc/udev/README.Debian.gz for details about predictable
# network interface names.
[Match]
Driver=virtio_net

[Link]
NamePolicy=onboard kernel
EOF
  fi

  # this is being written under upstart, but not under systemd; clean up
  # obsolete files
  if [ -d /run/systemd/system ]; then
      rm -f /var/log/udev || true
  fi

  # transition existing interface names on s390x for #1526808
  if dpkg --compare-versions "$2" lt-nl "228-4ubuntu2" &&
      [ "$(dpkg --print-architecture)" = "s390x" ]; then
    sed 's/enccw[0.]*/enc/' -i /etc/network/interfaces /etc/network/interfaces.d/* 2>/dev/null || true
  fi

  # VMWare BIOS reports implausibly high onboard numbers, this got fixed in
  # https://github.com/systemd/systemd/commit/6c1e69f9; migrate names in
  # ifupdown accordingly
  if dpkg --compare-versions "$2" lt-nl "229-2ubuntu2"; then
    for i in /sys/class/net/eno*; do
        if [ "${i#*/eno}" -gt 16383 ] 2>/dev/null; then
            if slotname=$(udevadm info --query=property $i | grep '^ID_NET_NAME_SLOT='); then
                slotname=${slotname#*=}
                echo "Migrating broken onboard-based network interface name ${i##*/} to slot-based name $slotname in /etc/network/interfaces and interfaces.d/..."
                sed -i "s/\\b${i##*/}\\b/$slotname/g" /etc/network/interfaces /etc/network/interfaces.d/* 2>/dev/null || true
            else
                echo "WARNING: Cannot determine proper name for broken onboard-based network interface name ${i##*/}; you must adjust /etc/network/interfaces and interfaces.d/ manually"
            fi
        fi
    done
  fi
}

update_hwdb() {
  systemd-hwdb --usr update || true
}

# In udev-204, we ship systemd-udevd.service (upstream name), whereas previous
# versions used udev.service. We replace udev.service with a symlink to
# systemd-udevd.service, but systemd (both 44 and 204) exposes weird behavior:
# After a daemon-reload, it forgets about the /sbin/udevd process in the
# udev.service cgroup, so a restart will lead to having two udevd processes
# running — one in the udev.service cgroup and one in the systemd-udevd.service
# cgroup.
#
# To fix this, we explicitly stop udev.service and the corresponding sockets,
# then issue the daemon-reload, then restart the new systemd-udevd.service (via
# invoke-rc.d).
handle_service_rename() {
  if dpkg --compare-versions "$2" lt "204-1"; then
    if [ -d /run/systemd/system ]; then
      systemctl stop udev.service udev-control.socket udev-kernel.socket >/dev/null 2>&1 || true
    fi
  fi
}

case "$1" in
    configure)
    # update/create hwdb before we (re)start udev
    update_hwdb

    # Add new system group used by udev rules
    addgroup --system input

    if [ -z "$2" ]; then # first install
      if ! chrooted && ! in_debootstrap; then
	enable_udev
      fi
    else # upgrades
      upgrade_fixes "$@"
      if ! chrooted; then
	if can_start_udevd; then
	  handle_service_rename
	  # This is necessary for the handle_service_rename case, but does not
	  # hurt in general (invoke-rc.d does it, too).
	  if [ -d /run/systemd/system ] ; then
	    systemctl daemon-reload || true
	  fi
	  invoke-rc.d udev restart
	fi
      fi
    fi

    update_initramfs
    ;;

    triggered)
    update_hwdb
    exit 0
    ;;
esac

# Automatically added by dh_installinit
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ]; then
	if [ -x "/etc/init.d/udev" ]; then
		update-rc.d udev defaults >/dev/null || exit $?
	fi
fi
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/init.d/udev-mtab 204-1~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/udev/links.conf 204-9~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/init.d/udev-finish 226-1~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/init/udev-finish.conf 226-1~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/init/udev-fallback-graphics.conf 226-1~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper symlink_to_dir /usr/share/doc/udev libudev1 221-2~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/udev/rules.d/README 215-5ubuntu1~ -- "$@"
# End automatically added section
# Automatically added by dh_installdeb
dpkg-maintscript-helper rm_conffile /etc/init.d/lvm2 228-2ubuntu2~ -- "$@"
# End automatically added section


