source: client/engine/Disk.lib @ 53fba30

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 53fba30 was 7dada73, checked in by ramon <ramongomez@…>, 13 years ago

Versión 1.0.2: función ogGetPartitionId detecta corretamente partición vacía (modifica #397).

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

  • Property mode set to 100755
File size: 31.0 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 ;;
[5804229]291    LINUX|EXT[234]|REISERFS|REISER4|XFS|JFS)
[42669ebf]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
[cbbb046]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
[7dada73]393#@brief   Devuelve el mnemónico con el tipo de partición.
[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.
[7dada73]398#@exception OG_ERR_NOTFOUND Disco o partición no corresponde con un dispositivo.
[a5df9b9]399#@note    Requisitos: sfdisk
[7dada73]400#@version 0.9 - Primera versión compatible con OpenGnSys.
[9f57de01]401#@author  Ramon Gomez, ETSII Universidad de Sevilla
402#@date    25/03/2009
[7dada73]403#@version 1.0.2 - Detectar partición vacía.
404#@author  Ramon Gomez, ETSII Universidad de Sevilla
405#@date    23/12/2011
[1e7eaab]406#*/ ##
[42669ebf]407function ogGetPartitionId ()
408{
[59f9ad2]409# Variables locales.
[7dada73]410local DISK ID
[2e15649]411
[1e7eaab]412# Si se solicita, mostrar ayuda.
[aae34f6]413if [ "$*" == "help" ]; then
414    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
415           "$FUNCNAME 1 1  =>  7"
416    return
417fi
[1e7eaab]418# Error si no se reciben 2 parámetros.
[aae34f6]419[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[2e15649]420
[7dada73]421# Detectar id. de tipo de partición y codificar al mnemónico.
[2e15649]422DISK=$(ogDiskToDev $1) || return $?
[7dada73]423ID=$(sfdisk --id $DISK $2 2>/dev/null) || ogRaiseError $OG_ERR_NOTFOUND "$1,$2" || return $?
424echo $ID
[9f29ba6]425}
426
[a5df9b9]427
428#/**
[42669ebf]429#         ogGetPartitionSize int_ndisk int_npartition
[a5df9b9]430#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]431#@param   int_ndisk      nº de orden del disco
432#@param   int_npartition nº de orden de la partición
433#@return  int_partsize - Tamaño en KB de la partición.
[a5df9b9]434#@exception OG_ERR_FORMAT   formato incorrecto.
435#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
436#@note    Requisitos: sfdisk, awk
[985bef0]437#@version 0.1 -  Integracion para Opengnsys  -  EAC: SizePartition () en ATA.lib
438#@author  Antonio J. Doblas Viso, Universidad de Malaga
439#@date    2008/10/27
440#@version 0.9 - Primera version para OpenGNSys
[a5df9b9]441#@author  Ramon Gomez, ETSII Universidad de Sevilla
442#@date    2009/07/24
[1e7eaab]443#*/ ##
[42669ebf]444function ogGetPartitionSize ()
445{
[59f9ad2]446# Variables locales.
[739d358]447local DISK PART
[a5df9b9]448
[1e7eaab]449# Si se solicita, mostrar ayuda.
[aae34f6]450if [ "$*" == "help" ]; then
451    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
452           "$FUNCNAME 1 1  =>  10000000"
453    return
454fi
[1e7eaab]455# Error si no se reciben 2 parámetros.
[aae34f6]456[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[a5df9b9]457
[1e7eaab]458# Obtener el tamaño de la partición.
[3543b3e]459DISK="$(ogDiskToDev $1)" || return $?
[a5df9b9]460PART="$(ogDiskToDev $1 $2)" || return $?
[3543b3e]461case "$(ogGetPartitionId $1 $2)" in
462    5|f)  # Procesar detección de tamaño de partición Extendida.
[55ad138c]463          sfdisk -l $DISK 2>/dev/null | \
[c438bd3]464                    awk -v p=$PART '{if ($1==p) {sub (/[^0-9]+/,"",$5); print $5} }'
[3543b3e]465          ;;
466    *)    sfdisk -s $PART
467          ;;
468esac
[a5df9b9]469}
470
471
[b094c59]472#/**
[344d6e7]473#         ogGetPartitionType int_ndisk int_npartition
[5804229]474#@brief   Devuelve el mnemonico con el tipo de partición.
475#@param   int_ndisk      nº de orden del disco
476#@param   int_npartition nº de orden de la partición
477#@return  Mnemonico
[824b0dd]478#@note    Mnemonico: { EXT2, EXT3, EXT4, REISERFS, XFS, JFS, LINUX-SWAP, LINUX-LVM, LINUX-RAID, SOLARIS, FAT16, HFAT16, FAT32, HFAT32, NTFS, HNTFS, WIN-DYNAMIC, CACHE, EMPTY, EXTENDED, UNKNOWN }
[5804229]479#@exception OG_ERR_FORMAT   Formato incorrecto.
[824b0dd]480#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
481#@version 0.1 -  Integracion para Opengnsys  -  EAC:   TypeFS() en ATA.lib
[5804229]482#@author  Antonio J. Doblas Viso. Universidad de Malaga
483#@date    2008-10-27
484#@version 0.9 - Primera adaptacion para OpenGnSys.
485#@author  Ramon Gomez, ETSII Universidad de Sevilla
486#@date    2009-07-21
487#@version 1.0.3 - Código trasladado de antigua función ogGetFsType.
488#@author  Ramon Gomez, ETSII Universidad de Sevilla
489#@date    2011-12-01
[344d6e7]490#*/ ##
491function ogGetPartitionType ()
492{
[5804229]493# Variables locales.
494local ID TYPE
495
496# Si se solicita, mostrar ayuda.
497if [ "$*" == "help" ]; then
498    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
499           "$FUNCNAME 1 1  =>  NTFS"
500    return
501fi
502# Error si no se reciben 2 parámetros.
503[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
504
505# Detectar id. de tipo de partición y codificar al mnemonico.
506ID=$(ogGetPartitionId "$1" "$2") || return $?
507case "$ID" in
508     0)         TYPE="EMPTY" ;;
509     1)         TYPE="FAT12" ;;
510     5|f)       TYPE="EXTENDED" ;;
511     6|e)       TYPE="FAT16" ;;
512     7)         TYPE="NTFS" ;;
513     b|c)       TYPE="FAT32" ;;
514     11)        TYPE="HFAT12" ;;
515     12)        TYPE="COMPAQDIAG" ;;
516     16|1e)     TYPE="HFAT16" ;;
517     17)        TYPE="HNTFS" ;;
518     1b|1c)     TYPE="HFAT32" ;;
519     42)        TYPE="WIN-DYNAMIC" ;;
520     82)        TYPE="LINUX-SWAP" ;;
521     83)        TYPE="LINUX" ;;
522     8e)        TYPE="LINUX-LVM" ;;
523     a7)        TYPE="CACHE" ;;         # (compatibilidad con Brutalix)
524     af)        TYPE="HFS" ;;
525     bf)        TYPE="SOLARIS" ;;
526     ca)        TYPE="CACHE" ;;
527     fd)        TYPE="LINUX-RAID" ;;
528     *)         TYPE="UNKNOWN" ;;
529esac
530echo "$TYPE"
[344d6e7]531}
532
533
534#/**
[b09d0fa]535#         ogHidePartition int_ndisk int_npartition
536#@brief   Oculta un apartición visible.
537#@param   int_ndisk      nº de orden del disco
538#@param   int_npartition nº de orden de la partición
539#@return  (nada)
540#@exception OG_ERR_FORMAT    formato incorrecto.
[053993f]541#@exception OG_ERR_NOTFOUND  disco o particion no detectado (no es un dispositivo).
[b09d0fa]542#@exception OG_ERR_PARTITION tipo de partición no reconocido.
543#@version 1.0 - Versión en pruebas.
544#@author  Ramon Gomez, ETSII Universidad de Sevilla
545#@date    2010/01/12
546#*/ ##
547function ogHidePartition ()
548{
549# Variables locales.
550local PART TYPE NEWTYPE
551# Si se solicita, mostrar ayuda.
552if [ "$*" == "help" ]; then
553    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
554           "$FUNCNAME 1 1"
555    return
556fi
557# Error si no se reciben 2 parámetros.
558[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
559PART=$(ogDiskToDev "$1" "$2") || return $?
560
561# Obtener tipo de partición.
562TYPE=$(ogGetPartitionType "$1" "$2")
563case "$TYPE" in
564    NTFS)   NEWTYPE="HNTFS"  ;;
565    FAT32)  NEWTYPE="HFAT32" ;;
566    FAT16)  NEWTYPE="HFAT16" ;;
567    FAT12)  NEWTYPE="HFAT12" ;;
568    *)      ogRaiseError $OG_ERR_PARTITION "$TYPE"
569            return $? ;;
570esac
571# Cambiar tipo de partición.
572ogSetPartitionId $1 $2 $NEWTYPE
573}
574
575
576#/**
[73c8417]577#         ogListPartitions int_ndisk
[a5df9b9]578#@brief   Lista las particiones definidas en un disco.
[42669ebf]579#@param   int_ndisk  nº de orden del disco
580#@return  str_parttype:int_partsize ...
[a5df9b9]581#@exception OG_ERR_FORMAT   formato incorrecto.
582#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
583#@note    Requisitos: \c parted \c awk
[73c8417]584#@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize
[59f9ad2]585#@attention Las tuplas de valores están separadas por espacios.
[a5df9b9]586#@version 0.9 - Primera versión para OpenGNSys
587#@author  Ramon Gomez, ETSII Universidad de Sevilla
588#@date    2009/07/24
[1e7eaab]589#*/ ##
[42669ebf]590function ogListPartitions ()
591{
[59f9ad2]592# Variables locales.
[55ad138c]593local DISK PART NPARTS TYPE SIZE
[aae34f6]594
[42669ebf]595# Si se solicita, mostrar ayuda.
[1a7130a]596if [ "$*" == "help" ]; then
[aae34f6]597    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \
[73c8417]598           "$FUNCNAME 1  =>  NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000"
[aae34f6]599    return
600fi
[42669ebf]601# Error si no se recibe 1 parámetro.
[5dbb046]602[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $?
[a5df9b9]603
[42669ebf]604# Procesar la salida de \c parted .
[b094c59]605DISK="$(ogDiskToDev $1)" || return $?
[3543b3e]606NPARTS=$(ogGetPartitionsNumber $1)
607for (( PART = 1; PART <= NPARTS; PART++ )); do
[5804229]608    TYPE=$(ogGetPartitionType $1 $PART 2>/dev/null)
[3543b3e]609    if [ $? -eq 0 ]; then
[55ad138c]610        SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null)
611        echo -n "$TYPE:$SIZE "
[3543b3e]612    else
613        echo -n "EMPTY:0 "
614    fi
[a5df9b9]615done
616echo
617}
618
[326cec3]619
620#/**
[55ad138c]621#         ogListPrimaryPartitions int_ndisk
622#@brief   Metafunción que lista las particiones primarias no vacías definidas en un disco.
[42669ebf]623#@param   int_ndisk  nº de orden del disco
[55ad138c]624#@see     ogListPartitions
[1e7eaab]625#*/ ##
[42669ebf]626function ogListPrimaryPartitions ()
627{
[55ad138c]628# Variables locales.
629local PARTS
630
631PARTS=$(ogListPartitions "$@") || return $?
632echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//'
633}
634
635
636#/**
637#         ogListLogicalPartitions int_ndisk
638#@brief   Metafunción que lista las particiones lógicas definidas en un disco.
[42669ebf]639#@param   int_ndisk  nº de orden del disco
[55ad138c]640#@see     ogListPartitions
[1e7eaab]641#*/ ##
[b061ad0]642function ogListLogicalPartitions ()
643{
[55ad138c]644# Variables locales.
645local PARTS
646
647PARTS=$(ogListPartitions "$@") || return $?
648echo $PARTS | cut -sf5- -d" "
649}
650
651
652#/**
[42669ebf]653#         ogSetPartitionActive int_ndisk int_npartition
[89403cd]654#@brief   Establece cual es la partición activa de un disco.
[42669ebf]655#@param   int_ndisk      nº de orden del disco
656#@param   int_npartition nº de orden de la partición
657#@return  (nada).
[326cec3]658#@exception OG_ERR_FORMAT   Formato incorrecto.
659#@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo.
660#@note    Requisitos: parted
[985bef0]661#@version 0.1 -  Integracion para Opengnsys  -  EAC: SetPartitionActive() en ATA.lib
662#@author  Antonio J. Doblas Viso, Universidad de Malaga
663#@date    2008/10/27
664#@version 0.9 - Primera version compatible con OpenGNSys.
[326cec3]665#@author  Ramon Gomez, ETSII Universidad de Sevilla
666#@date    2009/09/17
[1e7eaab]667#*/ ##
[42669ebf]668function ogSetPartitionActive ()
669{
[326cec3]670# Variables locales
671local DISK PART
672
[1e7eaab]673# Si se solicita, mostrar ayuda.
[326cec3]674if [ "$*" == "help" ]; then
675    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
676           "$FUNCNAME 1 1"
677    return
678fi
[1e7eaab]679# Error si no se reciben 2 parámetros.
[326cec3]680[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
681
[1e7eaab]682# Comprobar que el disco existe y activar la partición indicada.
[326cec3]683DISK="$(ogDiskToDev $1)" || return $?
684PART="$(ogDiskToDev $1 $2)" || return $?
685parted -s $DISK set $2 boot on 2>/dev/null
686}
687
688
[1553fc7]689#/**
[42669ebf]690#         ogSetPartitionSize int_ndisk int_npartition int_size
[2ecd096]691#@brief   Muestra el tamano en KB de una particion determinada.
[42669ebf]692#@param  int_ndisk      nº de orden del disco
693#@param   int_npartition nº de orden de la partición
694#@param   int_size       tamaño de la partición (en KB)
[2ecd096]695#@return  (nada)
696#@exception OG_ERR_FORMAT   formato incorrecto.
697#@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo).
698#@note    Requisitos: sfdisk, awk
699#@todo    Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición.
700#@version 0.9 - Primera versión para OpenGNSys
701#@author  Ramon Gomez, ETSII Universidad de Sevilla
702#@date    2009/07/24
[1e7eaab]703#*/ ##
[42669ebf]704function ogSetPartitionSize ()
705{
[2ecd096]706# Variables locales.
707local DISK PART SIZE
708
[1e7eaab]709# Si se solicita, mostrar ayuda.
[2ecd096]710if [ "$*" == "help" ]; then
[311532f]711    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition int_size" \
[2ecd096]712           "$FUNCNAME 1 1 10000000"
713    return
714fi
[1e7eaab]715# Error si no se reciben 3 parámetros.
[2ecd096]716[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
717
[1e7eaab]718# Obtener el tamaño de la partición.
[2ecd096]719DISK="$(ogDiskToDev $1)" || return $?
720PART="$(ogDiskToDev $1 $2)" || return $?
721# Convertir tamaño en KB a sectores de 512 B.
722SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $?
[3915005]723# Redefinir el tamaño de la partición.
[1c04494]724sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[bf1840e9]725partprobe 2>/dev/null
[2ecd096]726}
727
[b09d0fa]728#/**
729#         ogUnhidePartition int_ndisk int_npartition
730#@brief   Hace visible una partición oculta.
731#@param   int_ndisk      nº de orden del disco
732#@param   int_npartition nº de orden de la partición
733#@return  (nada)
734#@exception OG_ERR_FORMAT    formato incorrecto.
[053993f]735#@exception OG_ERR_NOTFOUND  disco o particion no detectado (no es un dispositivo).
[b09d0fa]736#@exception OG_ERR_PARTITION tipo de partición no reconocido.
737#@version 1.0 - Versión en pruebas.
738#@author  Ramon Gomez, ETSII Universidad de Sevilla
739#@date    2010/01/12
740#*/ ##
741function ogUnhidePartition ()
742{
743# Variables locales.
744local PART TYPE NEWTYPE
745# Si se solicita, mostrar ayuda.
746if [ "$*" == "help" ]; then
747    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
748           "$FUNCNAME 1 1"
749    return
750fi
751# Error si no se reciben 2 parámetros.
752[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
753PART=$(ogDiskToDev "$1" "$2") || return $?
754
755# Obtener tipo de partición.
756TYPE=$(ogGetPartitionType "$1" "$2")
757case "$TYPE" in
758    HNTFS)   NEWTYPE="NTFS"  ;;
759    HFAT32)  NEWTYPE="FAT32" ;;
760    HFAT16)  NEWTYPE="FAT16" ;;
761    HFAT12)  NEWTYPE="FAT12" ;;
762    *)      ogRaiseError $OG_ERR_PARTITION "$TYPE"
763            return $? ;;
764esac
765# Cambiar tipo de partición.
766ogSetPartitionId $1 $2 $NEWTYPE
767}
768
[2ecd096]769
770#/**
[6cdca0c]771#         ogUpdatePartitionTable
[1553fc7]772#@brief   Fuerza al kernel releer la tabla de particiones de los discos duros
[42669ebf]773#@param   no requiere
[1553fc7]774#@return  informacion propia de la herramienta
775#@note    Requisitos: \c partprobe
776#@warning pendiente estructurar la funcion a opengnsys
[985bef0]777#@version 0.1 -  Integracion para Opengnsys  -  EAC: UpdatePartitionTable() en ATA.lib
778#@author  Antonio J. Doblas Viso. Universidad de Malaga
779#@date    27/10/2008
[3915005]780#*/ ##
[42669ebf]781function ogUpdatePartitionTable ()
782{
[3915005]783local i
[c6087b9]784for i in `ogDiskToDev`
785do
786        partprobe $i
787done
[1553fc7]788}
[1a7130a]789
790
791
[3915005]792#/**
793#         ogGetPartitionsNumber int_ndisk
794#@brief   Detecta el numero de particiones del disco duro indicado.
795#@param   int_ndisk      nº de orden del disco
796#@return  Devuelve el numero paritiones del disco duro indicado
797#@warning Salidas de errores no determinada
[1a7130a]798#@attention Requisitos: parted
799#@note    Notas sin especificar
[985bef0]800#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DetectNumberPartition () en ATA.lib
801#@author  Antonio J. Doblas Viso. Universidad de Malaga
802#@date    Date: 27/10/2008
803#@version 1.0 - Uso de sfdisk Primera version para OpenGnSys
804#@author  Ramon Gomez, ETSII Universidad de Sevilla
805#@date    2009/07/24
[3915005]806#*/ ##
807function ogGetPartitionsNumber ()
808{
[55ad138c]809           #local disco totalpart
810           #disco=`ogDiskToDev $1`
811           #totalpart=`parted $disco print | egrep ^" [0123456789] " -c`
812           #echo $totalpart
813local DISK
[3915005]814# Contar el nº de veces que aparece el disco en su lista de particiones.
[55ad138c]815DISK=$(ogDiskToDev $1) 2>/dev/null
816sfdisk -l $DISK 2>/dev/null | grep -c "^$DISK"
[1a7130a]817}
[6cdca0c]818
819
820#/**  @function ogDiskToRelativeDev: @brief Traduce los ID de discos o particiones EAC a ID Linux relativos, es decir 1 1 => sda1
821#@param  Admite 1 parametro:   $1  int_numdisk
822#@param  Admite 2 parametro:   $1   int_numdisk                    $2  int_partition
823#@return  Para 1 parametros traduce Discos Duros: Devuelve la ruta relativa linux del disco duro indicado con nomenclatura EAC.........ejemplo: IdPartition 1 => sda
824#@return  Para 2 parametros traduce Particiones: Devuelve la ruta relativa linux de la particion indicado con nomenclatura EAC...........  ejemplo: IdPartition  2 1 => sdb1
825#@warning  No definidas
826#@attention
827#@note    Notas sin especificar
[985bef0]828#@version 0.1 -  Integracion para Opengnsys  -  EAC:  IdPartition en ATA.lib
829#@author  Antonio J. Doblas Viso. Universidad de Malaga
830#@date    27/10/2008
[6cdca0c]831#*/
[985bef0]832function ogDiskToRelativeDev () {
[6cdca0c]833if [ $# = 0 ]
834then
835        Msg "Info: Traduce el identificador del dispositivo EAC a dispositivo linux \n" info
836        Msg "Sintaxis1: IdPartition int_disk -----------------Ejemplo1: IdPartition 1 -> sda " example
837        Msg "Sintaxis2: IdPartition int_disk int_partition  --Ejemplo2: IdPartition 1 2 -> sda2 " example
838
839return
840fi
841#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.
842PART=$(ogDiskToDev|cut -f$1 -d' ')$2
843echo $PART | cut -f3 -d \/
844}
[26c729b]845
846#/**  @function ogDeletePartitionTable: @brief Borra la tabla de particiones del disco.
847#@param $1 opcion A (identificador LINUX)       str_ID_linux (/dev/sda)
848#@param $1 opcion B (Identifiador EAC)                  int_numdiskEAC(1)
849#@return   la informacion propia del fdisk
850#@warning    no definidos
851#@attention
852#@note
[985bef0]853#@version 0.1 -  Integracion para Opengnsys  -  EAC:  DeletePartitionTable () en ATA.lib
854#@author  Antonio J. Doblas Viso. Universidad de Malaga
855#@date    27/10/2008
[26c729b]856#*/
[985bef0]857function ogDeletePartitionTable () {
[26c729b]858if [ $# = 0 ]
859then
860        Msg "sintaxis1: ogDeletePartitionTable int_disk" red
861        Msg "sintaxis2: ogDeletePartitionTable str_/dev/sdX" red
862        return
863fi
864if [ -n "${1%/dev/*}" ]
865        then
866        dev=`DiskToDev $1`
867        else
868        dev=$1
869fi
870echo -ne "o\nw" | fdisk $dev
871}
[0df4b9f7]872
873
874#/**  @function ogSetPartitionId: @brief Cambia el identificador de la particion, pero no su sistema de archivos.
875#@param  $1 int_numdiskEAC
876#@param  $2 int_numpartitionEAC
877#@param  $3 str_tipoPartition admite EXT2 EXT3 NTFS FAT32 SWAP CACHE
878#@return   la propia del fdisk
879#@warning    no controla los parametros, si se introducen mal o simplemente no se introducen no muestra mensaje
880#@warning    Identifica por nombre del sistema de archivos no por número
881#@attention Requisitos:  fdisk
882#@note
[985bef0]883#@version 0.1 -  Integracion para Opengnsys  - SetPartitionType() en ATA.lib
884#@author  Antonio J. Doblas Viso. Universidad de Malaga
885#@date    27/10/2008
[0df4b9f7]886#*/
[985bef0]887function ogSetPartitionId() {
[be81649]888# Variables locales
889local DISK PART ID
890
[42669ebf]891# Si se solicita, mostrar ayuda.
[be81649]892if [ "$*" == "help" ]; then
[311532f]893    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_type" \
894           "$FUNCNAME 1 1 NTFS"
[be81649]895    return
[0df4b9f7]896fi
[42669ebf]897# Error si no se reciben 3 parámetros.
[be81649]898[ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
899
[42669ebf]900# Sustituye nº de disco por su dispositivo.
[be81649]901DISK=`ogDiskToDev $1` || return $?
902PART=`ogDiskToDev $1 $2` || return $?
903
[42669ebf]904# Elección del tipo de partición.
905ID=$(ogFsToId "$3")
[8971dc86]906[ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$3" || return $?
[42669ebf]907
[be81649]908echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK 1>/dev/null 2>&1
[0df4b9f7]909}
[cc6ad14]910
[be81649]911
[97da528]912#/**  @function ogDeletePartitionsLabels: @brief Elimina la informacion que tiene el kernel del cliente og sobre los labels de los sistemas de archivos
[cc6ad14]913#@param  No requiere
914#@return   Nada
915#@warning
916#@attention Requisitos:  comando interno linux rm
917#@note
[985bef0]918#@version 0.1 -  Integracion para Opengnsys  -  EAC:   DeletePartitionTable()  en ATA.lib
919#@author  Antonio J. Doblas Viso. Universidad de Malaga
920#@date    27/10/2008
[cc6ad14]921#*/
[985bef0]922function ogDeletePartitionsLabels () {
[cc6ad14]923rm /dev/disk/by-label/*    # */ COMENTARIO OBLIGATORIO PARA DOXYGEN
924}
925
Note: See TracBrowser for help on using the repository browser.