source: client/engine/Disk.lib @ 7caf5a7c

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 7caf5a7c was b994bc73, checked in by ramon <ramongomez@…>, 14 years ago

Versión 1.0.1: importar función ogGetDiskType de la rama engine-1.0

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

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