source: client/engine/Net.lib @ a37e5fc4

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since a37e5fc4 was 1928687, checked in by irina <irinagomez@…>, 7 years ago

#678 Con directorios de OU separados se resuelve errata de r5447 por la que al crear imagen en modo usuario no permite montar el repo en modo escritura.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5610 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 9.7 KB
Line 
1#!/bin/bash
2#/**
3#@file     Net.lib
4#@brief    Librería o clase Net
5#@class    Net
6#@brief    Funciones básicas de red.
7#@version  1.0.6
8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
13# ogChangeRepo  IPREPO [ OgUnit ]
14#@brief   Cambia el repositorio para el recurso remoto images.
15#@param 1 Ip Repositorio
16#@param 2 Abreviatura Unidad Organizativa
17#@return  Cambio recurso remoto en OGIMG.
18#@version 1.1 - Primera versión para OpenGnSys.
19#@author  Irina Gomez, ETSII Universidad de Sevilla
20#@date    2015-06-16
21#*/
22function ogChangeRepo {
23local SRCIMG NEWREPO REPO OGUNIT
24
25if [ "$*" == "help" ]; then
26    ogHelp "$FUNCNAME" "$FUNCNAME IPREPO [ OgUnit ]" \
27           "$FUNCNAME 10.1.120.3" \
28           "$FUNCNAME 10.1.120.3 cdc"
29    return
30fi
31
32
33if [ $# -lt 1 ]; then
34    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME IPREPO [ OgUnit ]"
35    return $?
36fi
37
38
39# Si REPO tomamos el repositorio y la unidad organizativa actual
40REPO=$(ogGetRepoIp)
41OGUNIT="$(df | awk -F " " '/ogimages/ {sub("//.*/ogimages","",$1); sub("/","",$1);  print $1}')"
42
43# Parametros de entrada. Si $1 = "REPO" dejo el repositorio actual
44[ "${1^^}" == "REPO" ] && NEWREPO="$REPO" || NEWREPO="${1}"
45
46# Si $1 y $2 son el repositorio y la OU actual me salgo
47[ "$NEWREPO" == "$REPO" ] && [ "$2" == "$OGUNIT" ]  && return 0
48
49source /scripts/functions
50source /scripts/ogfunctions
51umount $OGIMG
52[ "$2" == "" ] && SRCIMG="ogimages" || SRCIMG="ogimages/$2"
53eval $(grep "OPTIONS=" /scripts/ogfunctions)
54
55# Opciones de montaje: lectura o escritura
56mount |grep "ogimages.*rw," &>/dev/null && RW=",rw" || RW=",ro"
57
58ogEcho session log "$MSG_HELP_ogChangeRepo $NEWREPO ${2%/}"
59ogConnect $NEWREPO $ogprotocol $SRCIMG $OGIMG $RW
60
61# Si da error volvemos a montar el inicial
62if [ $? -ne 0 ]; then
63    ogConnect $REPO $ogprotocol $SRCIMG $OGIMG $RW
64    ogRaiseError session $OG_ERR_REPO "$NEWREPO"
65    return $?
66fi
67
68}
69
70
71#/**
72#         ogGetGroupDir [ str_repo ]
73#@brief   Devuelve el camino del directorio para el grupo del cliente.
74#@param   str_repo     repositorio de imágenes (opcional)
75#@return  path_dir  -  Camino al directorio del grupo.
76#@note    repo = { REPO, CACHE }     REPO por defecto
77#@exception OG_ERR_FORMAT    formato incorrecto.
78#@version 1.0.2 - Primera versión para OpenGnSys.
79#@author  Ramon Gomez, ETSII Universidad de Sevilla
80#@date    2011-10-03
81#*/
82function ogGetGroupDir ()
83{
84local REPO DIR GROUP
85if [ "$*" == "help" ]; then
86    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
87           "$FUNCNAME REPO  ==>  /opt/opengnsys/images/groups/Grupo1"
88    return
89fi
90# Error si se recibe más de 1 parámetro.
91case $# in
92    0)  REPO="REPO" ;;
93    1)  REPO="$1" ;;
94    *)  ogRaiseError $OG_ERR_FORMAT "$*"
95        return $? ;;
96esac
97
98GROUP="$(ogGetGroupName)"
99if [ -n "$GROUP" ]; then
100    DIR=$(ogGetPath "$REPO" "/groups/$GROUP" 2>/dev/null)
101    [ -d "$DIR" ] && echo "$DIR"
102fi
103# Para que no haya error al fallar la condición anterior
104return 0
105}
106
107
108#/**
109#         ogGetGroupName
110#@brief   Devuelve el nombre del grupo al que pertenece el cliente.
111#@return  str_group - Nombre de grupo.
112#@version 1.0.2 - Primera versión para OpenGnSys.
113#@author  Ramon Gomez, ETSII Universidad de Sevilla
114#@date    2011-10-03
115#*/
116function ogGetGroupName ()
117{
118if [ "$*" == "help" ]; then
119    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  Grupo1"
120    return
121fi
122[ -n "$group" ] && echo "$group"
123}
124
125
126#/**
127#         ogGetHostname
128#@brief   Muestra el nombre del cliente.
129#@return  str_host - nombre de máquina
130#@version 0.10 - Integración en OpenGnSys 0.10
131#@author  Ramon Gomez, ETSII Universidad de Sevilla
132#@date    2010-02-11
133#*/ ##
134function ogGetHostname ()
135{
136local HOST
137if [ "$*" == "help" ]; then
138    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  pc1"
139    return
140fi
141# Tomar nombre de la variable HOSTNAME
142HOST="$HOSTNAME"
143# Si no, tomar del DHCP, opción host-name       /* (comentario para Doxygen)
144[ -z "$HOST" ] && HOST=$(awk -F\" '/option host-name/ {gsub(/;/,""); host=$2}
145                                   END {print host}
146                                  ' /var/lib/dhcp3/dhclient.leases)
147# Si no, leer el parámetro del kernel hostname  (comentario para Doxygen) */
148[ -z "$HOST" ] && HOST=$(awk 'BEGIN {RS=""; FS="="}
149                              $1~/hostname/ {print $2}' /proc/cmdline)
150[ "$HOSTNAME" != "$HOST" ] && export HOSTNAME="$HOST"
151[ -n "$HOST" ] && echo $HOST
152}
153
154
155#/**
156#         ogGetIpAddress
157#@brief   Muestra la dirección IP del sistema
158#@return  str_ip - Dirección IP
159#@version 0.10 - Integración en OpenGnSys 0.10
160#@author  Ramon Gomez, ETSII Universidad de Sevilla
161#@date    2010-02-11
162#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
163#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
164#@author  Antonio J. Doblas Viso. Universidad de Malaga.
165#@date    2011-02-24
166#@version 1.0.2 - Soporte para varias tarjetas de red
167#@author  Ramon Gomez, ETSII Universidad de Sevilla
168#@date    2011-06-17
169#*/ ##
170function ogGetIpAddress ()
171{
172local IP
173if [ "$*" == "help" ]; then
174    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.10"
175    return
176fi
177if [ -n "$IPV4ADDR" ]; then
178     IP="$IPV4ADDR"
179else
180    # Obtener direcciones IP.
181    if [ -n "$DEVICE" ]; then
182        IP=$(ip -o address show up dev "$DEVICE" 2>/dev/null | awk '{if ($3~/inet$/) {printf ("%s ", $4)}}')
183    else
184        IP=$(ip -o address show up | awk '$2!~/lo/ {if ($3~/inet$/) {printf ("%s ", $4)}}')
185    fi
186fi
187# Mostrar solo la primera.
188echo "${IP%%/*}"       # (comentario para Doxygen) */
189}
190
191
192#/**
193#         ogGetMacAddress
194#@brief   Muestra la dirección Ethernet del cliente.
195#@return  str_ether - Dirección Ethernet
196#@version 0.10 - Integración en OpenGnSys 0.10
197#@author  Ramon Gomez, ETSII Universidad de Sevilla
198#@date    2010-02-11
199#@version 1.0.2 - Soporte para varias tarjetas de red
200#@author  Ramon Gomez, ETSII Universidad de Sevilla
201#@date    2011-06-17
202#*/ ##
203function ogGetMacAddress ()
204{
205local MAC
206if [ "$*" == "help" ]; then
207    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  00:11:22:33:44:55"
208    return
209fi
210# Obtener direcciones Ethernet.
211if [ -n "$DEVICE" ]; then
212    MAC=$(ip -o link show up dev "$DEVICE" 2>/dev/null | awk '{sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
213else
214    MAC=$(ip -o link show up | awk '$2!~/lo/ {sub (/.*\\/, ""); if ($1~/ether/) printf ("%s ", toupper($2));}')
215fi
216# Mostrar sólo la primera.
217echo ${MAC%% *}
218}
219
220
221#/**
222#         ogGetNetInterface
223#@brief   Muestra la interfaz de red del sistema
224#@return  str_interface - interfaz de red
225#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
226#@note    Usa las variables utilizadas por el initrd "/etc/net-ethX.conf
227#@author  Antonio J. Doblas Viso. Universidad de Malaga.
228#@date    2011-02-24
229#*/ ##
230function ogGetNetInterface ()
231{
232if [ "$*" == "help" ]; then
233    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  eth0"
234    return
235fi
236[ -n "$DEVICE" ] && echo "$DEVICE"
237}
238
239
240#/**
241#         ogGetRepoIp
242#@brief   Muestra la dirección IP del repositorio de datos.
243#@return  str_ip - Dirección IP
244#@version 0.10 - Integración en OpenGnSys 0.10
245#@author  Ramon Gomez, ETSII Universidad de Sevilla
246#@date    2011-01-13
247#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
248#@note   Comprobacion segun protocolo de conexion al Repo
249#@author  Antonio J. Doblas Viso. Universidad de Malaga.
250#@date    2011-02-24
251#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
252#@author  Ramon Gomez, ETSII Universidad de Sevilla
253#@date    2014-08-27
254#*/ ##
255function ogGetRepoIp ()
256{
257# Variables locales.
258local SOURCE FSTYPE
259
260# Mostrar ayuda.
261if [ "$*" == "help" ]; then
262    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
263    return
264fi
265
266# Obtener direcciones IP, según el tipo de montaje.
267eval $(findmnt -P -o SOURCE,FSTYPE $OGIMG)
268case "$FSTYPE" in
269    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
270    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
271esac
272}
273
274
275#/**
276#         ogGetServerIp
277#@brief   Muestra la dirección IP del Servidor de OpenGnSys.
278#@return  str_ip - Dirección IP
279#@version 0.10 - Integración en OpenGnSys 0.10
280#@author  Ramon Gomez, ETSII Universidad de Sevilla
281#@date    2011-01-13
282#@version 1.0 - Integración OpenGnSys 0.10 Opengnsys 0.10-testing
283#@note   Comprobacion segun protocolo de conexion al Repo
284#@author  Antonio J. Doblas Viso. Universidad de Malaga.
285#@date    2011-02-24
286#@version 1.0.6 - Obtener datos del punto de montaje, evitando fallo si $ogprotocol está vacía.
287#@author  Ramon Gomez, ETSII Universidad de Sevilla
288#@date    2014-08-27
289#*/ ##
290function ogGetServerIp ()
291{
292# Variables locales.
293local SOURCE FSTYPE
294
295# Mostrar ayuda.
296if [ "$*" == "help" ]; then
297    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  192.168.0.2"
298    return
299fi
300
301# Obtener direcciones IP, según el tipo de montaje.
302eval $(findmnt -P -o SOURCE,FSTYPE $OPENGNSYS)
303case "$FSTYPE" in
304    nfs)   echo "$SOURCE" | cut -f1 -d: ;;
305    cifs)  echo "$SOURCE" | cut -f3 -d/ ;;
306esac
307}
308
309
310#/**
311#         ogMakeGroupDir [ str_repo ]
312#@brief   Crea el directorio para el grupo del cliente.
313#@param   str_repo     repositorio de imágenes (opcional)
314#@return  (nada)
315#@note    repo = { REPO, CACHE }     REPO por defecto
316#@exception OG_ERR_FORMAT    formato incorrecto.
317#@version 1.0.5 - Primera versión para OpenGnSys.
318#@author  Ramon Gomez, ETSII Universidad de Sevilla
319#@date    2013-09-26
320#*/
321function ogMakeGroupDir ()
322{
323local REPO DIR GROUP
324if [ "$*" == "help" ]; then
325    ogHelp "$FUNCNAME" "$FUNCNAME str_repo" \
326           "$FUNCNAME" "$FUNCNAME REPO"
327    return
328fi
329# Error si se recibe más de 1 parámetro.
330case $# in
331    0)  REPO="REPO" ;;
332    1)  REPO="$1" ;;
333    *)  ogRaiseError $OG_ERR_FORMAT "$*"
334        return $? ;;
335esac
336# Comprobar tipo de repositorio.
337DIR=$(ogGetPath "$REPO" / 2>/dev/null)
338[ -n "$DIR" ] || ogRaiseError $OG_ERR_FORMAT "$1"
339GROUP="$(ogGetGroupName)"
340if [ -n "$GROUP" ]; then
341    mkdir -p "$DIR/groups/$GROUP" 2>/dev/null
342fi
343}
344
Note: See TracBrowser for help on using the repository browser.