]> git.proxmox.com Git - mirror_lxc.git/blob - hooks/dhclient.in
hooks: dhclient hook improvements
[mirror_lxc.git] / hooks / dhclient.in
1 #! /bin/bash
2
3 set -eu
4
5 LXC_DHCP_SCRIPT="@LXCHOOKDIR@/dhclient-script"
6 LXC_DHCP_CONFIG="@SYSCONFDIR@/lxc/dhclient.conf"
7
8 rootfs_path="${LXC_ROOTFS_PATH#*:}"
9 hookdir="${rootfs_path/%rootfs/hook}"
10
11 conffile_arg=""
12 if [ -e "${LXC_DHCP_CONFIG}" ]; then
13 conffile_arg="-cf ${LXC_DHCP_CONFIG}"
14 fi
15
16 debugfile="/dev/null"
17 if [ "${LXC_LOG_LEVEL}" = "DEBUG" ] || [ "${LXC_LOG_LEVEL}" = "TRACE" ]; then
18 debugfile="${hookdir}/dhclient.log"
19 echo "INFO: Writing dhclient log at ${debugfile}." >&2
20 fi
21
22 pidfile="${hookdir}/dhclient.pid"
23 leasefile="${hookdir}/dhclient.leases"
24
25 usage() {
26 echo "Usage: ${0##*/} <name> lxc {start-host|stop}"
27 }
28
29 dhclient_start() {
30 ns_args=("--uts" "--net")
31 if [ -z "$(readlink /proc/${LXC_PID}/ns/user /proc/self/ns/user | uniq -d)" ]; then
32 ns_args+=("--user")
33 fi
34
35 mkdir -p "${hookdir}"
36
37 if [ -e "${pidfile}" ]; then
38 echo "WARN: DHCP client is already running, skipping start hook." >> "${debugfile}"
39 else
40 echo "INFO: Starting DHCP client and acquiring a lease..." >> "${debugfile}"
41 nsenter ${ns_args[@]} --target "${LXC_PID}" -- \
42 /sbin/dhclient -1 ${conffile_arg} -pf "${pidfile}" -lf "${leasefile}" -e "ROOTFS=${rootfs_path}" -sf "${LXC_DHCP_SCRIPT}" -v >> "${debugfile}" 2>&1
43 fi
44 }
45
46 dhclient_stop() {
47 # We can't use LXC_PID here since the container process has exited,
48 # use the namespace file descriptors in the hook arguments instead.
49 ns_args=("")
50 if [ "${LXC_HOOK_VERSION:-0}" -eq 0 ]; then
51 for arg in "$@"; do
52 case "${arg}" in
53 uts:* | user:* | net:*) ns_args+=("--${arg/:/=}") ;;
54 *) ;;
55 esac
56 done
57 else
58 ns_args+=("--uts=${LXC_UTS_NS}")
59 ns_args+=("--net=${LXC_NET_NS}")
60 [ -n "${LXC_USER_NS:+x}" ] && ns_args+=("--user=${LXC_USER_NS}")
61 fi
62
63 if [ -e "${pidfile}" ]; then
64 echo "INFO: Stopping DHCP client and releasing leases..." >> "${debugfile}"
65 nsenter ${ns_args[@]} -- \
66 /sbin/dhclient -r ${conffile_arg} -pf "${pidfile}" -lf "${leasefile}" -e "ROOTFS=${rootfs_path}" -sf "${LXC_DHCP_SCRIPT}" -v >> "${debugfile}" 2>&1
67 else
68 echo "WARN: DHCP client is not running, skipping stop hook." >> "${debugfile}"
69 fi
70
71 # dhclient could fail to release the lease and shutdown, try to cleanup after ourselves just in case.
72 nsenter ${ns_args[@]} -- \
73 /bin/sh -c 'pkill --ns $$ --nslist net -f "^/sbin/dhclient"' || true
74 rm -f "${pidfile}"
75 }
76
77 HOOK_SECTION=
78 HOOK_TYPE=
79 case "${LXC_HOOK_VERSION:-0}" in
80 0) HOOK_SECTION="${2:-}"; HOOK_TYPE="${3:-}"; shift 3;;
81 1) HOOK_SECTION="${LXC_HOOK_SECTION:-}"; HOOK_TYPE="${LXC_HOOK_TYPE:-}";;
82 *) echo "ERROR: Unsupported hook version: ${LXC_HOOK_VERSION}." >&2; exit 1;;
83 esac
84
85 if [ "${HOOK_SECTION}" != "lxc" ]; then
86 echo "ERROR: Not running through LXC." >&2
87 exit 1
88 fi
89
90 case "${HOOK_TYPE}" in
91 start-host) dhclient_start $@;;
92 stop) dhclient_stop $@;;
93 *) usage; exit 1;;
94 esac
95
96 exit 0