Magellan Linux

Contents of /mcore-src/trunk/mcore-tools/src/modules/network/network.client.class.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2669 - (show annotations) (download)
Fri Dec 4 11:04:48 2015 UTC (8 years, 5 months ago) by niro
File size: 5665 byte(s)
-reload network configuration to inform a dhcp server about any hostname changes
1 # $Id$
2
3 provide basic-networking
4 require basic-system basic-init
5
6 help_network_hostname()
7 {
8 mecho "get network.hostname"
9 mecho " Displays the current hostname"
10 mecho
11 mecho "set network.hostname [hostname]"
12 mecho " Sets the hostname of the system"
13 mecho " mcore - the local hostname"
14 }
15
16 get_network_hostname()
17 {
18 local val
19
20 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
21 then
22 val=$(hostname)
23 else
24 val=$(< @@SYSCONFDIR@@/hostname)
25 fi
26
27 rvecho "${val}"
28 }
29
30 # set_network_hostname $value
31 set_network_hostname()
32 {
33 local value="${CLASS_ARGV[0]}"
34 local CONFIG
35 local socket
36 local cookie
37 local authtype
38 local networksvc
39
40 [[ -z ${value} ]] && help_network_hostname && return 1
41
42 CONFIG="@@SYSCONFDIR@@/hostname"
43 clearconfig
44 addconfig "${value}"
45 hostname "${value}"
46
47 # update hosts file
48 CONFIG="@@SYSCONFDIR@@/hosts"
49 clearconfig
50 addconfig "127.0.0.1 localhost ${value}"
51 # add ipv6 defaults
52 addconfig "::1 ip6-localhost ip6-loopback"
53 addconfig "fe00::0 ip6-localnet"
54 addconfig "ff00::0 ip6-mcastprefix"
55 addconfig "ff02::1 ip6-allnodes"
56 addconfig "ff02::2 ip6-allrouters"
57 addconfig "ff02::3 ip6-allhosts"
58
59 # check for running x11 and recreate the xauth cookie with the correct hostname
60 if [[ ! -z $(pidof X) ]] || [[ -n $(pidof Xorg) ]]
61 then
62 socket="${value}/unix${MCORE_XORG_DISPLAY}"
63 cookie=$(x11runas "xauth list | sed 's:.*\ \(.*\):\1:'")
64 authtype="MIT-MAGIC-COOKIE-1"
65 # add the new hostname to the xauthority file
66 x11runas "xauth add ${socket} ${authtype} ${cookie}"
67 fi
68
69 # reload network configuration to inform a dhcp server about the changed hostname
70 if is_provided systemd
71 then
72 networksvc="systemd-networkd"
73 elif is_provided sysvinit
74 networksvc="network"
75 fi
76 ${MCORE_LIBDIR}/mcore-system-service --restart --service "${service}" ;;
77 }
78
79 help_network_iface()
80 {
81 mecho "get network.iface"
82 mecho " Lists all configured network interfaces"
83 mecho
84 mecho "set network.iface [iface] [networking] [ip] [netmask] [broadcast] [network]"
85 mecho " Configure network interfaces"
86 mecho " iface - the interface name"
87 mecho " networking - may be 'static' or 'dhcp'"
88 mecho " ip, netmask, broadcast and network are optional and only required on static networking"
89 }
90
91 get_network_iface()
92 {
93 local iface
94 for iface in $(find ${MROOT}@@CONFDDIR@@ -maxdepth 1 -name 'net.*' -printf '%f\n')
95 do
96 case ${iface} in
97 # exclude routes and samples
98 net.sample|net.routes) continue ;;
99 esac
100 rvecho -n "${iface//net.}"
101 ( cat ${MROOT}@@CONFDDIR@@${iface}; echo ) | while read line
102 do
103 rvecho -n ";${line}"
104 done
105 rvecho
106 done
107 }
108
109 set_network_iface()
110 {
111 local iface="${CLASS_ARGV[0]}"
112 local networking="${CLASS_ARGV[1]}"
113 local ip="${CLASS_ARGV[2]}"
114 local netmask="${CLASS_ARGV[3]}"
115 local broadcast="${CLASS_ARGV[4]}"
116 local network="${CLASS_ARGV[5]}"
117 local CONFIG
118 local dhcp_prog
119
120 [[ -z ${iface} ]] && help_network_iface && return 1
121 [[ -z ${networking} ]] && help_network_iface && return 1
122
123 if [[ ${networking} = static ]]
124 then
125 [[ -z ${ip} ]] && help_network_iface && return 1
126 [[ -z ${netmask} ]] && help_network_iface && return 1
127 [[ -z ${broadcast} ]] && help_network_iface && return 1
128 [[ -z ${network} ]] && help_network_iface && return 1
129 fi
130
131 if is_provided systemd
132 then
133 decho "set_network_iface() is not supported with systemd, only sysvinit atm"
134 return 0
135 fi
136
137 CONFIG="@@CONFDDIR@@/net.${iface}"
138 clearconfig
139 addconfig 'ONBOOT="yes"'
140
141 case ${networking} in
142 static)
143 addconfig 'NETWORKING="static"'
144 addconfig "IP=\"${ip}\""
145 addconfig "NETMASK=\"${netmask}\""
146 addconfig "BROADCAST=\"${broadcast}\""
147 addconfig "NETWORK=\"${network}\""
148 ;;
149
150 dhcp)
151 addconfig 'NETWORKING="dhcp"'
152 ;;
153 esac
154
155 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
156 then
157 if [[ ! -z $(ip addr | grep "${iface}.*UP.*") ]]
158 then
159 ${MCORE_LIBDIR}/mcore-system-service --restart --service network "${iface}"
160 else
161 ${MCORE_LIBDIR}/mcore-system-service --start --service network "${iface}"
162 fi
163 else
164 case ${networking} in
165 static)
166 ifconfig "${iface}" "${ip}" netmask "${netmask}" broadcast "${broadcast}"
167 ;;
168 dhcp)
169 source @@CONFDDIR@@/network
170 [[ ! -z $(pidof ${dhcp_prog}) ]] && killall ${DEFAULT_DHCP_PROG}
171 ${DEFAULT_DHCP_PROG} ${DEFAULT_DHCP_START} "${iface}"
172 ;;
173 esac
174 fi
175 }
176
177 help_network_gateway()
178 {
179 mecho "get network.gateway"
180 mecho " displays the current network gateway"
181 mecho
182 mecho "set network.gateway [ip]"
183 mecho " sets the network gateway to [ip]"
184 }
185
186 get_network_gateway()
187 {
188 local gw
189 gw=$(ip route | grep default | sed 's:.*via[[:space:]]\(.*\)[[:space:]]dev.*:\1:')
190 rvecho "${gw}"
191 }
192
193 set_network_gateway()
194 {
195 local value="${CLASS_ARGV[0]}"
196 local CONFIG
197 local i
198
199 [[ -z ${value} ]] && help_network_gateway && return 1
200
201 CONFIG="@@CONFDDIR@@/net.routes"
202 clearconfig
203 addconfig "default gw ${value}"
204
205 # delete other default gw first
206 for i in $(ip route | grep default | sed 's:.*via\ \(.*\)\ dev.*:\1:')
207 do
208 route del default gw "${i}"
209 done
210 route add default gw "${value}"
211 }
212
213 help_network_nameserver()
214 {
215 mecho "get network.nameserver"
216 mecho " displays the current nameserver of the system"
217 mecho
218 mecho "set network.nameserver [ip1] [ip2] .. [ipN]"
219 mecho " adds given ips as nameserver to the system"
220 }
221
222 get_network_nameserver()
223 {
224 local dns
225 local i
226
227 dns=$(grep nameserver @@SYSCONFDIR@@/resolv.conf | sed 's:.*[[:space:]]\(.*\):\1:g')
228 for i in ${dns}
229 do
230 rvecho "${i}"
231 done
232 }
233
234 set_network_nameserver()
235 {
236 local values="${CLASS_ARGV[*]}"
237 local CONFIG
238 local i
239
240 [[ -z ${values} ]] && help_network_nameserver && return 1
241
242 CONFIG="@@SYSCONFDIR@@/resolv.conf"
243 clearconfig
244 for i in ${values}
245 do
246 addconfig "nameserver ${i}"
247 done
248 }