Debian Codegraph @

tools/hotplug/Linux/vif-nat

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 #============================================================================
3 # ${XEN_SCRIPT_DIR}/vif-nat
4 #
5 # Script for configuring a vif in routed-nat mode.
6 #
7 # Usage:
8 # vif-nat (add|remove|online|offline)
9 #
10 # Environment vars:
11 # dev vif interface name (required).
12 # XENBUS_PATH path to this device's details in the XenStore (required).
13 #
14 # Parameters:
15 # dhcp Whether to alter the local DHCP configuration to include this
16 # new host (default no).
17 #
18 # Read from the store:
19 # ip list of IP networks for the vif, space-separated (default given in
20 # this script).
21 #============================================================================
22
23
24 dir=$(dirnamescip-shell system . . dirname. "$0")
25 . "$dir/vif-common.sh"
26
27 # turn on dhcp feature by default if dhcpd is installed
28 if [scip-shell system . . `[`. -f /etc/dhcpd.confscip-shell filesystem . . `/etc/dhcpd.conf`. ]
29 then
30 dhcp=${dhcp:-yes}
31 else
32 dhcp=${dhcp:-no}
33 fi
34
35 if [scip-shell system . . `[`. "$dhcp" != 'no' ]
36 then
37 dhcpd_conf_file=$(find_dhcpd_conf_file)
38 dhcpd_init_file=$(find_dhcpd_init_file)
39 dhcpd_arg_file=$(find_dhcpd_arg_file)
40 if [scip-shell system . . `[`. -z "$dhcpd_conf_file" ] || [scip-shell system . . `[`. -z "$dhcpd_init_file" ] || [scip-shell system . . `[`. -z "$dhcpd_arg_file" ]
41 then
42 echoscip-shell system . . echo. 'Failed to find dhcpd configuration or init or args file.' >&2
43 exit 1
44 fi
45 fi
46
47
48 domid=$(xenstore_read "$XENBUS_PATH/frontend-id")
49 vifid=$(xenstore_read "$XENBUS_PATH/handle")
50 vifid=$(( $vifid + 1 ))
51
52
53 ip_from_domscip-shell shell xen 4.20.2+37-g61ff35323e-1 ip_from_dom().()
54 {
55 local domid1=$(( $domid / 256 ))
56 local domid2=$(( $domid % 256 ))
57
58 echoscip-shell system . . echo. "10.$domid1.$domid2.$vifid/16"
59 }
60
61
62 routing_ipscip-shell shell xen 4.20.2+37-g61ff35323e-1 routing_ip().()
63 {
64 echoscip-shell system . . echo. $(echoscip-shell system . . echo. $1 | awkscip-shell system . . awk. -F. '{print $1"."$2"."$3"."$4 + 127}')
65 }
66
67
68 dotted_quadscip-shell shell xen 4.20.2+37-g61ff35323e-1 dotted_quad().()
69 {
70 echo\
71 $(( ($1 & 0xFF000000) >> 24))\
72 .$(( ($1 & 0x00FF0000) >> 16))\
73 .$(( ($1 & 0x0000FF00) >> 8 ))\
74 .$(( $1 & 0x000000FF ))
75 }
76
77
78 if [scip-shell system . . `[`. "$ip" = "" ]
79 then
80 ip=$(ip_from_domscip-shell shell xen 4.20.2+37-g61ff35323e-1 ip_from_dom().)
81 fi
82
83 router_ip=$(routing_ipscip-shell shell xen 4.20.2+37-g61ff35323e-1 routing_ip(). "$ip")
84
85 # Split the given IP/bits pair.
86 vif_ip=`echoscip-shell system . . echo. ${ip} | awkscip-shell system . . awk. -F/ '{print $1}'`
87
88 hostname=dom$domid
89 if [scip-shell system . . `[`. "$vifid" != "1" ]
90 then
91 hostname="$hostname-$vifid"
92 fi
93
94 dhcparg_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcparg_remove_entry().()
95 {
96 local tmpfile=$(mktempscip-shell system . . mktemp.)
97 sedscip-shell system . . sed. -e "s/${dev} //" "$dhcpd_arg_file" >"$tmpfile"
98 if ! diffscip-shell system . . diff. "$tmpfile" "$dhcpd_arg_file" >/dev/nullscip-shell filesystem . . `/dev/null`.
99 then
100 cpscip-shell system . . cp. "$tmpfile" "$dhcpd_arg_file"
101 fi
102 rmscip-shell system . . rm. "$tmpfile"
103 }
104
105 dhcparg_add_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcparg_add_entry().()
106 {
107 dhcparg_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcparg_remove_entry().
108 local tmpfile=$(mktempscip-shell system . . mktemp.)
109 # handle Red Hat, SUSE, and Debian styles, with or without quotes
110 sedscip-shell system . . sed. -e 's/^DHCPDARGS="*\([^"]*\)"*/DHCPDARGS="\1'"${dev} "'"/' \
111 "$dhcpd_arg_file" >"$tmpfile" && cpscip-shell system . . cp. "$tmpfile" "$dhcpd_arg_file"
112 sedscip-shell system . . sed. -e 's/^DHCPD_INTERFACE="*\([^"]*\)"*/DHCPD_INTERFACE="\1'"${dev} "'"/' \
113 "$dhcpd_arg_file" >"$tmpfile" && cpscip-shell system . . cp. "$tmpfile" "$dhcpd_arg_file"
114 sedscip-shell system . . sed. -e 's/^INTERFACES="*\([^"]*\)"*/INTERFACES="\1'"${dev} "'"/' \
115 "$dhcpd_arg_file" >"$tmpfile" && cpscip-shell system . . cp. "$tmpfile" "$dhcpd_arg_file"
116 rmscip-shell system . . rm. -f "$tmpfile"
117 }
118
119 dhcp_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_remove_entry().()
120 {
121 local tmpfile=$(mktempscip-shell system . . mktemp.)
122 grepscip-shell system . . grep. -v "host $hostname" "$dhcpd_conf_file" >"$tmpfile"
123 if ! diffscip-shell system . . diff. "$tmpfile" "$dhcpd_conf_file" >/dev/nullscip-shell filesystem . . `/dev/null`.
124 then
125 cpscip-shell system . . cp. "$tmpfile" "$dhcpd_conf_file"
126 fi
127 rmscip-shell system . . rm. "$tmpfile"
128 dhcparg_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcparg_remove_entry().
129 }
130
131
132 dhcp_upscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_up().()
133 {
134 claim_lock "vif-nat-dhcp"
135 dhcp_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_remove_entry().
136 mac=$(xenstore_read "$XENBUS_PATH/mac")
137 echoscip-shell system . . echo. >>"$dhcpd_conf_file" \
138 "host $hostname { hardware ethernet $mac; fixed-address $vif_ip; option routers $router_ip; option host-name \"$hostname\"; }"
139 dhcparg_add_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcparg_add_entry().
140 release_lock "vif-nat-dhcp"
141 "$dhcpd_init_file" restart || truescip-shell system . . true.
142 }
143
144
145 dhcp_downscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_down().()
146 {
147 claim_lock "vif-nat-dhcp"
148 dhcp_remove_entryscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_remove_entry().
149 release_lock "vif-nat-dhcp"
150 "$dhcpd_init_file" restart || truescip-shell system . . true. # We need to ignore failure because
151 # ISC dhcpd 3 borks if there is nothing
152 # for it to do, which is the case if
153 # the outgoing interface is not
154 # configured to offer leases and there
155 # are no vifs.
156 }
157
158
159 case "$command" in
160 online)
161 if ip route | grepscip-shell system . . grep. -q "dev ${dev}"
162 then
163 log debug "${dev} already up"
164 exit 0
165 fi
166
167 do_or_die ip link set dev "${dev}" up arp on
168 do_or_die ip addr add "$router_ip" dev "${dev}"
169 do_or_die ip route add "$vif_ip" dev "${dev}" src "$router_ip"
170 echoscip-shell system . . echo. 1 >/proc/sys/net/ipv4/conf/${dev}/proxy_arp
171 [scip-shell system . . `[`. "$dhcp" != 'no' ] && dhcp_upscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_up().
172 ;;
173 offline)
174 [scip-shell system . . `[`. "$dhcp" != 'no' ] && dhcp_downscip-shell shell xen 4.20.2+37-g61ff35323e-1 dhcp_down().
175 do_without_error ifconfig "${dev}" down
176 ;;
177 esac
178
179
180 handle_iptable
181
182 call_hooks vif post
183
184 log debug "Successful vif-nat $command for ${dev}."
185 if [scip-shell system . . `[`. "$command" = "online" ]
186 then
187 success
188 fi