[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 |
---|
[1e7eaab] | 28 | #*/ ## |
---|
[73c8417] | 29 | function ogCreatePartitions () { |
---|
| 30 | |
---|
| 31 | # Variables locales. |
---|
| 32 | local DISK PART SECTORS START SIZE TYPE EXTSTART EXTSIZE tmpsfdisk |
---|
[1e7eaab] | 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 |
---|
[1e7eaab] | 39 | # Error si no se reciben menos de 2 parámetros. |
---|
[55ad138c] | 40 | [ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[73c8417] | 41 | |
---|
[1e7eaab] | 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 ;; |
---|
[1e7eaab] | 72 | EXTENDED) ID=5 |
---|
[be81649] | 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" |
---|
[1e7eaab] | 93 | return $? ;; |
---|
[73c8417] | 94 | esac |
---|
[1e7eaab] | 95 | # Incluir particiones lógicas dentro de la partición extendida. |
---|
[73c8417] | 96 | if [ $PART = 5 ]; then |
---|
| 97 | [ -z "$EXTSTART" ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
| 98 | START=$EXTSTART |
---|
| 99 | SECTORS=$[EXTSTART+EXTSIZE] |
---|
| 100 | fi |
---|
[1e7eaab] | 101 | # Generar datos para la partición. |
---|
[73c8417] | 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" |
---|
[1e7eaab] | 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 |
---|
[1e7eaab] | 139 | #*/ ## |
---|
[5dbb046] | 140 | function ogDevToDisk () { |
---|
| 141 | |
---|
[73c8417] | 142 | # Variables locales. |
---|
[5dbb046] | 143 | local d n |
---|
[1e7eaab] | 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 | |
---|
[1e7eaab] | 151 | # Error si no se recibe 1 parámetro. |
---|
[5dbb046] | 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 | |
---|
[1e7eaab] | 156 | # Procesa todos los discos para devolver su nº de orden y de partición. |
---|
[5dbb046] | 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 |
---|
[1e7eaab] | 181 | #*/ ## |
---|
[9f57de01] | 182 | function ogDiskToDev () { |
---|
[9f29ba6] | 183 | |
---|
[59f9ad2] | 184 | # Variables locales |
---|
[2717297] | 185 | local ALLDISKS VOLGROUPS DISK PART |
---|
[9f29ba6] | 186 | |
---|
[1e7eaab] | 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 | |
---|
[1e7eaab] | 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 | |
---|
[1e7eaab] | 201 | # Mostrar salidas segun el número de parametros. |
---|
[9f29ba6] | 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" |
---|
[1e7eaab] | 218 | # Comprobar si es partición. |
---|
[2717297] | 219 | if [ -b "$PART" ]; then |
---|
| 220 | echo "$PART" |
---|
| 221 | elif [ -n "$VOLGROUPS" ]; then |
---|
[1e7eaab] | 222 | # Comprobar si volumen lógico. |
---|
[2717297] | 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 |
---|
[1e7eaab] | 250 | #*/ ## |
---|
[a5df9b9] | 251 | function ogGetPartitionActive () { |
---|
| 252 | |
---|
[59f9ad2] | 253 | # Variables locales |
---|
[a5df9b9] | 254 | local DISK |
---|
| 255 | |
---|
[1e7eaab] | 256 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 257 | if [ "$*" == "help" ]; then |
---|
| 258 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1 => 1" |
---|
| 259 | return |
---|
| 260 | fi |
---|
[1e7eaab] | 261 | # Error si no se recibe 1 parámetro. |
---|
[aae34f6] | 262 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[a5df9b9] | 263 | |
---|
[1e7eaab] | 264 | # Comprobar que el disco existe y listar su partición activa. |
---|
[a5df9b9] | 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 |
---|
[1e7eaab] | 282 | #*/ ## |
---|
[9f57de01] | 283 | function ogGetPartitionId () { |
---|
[2e15649] | 284 | |
---|
[59f9ad2] | 285 | # Variables locales. |
---|
[a5df9b9] | 286 | local DISK PART |
---|
[2e15649] | 287 | |
---|
[1e7eaab] | 288 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 289 | if [ "$*" == "help" ]; then |
---|
| 290 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 291 | "$FUNCNAME 1 1 => 7" |
---|
| 292 | return |
---|
| 293 | fi |
---|
[1e7eaab] | 294 | # Error si no se reciben 2 parámetros. |
---|
[aae34f6] | 295 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[2e15649] | 296 | |
---|
[1e7eaab] | 297 | # Detectar id. de tipo de particion y codificar al mnemonico. |
---|
[2e15649] | 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 |
---|
[1e7eaab] | 316 | #*/ ## |
---|
[a5df9b9] | 317 | function ogGetPartitionSize () { |
---|
| 318 | |
---|
[59f9ad2] | 319 | # Variables locales. |
---|
| 320 | local PART |
---|
[a5df9b9] | 321 | |
---|
[1e7eaab] | 322 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 323 | if [ "$*" == "help" ]; then |
---|
| 324 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 325 | "$FUNCNAME 1 1 => 10000000" |
---|
| 326 | return |
---|
| 327 | fi |
---|
[1e7eaab] | 328 | # Error si no se reciben 2 parámetros. |
---|
[aae34f6] | 329 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[a5df9b9] | 330 | |
---|
[1e7eaab] | 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. |
---|
[1e7eaab] | 336 | # /* (comentario Doxygen) |
---|
[55ad138c] | 337 | sfdisk -l $DISK 2>/dev/null | \ |
---|
[3543b3e] | 338 | awk -v p=$PART '{if ($1==p) {sub (/\*/,""); print $5} }' |
---|
[1e7eaab] | 339 | # (comentario Doxygen) */ |
---|
[3543b3e] | 340 | ;; |
---|
| 341 | *) sfdisk -s $PART |
---|
| 342 | ;; |
---|
| 343 | esac |
---|
[a5df9b9] | 344 | } |
---|
| 345 | |
---|
| 346 | |
---|
[b094c59] | 347 | #/** |
---|
[73c8417] | 348 | # ogListPartitions int_ndisk |
---|
[a5df9b9] | 349 | #@brief Lista las particiones definidas en un disco. |
---|
[73c8417] | 350 | #@arg \c int_ndisk nº de orden del disco |
---|
| 351 | #@return tipo:tamanoKB ... |
---|
[a5df9b9] | 352 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 353 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
| 354 | #@note Requisitos: \c parted \c awk |
---|
[73c8417] | 355 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
[59f9ad2] | 356 | #@attention Las tuplas de valores están separadas por espacios. |
---|
[a5df9b9] | 357 | #@version 0.9 - Primera versión para OpenGNSys |
---|
| 358 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 359 | #@date 2009/07/24 |
---|
[1e7eaab] | 360 | #*/ ## |
---|
[a5df9b9] | 361 | function ogListPartitions () { |
---|
| 362 | |
---|
[59f9ad2] | 363 | # Variables locales. |
---|
[55ad138c] | 364 | local DISK PART NPARTS TYPE SIZE |
---|
[aae34f6] | 365 | |
---|
| 366 | #/// Si se solicita, mostrar ayuda. |
---|
[1a7130a] | 367 | if [ "$*" == "help" ]; then |
---|
[aae34f6] | 368 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
[73c8417] | 369 | "$FUNCNAME 1 => NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
[aae34f6] | 370 | return |
---|
| 371 | fi |
---|
[a5df9b9] | 372 | #/// Error si no se recibe 1 parámetro. |
---|
[5dbb046] | 373 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $? |
---|
[a5df9b9] | 374 | |
---|
| 375 | #/// Procesar la salida de \c parted . |
---|
[b094c59] | 376 | DISK="$(ogDiskToDev $1)" || return $? |
---|
[3543b3e] | 377 | NPARTS=$(ogGetPartitionsNumber $1) |
---|
| 378 | for (( PART = 1; PART <= NPARTS; PART++ )); do |
---|
[55ad138c] | 379 | TYPE=$(ogGetFsType $1 $PART 2>/dev/null) |
---|
[3543b3e] | 380 | if [ $? -eq 0 ]; then |
---|
[55ad138c] | 381 | SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null) |
---|
| 382 | echo -n "$TYPE:$SIZE " |
---|
[3543b3e] | 383 | else |
---|
| 384 | echo -n "EMPTY:0 " |
---|
| 385 | fi |
---|
[a5df9b9] | 386 | done |
---|
| 387 | echo |
---|
| 388 | } |
---|
| 389 | |
---|
[326cec3] | 390 | |
---|
| 391 | #/** |
---|
[55ad138c] | 392 | # ogListPrimaryPartitions int_ndisk |
---|
| 393 | #@brief Metafunción que lista las particiones primarias no vacías definidas en un disco. |
---|
| 394 | #@arg \c int_ndisk nº de orden del disco |
---|
| 395 | #@see ogListPartitions |
---|
[1e7eaab] | 396 | #*/ ## |
---|
[55ad138c] | 397 | function ogListPrimaryPartitions () { |
---|
| 398 | |
---|
| 399 | # Variables locales. |
---|
| 400 | local PARTS |
---|
| 401 | |
---|
| 402 | PARTS=$(ogListPartitions "$@") || return $? |
---|
| 403 | echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//' |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | |
---|
| 407 | #/** |
---|
| 408 | # ogListLogicalPartitions int_ndisk |
---|
| 409 | #@brief Metafunción que lista las particiones lógicas definidas en un disco. |
---|
| 410 | #@arg \c int_ndisk nº de orden del disco |
---|
| 411 | #@see ogListPartitions |
---|
[1e7eaab] | 412 | #*/ ## |
---|
[55ad138c] | 413 | function ogListLogicalPartitions () { |
---|
| 414 | |
---|
| 415 | # Variables locales. |
---|
| 416 | local PARTS |
---|
| 417 | |
---|
| 418 | PARTS=$(ogListPartitions "$@") || return $? |
---|
| 419 | echo $PARTS | cut -sf5- -d" " |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | |
---|
| 423 | #/** |
---|
[326cec3] | 424 | # ogSetPartitionActive ndisk npartition |
---|
[89403cd] | 425 | #@brief Establece cual es la partición activa de un disco. |
---|
[326cec3] | 426 | #@arg \c int_ndisk nº de orden del disco |
---|
| 427 | #@arg \c int_npartition nº de orden de la partición |
---|
| 428 | #@return Nada. |
---|
| 429 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 430 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
| 431 | #@note Requisitos: parted |
---|
| 432 | #@version 0.9 - Primera versión compatible con OpenGNSys. |
---|
| 433 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 434 | #@date 2009/09/17 |
---|
[1e7eaab] | 435 | #*/ ## |
---|
[326cec3] | 436 | function ogSetPartitionActive () { |
---|
| 437 | |
---|
| 438 | # Variables locales |
---|
| 439 | local DISK PART |
---|
| 440 | |
---|
[1e7eaab] | 441 | # Si se solicita, mostrar ayuda. |
---|
[326cec3] | 442 | if [ "$*" == "help" ]; then |
---|
| 443 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 444 | "$FUNCNAME 1 1" |
---|
| 445 | return |
---|
| 446 | fi |
---|
[1e7eaab] | 447 | # Error si no se reciben 2 parámetros. |
---|
[326cec3] | 448 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 449 | |
---|
[1e7eaab] | 450 | # Comprobar que el disco existe y activar la partición indicada. |
---|
[326cec3] | 451 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 452 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
| 453 | parted -s $DISK set $2 boot on 2>/dev/null |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | |
---|
[1553fc7] | 457 | #/** |
---|
[2ecd096] | 458 | # ogSetPartitionSize ndisk npartition size |
---|
| 459 | #@brief Muestra el tamano en KB de una particion determinada. |
---|
| 460 | #@arg \c int_ndisk nº de orden del disco |
---|
| 461 | #@arg \c int_npartition nº de orden de la partición |
---|
| 462 | #@arg \c int_size tamaño de la partición (en KB) |
---|
| 463 | #@return (nada) |
---|
| 464 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 465 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
| 466 | #@note Requisitos: sfdisk, awk |
---|
| 467 | #@todo Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición. |
---|
| 468 | #@version 0.9 - Primera versión para OpenGNSys |
---|
| 469 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 470 | #@date 2009/07/24 |
---|
[1e7eaab] | 471 | #*/ ## |
---|
[2ecd096] | 472 | function ogSetPartitionSize () { |
---|
| 473 | |
---|
| 474 | # Variables locales. |
---|
| 475 | local DISK PART SIZE |
---|
| 476 | |
---|
[1e7eaab] | 477 | # Si se solicita, mostrar ayuda. |
---|
[2ecd096] | 478 | if [ "$*" == "help" ]; then |
---|
| 479 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 480 | "$FUNCNAME 1 1 10000000" |
---|
| 481 | return |
---|
| 482 | fi |
---|
[1e7eaab] | 483 | # Error si no se reciben 3 parámetros. |
---|
[2ecd096] | 484 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 485 | |
---|
[1e7eaab] | 486 | # Obtener el tamaño de la partición. |
---|
[2ecd096] | 487 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 488 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
| 489 | # Convertir tamaño en KB a sectores de 512 B. |
---|
| 490 | SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[1e7eaab] | 491 | # Usar \c sfdisk para redefinir el tamaño. |
---|
[1c04494] | 492 | sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
[2ecd096] | 493 | } |
---|
| 494 | |
---|
| 495 | |
---|
| 496 | #/** |
---|
[6cdca0c] | 497 | # ogUpdatePartitionTable |
---|
[1553fc7] | 498 | #@brief Fuerza al kernel releer la tabla de particiones de los discos duros |
---|
[73c8417] | 499 | #@arg \c no requiere |
---|
[1553fc7] | 500 | #@return informacion propia de la herramienta |
---|
| 501 | #@note Requisitos: \c partprobe |
---|
| 502 | #@warning pendiente estructurar la funcion a opengnsys |
---|
| 503 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 504 | #@note funcion importada de EAC |
---|
| 505 | #*/ |
---|
| 506 | |
---|
[6cdca0c] | 507 | function ogUpdatePartitionTable () { |
---|
[1553fc7] | 508 | echo "Forzando al kernel la lectura de la tabla de particiones" |
---|
| 509 | list=`partprobe -s | cut -f1 -d: ` 2>/dev/null |
---|
| 510 | echo $list > /tmp/disk |
---|
| 511 | } |
---|
[1a7130a] | 512 | |
---|
| 513 | |
---|
| 514 | |
---|
| 515 | |
---|
| 516 | function ogGetPartitionsNumber () { |
---|
[24f2399] | 517 | #/** @function ogGetPartitionsNumber: @brief detecta el numero de particiones del disco duro indicado. |
---|
[1a7130a] | 518 | #@param int_numdisk (indentificado EAC del disco) |
---|
| 519 | #@return devuelve el numero paritiones del disco duro indicado |
---|
| 520 | #@warning Salidas de errores no determinada |
---|
| 521 | #@attention Requisitos: parted |
---|
| 522 | #@note Notas sin especificar |
---|
| 523 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 524 | #*/ |
---|
[55ad138c] | 525 | #local disco totalpart |
---|
| 526 | #disco=`ogDiskToDev $1` |
---|
| 527 | #totalpart=`parted $disco print | egrep ^" [0123456789] " -c` |
---|
| 528 | #echo $totalpart |
---|
| 529 | local DISK |
---|
| 530 | #/// Contar el nº de veces que aparece el disco en su lista de particiones. |
---|
| 531 | DISK=$(ogDiskToDev $1) 2>/dev/null |
---|
| 532 | sfdisk -l $DISK 2>/dev/null | grep -c "^$DISK" |
---|
[1a7130a] | 533 | } |
---|
[6cdca0c] | 534 | |
---|
| 535 | |
---|
| 536 | function ogDiskToRelativeDev () { |
---|
| 537 | #/** @function ogDiskToRelativeDev: @brief Traduce los ID de discos o particiones EAC a ID Linux relativos, es decir 1 1 => sda1 |
---|
| 538 | #@param Admite 1 parametro: $1 int_numdisk |
---|
| 539 | #@param Admite 2 parametro: $1 int_numdisk $2 int_partition |
---|
| 540 | #@return Para 1 parametros traduce Discos Duros: Devuelve la ruta relativa linux del disco duro indicado con nomenclatura EAC.........ejemplo: IdPartition 1 => sda |
---|
| 541 | #@return Para 2 parametros traduce Particiones: Devuelve la ruta relativa linux de la particion indicado con nomenclatura EAC........... ejemplo: IdPartition 2 1 => sdb1 |
---|
| 542 | #@warning No definidas |
---|
| 543 | #@attention |
---|
| 544 | #@note Notas sin especificar |
---|
| 545 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 546 | #*/ |
---|
| 547 | |
---|
| 548 | if [ $# = 0 ] |
---|
| 549 | then |
---|
| 550 | Msg "Info: Traduce el identificador del dispositivo EAC a dispositivo linux \n" info |
---|
| 551 | Msg "Sintaxis1: IdPartition int_disk -----------------Ejemplo1: IdPartition 1 -> sda " example |
---|
| 552 | Msg "Sintaxis2: IdPartition int_disk int_partition --Ejemplo2: IdPartition 1 2 -> sda2 " example |
---|
| 553 | |
---|
| 554 | return |
---|
| 555 | fi |
---|
| 556 | #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. |
---|
| 557 | PART=$(ogDiskToDev|cut -f$1 -d' ')$2 |
---|
| 558 | echo $PART | cut -f3 -d \/ |
---|
| 559 | } |
---|
[26c729b] | 560 | |
---|
| 561 | |
---|
| 562 | function ogDeletePartitionTable () { |
---|
| 563 | #/** @function ogDeletePartitionTable: @brief Borra la tabla de particiones del disco. |
---|
| 564 | #@param $1 opcion A (identificador LINUX) str_ID_linux (/dev/sda) |
---|
| 565 | #@param $1 opcion B (Identifiador EAC) int_numdiskEAC(1) |
---|
| 566 | #@return la informacion propia del fdisk |
---|
| 567 | #@warning no definidos |
---|
| 568 | #@attention |
---|
| 569 | #@note |
---|
| 570 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 571 | #*/ |
---|
| 572 | if [ $# = 0 ] |
---|
| 573 | then |
---|
| 574 | Msg "sintaxis1: ogDeletePartitionTable int_disk" red |
---|
| 575 | Msg "sintaxis2: ogDeletePartitionTable str_/dev/sdX" red |
---|
| 576 | return |
---|
| 577 | fi |
---|
| 578 | if [ -n "${1%/dev/*}" ] |
---|
| 579 | then |
---|
| 580 | dev=`DiskToDev $1` |
---|
| 581 | else |
---|
| 582 | dev=$1 |
---|
| 583 | fi |
---|
| 584 | echo -ne "o\nw" | fdisk $dev |
---|
| 585 | } |
---|
[0df4b9f7] | 586 | |
---|
| 587 | |
---|
| 588 | |
---|
| 589 | function ogSetPartitionId() { |
---|
| 590 | #/** @function ogSetPartitionId: @brief Cambia el identificador de la particion, pero no su sistema de archivos. |
---|
| 591 | #@param $1 int_numdiskEAC |
---|
| 592 | #@param $2 int_numpartitionEAC |
---|
| 593 | #@param $3 str_tipoPartition admite EXT2 EXT3 NTFS FAT32 SWAP CACHE |
---|
| 594 | #@return la propia del fdisk |
---|
| 595 | #@warning no controla los parametros, si se introducen mal o simplemente no se introducen no muestra mensaje |
---|
| 596 | #@warning Identifica por nombre del sistema de archivos no por número |
---|
| 597 | #@attention Requisitos: fdisk |
---|
| 598 | #@note |
---|
| 599 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 600 | #*/ |
---|
[be81649] | 601 | |
---|
| 602 | # Variables locales |
---|
| 603 | local DISK PART ID |
---|
| 604 | |
---|
| 605 | #/// Si se solicita, mostrar ayuda. |
---|
| 606 | if [ "$*" == "help" ]; then |
---|
| 607 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 608 | "$FUNCNAME 1 1 10000000" |
---|
| 609 | return |
---|
[0df4b9f7] | 610 | fi |
---|
[be81649] | 611 | #/// Error si no se reciben 3 parámetros. |
---|
| 612 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 613 | |
---|
| 614 | #/// Sustituye nº de disco por su dispositivo. |
---|
| 615 | DISK=`ogDiskToDev $1` || return $? |
---|
| 616 | PART=`ogDiskToDev $1 $2` || return $? |
---|
| 617 | |
---|
| 618 | #/// Elección del tipo de partición. |
---|
| 619 | case "$3" in |
---|
| 620 | EMPTY) ID=0 ;; |
---|
[c7d9af7] | 621 | FAT12) ID=1 ;; |
---|
[be81649] | 622 | EXTENDED) ID=5 ;; |
---|
[c7d9af7] | 623 | FAT16) ID=6 ;; |
---|
[be81649] | 624 | NTFS|EXFAT) ID=7 ;; |
---|
| 625 | FAT32) ID=b ;; |
---|
[c7d9af7] | 626 | HFAT12) ID=11 ;; |
---|
| 627 | HFAT16) ID=16 ;; |
---|
[be81649] | 628 | HNTFS) ID=17 ;; |
---|
| 629 | HFAT32) ID=1b ;; |
---|
| 630 | LINUX-SWAP) ID=82 ;; |
---|
[c7d9af7] | 631 | LINUX-LVM) ID=8e ;; |
---|
[be81649] | 632 | EXT[234]|REISERFS|REISER4|XFS|JFS) |
---|
| 633 | ID=83 ;; |
---|
| 634 | SOLARIS) ID=bf ;; |
---|
| 635 | CACHE) ID=ca ;; |
---|
[c7d9af7] | 636 | LINUX-RAID) ID=fd ;; |
---|
[be81649] | 637 | *) ogRaiseError $OG_ERR_PARTITION "$TYPE" |
---|
| 638 | return $? ;; |
---|
| 639 | esac |
---|
| 640 | echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK 1>/dev/null 2>&1 |
---|
[0df4b9f7] | 641 | } |
---|
[cc6ad14] | 642 | |
---|
[be81649] | 643 | |
---|
[cc6ad14] | 644 | function ogDeletePartitionsLabels () { |
---|
[97da528] | 645 | #/** @function ogDeletePartitionsLabels: @brief Elimina la informacion que tiene el kernel del cliente og sobre los labels de los sistemas de archivos |
---|
[cc6ad14] | 646 | #@param No requiere |
---|
| 647 | #@return Nada |
---|
| 648 | #@warning |
---|
| 649 | #@attention Requisitos: comando interno linux rm |
---|
| 650 | #@note |
---|
| 651 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 652 | #*/ |
---|
| 653 | rm /dev/disk/by-label/* # */ COMENTARIO OBLIGATORIO PARA DOXYGEN |
---|
| 654 | } |
---|
| 655 | |
---|