source: client/engine/Disk.lib @ fb8c54b

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

Corregir errata en ogCreatePartitions solucioando pérdida del parámetro disco (Close #257).
Modificar script createImage para chequear el sistema de archivos.

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

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