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