blob: d80c36808d62658982527bf5a50550ddbccdaf98 [file] [log] [blame]
Christopher Wiley2f48d952013-02-22 09:51:47 -08001#!/bin/sh
2# Virtual ethernet interface control script
3# Sometimes, we need a virtual interface of arbitrary name and configuration
4# that we can do dhcp on. This script is for those times.
5
6# Basically,
7# $ veth setup foo 127.0.1
8# $ dhclient foo
9# ...
10# $ veth teardown foo
11# Would set up an ethernet interface called 'foo' whose dhcpd is at 127.0.1.1
12# and which will allocate addresses from 127.0.1.0/24. Note that using anything
13# inside 127.0.0.0/8 is a bad idea here, since lo already handles those.
14
15usage () {
16 echo "Usage: $0 <command> [args...]"
17 echo " setup <iface> <base> Sets up <iface> for <base>.0/24"
18 echo " teardown <iface> Tears down <iface>"
19}
20
21setup () {
22 iface="$1"
23 base="$2"
24 peer_iface="${iface}p"
25 lease_file="/tmp/dnsmasq.${iface}.leases"
26 pid_file="/tmp/dnsmasq.${iface}.pid"
27 ip link add name "$iface" type veth peer name "$peer_iface"
28 ifconfig "$peer_iface" "${base}.0/32"
29 ifconfig "$peer_iface" up
30 ifconfig "$iface" up
31 route add -host 255.255.255.255 dev "$peer_iface"
32 truncate -s 0 "$lease_file"
33 dnsmasq --pid-file="$pid_file" \
34 --dhcp-leasefile="$lease_file" \
35 --dhcp-range="${base}.2,${base}.254" \
36 --port=0 \
37 --interface="$peer_iface" \
38 --bind-interfaces
39}
40
41teardown () {
42 iface="$1"
43 pid_file="/tmp/dnsmasq.${iface}.pid"
44 [ -f "$pid_file" ] && kill -TERM $(cat "$pid_file")
45 route del -host 255.255.255.255
46 ip link del "$iface"
47}
48
49if [ -z "$1" ]; then
50 usage
51 exit 1
52fi
53
54command="$1" ; shift
55case "$command" in
56 setup)
57 setup "$@"
58 ;;
59 teardown)
60 teardown "$@"
61 ;;
62 *)
63 usage
64 ;;
65esac