forked from r-pufky/wireguard-initramfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-premount
executable file
·106 lines (90 loc) · 2.4 KB
/
init-premount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
PREREQ="udev"
prereqs() {
echo "${PREREQ}"
}
case "${1}" in
prereqs)
prereqs
exit 0
;;
esac
. /scripts/functions
if [ ! -e /sbin/wg ]; then
log_failure_msg 'Wireguard binary not found; skipping start'
exit 0
fi
if [ ! -e /etc/wireguard/config ]; then
log_failure_msg 'Wireguard config not found; skipping start'
exit 0
fi
if [ ! -e /etc/wireguard/private_key ]; then
log_failure_msg 'Wireguard client private keyfile not found, skipping start'
exit 0
fi
log_begin_msg 'Loading wireguard config'
. /etc/wireguard/config
if [ -z ${INTERFACE} ]; then
log_failure_msg 'Interface name is not defined'
return 1
fi
if [ -z ${INTERFACE_ADDR} ]; then
log_failure_msg 'Interface address is not defined'
return 1
fi
if [ -z ${PEER_PUBLIC_KEY} ]; then
log_failure_msg 'Peer public key is not defined'
return 1
fi
if [ ! -z ${PRE_SHARED_KEY} ] && [ ! -s "/etc/wireguard/pre_shared_key" ]; then
log_failure_msg 'Pre shared key is not defined'
return 1
fi
if [ -z ${PEER_ENDPOINT} ]; then
log_failure_msg 'Peer endpoint is not defined'
return 1
fi
if [ -z ${PERSISTENT_KEEPALIVES} ]; then
log_failure_msg 'Persistent keepalive is not defined'
return 1
fi
if [ -z ${ALLOWED_IPS} ]; then
log_failure_msg 'Allowed IPs is not defined'
return 1
fi
log_end_msg
log_begin_msg 'Starting wireguard'
# Ensure networking is started (idempotent) and setup DNS.
configure_networking
touch /etc/resolv.conf
for adapter in /run/net-*.conf; do
. "${adapter}"
if [ ! -z "${IPV4DNS0}" ]; then
echo nameserver "${IPV4DNS0}" >> /etc/resolv.conf
echo nameserver "${IPV4DNS1}" >> /etc/resolv.conf
fi
if [ ! -z "${IPV6DNS0}" ]; then
echo nameserver "${IPV6DNS0}" >> /etc/resolv.conf
fi
done
ip link add dev ${INTERFACE} type wireguard
if [ -z ${PRE_SHARED_KEY} ]; then
/sbin/wg set ${INTERFACE} \
private-key /etc/wireguard/private_key \
peer ${PEER_PUBLIC_KEY} \
endpoint ${PEER_ENDPOINT} \
persistent-keepalive ${PERSISTENT_KEEPALIVES} \
allowed-ips ${ALLOWED_IPS}
else
/sbin/wg set ${INTERFACE} \
private-key /etc/wireguard/private_key \
peer ${PEER_PUBLIC_KEY} \
preshared-key /etc/wireguard/pre_shared_key \
endpoint ${PEER_ENDPOINT} \
persistent-keepalive ${PERSISTENT_KEEPALIVES} \
allowed-ips ${ALLOWED_IPS}
fi
ip addr add ${INTERFACE_ADDR} dev ${INTERFACE}
ip link set ${INTERFACE} up
ip route add ${ALLOWED_IPS} dev ${INTERFACE}
log_end_msg