Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2669 - (hide 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 niro 1248 # $Id$
2    
3     provide basic-networking
4 niro 2667 require basic-system basic-init
5 niro 1248
6     help_network_hostname()
7     {
8 niro 1348 mecho "get network.hostname"
9 niro 2076 mecho " Displays the current hostname"
10 niro 1348 mecho
11 niro 1248 mecho "set network.hostname [hostname]"
12 niro 1348 mecho " Sets the hostname of the system"
13 niro 1248 mecho " mcore - the local hostname"
14     }
15    
16 niro 1348 get_network_hostname()
17     {
18 niro 2026 local val
19    
20     if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
21     then
22     val=$(hostname)
23     else
24 niro 2187 val=$(< @@SYSCONFDIR@@/hostname)
25 niro 2026 fi
26    
27     rvecho "${val}"
28 niro 1348 }
29    
30 niro 1248 # set_network_hostname $value
31     set_network_hostname()
32     {
33 niro 2269 local value="${CLASS_ARGV[0]}"
34 niro 1260 local CONFIG
35 niro 1351 local socket
36     local cookie
37     local authtype
38 niro 2669 local networksvc
39 niro 1351
40 niro 1248 [[ -z ${value} ]] && help_network_hostname && return 1
41    
42 niro 2187 CONFIG="@@SYSCONFDIR@@/hostname"
43 niro 1260 clearconfig
44     addconfig "${value}"
45 niro 1248 hostname "${value}"
46 niro 1351
47 niro 1605 # update hosts file
48 niro 2187 CONFIG="@@SYSCONFDIR@@/hosts"
49 niro 1605 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 niro 1351 # check for running x11 and recreate the xauth cookie with the correct hostname
60 niro 2398 if [[ ! -z $(pidof X) ]] || [[ -n $(pidof Xorg) ]]
61 niro 1351 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 niro 2669
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 niro 1248 }
78    
79     help_network_iface()
80     {
81 niro 2076 mecho "get network.iface"
82     mecho " Lists all configured network interfaces"
83     mecho
84 niro 1248 mecho "set network.iface [iface] [networking] [ip] [netmask] [broadcast] [network]"
85 niro 2076 mecho " Configure network interfaces"
86 niro 1248 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 niro 2076 get_network_iface()
92     {
93     local iface
94 niro 2187 for iface in $(find ${MROOT}@@CONFDDIR@@ -maxdepth 1 -name 'net.*' -printf '%f\n')
95 niro 2076 do
96     case ${iface} in
97     # exclude routes and samples
98     net.sample|net.routes) continue ;;
99     esac
100     rvecho -n "${iface//net.}"
101 niro 2187 ( cat ${MROOT}@@CONFDDIR@@${iface}; echo ) | while read line
102 niro 2076 do
103     rvecho -n ";${line}"
104     done
105     rvecho
106     done
107     }
108    
109 niro 1248 set_network_iface()
110     {
111 niro 2269 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 niro 1260 local CONFIG
118 niro 2032 local dhcp_prog
119 niro 1248
120     [[ -z ${iface} ]] && help_network_iface && return 1
121     [[ -z ${networking} ]] && help_network_iface && return 1
122    
123 niro 1260 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 niro 1248
131 niro 2668 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 niro 2187 CONFIG="@@CONFDDIR@@/net.${iface}"
138 niro 1260 clearconfig
139     addconfig 'ONBOOT="yes"'
140    
141 niro 1248 case ${networking} in
142     static)
143 niro 1260 addconfig 'NETWORKING="static"'
144     addconfig "IP=\"${ip}\""
145     addconfig "NETMASK=\"${netmask}\""
146     addconfig "BROADCAST=\"${broadcast}\""
147     addconfig "NETWORK=\"${network}\""
148 niro 1248 ;;
149    
150     dhcp)
151 niro 1260 addconfig 'NETWORKING="dhcp"'
152 niro 1248 ;;
153     esac
154    
155 niro 2032 if [[ -z ${MROOT} ]] || [[ ${MROOT} = / ]]
156 niro 1248 then
157 niro 2032 if [[ ! -z $(ip addr | grep "${iface}.*UP.*") ]]
158     then
159 niro 2667 ${MCORE_LIBDIR}/mcore-system-service --restart --service network "${iface}"
160 niro 2032 else
161 niro 2667 ${MCORE_LIBDIR}/mcore-system-service --start --service network "${iface}"
162 niro 2032 fi
163 niro 1248 else
164 niro 2032 case ${networking} in
165     static)
166     ifconfig "${iface}" "${ip}" netmask "${netmask}" broadcast "${broadcast}"
167     ;;
168     dhcp)
169 niro 2187 source @@CONFDDIR@@/network
170 niro 2032 [[ ! -z $(pidof ${dhcp_prog}) ]] && killall ${DEFAULT_DHCP_PROG}
171     ${DEFAULT_DHCP_PROG} ${DEFAULT_DHCP_START} "${iface}"
172     ;;
173     esac
174 niro 1248 fi
175     }
176    
177 niro 1348 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 niro 1646 rvecho "${gw}"
191 niro 1348 }
192    
193 niro 1284 set_network_gateway()
194 niro 1248 {
195 niro 2269 local value="${CLASS_ARGV[0]}"
196 niro 1260 local CONFIG
197     local i
198    
199 niro 1348 [[ -z ${value} ]] && help_network_gateway && return 1
200    
201 niro 2187 CONFIG="@@CONFDDIR@@/net.routes"
202 niro 1260 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 niro 1248 }
212    
213 niro 1348 help_network_nameserver()
214 niro 1248 {
215 niro 1348 mecho "get network.nameserver"
216     mecho " displays the current nameserver of the system"
217     mecho
218 niro 1351 mecho "set network.nameserver [ip1] [ip2] .. [ipN]"
219 niro 1348 mecho " adds given ips as nameserver to the system"
220     }
221    
222     get_network_nameserver()
223     {
224     local dns
225     local i
226    
227 niro 2187 dns=$(grep nameserver @@SYSCONFDIR@@/resolv.conf | sed 's:.*[[:space:]]\(.*\):\1:g')
228 niro 1348 for i in ${dns}
229     do
230 niro 1646 rvecho "${i}"
231 niro 1348 done
232     }
233    
234     set_network_nameserver()
235     {
236 niro 2269 local values="${CLASS_ARGV[*]}"
237 niro 1260 local CONFIG
238     local i
239 niro 1248
240 niro 1348 [[ -z ${values} ]] && help_network_nameserver && return 1
241    
242 niro 2187 CONFIG="@@SYSCONFDIR@@/resolv.conf"
243 niro 1260 clearconfig
244     for i in ${values}
245 niro 1248 do
246 niro 1260 addconfig "nameserver ${i}"
247 niro 1248 done
248     }