source: client/engine/Disk.lib @ 739d358

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 739d358 was 739d358, checked in by ramon <ramongomez@…>, 15 years ago

Nueva función ogGetDiskSize; engine-1.0: script listDisks.

git-svn-id: https://opengnsys.es/svn/trunk@1198 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 25.4 KB
RevLine 
[9f29ba6]1#!/bin/bash
2#/**
3#@file    Disk.lib
[9f57de01]4#@brief   Librería o clase Disk
[9f29ba6]5#@class   Disk
[2e15649]6#@brief   Funciones para gestión de discos y particiones.
[9f29ba6]7#@version 0.9
8#@warning License: GNU GPLv3+
9#*/
10
[5dbb046]11
12#/**
[42669ebf]13#         ogCreatePartitions int_ndisk str_parttype:int_partsize ...
[b094c59]14#@brief   Define el conjunto de particiones de un disco.
[42669ebf]15#@param   int_ndisk      nº de orden del disco
16#@param   str_parttype   mnemónico del tipo de partición
17#@param   int_partsize   tamaño de la partición (en KB)
[73c8417]18#@return  (nada, por determinar)
19#@exception OG_ERR_FORMAT   formato incorrecto.
20#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
21#@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize
22#@attention Pueden definirse particiones vacías de tipo \c EMPTY
[16f7627]23#@attention No puede definirse partición de cache y no se modifica si existe.
[73c8417]24#@note    Requisitos: sfdisk, parted, partprobe, awk
25#@todo    Definir atributos (arranque, oculta) y tamaños en MB, GB, etc.
26#@version 0.9 - Primera versión para OpenGNSys
27#@author  Ramon Gomez, ETSII Universidad de Sevilla
28#@date    2009/09/09
[bc7dfe7]29#@version 0.9.1 - Corrección del redondeo del tamaño del disco.
[4b45aff]30#@author  Ramon Gomez, ETSII Universidad de Sevilla
31#@date    2010/03/09
[1e7eaab]32#*/ ##
[42669ebf]33function ogCreatePartitions ()
34{
[73c8417]35# Variables locales.
[16f7627]36local DISK PART SECTORS CYLS START SIZE TYPE CACHESIZE EXTSTART EXTSIZE tmpsfdisk
[1e7eaab]37# Si se solicita, mostrar ayuda.
[1a7130a]38if [ "$*" == "help" ]; then
[73c8417]39    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk str_parttype:int_partsize ..." \
40           "$FUNCNAME 1 NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000"
41    return
42fi
[1e7eaab]43# Error si no se reciben menos de 2 parámetros.
[55ad138c]44[ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[73c8417]45
[16f7627]46# Desmontar todos los sistemas de archivos del disco.
47DISK=$(ogDiskToDev "$1") || return $?
48ogUnmountAll $1
[4b45aff]49# Nº total de sectores, para evitar desbordamiento (evitar redondeo).
[73c8417]50SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions)
[4b45aff]51CYLS=$(sfdisk -g $DISK | cut -f2 -d" ")
52SECTORS=$[SECTORS/CYLS*CYLS-1]
[16f7627]53# Se recalcula el nº de sectores del disco 1, si existe partición de caché.
[bc7dfe7]54CACHESIZE=$(ogGetCacheSize 2>/dev/null | awk '{print $0*2}')
55[ $1 == 1 -a -n "$CACHESIZE" ] && SECTORS=$[SECTORS-CACHESIZE]
[16f7627]56ENDPART3=$(sfdisk -uS -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}')
57# Sector de inicio (la partición 1 empieza en el sector 63).
[73c8417]58START=63
59PART=1
60
[b094c59]61# Fichero temporal de entrada para "sfdisk"
[73c8417]62tmpsfdisk=/tmp/sfdisk$$
63trap "rm -f $tmpsfdisk" 1 2 3 9 15
64
65echo "unit: sectors" >$tmpsfdisk
66echo                >>$tmpsfdisk
67
[42669ebf]68# Generar fichero de entrada para "sfdisk" con las particiones.
[16f7627]69shift
[73c8417]70while [ $# -gt 0 ]; do
[16f7627]71    # Conservar los datos de la partición de caché.
72    if [ -n "$CACHESIZE" -a $PART == 4 ]; then
73        echo "$DISK$PART : start=$[SECTORS+1], size=$CACHESIZE, Id=ca" >>$tmpsfdisk
74        PART=$[PART+1]
75    fi
[42669ebf]76    # Leer formato de cada parámetro - Tipo:Tamaño
[73c8417]77    TYPE="${1%%:*}"
[16f7627]78    [ "$TYPE" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$TYPE" || return $?
[73c8417]79    SIZE="${1#*:}"
[42669ebf]80    # Convertir en sectores de 512 B.
[16f7627]81    SIZE=$(echo $SIZE|awk '{print $0*2}')      # (solución error de sintaxis)
82    [ $SIZE -ne 0 ] || ogRaiseError $OG_ERR_FORMAT "$SIZE" || return $?
[42669ebf]83    # Obtener identificador de tipo de partición.
84    ID=$(ogFsToId "$TYPE")
85    [ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $?
86    # Comprobar si la partición es extendida.
87    if [ $ID = 5 ]; then
88        [ $PART -gt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $?
89        EXTSTART=$START
90        EXTSIZE=$SIZE
91    fi
[1e7eaab]92    # Incluir particiones lógicas dentro de la partición extendida.
[73c8417]93    if [ $PART = 5 ]; then
94        [ -z "$EXTSTART" ] && ogRaiseError $OG_ERR_FORMAT && return $?
95        START=$EXTSTART
96        SECTORS=$[EXTSTART+EXTSIZE]
97    fi
[1e7eaab]98    # Generar datos para la partición.
[73c8417]99    echo "$DISK$PART : start=$START, size=$SIZE, Id=$ID" >>$tmpsfdisk
[42669ebf]100    # Error si se supera el nº total de sectores.
[73c8417]101    START=$[START+SIZE]
[16f7627]102    [ $START -le $SECTORS ] || ogRaiseError $OG_ERR_FORMAT "$[START/2] > $[SECTORS/2]" || return $?
[73c8417]103    PART=$[PART+1]
104    shift
105done
[16f7627]106# Si no se indican las 4 particiones primarias, definirlas como vacías, conservando la partición de caché.
[73c8417]107while [ $PART -le 4 ]; do
[16f7627]108    if [ -n "$CACHESIZE" -a $PART == 4 ]; then
109        echo "$DISK$PART : start=$[SECTORS+1], size=$CACHESIZE, Id=ca" >>$tmpsfdisk
110    else
111        echo "$DISK$PART : start=0, size=0, Id=0" >>$tmpsfdisk
112    fi
[73c8417]113    PART=$[PART+1]
114done
[b094c59]115# Si se define partición extendida sin lógicas, crear particion 5 vacía.
[73c8417]116if [ $PART = 5 -a -n "$EXTSTART" ]; then
117    echo "${DISK}5 : start=$EXTSTART, size=$EXTSIZE, Id=0" >>$tmpsfdisk
118fi
119
120# Si la tabla de particiones no es valida, volver a generarla.
121[ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w"
[1e7eaab]122# Definir particiones y notificar al kernel.
[311532f]123sfdisk -f $DISK < $tmpsfdisk 2>/dev/null && partprobe
[73c8417]124rm -f $tmpsfdisk
125}
126
127
128#/**
[42669ebf]129#         ogDevToDisk path_device
[5dbb046]130#@brief   Devuelve el nº de orden de dicso (y partición) correspondiente al nombre de fichero de dispositivo.
[42669ebf]131#@param   path_device Camino del fichero de dispositivo.
132#@return  int_ndisk (para dispositivo de disco)
133#@return  int_ndisk int_npartition (para dispositivo de partición).
[5dbb046]134#@exception OG_ERR_FORMAT   Formato incorrecto.
135#@exception OG_ERR_NOTFOUND Dispositivo no detectado.
136#@note    Requisitos: awk
[985bef0]137#@version 0.1 -  Integracion para Opengnsys  -  EAC: DiskEAC() en ATA.lib
138#@author  Antonio J. Doblas Viso, Universidad de Malaga
139#@date    2008/10/27
140#@version 0.9 - Primera version para OpenGNSys
[5dbb046]141#@author  Ramon Gomez, ETSII Universidad Sevilla
[985bef0]142#@date    2009/07/20
[1e7eaab]143#*/ ##
[42669ebf]144function ogDevToDisk ()
145{
[73c8417]146# Variables locales.
[5dbb046]147local d n
[1e7eaab]148# Si se solicita, mostrar ayuda.
[1a7130a]149if [ "$*" == "help" ]; then
[5dbb046]150    ogHelp "$FUNCNAME" "$FUNCNAME path_device" \
151           "$FUNCNAME /dev/sda  =>  1 1"
152    return
153fi
154
[1e7eaab]155# Error si no se recibe 1 parámetro.
[5dbb046]156[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[42669ebf]157# Error si no es fichero de bloques.
[5dbb046]158[ -b "$1" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $?
159
[1e7eaab]160# Procesa todos los discos para devolver su nº de orden y de partición.
[5dbb046]161n=1
162for d in $(ogDiskToDev); do
163    [ -n "$(echo $1 | grep $d)" ] && echo "$n ${1#$d}" && return
164    n=$[n+1]
165done
166ogRaiseError $OG_ERR_NOTFOUND "$1"
167return $OG_ERR_NOTFOUND
168}
169
170
[9f29ba6]171#/**
[42669ebf]172#         ogDiskToDev [int_ndisk [int_npartition]]
[9f57de01]173#@brief   Devuelve la equivalencia entre el nº de orden del dispositivo (dicso o partición) y el nombre de fichero de dispositivo correspondiente.
[42669ebf]174#@param   int_ndisk      nº de orden del disco
175#@param   int_npartition nº de orden de la partición
[9f57de01]176#@return  Para 0 parametros: Devuelve los nombres de ficheros  de los dispositivos sata/ata/usb linux encontrados.
177#@return  Para 1 parametros: Devuelve la ruta del disco duro indicado.
178#@return  Para 2 parametros: Devuelve la ruta de la particion indicada.
179#@exception OG_ERR_FORMAT   Formato incorrecto.
180#@exception OG_ERR_NOTFOUND Dispositivo no detectado.
[2717297]181#@note    Requisitos: awk, lvm
[985bef0]182#@version 0.1 -  Integracion para Opengnsys  -  EAC: Disk() en ATA.lib;  HIDRA: DetectarDiscos.sh
183#@author Ramon Gomez, ETSII Universidad de Sevilla
184#@Date    2008/06/19
185#@author  Antonio J. Doblas Viso, Universidad de Malaga
186#@date    2008/10/27
187#@version 0.9 - Primera version para OpenGNSys
[9f57de01]188#@author  Ramon Gomez, ETSII Universidad Sevilla
189#@date    2009-07-20
[1e7eaab]190#*/ ##
[42669ebf]191function ogDiskToDev ()
192{
[59f9ad2]193# Variables locales
[2717297]194local ALLDISKS VOLGROUPS DISK PART
[9f29ba6]195
[1e7eaab]196# Si se solicita, mostrar ayuda.
[1a7130a]197if [ "$*" == "help" ]; then
[aae34f6]198    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk [int_npartition]" \
199           "$FUNCNAME      =>  /dev/sda /dev/sdb" \
200           "$FUNCNAME 1    =>  /dev/sda" \
201           "$FUNCNAME 1 1  =>  /dev/sda1"
202    return
203fi
204
[1e7eaab]205# Listar dispositivo para los discos duros (tipos: 3=hd, 8=sd).
[a5df9b9]206ALLDISKS=$(awk '($1==3 || $1==8) && $4!~/[0-9]/ {printf "/dev/%s ",$4}' /proc/partitions)
[13ccdf5]207VOLGROUPS=$(vgs -a --noheadings 2>/dev/null | awk '{printf "/dev/%s ",$1}')
[2717297]208ALLDISKS="$ALLDISKS $VOLGROUPS"
[9f29ba6]209
[1e7eaab]210# Mostrar salidas segun el número de parametros.
[9f29ba6]211case $# in
[2717297]212    0)  # Muestra todos los discos, separados por espacios.
213        echo $ALLDISKS
214        ;;
215    1)  # Error si el parámetro no es un digito.
216        [ -z "${1/[1-9]/}" ] || ogRaiseError $OG_ERR_FORMAT || return $?
217        DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}')
218        # Error si el fichero no existe.
219        [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $?
220        echo "$DISK"
221        ;;
222    2)  # Error si los 2 parámetros no son digitos.
223        [ -z "${1/[1-9]/}" -a -z "${2/[1-9]/}" ] || ogRaiseError $OG_ERR_FORMAT|| return $?
224        DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}')
225        [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $?
226        PART="$DISK$2"
[1e7eaab]227        # Comprobar si es partición.
[2717297]228        if [ -b "$PART" ]; then
229            echo "$PART"
230        elif [ -n "$VOLGROUPS" ]; then
[0bfbbe1]231            # Comprobar si volumen lógico.      /* (comentario Doxygen)
[2717297]232            PART=$(lvscan -a 2>/dev/null | grep "'$DISK/" | awk -v n=$2 -F\' '{if (NR==n) print $2}')
233            [ -e "$PART" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $?
[0bfbbe1]234            #                                   (comentario Doxygen) */
[2717297]235            echo "$PART"
236        else
237            ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $?
238        fi
239        ;;
240    *)  # Formato erroneo.
241        ogRaiseError $OG_ERR_FORMAT
[aae34f6]242        return $OG_ERR_FORMAT
243        ;;
[9f29ba6]244esac
245}
246
247
248#/**
[42669ebf]249#         ogFsToId str_fstype
250#@brief   Devuelve el identificador de partición correspondiente a un tipo de sistema de archivos.
251#@param   str_fstype  mnemónico de tipo de sistema de archivos
252#@return  int_idpart  nº identificador de tipo de partición.
253#@exception OG_ERR_FORMAT   Formato incorrecto.
[985bef0]254#@version 0.1 -  Integracion para Opengnsys  -  EAC: TypeFS () en ATA.lib
255#@author  Antonio J. Doblas Viso, Universidad de Malaga
256#@date    2008/10/27
257#@version 0.9 - Primera version para OpenGNSys
[42669ebf]258#@author  Ramon Gomez, ETSII Universidad Sevilla
259#@date    2009-12-14
260#*/ ##
261function ogFsToId ()
262{
263# Variables locales
264local ID
265
266# Si se solicita, mostrar ayuda.
267if [ "$*" == "help" ]; then
268    ogHelp "$FUNCNAME" "$FUNCNAME str_fstype" "$FUNCNAME EXT3  =>  83"
269    return
270fi
271# Error si no se recibe 1 parámetro.
272[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
273
274# Asociar id. de partición para su mnemónico de sistema de archivos.
275case "$1" in
276    EMPTY)      ID=0  ;;
277    FAT12)      ID=1  ;;
278    EXTENDED)   ID=5  ;;
279    FAT16)      ID=6  ;;
280    NTFS|EXFAT) ID=7  ;;
281    FAT32)      ID=b  ;;
282    HFAT12)     ID=11 ;;
283    HFAT16)     ID=16 ;;
284    HNTFS)      ID=17 ;;
285    HFAT32)     ID=1b ;;
286    LINUX-SWAP) ID=82 ;;
287    EXT[234]|REISERFS|REISER4|XFS|JFS)
288                ID=83 ;;
289    LINUX-LVM)  ID=8e ;;
290    SOLARIS)    ID=bf ;;
291    CACHE)      ID=ca ;;
292    LINUX-RAID) ID=fd ;;
293    *)          ID="" ;;
294esac
295echo $ID
296}
297
298
[739d358]299#/**
300#         ogGetDiskSize int_ndisk
301#@brief   Muestra el tamaño en KB de un disco.
302#@param   int_ndisk   nº de orden del disco
303#@return  int_size  - Tamaño en KB del disco.
304#@exception OG_ERR_FORMAT   formato incorrecto.
305#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un
306dispositivo).
307#@note    Requisitos: sfdisk, awk
308#@version 0.9.2 - Primera version para OpenGnSys
309#@author  Ramon Gomez, ETSII Universidad de Sevilla
310#@date    2010/09/15
311#*/ ##
312function ogGetDiskSize ()
313{
314# Variables locales.
315local DISK
316
317# Si se solicita, mostrar ayuda.
318if [ "$*" == "help" ]; then
319    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ "$FUNCNAME 1  =>
320244198584"
321    return
322fi
323# Error si no se recibe 1 parámetro.
324[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
325
326# Obtener el tamaño de la partición.
327DISK="$(ogDiskToDev $1)" || return $?
328sfdisk -s $DISK
329}
330
331
[42669ebf]332#         ogGetPartitionActive int_ndisk
[a5df9b9]333#@brief   Muestra que particion de un disco esta marcada como de activa.
[b9e1a8c]334#@param   int_ndisk   nº de orden del disco
335#@return  int_npart   Nº de partición activa
[a5df9b9]336#@exception OG_ERR_FORMAT Formato incorrecto.
337#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
338#@note    Requisitos: parted
[59f9ad2]339#@todo    Queda definir formato para atributos (arranque, oculta, ...).
[985bef0]340#@version 0.9 - Primera version compatible con OpenGNSys.
[a5df9b9]341#@author  Ramon Gomez, ETSII Universidad de Sevilla
[985bef0]342#@date    2009/09/17
[1e7eaab]343#*/ ##
[42669ebf]344function ogGetPartitionActive ()
345{
[59f9ad2]346# Variables locales
[a5df9b9]347local DISK
348
[1e7eaab]349# Si se solicita, mostrar ayuda.
[aae34f6]350if [ "$*" == "help" ]; then
351    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1  =>  1"
352    return
353fi
[1e7eaab]354# Error si no se recibe 1 parámetro.
[aae34f6]355[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[a5df9b9]356
[1e7eaab]357# Comprobar que el disco existe y listar su partición activa.
[a5df9b9]358DISK="$(ogDiskToDev $1)" || return $?
359parted $DISK print 2>/dev/null | awk '/boot/ {print $1}'
360}
361
362
363#/**
[42669ebf]364#         ogGetPartitionId int_ndisk int_npartition
[9f57de01]365#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
[42669ebf]366#@param   int_ndisk      nº de orden del disco
367#@param   int_npartition nº de orden de la partición
[9f57de01]368#@return  Identificador de tipo de partición.
[326cec3]369#@exception OG_ERR_FORMAT   Formato incorrecto.
[9f57de01]370#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
[a5df9b9]371#@note    Requisitos: sfdisk
[9f57de01]372#@version 0.9 - Primera versión compatible con OpenGNSys.
373#@author  Ramon Gomez, ETSII Universidad de Sevilla
374#@date    25/03/2009
[1e7eaab]375#*/ ##
[42669ebf]376function ogGetPartitionId ()
377{
[59f9ad2]378# Variables locales.
[a5df9b9]379local DISK PART
[2e15649]380
[1e7eaab]381# Si se solicita, mostrar ayuda.
[aae34f6]382if [ "$*" == "help" ]; then
383    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
384           "$FUNCNAME 1 1  =>  7"
385    return
386fi
[1e7eaab]387# Error si no se reciben 2 parámetros.
[aae34f6]388[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[2e15649]389
[1e7eaab]390# Detectar id. de tipo de particion y codificar al mnemonico.
[2e15649]391DISK=$(ogDiskToDev $1) || return $?
392PART=$(ogDiskToDev $1 $2) || return $?
393echo $(sfdisk --id $DISK $2 2>/dev/null)
[9f29ba6]394}
395
[a5df9b9]396
397#/**
[42669ebf]398#         ogGetPartitionSize int_ndisk int_npartition
[a5df9b9]399#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]400#@param   int_ndisk      nº de orden del disco
401#@param   int_npartition nº de orden de la partición
402#@return  int_partsize - Tamaño en KB de la partición.
[a5df9b9]403#@exception OG_ERR_FORMAT   formato incorrecto.
404#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
405#@note    Requisitos: sfdisk, awk
[985bef0]406#@version 0.1 -  Integracion para Opengnsys  -  EAC: SizePartition () en ATA.lib
407#@author  Antonio J. Doblas Viso, Universidad de Malaga
408#@date    2008/10/27
409#@version 0.9 - Primera version para OpenGNSys
[a5df9b9]410#@author  Ramon Gomez, ETSII Universidad de Sevilla
411#@date    2009/07/24
[1e7eaab]412#*/ ##
[42669ebf]413function ogGetPartitionSize ()
414{
[59f9ad2]415# Variables locales.
[739d358]416local DISK PART
[a5df9b9]417
[1e7eaab]418# Si se solicita, mostrar ayuda.
[aae34f6]419if [ "$*" == "help" ]; then
420    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
421           "$FUNCNAME 1 1  =>  10000000"
422    return
423fi
[1e7eaab]424# Error si no se reciben 2 parámetros.
[aae34f6]425[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[a5df9b9]426
[1e7eaab]427# Obtener el tamaño de la partición.
[3543b3e]428DISK="$(ogDiskToDev $1)" || return $?
[a5df9b9]429PART="$(ogDiskToDev $1 $2)" || return $?
[3543b3e]430case "$(ogGetPartitionId $1 $2)" in
431    5|f)  # Procesar detección de tamaño de partición Extendida.
[55ad138c]432          sfdisk -l $DISK 2>/dev/null | \
[3543b3e]433                    awk -v p=$PART '{if ($1==p) {sub (/\*/,""); print $5} }'
434          ;;
435    *)    sfdisk -s $PART
436          ;;
437esac
[a5df9b9]438}
439
440
[b094c59]441#/**
[73c8417]442#         ogListPartitions int_ndisk
[a5df9b9]443#@brief   Lista las particiones definidas en un disco.
[42669ebf]444#@param   int_ndisk  nº de orden del disco
445#@return  str_parttype:int_partsize ...
[a5df9b9]446#@exception OG_ERR_FORMAT   formato incorrecto.
447#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
448#@note    Requisitos: \c parted \c awk
[73c8417]449#@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize
[59f9ad2]450#@attention Las tuplas de valores están separadas por espacios.
[a5df9b9]451#@version 0.9 - Primera versión para OpenGNSys
452#@author  Ramon Gomez, ETSII Universidad de Sevilla
453#@date    2009/07/24
[1e7eaab]454#*/ ##
[42669ebf]455function ogListPartitions ()
456{
[59f9ad2]457# Variables locales.
[55ad138c]458local DISK PART NPARTS TYPE SIZE
[aae34f6]459
[42669ebf]460# Si se solicita, mostrar ayuda.
[1a7130a]461if [ "$*" == "help" ]; then
[aae34f6]462    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \
[73c8417]463           "$FUNCNAME 1  =>  NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000"
[aae34f6]464    return
465fi
[42669ebf]466# Error si no se recibe 1 parámetro.
[5dbb046]467[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $?
[a5df9b9]468
[42669ebf]469# Procesar la salida de \c parted .
[b094c59]470DISK="$(ogDiskToDev $1)" || return $?
[3543b3e]471NPARTS=$(ogGetPartitionsNumber $1)
472for (( PART = 1; PART <= NPARTS; PART++ )); do
[55ad138c]473    TYPE=$(ogGetFsType $1 $PART 2>/dev/null)
[3543b3e]474    if [ $? -eq 0 ]; then
[55ad138c]475        SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null)
476        echo -n "$TYPE:$SIZE "
[3543b3e]477    else
478        echo -n "EMPTY:0 "
479    fi
[a5df9b9]480done
481echo
482}
483
[326cec3]484
485#/**
[55ad138c]486#         ogListPrimaryPartitions int_ndisk
487#@brief   Metafunción que lista las particiones primarias no vacías definidas en un disco.
[42669ebf]488#@param   int_ndisk  nº de orden del disco
[55ad138c]489#@see     ogListPartitions
[1e7eaab]490#*/ ##
[42669ebf]491function ogListPrimaryPartitions ()
492{
[55ad138c]493# Variables locales.
494local PARTS
495
496PARTS=$(ogListPartitions "$@") || return $?
497echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//'
498}
499
500
501#/**
502#         ogListLogicalPartitions int_ndisk
503#@brief   Metafunción que lista las particiones lógicas definidas en un disco.
[42669ebf]504#@param   int_ndisk  nº de orden del disco
[55ad138c]505#@see     ogListPartitions
[1e7eaab]506#*/ ##
[b061ad0]507function ogListLogicalPartitions ()
508{
[55ad138c]509# Variables locales.
510local PARTS
511
512PARTS=$(ogListPartitions "$@") || return $?
513echo $PARTS | cut -sf5- -d" "
514}
515
516
517#/**
[42669ebf]518#         ogSetPartitionActive int_ndisk int_npartition
[89403cd]519#@brief   Establece cual es la partición activa de un disco.
[42669ebf]520#@param   int_ndisk      nº de orden del disco
521#@param   int_npartition nº de orden de la partición
522#@return  (nada).
[326cec3]523#@exception OG_ERR_FORMAT   Formato incorrecto.
524#@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo.
525#@note    Requisitos: parted
[985bef0]526#@version 0.1 -  Integracion para Opengnsys  -  EAC: SetPartitionActive() en ATA.lib
527#@author  Antonio J. Doblas Viso, Universidad de Malaga
528#@date    2008/10/27
529#@version 0.9 - Primera version compatible con OpenGNSys.
[326cec3]530#@author  Ramon Gomez, ETSII Universidad de Sevilla
531#@date    2009/09/17
[1e7eaab]532#*/ ##
[42669ebf]533function ogSetPartitionActive ()
534{
[326cec3]535# Variables locales
536local DISK PART
537
[1e7eaab]538# Si se solicita, mostrar ayuda.
[326cec3]539if [ "$*" == "help" ]; then
540    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
541           "$FUNCNAME 1 1"
542    return
543fi
[1e7eaab]544# Error si no se reciben 2 parámetros.
[326cec3]545[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
546
[1e7eaab]547# Comprobar que el disco existe y activar la partición indicada.
[326cec3]548DISK="$(ogDiskToDev $1)" || return $?
549PART="$(ogDiskToDev $1 $2)" || return $?
550parted -s $DISK set $2 boot on 2>/dev/null
551}
552
553
[1553fc7]554#/**
[42669ebf]555#         ogSetPartitionSize int_ndisk int_npartition int_size
[2ecd096]556#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]557#@param  int_ndisk      nº de orden del disco
558#@param   int_npartition nº de orden de la partición
559#@param   int_size       tamaño de la partición (en KB)
[2ecd096]560#@return  (nada)
561#@exception OG_ERR_FORMAT   formato incorrecto.
562#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
563#@note    Requisitos: sfdisk, awk
564#@todo    Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición.
565#@version 0.9 - Primera versión para OpenGNSys
566#@author  Ramon Gomez, ETSII Universidad de Sevilla
567#@date    2009/07/24
[1e7eaab]568#*/ ##
[42669ebf]569function ogSetPartitionSize ()
570{
[2ecd096]571# Variables locales.
572local DISK PART SIZE
573
[1e7eaab]574# Si se solicita, mostrar ayuda.
[2ecd096]575if [ "$*" == "help" ]; then
[311532f]576    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition int_size" \
[2ecd096]577           "$FUNCNAME 1 1 10000000"
578    return
579fi
[1e7eaab]580# Error si no se reciben 3 parámetros.
[2ecd096]581[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
582
[1e7eaab]583# Obtener el tamaño de la partición.
[2ecd096]584DISK="$(ogDiskToDev $1)" || return $?
585PART="$(ogDiskToDev $1 $2)" || return $?
586# Convertir tamaño en KB a sectores de 512 B.
587SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $?
[1e7eaab]588# Usar \c sfdisk para redefinir el tamaño.
[1c04494]589sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[bf1840e9]590partprobe 2>/dev/null
[2ecd096]591}
592
593
594#/**
[6cdca0c]595#         ogUpdatePartitionTable
[1553fc7]596#@brief   Fuerza al kernel releer la tabla de particiones de los discos duros
[42669ebf]597#@param   no requiere
[1553fc7]598#@return  informacion propia de la herramienta
599#@note    Requisitos: \c partprobe
600#@warning pendiente estructurar la funcion a opengnsys
[985bef0]601#@version 0.1 -  Integracion para Opengnsys  -  EAC: UpdatePartitionTable() en ATA.lib
602#@author  Antonio J. Doblas Viso. Universidad de Malaga
603#@date    27/10/2008
[1553fc7]604#*/
605
[42669ebf]606function ogUpdatePartitionTable ()
607{
[1553fc7]608echo "Forzando al kernel la lectura de la tabla de particiones"
609list=`partprobe -s | cut -f1 -d: ` 2>/dev/null
610echo $list > /tmp/disk
611}
[1a7130a]612
613
614
[24f2399]615#/**  @function ogGetPartitionsNumber: @brief detecta el numero de particiones del disco duro indicado.
[42669ebf]616#@param   int_numdisk   (indentificado EAC del disco)
[1a7130a]617#@return  devuelve el numero paritiones del disco duro indicado
618#@warning  Salidas de errores no determinada
619#@attention Requisitos: parted
620#@note    Notas sin especificar
[985bef0]621#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DetectNumberPartition () en ATA.lib
622#@author  Antonio J. Doblas Viso. Universidad de Malaga
623#@date    Date: 27/10/2008
624#@version 1.0 - Uso de sfdisk Primera version para OpenGnSys
625#@author  Ramon Gomez, ETSII Universidad de Sevilla
626#@date    2009/07/24
[1a7130a]627#*/
[985bef0]628function ogGetPartitionsNumber () {
[55ad138c]629           #local disco totalpart
630           #disco=`ogDiskToDev $1`
631           #totalpart=`parted $disco print | egrep ^" [0123456789] " -c`
632           #echo $totalpart
633local DISK
634#/// Contar el nº de veces que aparece el disco en su lista de particiones.
635DISK=$(ogDiskToDev $1) 2>/dev/null
636sfdisk -l $DISK 2>/dev/null | grep -c "^$DISK"
[1a7130a]637}
[6cdca0c]638
639
640#/**  @function ogDiskToRelativeDev: @brief Traduce los ID de discos o particiones EAC a ID Linux relativos, es decir 1 1 => sda1
641#@param  Admite 1 parametro:   $1  int_numdisk
642#@param  Admite 2 parametro:   $1   int_numdisk                    $2  int_partition
643#@return  Para 1 parametros traduce Discos Duros: Devuelve la ruta relativa linux del disco duro indicado con nomenclatura EAC.........ejemplo: IdPartition 1 => sda
644#@return  Para 2 parametros traduce Particiones: Devuelve la ruta relativa linux de la particion indicado con nomenclatura EAC...........  ejemplo: IdPartition  2 1 => sdb1
645#@warning  No definidas
646#@attention
647#@note    Notas sin especificar
[985bef0]648#@version 0.1 -  Integracion para Opengnsys  -  EAC:  IdPartition en ATA.lib
649#@author  Antonio J. Doblas Viso. Universidad de Malaga
650#@date    27/10/2008
[6cdca0c]651#*/
[985bef0]652function ogDiskToRelativeDev () {
[6cdca0c]653if [ $# = 0 ]
654then
655        Msg "Info: Traduce el identificador del dispositivo EAC a dispositivo linux \n" info
656        Msg "Sintaxis1: IdPartition int_disk -----------------Ejemplo1: IdPartition 1 -> sda " example
657        Msg "Sintaxis2: IdPartition int_disk int_partition  --Ejemplo2: IdPartition 1 2 -> sda2 " example
658
659return
660fi
661#PART="$(Disk|cut -f$1 -d' ')$2"    # se comenta esta linea porque doxygen no reconoce la funcion disk y no crea los enlaces y referencias correctas.
662PART=$(ogDiskToDev|cut -f$1 -d' ')$2
663echo $PART | cut -f3 -d \/
664}
[26c729b]665
666#/**  @function ogDeletePartitionTable: @brief Borra la tabla de particiones del disco.
667#@param $1 opcion A (identificador LINUX)       str_ID_linux (/dev/sda)
668#@param $1 opcion B (Identifiador EAC)                  int_numdiskEAC(1)
669#@return   la informacion propia del fdisk
670#@warning    no definidos
671#@attention
672#@note
[985bef0]673#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DeletePartitionTable () en ATA.lib
674#@author  Antonio J. Doblas Viso. Universidad de Malaga
675#@date    27/10/2008
[26c729b]676#*/
[985bef0]677function ogDeletePartitionTable () {
[26c729b]678if [ $# = 0 ]
679then
680        Msg "sintaxis1: ogDeletePartitionTable int_disk" red
681        Msg "sintaxis2: ogDeletePartitionTable str_/dev/sdX" red
682        return
683fi
684if [ -n "${1%/dev/*}" ]
685        then
686        dev=`DiskToDev $1`
687        else
688        dev=$1
689fi
690echo -ne "o\nw" | fdisk $dev
691}
[0df4b9f7]692
693
694#/**  @function ogSetPartitionId: @brief Cambia el identificador de la particion, pero no su sistema de archivos.
695#@param  $1 int_numdiskEAC
696#@param  $2 int_numpartitionEAC
697#@param  $3 str_tipoPartition admite EXT2 EXT3 NTFS FAT32 SWAP CACHE
698#@return   la propia del fdisk
699#@warning    no controla los parametros, si se introducen mal o simplemente no se introducen no muestra mensaje
700#@warning    Identifica por nombre del sistema de archivos no por número
701#@attention Requisitos:  fdisk
702#@note
[985bef0]703#@version 0.1 -  Integracion para Opengnsys  - SetPartitionType() en ATA.lib
704#@author  Antonio J. Doblas Viso. Universidad de Malaga
705#@date    27/10/2008
[0df4b9f7]706#*/
[985bef0]707function ogSetPartitionId() {
[be81649]708# Variables locales
709local DISK PART ID
710
[42669ebf]711# Si se solicita, mostrar ayuda.
[be81649]712if [ "$*" == "help" ]; then
[311532f]713    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_type" \
714           "$FUNCNAME 1 1 NTFS"
[be81649]715    return
[0df4b9f7]716fi
[42669ebf]717# Error si no se reciben 3 parámetros.
[be81649]718[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
719
[42669ebf]720# Sustituye nº de disco por su dispositivo.
[be81649]721DISK=`ogDiskToDev $1` || return $?
722PART=`ogDiskToDev $1 $2` || return $?
723
[42669ebf]724# Elección del tipo de partición.
725ID=$(ogFsToId "$3")
726[ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $?
727
[be81649]728echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK 1>/dev/null 2>&1
[0df4b9f7]729}
[cc6ad14]730
[be81649]731
[97da528]732#/**  @function ogDeletePartitionsLabels: @brief Elimina la informacion que tiene el kernel del cliente og sobre los labels de los sistemas de archivos
[cc6ad14]733#@param  No requiere
734#@return   Nada
735#@warning
736#@attention Requisitos:  comando interno linux rm
737#@note
[985bef0]738#@version 0.1 -  Integracion para Opengnsys  -  EAC:   DeletePartitionTable()  en ATA.lib
739#@author  Antonio J. Doblas Viso. Universidad de Malaga
740#@date    27/10/2008
[cc6ad14]741#*/
[985bef0]742function ogDeletePartitionsLabels () {
[cc6ad14]743rm /dev/disk/by-label/*    # */ COMENTARIO OBLIGATORIO PARA DOXYGEN
744}
745
Note: See TracBrowser for help on using the repository browser.