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