source: client/engine/Disk.lib @ bfeb89a

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

Corrección errata en librería Disk.lib

git-svn-id: https://opengnsys.es/svn/trunk@1257 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.
[be0a5cf]305#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
[739d358]306#@note    Requisitos: sfdisk, awk
307#@version 0.9.2 - Primera version para OpenGnSys
308#@author  Ramon Gomez, ETSII Universidad de Sevilla
309#@date    2010/09/15
310#*/ ##
311function ogGetDiskSize ()
312{
313# Variables locales.
314local DISK
315
316# Si se solicita, mostrar ayuda.
317if [ "$*" == "help" ]; then
[be0a5cf]318    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ "$FUNCNAME 1  => 244198584"
[739d358]319    return
320fi
321# Error si no se recibe 1 parámetro.
322[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
323
324# Obtener el tamaño de la partición.
325DISK="$(ogDiskToDev $1)" || return $?
326sfdisk -s $DISK
327}
328
329
[42669ebf]330#         ogGetPartitionActive int_ndisk
[a5df9b9]331#@brief   Muestra que particion de un disco esta marcada como de activa.
[b9e1a8c]332#@param   int_ndisk   nº de orden del disco
333#@return  int_npart   Nº de partición activa
[a5df9b9]334#@exception OG_ERR_FORMAT Formato incorrecto.
335#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
336#@note    Requisitos: parted
[59f9ad2]337#@todo    Queda definir formato para atributos (arranque, oculta, ...).
[985bef0]338#@version 0.9 - Primera version compatible con OpenGNSys.
[a5df9b9]339#@author  Ramon Gomez, ETSII Universidad de Sevilla
[985bef0]340#@date    2009/09/17
[1e7eaab]341#*/ ##
[42669ebf]342function ogGetPartitionActive ()
343{
[59f9ad2]344# Variables locales
[a5df9b9]345local DISK
346
[1e7eaab]347# Si se solicita, mostrar ayuda.
[aae34f6]348if [ "$*" == "help" ]; then
349    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1  =>  1"
350    return
351fi
[1e7eaab]352# Error si no se recibe 1 parámetro.
[aae34f6]353[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[a5df9b9]354
[1e7eaab]355# Comprobar que el disco existe y listar su partición activa.
[a5df9b9]356DISK="$(ogDiskToDev $1)" || return $?
357parted $DISK print 2>/dev/null | awk '/boot/ {print $1}'
358}
359
360
361#/**
[42669ebf]362#         ogGetPartitionId int_ndisk int_npartition
[9f57de01]363#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
[42669ebf]364#@param   int_ndisk      nº de orden del disco
365#@param   int_npartition nº de orden de la partición
[9f57de01]366#@return  Identificador de tipo de partición.
[326cec3]367#@exception OG_ERR_FORMAT   Formato incorrecto.
[9f57de01]368#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
[a5df9b9]369#@note    Requisitos: sfdisk
[9f57de01]370#@version 0.9 - Primera versión compatible con OpenGNSys.
371#@author  Ramon Gomez, ETSII Universidad de Sevilla
372#@date    25/03/2009
[1e7eaab]373#*/ ##
[42669ebf]374function ogGetPartitionId ()
375{
[59f9ad2]376# Variables locales.
[a5df9b9]377local DISK PART
[2e15649]378
[1e7eaab]379# Si se solicita, mostrar ayuda.
[aae34f6]380if [ "$*" == "help" ]; then
381    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
382           "$FUNCNAME 1 1  =>  7"
383    return
384fi
[1e7eaab]385# Error si no se reciben 2 parámetros.
[aae34f6]386[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[2e15649]387
[1e7eaab]388# Detectar id. de tipo de particion y codificar al mnemonico.
[2e15649]389DISK=$(ogDiskToDev $1) || return $?
390PART=$(ogDiskToDev $1 $2) || return $?
391echo $(sfdisk --id $DISK $2 2>/dev/null)
[9f29ba6]392}
393
[a5df9b9]394
395#/**
[42669ebf]396#         ogGetPartitionSize int_ndisk int_npartition
[a5df9b9]397#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]398#@param   int_ndisk      nº de orden del disco
399#@param   int_npartition nº de orden de la partición
400#@return  int_partsize - Tamaño en KB de la partición.
[a5df9b9]401#@exception OG_ERR_FORMAT   formato incorrecto.
402#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
403#@note    Requisitos: sfdisk, awk
[985bef0]404#@version 0.1 -  Integracion para Opengnsys  -  EAC: SizePartition () en ATA.lib
405#@author  Antonio J. Doblas Viso, Universidad de Malaga
406#@date    2008/10/27
407#@version 0.9 - Primera version para OpenGNSys
[a5df9b9]408#@author  Ramon Gomez, ETSII Universidad de Sevilla
409#@date    2009/07/24
[1e7eaab]410#*/ ##
[42669ebf]411function ogGetPartitionSize ()
412{
[59f9ad2]413# Variables locales.
[739d358]414local DISK PART
[a5df9b9]415
[1e7eaab]416# Si se solicita, mostrar ayuda.
[aae34f6]417if [ "$*" == "help" ]; then
418    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
419           "$FUNCNAME 1 1  =>  10000000"
420    return
421fi
[1e7eaab]422# Error si no se reciben 2 parámetros.
[aae34f6]423[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[a5df9b9]424
[1e7eaab]425# Obtener el tamaño de la partición.
[3543b3e]426DISK="$(ogDiskToDev $1)" || return $?
[a5df9b9]427PART="$(ogDiskToDev $1 $2)" || return $?
[3543b3e]428case "$(ogGetPartitionId $1 $2)" in
429    5|f)  # Procesar detección de tamaño de partición Extendida.
[55ad138c]430          sfdisk -l $DISK 2>/dev/null | \
[3543b3e]431                    awk -v p=$PART '{if ($1==p) {sub (/\*/,""); print $5} }'
432          ;;
433    *)    sfdisk -s $PART
434          ;;
435esac
[a5df9b9]436}
437
438
[b094c59]439#/**
[73c8417]440#         ogListPartitions int_ndisk
[a5df9b9]441#@brief   Lista las particiones definidas en un disco.
[42669ebf]442#@param   int_ndisk  nº de orden del disco
443#@return  str_parttype:int_partsize ...
[a5df9b9]444#@exception OG_ERR_FORMAT   formato incorrecto.
445#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
446#@note    Requisitos: \c parted \c awk
[73c8417]447#@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize
[59f9ad2]448#@attention Las tuplas de valores están separadas por espacios.
[a5df9b9]449#@version 0.9 - Primera versión para OpenGNSys
450#@author  Ramon Gomez, ETSII Universidad de Sevilla
451#@date    2009/07/24
[1e7eaab]452#*/ ##
[42669ebf]453function ogListPartitions ()
454{
[59f9ad2]455# Variables locales.
[55ad138c]456local DISK PART NPARTS TYPE SIZE
[aae34f6]457
[42669ebf]458# Si se solicita, mostrar ayuda.
[1a7130a]459if [ "$*" == "help" ]; then
[aae34f6]460    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \
[73c8417]461           "$FUNCNAME 1  =>  NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000"
[aae34f6]462    return
463fi
[42669ebf]464# Error si no se recibe 1 parámetro.
[5dbb046]465[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $?
[a5df9b9]466
[42669ebf]467# Procesar la salida de \c parted .
[b094c59]468DISK="$(ogDiskToDev $1)" || return $?
[3543b3e]469NPARTS=$(ogGetPartitionsNumber $1)
470for (( PART = 1; PART <= NPARTS; PART++ )); do
[55ad138c]471    TYPE=$(ogGetFsType $1 $PART 2>/dev/null)
[3543b3e]472    if [ $? -eq 0 ]; then
[55ad138c]473        SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null)
474        echo -n "$TYPE:$SIZE "
[3543b3e]475    else
476        echo -n "EMPTY:0 "
477    fi
[a5df9b9]478done
479echo
480}
481
[326cec3]482
483#/**
[55ad138c]484#         ogListPrimaryPartitions int_ndisk
485#@brief   Metafunción que lista las particiones primarias no vacías definidas en un disco.
[42669ebf]486#@param   int_ndisk  nº de orden del disco
[55ad138c]487#@see     ogListPartitions
[1e7eaab]488#*/ ##
[42669ebf]489function ogListPrimaryPartitions ()
490{
[55ad138c]491# Variables locales.
492local PARTS
493
494PARTS=$(ogListPartitions "$@") || return $?
495echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//'
496}
497
498
499#/**
500#         ogListLogicalPartitions int_ndisk
501#@brief   Metafunción que lista las particiones lógicas definidas en un disco.
[42669ebf]502#@param   int_ndisk  nº de orden del disco
[55ad138c]503#@see     ogListPartitions
[1e7eaab]504#*/ ##
[b061ad0]505function ogListLogicalPartitions ()
506{
[55ad138c]507# Variables locales.
508local PARTS
509
510PARTS=$(ogListPartitions "$@") || return $?
511echo $PARTS | cut -sf5- -d" "
512}
513
514
515#/**
[42669ebf]516#         ogSetPartitionActive int_ndisk int_npartition
[89403cd]517#@brief   Establece cual es la partición activa de un disco.
[42669ebf]518#@param   int_ndisk      nº de orden del disco
519#@param   int_npartition nº de orden de la partición
520#@return  (nada).
[326cec3]521#@exception OG_ERR_FORMAT   Formato incorrecto.
522#@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo.
523#@note    Requisitos: parted
[985bef0]524#@version 0.1 -  Integracion para Opengnsys  -  EAC: SetPartitionActive() en ATA.lib
525#@author  Antonio J. Doblas Viso, Universidad de Malaga
526#@date    2008/10/27
527#@version 0.9 - Primera version compatible con OpenGNSys.
[326cec3]528#@author  Ramon Gomez, ETSII Universidad de Sevilla
529#@date    2009/09/17
[1e7eaab]530#*/ ##
[42669ebf]531function ogSetPartitionActive ()
532{
[326cec3]533# Variables locales
534local DISK PART
535
[1e7eaab]536# Si se solicita, mostrar ayuda.
[326cec3]537if [ "$*" == "help" ]; then
538    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
539           "$FUNCNAME 1 1"
540    return
541fi
[1e7eaab]542# Error si no se reciben 2 parámetros.
[326cec3]543[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
544
[1e7eaab]545# Comprobar que el disco existe y activar la partición indicada.
[326cec3]546DISK="$(ogDiskToDev $1)" || return $?
547PART="$(ogDiskToDev $1 $2)" || return $?
548parted -s $DISK set $2 boot on 2>/dev/null
549}
550
551
[1553fc7]552#/**
[42669ebf]553#         ogSetPartitionSize int_ndisk int_npartition int_size
[2ecd096]554#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]555#@param  int_ndisk      nº de orden del disco
556#@param   int_npartition nº de orden de la partición
557#@param   int_size       tamaño de la partición (en KB)
[2ecd096]558#@return  (nada)
559#@exception OG_ERR_FORMAT   formato incorrecto.
560#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
561#@note    Requisitos: sfdisk, awk
562#@todo    Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición.
563#@version 0.9 - Primera versión para OpenGNSys
564#@author  Ramon Gomez, ETSII Universidad de Sevilla
565#@date    2009/07/24
[1e7eaab]566#*/ ##
[42669ebf]567function ogSetPartitionSize ()
568{
[2ecd096]569# Variables locales.
570local DISK PART SIZE
571
[1e7eaab]572# Si se solicita, mostrar ayuda.
[2ecd096]573if [ "$*" == "help" ]; then
[311532f]574    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition int_size" \
[2ecd096]575           "$FUNCNAME 1 1 10000000"
576    return
577fi
[1e7eaab]578# Error si no se reciben 3 parámetros.
[2ecd096]579[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
580
[1e7eaab]581# Obtener el tamaño de la partición.
[2ecd096]582DISK="$(ogDiskToDev $1)" || return $?
583PART="$(ogDiskToDev $1 $2)" || return $?
584# Convertir tamaño en KB a sectores de 512 B.
585SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $?
[1e7eaab]586# Usar \c sfdisk para redefinir el tamaño.
[1c04494]587sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[bf1840e9]588partprobe 2>/dev/null
[2ecd096]589}
590
591
592#/**
[6cdca0c]593#         ogUpdatePartitionTable
[1553fc7]594#@brief   Fuerza al kernel releer la tabla de particiones de los discos duros
[42669ebf]595#@param   no requiere
[1553fc7]596#@return  informacion propia de la herramienta
597#@note    Requisitos: \c partprobe
598#@warning pendiente estructurar la funcion a opengnsys
[985bef0]599#@version 0.1 -  Integracion para Opengnsys  -  EAC: UpdatePartitionTable() en ATA.lib
600#@author  Antonio J. Doblas Viso. Universidad de Malaga
601#@date    27/10/2008
[1553fc7]602#*/
603
[42669ebf]604function ogUpdatePartitionTable ()
605{
[1553fc7]606echo "Forzando al kernel la lectura de la tabla de particiones"
607list=`partprobe -s | cut -f1 -d: ` 2>/dev/null
608echo $list > /tmp/disk
609}
[1a7130a]610
611
612
[24f2399]613#/**  @function ogGetPartitionsNumber: @brief detecta el numero de particiones del disco duro indicado.
[42669ebf]614#@param   int_numdisk   (indentificado EAC del disco)
[1a7130a]615#@return  devuelve el numero paritiones del disco duro indicado
616#@warning  Salidas de errores no determinada
617#@attention Requisitos: parted
618#@note    Notas sin especificar
[985bef0]619#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DetectNumberPartition () en ATA.lib
620#@author  Antonio J. Doblas Viso. Universidad de Malaga
621#@date    Date: 27/10/2008
622#@version 1.0 - Uso de sfdisk Primera version para OpenGnSys
623#@author  Ramon Gomez, ETSII Universidad de Sevilla
624#@date    2009/07/24
[1a7130a]625#*/
[985bef0]626function ogGetPartitionsNumber () {
[55ad138c]627           #local disco totalpart
628           #disco=`ogDiskToDev $1`
629           #totalpart=`parted $disco print | egrep ^" [0123456789] " -c`
630           #echo $totalpart
631local DISK
632#/// Contar el nº de veces que aparece el disco en su lista de particiones.
633DISK=$(ogDiskToDev $1) 2>/dev/null
634sfdisk -l $DISK 2>/dev/null | grep -c "^$DISK"
[1a7130a]635}
[6cdca0c]636
637
638#/**  @function ogDiskToRelativeDev: @brief Traduce los ID de discos o particiones EAC a ID Linux relativos, es decir 1 1 => sda1
639#@param  Admite 1 parametro:   $1  int_numdisk
640#@param  Admite 2 parametro:   $1   int_numdisk                    $2  int_partition
641#@return  Para 1 parametros traduce Discos Duros: Devuelve la ruta relativa linux del disco duro indicado con nomenclatura EAC.........ejemplo: IdPartition 1 => sda
642#@return  Para 2 parametros traduce Particiones: Devuelve la ruta relativa linux de la particion indicado con nomenclatura EAC...........  ejemplo: IdPartition  2 1 => sdb1
643#@warning  No definidas
644#@attention
645#@note    Notas sin especificar
[985bef0]646#@version 0.1 -  Integracion para Opengnsys  -  EAC:  IdPartition en ATA.lib
647#@author  Antonio J. Doblas Viso. Universidad de Malaga
648#@date    27/10/2008
[6cdca0c]649#*/
[985bef0]650function ogDiskToRelativeDev () {
[6cdca0c]651if [ $# = 0 ]
652then
653        Msg "Info: Traduce el identificador del dispositivo EAC a dispositivo linux \n" info
654        Msg "Sintaxis1: IdPartition int_disk -----------------Ejemplo1: IdPartition 1 -> sda " example
655        Msg "Sintaxis2: IdPartition int_disk int_partition  --Ejemplo2: IdPartition 1 2 -> sda2 " example
656
657return
658fi
659#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.
660PART=$(ogDiskToDev|cut -f$1 -d' ')$2
661echo $PART | cut -f3 -d \/
662}
[26c729b]663
664#/**  @function ogDeletePartitionTable: @brief Borra la tabla de particiones del disco.
665#@param $1 opcion A (identificador LINUX)       str_ID_linux (/dev/sda)
666#@param $1 opcion B (Identifiador EAC)                  int_numdiskEAC(1)
667#@return   la informacion propia del fdisk
668#@warning    no definidos
669#@attention
670#@note
[985bef0]671#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DeletePartitionTable () en ATA.lib
672#@author  Antonio J. Doblas Viso. Universidad de Malaga
673#@date    27/10/2008
[26c729b]674#*/
[985bef0]675function ogDeletePartitionTable () {
[26c729b]676if [ $# = 0 ]
677then
678        Msg "sintaxis1: ogDeletePartitionTable int_disk" red
679        Msg "sintaxis2: ogDeletePartitionTable str_/dev/sdX" red
680        return
681fi
682if [ -n "${1%/dev/*}" ]
683        then
684        dev=`DiskToDev $1`
685        else
686        dev=$1
687fi
688echo -ne "o\nw" | fdisk $dev
689}
[0df4b9f7]690
691
692#/**  @function ogSetPartitionId: @brief Cambia el identificador de la particion, pero no su sistema de archivos.
693#@param  $1 int_numdiskEAC
694#@param  $2 int_numpartitionEAC
695#@param  $3 str_tipoPartition admite EXT2 EXT3 NTFS FAT32 SWAP CACHE
696#@return   la propia del fdisk
697#@warning    no controla los parametros, si se introducen mal o simplemente no se introducen no muestra mensaje
698#@warning    Identifica por nombre del sistema de archivos no por número
699#@attention Requisitos:  fdisk
700#@note
[985bef0]701#@version 0.1 -  Integracion para Opengnsys  - SetPartitionType() en ATA.lib
702#@author  Antonio J. Doblas Viso. Universidad de Malaga
703#@date    27/10/2008
[0df4b9f7]704#*/
[985bef0]705function ogSetPartitionId() {
[be81649]706# Variables locales
707local DISK PART ID
708
[42669ebf]709# Si se solicita, mostrar ayuda.
[be81649]710if [ "$*" == "help" ]; then
[311532f]711    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_type" \
712           "$FUNCNAME 1 1 NTFS"
[be81649]713    return
[0df4b9f7]714fi
[42669ebf]715# Error si no se reciben 3 parámetros.
[be81649]716[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
717
[42669ebf]718# Sustituye nº de disco por su dispositivo.
[be81649]719DISK=`ogDiskToDev $1` || return $?
720PART=`ogDiskToDev $1 $2` || return $?
721
[42669ebf]722# Elección del tipo de partición.
723ID=$(ogFsToId "$3")
724[ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $?
725
[be81649]726echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK 1>/dev/null 2>&1
[0df4b9f7]727}
[cc6ad14]728
[be81649]729
[97da528]730#/**  @function ogDeletePartitionsLabels: @brief Elimina la informacion que tiene el kernel del cliente og sobre los labels de los sistemas de archivos
[cc6ad14]731#@param  No requiere
732#@return   Nada
733#@warning
734#@attention Requisitos:  comando interno linux rm
735#@note
[985bef0]736#@version 0.1 -  Integracion para Opengnsys  -  EAC:   DeletePartitionTable()  en ATA.lib
737#@author  Antonio J. Doblas Viso. Universidad de Malaga
738#@date    27/10/2008
[cc6ad14]739#*/
[985bef0]740function ogDeletePartitionsLabels () {
[cc6ad14]741rm /dev/disk/by-label/*    # */ COMENTARIO OBLIGATORIO PARA DOXYGEN
742}
743
Note: See TracBrowser for help on using the repository browser.