[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. |
---|
[0d6e7222] | 7 | #@version 1.1.0 |
---|
[9f29ba6] | 8 | #@warning License: GNU GPLv3+ |
---|
| 9 | #*/ |
---|
| 10 | |
---|
[5dbb046] | 11 | |
---|
[be48687] | 12 | # Función ficticia para lanzar parted con timeout, evitando cuelgues del programa. |
---|
| 13 | function parted () |
---|
| 14 | { |
---|
| 15 | timeout -k 5s -s KILL 3s $(which parted) "$@" |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | |
---|
[5dbb046] | 19 | #/** |
---|
[42669ebf] | 20 | # ogCreatePartitions int_ndisk str_parttype:int_partsize ... |
---|
[b094c59] | 21 | #@brief Define el conjunto de particiones de un disco. |
---|
[42669ebf] | 22 | #@param int_ndisk nº de orden del disco |
---|
| 23 | #@param str_parttype mnemónico del tipo de partición |
---|
| 24 | #@param int_partsize tamaño de la partición (en KB) |
---|
[73c8417] | 25 | #@return (nada, por determinar) |
---|
[73488c9] | 26 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 27 | #@exception OG_ERR_NOTFOUND disco o partición no detectado (no es un dispositivo). |
---|
| 28 | #@exception OG_ERR_PARTITION error en partición o en tabla de particiones. |
---|
[73c8417] | 29 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
| 30 | #@attention Pueden definirse particiones vacías de tipo \c EMPTY |
---|
[16f7627] | 31 | #@attention No puede definirse partición de cache y no se modifica si existe. |
---|
[73c8417] | 32 | #@note Requisitos: sfdisk, parted, partprobe, awk |
---|
| 33 | #@todo Definir atributos (arranque, oculta) y tamaños en MB, GB, etc. |
---|
[afc1e74] | 34 | #@version 0.9 - Primera versión para OpenGnSys |
---|
[73c8417] | 35 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 36 | #@date 2009/09/09 |
---|
[bc7dfe7] | 37 | #@version 0.9.1 - Corrección del redondeo del tamaño del disco. |
---|
[4b45aff] | 38 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 39 | #@date 2010/03/09 |
---|
[73488c9] | 40 | #@version 1.0.4 - Llamada a función específica para tablas GPT. |
---|
| 41 | #@author Universidad de Huelva |
---|
| 42 | #@date 2012/03/30 |
---|
[1e7eaab] | 43 | #*/ ## |
---|
[42669ebf] | 44 | function ogCreatePartitions () |
---|
| 45 | { |
---|
[73c8417] | 46 | # Variables locales. |
---|
[0cea822] | 47 | local ND DISK PTTYPE PART SECTORS START SIZE TYPE CACHEPART CACHESIZE EXTSTART EXTSIZE tmpsfdisk |
---|
[1e7eaab] | 48 | # Si se solicita, mostrar ayuda. |
---|
[1a7130a] | 49 | if [ "$*" == "help" ]; then |
---|
[73c8417] | 50 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk str_parttype:int_partsize ..." \ |
---|
| 51 | "$FUNCNAME 1 NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
| 52 | return |
---|
| 53 | fi |
---|
[a73649d] | 54 | # Error si no se reciben al menos 2 parámetros. |
---|
[55ad138c] | 55 | [ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[73c8417] | 56 | |
---|
[4b45aff] | 57 | # Nº total de sectores, para evitar desbordamiento (evitar redondeo). |
---|
[6d3f526] | 58 | ND="$1" |
---|
| 59 | DISK=$(ogDiskToDev "$ND") || return $? |
---|
[73488c9] | 60 | PTTYPE=$(ogGetPartitionTableType $1) |
---|
[a06ac2d] | 61 | PTTYPE=${PTTYPE:-"MSDOS"} # Por defecto para discos vacíos. |
---|
[73488c9] | 62 | case "$PTTYPE" in |
---|
| 63 | GPT) ogCreateGptPartitions "$@" |
---|
| 64 | return $? ;; |
---|
[0cea822] | 65 | MSDOS) ;; |
---|
[73488c9] | 66 | *) ogRaiseError $OG_ERR_PARTITION "$PTTYPE" |
---|
| 67 | return $? ;; |
---|
| 68 | esac |
---|
[0cea822] | 69 | SECTORS=$(ogGetLastSector $1) |
---|
[16f7627] | 70 | # Se recalcula el nº de sectores del disco 1, si existe partición de caché. |
---|
[d7c35ad] | 71 | CACHEPART=$(ogFindCache 2>/dev/null) |
---|
[6d3f526] | 72 | [ "$ND" = "${CACHEPART% *}" ] && CACHESIZE=$(ogGetCacheSize 2>/dev/null | awk '{print $0*2}') |
---|
[d7c35ad] | 73 | [ -n "$CACHESIZE" ] && SECTORS=$[SECTORS-CACHESIZE] |
---|
[16f7627] | 74 | # Sector de inicio (la partición 1 empieza en el sector 63). |
---|
[73c8417] | 75 | START=63 |
---|
| 76 | PART=1 |
---|
| 77 | |
---|
[b094c59] | 78 | # Fichero temporal de entrada para "sfdisk" |
---|
[73c8417] | 79 | tmpsfdisk=/tmp/sfdisk$$ |
---|
| 80 | trap "rm -f $tmpsfdisk" 1 2 3 9 15 |
---|
| 81 | |
---|
| 82 | echo "unit: sectors" >$tmpsfdisk |
---|
| 83 | echo >>$tmpsfdisk |
---|
| 84 | |
---|
[42669ebf] | 85 | # Generar fichero de entrada para "sfdisk" con las particiones. |
---|
[16f7627] | 86 | shift |
---|
[73c8417] | 87 | while [ $# -gt 0 ]; do |
---|
[16f7627] | 88 | # Conservar los datos de la partición de caché. |
---|
[6d3f526] | 89 | if [ "$ND $PART" == "$CACHEPART" -a -n "$CACHESIZE" ]; then |
---|
[16f7627] | 90 | echo "$DISK$PART : start=$[SECTORS+1], size=$CACHESIZE, Id=ca" >>$tmpsfdisk |
---|
| 91 | PART=$[PART+1] |
---|
| 92 | fi |
---|
[42669ebf] | 93 | # Leer formato de cada parámetro - Tipo:Tamaño |
---|
[73c8417] | 94 | TYPE="${1%%:*}" |
---|
| 95 | SIZE="${1#*:}" |
---|
[42e31fd] | 96 | # Obtener identificador de tipo de partición válido. |
---|
[5af5d5f] | 97 | ID=$(ogTypeToId "$TYPE" MSDOS) |
---|
[42e31fd] | 98 | [ "$TYPE" != "CACHE" -a -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $? |
---|
| 99 | # Comprobar tamaño numérico y convertir en sectores de 512 B. |
---|
| 100 | [[ "$SIZE" == *([0-9]) ]] || ogRaiseError $OG_ERR_FORMAT "$SIZE" || return $? |
---|
| 101 | SIZE=$[SIZE*2] |
---|
[42669ebf] | 102 | # Comprobar si la partición es extendida. |
---|
| 103 | if [ $ID = 5 ]; then |
---|
[6bde19d] | 104 | [ $PART -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[42669ebf] | 105 | EXTSTART=$START |
---|
| 106 | EXTSIZE=$SIZE |
---|
| 107 | fi |
---|
[1e7eaab] | 108 | # Incluir particiones lógicas dentro de la partición extendida. |
---|
[73c8417] | 109 | if [ $PART = 5 ]; then |
---|
[6bde19d] | 110 | [ -n "$EXTSTART" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[73c8417] | 111 | START=$EXTSTART |
---|
| 112 | SECTORS=$[EXTSTART+EXTSIZE] |
---|
| 113 | fi |
---|
[1e7eaab] | 114 | # Generar datos para la partición. |
---|
[73c8417] | 115 | echo "$DISK$PART : start=$START, size=$SIZE, Id=$ID" >>$tmpsfdisk |
---|
[42669ebf] | 116 | # Error si se supera el nº total de sectores. |
---|
[73c8417] | 117 | START=$[START+SIZE] |
---|
[16f7627] | 118 | [ $START -le $SECTORS ] || ogRaiseError $OG_ERR_FORMAT "$[START/2] > $[SECTORS/2]" || return $? |
---|
[73c8417] | 119 | PART=$[PART+1] |
---|
| 120 | shift |
---|
| 121 | done |
---|
[16f7627] | 122 | # Si no se indican las 4 particiones primarias, definirlas como vacías, conservando la partición de caché. |
---|
[73c8417] | 123 | while [ $PART -le 4 ]; do |
---|
[6d3f526] | 124 | if [ "$ND $PART" == "$CACHEPART" -a -n "$CACHESIZE" ]; then |
---|
[16f7627] | 125 | echo "$DISK$PART : start=$[SECTORS+1], size=$CACHESIZE, Id=ca" >>$tmpsfdisk |
---|
| 126 | else |
---|
| 127 | echo "$DISK$PART : start=0, size=0, Id=0" >>$tmpsfdisk |
---|
| 128 | fi |
---|
[73c8417] | 129 | PART=$[PART+1] |
---|
| 130 | done |
---|
[b094c59] | 131 | # Si se define partición extendida sin lógicas, crear particion 5 vacía. |
---|
[73c8417] | 132 | if [ $PART = 5 -a -n "$EXTSTART" ]; then |
---|
| 133 | echo "${DISK}5 : start=$EXTSTART, size=$EXTSIZE, Id=0" >>$tmpsfdisk |
---|
| 134 | fi |
---|
| 135 | |
---|
[7510561] | 136 | # Desmontar los sistemas de archivos del disco antes de realizar las operaciones. |
---|
[6d3f526] | 137 | ogUnmountAll $ND 2>/dev/null |
---|
| 138 | [ -n "$CACHESIZE" ] && ogUnmountCache 2>/dev/null |
---|
[7510561] | 139 | |
---|
[73c8417] | 140 | # Si la tabla de particiones no es valida, volver a generarla. |
---|
[0cea822] | 141 | ogCreatePartitionTable $ND |
---|
[1e7eaab] | 142 | # Definir particiones y notificar al kernel. |
---|
[c6087b9] | 143 | sfdisk -f $DISK < $tmpsfdisk 2>/dev/null && partprobe $DISK |
---|
[73c8417] | 144 | rm -f $tmpsfdisk |
---|
[2bd7547] | 145 | [ -n "$CACHESIZE" ] && ogMountCache 2>/dev/null || return 0 |
---|
[73c8417] | 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | #/** |
---|
[73488c9] | 150 | # ogCreateGptPartitions int_ndisk str_parttype:int_partsize ... |
---|
| 151 | #@brief Define el conjunto de particiones de un disco GPT |
---|
| 152 | #@param int_ndisk nº de orden del disco |
---|
| 153 | #@param str_parttype mnemónico del tipo de partición |
---|
| 154 | #@param int_partsize tamaño de la partición (en KB) |
---|
| 155 | #@return (nada, por determinar) |
---|
| 156 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 157 | #@exception OG_ERR_NOTFOUND disco o partición no detectado (no es un dispositivo). |
---|
| 158 | #@exception OG_ERR_PARTITION error en partición o en tabla de particiones. |
---|
| 159 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
| 160 | #@attention Pueden definirse particiones vacías de tipo \c EMPTY |
---|
| 161 | #@attention No puede definirse partición de caché y no se modifica si existe. |
---|
| 162 | #@note Requisitos: sfdisk, parted, partprobe, awk |
---|
| 163 | #@todo Definir atributos (arranque, oculta) y tamaños en MB, GB, etc. |
---|
| 164 | #@version 1.0.4 - Primera versión para OpenGnSys |
---|
| 165 | #@author Universidad de Huelva |
---|
| 166 | #@date 2012/03/30 |
---|
| 167 | #*/ ## |
---|
| 168 | function ogCreateGptPartitions () |
---|
| 169 | { |
---|
| 170 | # Variables locales. |
---|
[0cea822] | 171 | local ND DISK PART SECTORS ALIGN START SIZE TYPE CACHEPART CACHESIZE DELOPTIONS OPTIONS |
---|
[73488c9] | 172 | # Si se solicita, mostrar ayuda. |
---|
| 173 | if [ "$*" == "help" ]; then |
---|
| 174 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk str_parttype:int_partsize ..." \ |
---|
| 175 | "$FUNCNAME 1 NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
| 176 | return |
---|
| 177 | fi |
---|
| 178 | # Error si no se reciben menos de 2 parámetros. |
---|
| 179 | [ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 180 | |
---|
| 181 | # Nº total de sectores, para evitar desbordamiento (evitar redondeo). |
---|
| 182 | ND="$1" |
---|
| 183 | DISK=$(ogDiskToDev "$ND") || return $? |
---|
| 184 | # Se calcula el ultimo sector del disco (total de sectores usables) |
---|
[0cea822] | 185 | SECTORS=$(ogGetLastSector $1) |
---|
[e3f557f] | 186 | # Se recalcula el nº de sectores del disco si existe partición de caché. |
---|
| 187 | CACHEPART=$(ogFindCache 2>/dev/null) |
---|
[0cea822] | 188 | [ "$ND" = "${CACHEPART% *}" ] && CACHESIZE=$(ogGetCacheSize 2>/dev/null | awk '{print $0*2}') |
---|
| 189 | [ -n "$CACHESIZE" ] && SECTORS=$[SECTORS-CACHESIZE] |
---|
| 190 | # Si el disco es GPT empieza en el sector 2048 por defecto, pero podria cambiarse |
---|
[499bf46] | 191 | ALIGN=$(sgdisk -D $DISK 2>/dev/null) |
---|
[0cea822] | 192 | START=$ALIGN |
---|
| 193 | PART=1 |
---|
[73488c9] | 194 | |
---|
[0cea822] | 195 | # Leer parámetros con definición de particionado. |
---|
| 196 | shift |
---|
| 197 | |
---|
| 198 | while [ $# -gt 0 ]; do |
---|
| 199 | # Si PART es la cache, nos la saltamos y seguimos con el siguiente numero para conservar los datos de la partición de caché. |
---|
| 200 | if [ "$ND $PART" == "$CACHEPART" -a -n "$CACHESIZE" ]; then |
---|
| 201 | PART=$[PART+1] |
---|
| 202 | fi |
---|
| 203 | # Leer formato de cada parámetro - Tipo:Tamaño |
---|
| 204 | TYPE="${1%%:*}" |
---|
| 205 | SIZE="${1#*:}" |
---|
| 206 | # Error si la partición es extendida (no válida en discos GPT). |
---|
[499bf46] | 207 | if [ "$TYPE" == "EXTENDED" ]; then |
---|
| 208 | ogRaiseError $OG_ERR_PARTITION "EXTENDED" |
---|
| 209 | return $? |
---|
| 210 | fi |
---|
[0cea822] | 211 | # Comprobar si existe la particion actual, capturamos su tamaño para ver si cambio o no |
---|
[499bf46] | 212 | PARTSIZE=$(ogGetPartitionSize $ND $PART 2>/dev/null) |
---|
[0cea822] | 213 | # En sgdisk no se pueden redimensionar las particiones, es necesario borrarlas y volver a crealas |
---|
| 214 | [ $PARTSIZE ] && DELOPTIONS="$DELOPTIONS -d$PART" |
---|
| 215 | # Creamos la particion |
---|
| 216 | # Obtener identificador de tipo de partición válido. |
---|
[5af5d5f] | 217 | ID=$(ogTypeToId "$TYPE" GPT) |
---|
[0cea822] | 218 | [ "$TYPE" != "CACHE" -a -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $? |
---|
| 219 | # Comprobar tamaño numérico y convertir en sectores de 512 B. |
---|
| 220 | [[ "$SIZE" == *([0-9]) ]] || ogRaiseError $OG_ERR_FORMAT "$SIZE" || return $? |
---|
| 221 | SIZE=$[SIZE*2] |
---|
| 222 | # SIZE debe ser múltiplo de ALIGN, si no gdisk lo mueve automáticamente. |
---|
| 223 | DIV=$[$SIZE/$ALIGN] |
---|
| 224 | SIZE=$[$DIV*$ALIGN] |
---|
| 225 | # En el caso de que la partición sea EMPTY no se crea nada |
---|
| 226 | if [ "$TYPE" != "EMPTY" ]; then |
---|
| 227 | OPTIONS="$OPTIONS -n$PART:$START:+$SIZE -t$PART:$ID " |
---|
| 228 | fi |
---|
| 229 | START=$[START+SIZE] |
---|
| 230 | # Error si se supera el nº total de sectores. |
---|
| 231 | [ $START -le $SECTORS ] || ogRaiseError $OG_ERR_FORMAT "$[START/2] > $[SECTORS/2]" || return $? |
---|
| 232 | PART=$[PART+1] |
---|
| 233 | shift |
---|
| 234 | done |
---|
| 235 | |
---|
| 236 | # Desmontar los sistemas de archivos del disco antes de realizar las operaciones. |
---|
| 237 | ogUnmountAll $ND 2>/dev/null |
---|
| 238 | [ -n "$CACHESIZE" ] && ogUnmountCache 2>/dev/null |
---|
| 239 | |
---|
| 240 | # Si la tabla de particiones no es valida, volver a generarla. |
---|
| 241 | ogCreatePartitionTable $ND |
---|
| 242 | # Definir particiones y notificar al kernel. |
---|
| 243 | # Borramos primero las particiones y luego creamos las nuevas |
---|
[499bf46] | 244 | sgdisk $DELOPTIONS $OPTIONS $DISK 2>/dev/null && partprobe $DISK |
---|
[0cea822] | 245 | [ -n "$CACHESIZE" ] && ogMountCache 2>/dev/null |
---|
[73488c9] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | #/** |
---|
[942dfd7] | 250 | # ogCreatePartitionTable int_ndisk [str_tabletype] |
---|
[f2c8049] | 251 | #@brief Genera una tabla de particiones en caso de que no sea valida, si es valida no hace nada. |
---|
[942dfd7] | 252 | #@param int_ndisk nº de orden del disco |
---|
| 253 | #@param str_tabletype tipo de tabla de particiones (opcional) |
---|
| 254 | #@return (por determinar) |
---|
| 255 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
[f2c8049] | 256 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
[2a05172] | 257 | #@note tabletype: { MSDOS, GPT }, MSDOS por defecto |
---|
| 258 | #@note Requisitos: fdisk, gdisk, parted |
---|
[afc1e74] | 259 | #@version 1.0.4 - Primera versión compatible con OpenGnSys. |
---|
[942dfd7] | 260 | #@author Universidad de Huelva |
---|
| 261 | #@date 2012/03/06 |
---|
[31f5b7a] | 262 | #@version 1.0.6a - Adaptar creación de nueva tabla MSDOS. |
---|
[2a05172] | 263 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 264 | #@date 2016/01/29 |
---|
[942dfd7] | 265 | #*/ ## |
---|
[6e390b1] | 266 | function ogCreatePartitionTable () |
---|
[942dfd7] | 267 | { |
---|
| 268 | # Variables locales. |
---|
| 269 | local DISK PTTYPE CREATE CREATEPTT |
---|
| 270 | |
---|
| 271 | # Si se solicita, mostrar ayuda. |
---|
| 272 | if [ "$*" == "help" ]; then |
---|
| 273 | ogHelp "$FUNCNAME int_ndisk [str_partype]" \ |
---|
| 274 | "$FUNCNAME 1 GPT" "$FUNCNAME 1" |
---|
| 275 | return |
---|
| 276 | fi |
---|
| 277 | # Error si no se reciben 1 o 2 parámetros. |
---|
| 278 | case $# in |
---|
[f2c8049] | 279 | 1) CREATEPTT="" ;; |
---|
| 280 | 2) CREATEPTT="$2" ;; |
---|
| 281 | *) ogRaiseError $OG_ERR_FORMAT |
---|
| 282 | return $? ;; |
---|
[942dfd7] | 283 | esac |
---|
| 284 | |
---|
| 285 | # Capturamos el tipo de tabla de particiones actual |
---|
| 286 | DISK=$(ogDiskToDev $1) || return $? |
---|
| 287 | PTTYPE=$(ogGetPartitionTableType $1) |
---|
[a06ac2d] | 288 | PTTYPE=${PTTYPE:-"MSDOS"} # Por defecto para discos vacíos. |
---|
[942dfd7] | 289 | CREATEPTT=${CREATEPTT:-"$PTTYPE"} |
---|
| 290 | |
---|
[a06ac2d] | 291 | # Si la tabla actual y la que se indica son iguales, se comprueba si hay que regenerarla. |
---|
[942dfd7] | 292 | if [ "$CREATEPTT" == "$PTTYPE" ]; then |
---|
| 293 | case "$PTTYPE" in |
---|
| 294 | GPT) [ ! $(sgdisk -p $DISK 2>&1 >/dev/null) ] || CREATE="GPT" ;; |
---|
| 295 | MSDOS) [ $(parted -s $DISK print >/dev/null) ] || CREATE="MSDOS" ;; |
---|
| 296 | esac |
---|
| 297 | else |
---|
| 298 | CREATE="$CREATEPTT" |
---|
| 299 | fi |
---|
| 300 | # Dependiendo del valor de CREATE, creamos la tabla de particiones en cada caso. |
---|
| 301 | case "$CREATE" in |
---|
| 302 | GPT) |
---|
| 303 | # Si es necesario crear una tabla GPT pero la actual es MSDOS |
---|
| 304 | if [ "$PTTYPE" == "MSDOS" ]; then |
---|
| 305 | sgdisk -g $DISK |
---|
| 306 | else |
---|
| 307 | echo -e "2\nw\nY\n" | gdisk $DISK |
---|
| 308 | fi |
---|
| 309 | partprobe $DISK 2>/dev/null |
---|
| 310 | ;; |
---|
| 311 | MSDOS) |
---|
| 312 | # Si es necesario crear una tabla MSDOS pero la actual es GPT |
---|
| 313 | if [ "$PTTYPE" == "GPT" ]; then |
---|
[2ba98be] | 314 | sgdisk -Z $DISK |
---|
[942dfd7] | 315 | fi |
---|
[2a05172] | 316 | # Crear y borrar una partición para que la tabla se genere bien. |
---|
| 317 | echo -e "o\nn\np\n\n\n\nd\n\nw" | fdisk $DISK |
---|
[942dfd7] | 318 | partprobe $DISK 2>/dev/null |
---|
| 319 | ;; |
---|
| 320 | esac |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | #/** |
---|
[43892687] | 325 | # ogDeletePartitionTable ndisk |
---|
| 326 | #@brief Borra la tabla de particiones del disco. |
---|
| 327 | #@param int_ndisk nº de orden del disco |
---|
| 328 | #@return la informacion propia del fdisk |
---|
| 329 | #@version 0.1 - Integracion para OpenGnSys |
---|
| 330 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
[942dfd7] | 331 | #@date 2008/10/27 |
---|
[43892687] | 332 | #@version 1.0.4 - Adaptado para su uso con discos GPT |
---|
| 333 | #@author Universidad de Huelva |
---|
| 334 | #@date 2012/03/13 |
---|
| 335 | #*/ ## |
---|
| 336 | function ogDeletePartitionTable () |
---|
| 337 | { |
---|
| 338 | # Variables locales. |
---|
| 339 | local DISK |
---|
| 340 | |
---|
| 341 | # Si se solicita, mostrar ayuda. |
---|
| 342 | if [ "$*" == "help" ]; then |
---|
| 343 | ogHelp "$FUNCNAME int_ndisk" "$FUNCNAME 1" |
---|
| 344 | return |
---|
| 345 | fi |
---|
| 346 | # Error si no se reciben 1 parámetros. |
---|
| 347 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 348 | |
---|
| 349 | # Obteniendo Identificador linux del disco. |
---|
| 350 | DISK=$(ogDiskToDev $1) || return $? |
---|
| 351 | # Crear una tabla de particiones vacía. |
---|
| 352 | case "$(ogGetPartitionTableType $1)" in |
---|
| 353 | GPT) sgdisk -o $DISK ;; |
---|
| 354 | MSDOS) echo -ne "o\nw" | fdisk $DISK ;; |
---|
| 355 | esac |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | |
---|
| 359 | #/** |
---|
[95e9664] | 360 | # ogDevToDisk path_device | LABEL="str_label" | UUID="str_uuid" |
---|
| 361 | #@brief Devuelve el nº de orden de dicso (y partición) correspondiente al nombre de fichero de dispositivo o a la etiqueta o UUID del sistema de archivos asociado. |
---|
| 362 | #@param path_device Camino del fichero de dispositivo. |
---|
| 363 | #@param str_label etiqueta de sistema de archivos. |
---|
| 364 | #@param str_uuid UUID de sistema de archivos. |
---|
[42669ebf] | 365 | #@return int_ndisk (para dispositivo de disco) |
---|
| 366 | #@return int_ndisk int_npartition (para dispositivo de partición). |
---|
[5dbb046] | 367 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 368 | #@exception OG_ERR_NOTFOUND Dispositivo no detectado. |
---|
[95e9664] | 369 | #@note Solo se acepta en cada llamada 1 de los 3 tipos de parámetros. |
---|
[985bef0] | 370 | #@version 0.1 - Integracion para Opengnsys - EAC: DiskEAC() en ATA.lib |
---|
| 371 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
| 372 | #@date 2008/10/27 |
---|
[afc1e74] | 373 | #@version 0.9 - Primera version para OpenGnSys |
---|
[5dbb046] | 374 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
[985bef0] | 375 | #@date 2009/07/20 |
---|
[95e9664] | 376 | #@version 1.0.6 - Soporta parámetro con UIID o etiqueta. |
---|
| 377 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 378 | #@date 2014/07/13 |
---|
[1e7eaab] | 379 | #*/ ## |
---|
[42669ebf] | 380 | function ogDevToDisk () |
---|
| 381 | { |
---|
[73c8417] | 382 | # Variables locales. |
---|
[95e9664] | 383 | local DEV d n |
---|
[1e7eaab] | 384 | # Si se solicita, mostrar ayuda. |
---|
[1a7130a] | 385 | if [ "$*" == "help" ]; then |
---|
[95e9664] | 386 | ogHelp "$FUNCNAME" "$FUNCNAME path_device | LABEL=str_label | UUID=str_uuid" \ |
---|
| 387 | "$FUNCNAME /dev/sda => 1" \ |
---|
| 388 | "$FUNCNAME /dev/sda1 => 1 1" \ |
---|
| 389 | "$FUNCNAME LABEL=CACHE => 1 4" |
---|
[5dbb046] | 390 | return |
---|
| 391 | fi |
---|
| 392 | |
---|
[1e7eaab] | 393 | # Error si no se recibe 1 parámetro. |
---|
[5dbb046] | 394 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[95e9664] | 395 | |
---|
| 396 | # Obtener dispositivo a partir de camino, etiqueta o UUID. |
---|
| 397 | DEV="$1" |
---|
| 398 | case "$DEV" in |
---|
| 399 | LABEL=*) DEV=$(blkid -L "${1#*=}") ;; |
---|
| 400 | UUID=*) DEV=$(blkid -U "${1#*=}") ;; |
---|
| 401 | esac |
---|
| 402 | |
---|
[42669ebf] | 403 | # Error si no es fichero de bloques. |
---|
[95e9664] | 404 | [ -b "$DEV" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
[5dbb046] | 405 | |
---|
[1e7eaab] | 406 | # Procesa todos los discos para devolver su nº de orden y de partición. |
---|
[5dbb046] | 407 | n=1 |
---|
| 408 | for d in $(ogDiskToDev); do |
---|
[95e9664] | 409 | [ -n "$(echo $DEV | grep $d)" ] && echo "$n ${DEV#$d}" && return |
---|
[5dbb046] | 410 | n=$[n+1] |
---|
| 411 | done |
---|
| 412 | ogRaiseError $OG_ERR_NOTFOUND "$1" |
---|
| 413 | return $OG_ERR_NOTFOUND |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | |
---|
[9f29ba6] | 417 | #/** |
---|
[42669ebf] | 418 | # ogDiskToDev [int_ndisk [int_npartition]] |
---|
[9f57de01] | 419 | #@brief Devuelve la equivalencia entre el nº de orden del dispositivo (dicso o partición) y el nombre de fichero de dispositivo correspondiente. |
---|
[42669ebf] | 420 | #@param int_ndisk nº de orden del disco |
---|
| 421 | #@param int_npartition nº de orden de la partición |
---|
[9f57de01] | 422 | #@return Para 0 parametros: Devuelve los nombres de ficheros de los dispositivos sata/ata/usb linux encontrados. |
---|
| 423 | #@return Para 1 parametros: Devuelve la ruta del disco duro indicado. |
---|
| 424 | #@return Para 2 parametros: Devuelve la ruta de la particion indicada. |
---|
| 425 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 426 | #@exception OG_ERR_NOTFOUND Dispositivo no detectado. |
---|
[2717297] | 427 | #@note Requisitos: awk, lvm |
---|
[985bef0] | 428 | #@version 0.1 - Integracion para Opengnsys - EAC: Disk() en ATA.lib; HIDRA: DetectarDiscos.sh |
---|
| 429 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 430 | #@Date 2008/06/19 |
---|
| 431 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
| 432 | #@date 2008/10/27 |
---|
[afc1e74] | 433 | #@version 0.9 - Primera version para OpenGnSys |
---|
[9f57de01] | 434 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 435 | #@date 2009-07-20 |
---|
[19b1a2f] | 436 | #@version 1.0.5 - Comprobación correcta de parámetros para soportar valores > 9. |
---|
| 437 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 438 | #@date 2013-05-07 |
---|
[0d6e7222] | 439 | #@version 1.0.6 - Soportar RAID hardware y Multipath. |
---|
[fd1846f] | 440 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 441 | #@date 2014-09-23 |
---|
[b19d678] | 442 | #@version 1.1.0 - Usar caché de datos y soportar pool de volúmenes ZFS. |
---|
[0d6e7222] | 443 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
[b19d678] | 444 | #@date 2016-05-27 |
---|
[1e7eaab] | 445 | #*/ ## |
---|
[42669ebf] | 446 | function ogDiskToDev () |
---|
| 447 | { |
---|
[59f9ad2] | 448 | # Variables locales |
---|
[b19d678] | 449 | local CACHEFILE ALLDISKS MPATH VOLGROUPS ZFSVOLS DISK PART ZPOOL i |
---|
[9f29ba6] | 450 | |
---|
[1e7eaab] | 451 | # Si se solicita, mostrar ayuda. |
---|
[1a7130a] | 452 | if [ "$*" == "help" ]; then |
---|
[aae34f6] | 453 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk [int_npartition]" \ |
---|
| 454 | "$FUNCNAME => /dev/sda /dev/sdb" \ |
---|
| 455 | "$FUNCNAME 1 => /dev/sda" \ |
---|
| 456 | "$FUNCNAME 1 1 => /dev/sda1" |
---|
| 457 | return |
---|
| 458 | fi |
---|
| 459 | |
---|
[b19d678] | 460 | # Borrar fichero de caché de configuración si hay cambios en las particiones. |
---|
| 461 | CACHEFILE=/var/cache/disks.cfg |
---|
[13750f5] | 462 | if ! diff -q <(cat /proc/partitions) /tmp/.partitions &>/dev/null; then |
---|
[b19d678] | 463 | # Guardar copia de las particiones definidas para comprobar cambios. |
---|
| 464 | cp -a /proc/partitions /tmp/.partitions |
---|
| 465 | rm -f $CACHEFILE |
---|
| 466 | fi |
---|
| 467 | |
---|
| 468 | # Si existe una correspondencia con disco/dispositivo en el caché; mostrarlo y salir. |
---|
[13750f5] | 469 | PART=$(awk -F: -v d="$*" '{if ($1==d) {print $2}}' $CACHEFILE 2>/dev/null) |
---|
[b19d678] | 470 | if [ -n "$PART" ]; then |
---|
| 471 | echo "$PART" |
---|
| 472 | return |
---|
| 473 | fi |
---|
| 474 | |
---|
| 475 | # Continuar para detectar nuevos dispositivos. |
---|
[a02322e] | 476 | # Listar dispositivos de discos. |
---|
| 477 | ALLDISKS=$(lsblk -n -e 1,2 | awk '$6~/^disk$/ {gsub(/!/,"/"); printf "/dev/%s ",$1}') |
---|
[fd1846f] | 478 | |
---|
| 479 | # Listar volúmenes lógicos. |
---|
[13ccdf5] | 480 | VOLGROUPS=$(vgs -a --noheadings 2>/dev/null | awk '{printf "/dev/%s ",$1}') |
---|
[2717297] | 481 | ALLDISKS="$ALLDISKS $VOLGROUPS" |
---|
[9f29ba6] | 482 | |
---|
[fd1846f] | 483 | # Detectar caminos múltiples (ignorar mensaje si no está configurado Multipath). |
---|
| 484 | if MPATH=$(multipath -l -v 1 2>/dev/null | awk '{printf "/dev/mapper/%s ",$1}'; exit ${PIPESTATUS[0]}); then |
---|
| 485 | # Quitar de la lista los discos que forman parte de Multipath. |
---|
| 486 | for i in $(multipath -ll | awk '$6=="ready" {printf "/dev/%s ",$3}'); do |
---|
| 487 | ALLDISKS="${ALLDISKS//$i/}" |
---|
| 488 | done |
---|
| 489 | # Añadir caminos múltiples a los discos detectados. |
---|
| 490 | ALLDISKS="$ALLDISKS $MPATH" |
---|
| 491 | fi |
---|
| 492 | |
---|
[0d6e7222] | 493 | # Detectar volúmenes ZFS. |
---|
| 494 | ZFSVOLS=$(blkid | awk -F: '/zfs/ {print $1}') |
---|
| 495 | ALLDISKS="$ALLDISKS $ZFSVOLS" |
---|
| 496 | |
---|
[1e7eaab] | 497 | # Mostrar salidas segun el número de parametros. |
---|
[9f29ba6] | 498 | case $# in |
---|
[2717297] | 499 | 0) # Muestra todos los discos, separados por espacios. |
---|
| 500 | echo $ALLDISKS |
---|
| 501 | ;; |
---|
[19b1a2f] | 502 | 1) # Error si el parámetro no es un número positivo. |
---|
| 503 | [[ "$1" =~ ^[1-9][0-9]*$ ]] || ogRaiseError $OG_ERR_FORMAT "$1" || return $? |
---|
[2717297] | 504 | DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}') |
---|
| 505 | # Error si el fichero no existe. |
---|
| 506 | [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
[b19d678] | 507 | # Actualizar caché de configuración y mostrar dispositivo. |
---|
| 508 | echo "$*:$DISK" >> $CACHEFILE |
---|
[2717297] | 509 | echo "$DISK" |
---|
| 510 | ;; |
---|
[19b1a2f] | 511 | 2) # Error si los 2 parámetros no son números positivos. |
---|
| 512 | [[ "$1" =~ ^[1-9][0-9]*$ ]] && [[ "$2" =~ ^[1-9][0-9]*$ ]] || ogRaiseError $OG_ERR_FORMAT "$1 $2" || return $? |
---|
[2717297] | 513 | DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}') |
---|
| 514 | [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
| 515 | PART="$DISK$2" |
---|
[1e7eaab] | 516 | # Comprobar si es partición. |
---|
[2717297] | 517 | if [ -b "$PART" ]; then |
---|
[b19d678] | 518 | # Actualizar caché de configuración y mostrar dispositivo. |
---|
| 519 | echo "$*:$PART" >> $CACHEFILE |
---|
[2717297] | 520 | echo "$PART" |
---|
| 521 | else |
---|
[fd1846f] | 522 | # Comprobar si RAID o Multipath (tener en cuenta enlace simbólico). |
---|
| 523 | PART="${DISK}p$2" |
---|
| 524 | if [ "$(stat -L -c "%A" "$PART" 2>/dev/null | cut -c1)" == "b" ]; then |
---|
[b19d678] | 525 | # Actualizar caché de configuración y mostrar dispositivo. |
---|
| 526 | echo "$*:$PART" >> $CACHEFILE |
---|
[fd1846f] | 527 | echo "$PART" |
---|
| 528 | else |
---|
[0d6e7222] | 529 | PART="" |
---|
| 530 | # Comprobar si volumen lógico. /* (comentario Doxygen) |
---|
| 531 | if ogCheckStringInGroup "$DISK" "$VOLGROUPS"; then |
---|
| 532 | PART=$(lvscan -a 2>/dev/null | \ |
---|
| 533 | awk -F\' -v n=$2 "\$2~/^${DISK//\//\\/}\// {if (NR==n) print \$2}") |
---|
| 534 | [ -e "$PART" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $? |
---|
| 535 | # (comentario Doxygen) */ |
---|
| 536 | fi |
---|
| 537 | # Comprobar si volumen ZFS que puede ser montado. |
---|
| 538 | if ogCheckStringInGroup "$DISK" "$ZFSVOLS"; then |
---|
| 539 | zpool import -f -R /mnt -N -a 2>/dev/null |
---|
| 540 | ZPOOL=$(blkid -s LABEL -o value $DISK) |
---|
| 541 | PART=$(zfs list -Hp -o name,canmount,mountpoint -r $ZPOOL | \ |
---|
| 542 | awk -v n=$2 '$2=="on" && $3!="none" {c++; if (c==n) print $1}') |
---|
| 543 | fi |
---|
| 544 | # Salir si no se encuentra dispositivo. |
---|
| 545 | [ -n "$PART" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $? |
---|
| 546 | # Devolver camino al dispositivo. |
---|
[b19d678] | 547 | # Actualizar caché de configuración y mostrar dispositivo. |
---|
| 548 | echo "$*:$PART" >> $CACHEFILE |
---|
[0d6e7222] | 549 | echo "$PART" |
---|
[fd1846f] | 550 | fi |
---|
[2717297] | 551 | fi |
---|
| 552 | ;; |
---|
| 553 | *) # Formato erroneo. |
---|
| 554 | ogRaiseError $OG_ERR_FORMAT |
---|
[aae34f6] | 555 | return $OG_ERR_FORMAT |
---|
| 556 | ;; |
---|
[9f29ba6] | 557 | esac |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | #/** |
---|
[739d358] | 562 | # ogGetDiskSize int_ndisk |
---|
| 563 | #@brief Muestra el tamaño en KB de un disco. |
---|
| 564 | #@param int_ndisk nº de orden del disco |
---|
| 565 | #@return int_size - Tamaño en KB del disco. |
---|
| 566 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
[be0a5cf] | 567 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
[739d358] | 568 | #@note Requisitos: sfdisk, awk |
---|
| 569 | #@version 0.9.2 - Primera version para OpenGnSys |
---|
| 570 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 571 | #@date 2010/09/15 |
---|
[95e9664] | 572 | #@version 1.0.6 - Soportar LVM. |
---|
| 573 | #@author Universidad de Huelva |
---|
| 574 | #@date 2014/09/04 |
---|
[739d358] | 575 | #*/ ## |
---|
| 576 | function ogGetDiskSize () |
---|
| 577 | { |
---|
| 578 | # Variables locales. |
---|
[95e9664] | 579 | local DISK SIZE |
---|
[739d358] | 580 | |
---|
| 581 | # Si se solicita, mostrar ayuda. |
---|
| 582 | if [ "$*" == "help" ]; then |
---|
[cbbb046] | 583 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1 => 244198584" |
---|
[739d358] | 584 | return |
---|
| 585 | fi |
---|
| 586 | # Error si no se recibe 1 parámetro. |
---|
| 587 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 588 | |
---|
[43892687] | 589 | # Obtener el tamaño del disco. |
---|
[739d358] | 590 | DISK="$(ogDiskToDev $1)" || return $? |
---|
[95e9664] | 591 | SIZE=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3}}' /proc/partitions) |
---|
| 592 | # Si no, obtener tamaño del grupo de volúmenes. |
---|
| 593 | [ -z "$SIZE" ] && SIZE=$(vgs --noheadings --units=B -o dev_size $DISK 2>/dev/null | \ |
---|
| 594 | awk '{print $1/1024}') |
---|
| 595 | |
---|
| 596 | # Mostrar salida. |
---|
| 597 | [ -n "$SIZE" ] && echo "$SIZE" |
---|
[739d358] | 598 | } |
---|
| 599 | |
---|
| 600 | |
---|
[d7c35ad] | 601 | #/** |
---|
[b994bc73] | 602 | # ogGetDiskType path_device |
---|
| 603 | #@brief Muestra el tipo de disco (real, RAID, meta-disco, etc.). |
---|
| 604 | #@warning Función en pruebas |
---|
| 605 | #*/ ## |
---|
| 606 | function ogGetDiskType () |
---|
| 607 | { |
---|
| 608 | local DEV MAJOR TYPE |
---|
| 609 | |
---|
| 610 | # Obtener el driver del dispositivo de bloques. |
---|
| 611 | [ -b "$1" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 612 | DEV=${1#/dev/} |
---|
| 613 | MAJOR=$(awk -v D="$DEV" '{if ($4==D) print $1;}' /proc/partitions) |
---|
| 614 | TYPE=$(awk -v D=$MAJOR '/Block/ {bl=1} {if ($1==D&&bl) print toupper($2)}' /proc/devices) |
---|
| 615 | # Devolver mnemónico del driver de dispositivo. |
---|
| 616 | case "$TYPE" in |
---|
| 617 | SD) TYPE="DISK" ;; |
---|
| 618 | SR|IDE*) TYPE="CDROM" ;; # FIXME Comprobar discos IDE. |
---|
| 619 | MD|CCISS*) TYPE="RAID" ;; |
---|
| 620 | DEVICE-MAPPER) TYPE="MAPPER" ;; # FIXME Comprobar LVM y RAID. |
---|
| 621 | esac |
---|
| 622 | echo $TYPE |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | |
---|
| 626 | #/** |
---|
[73488c9] | 627 | # ogGetLastSector int_ndisk [int_npart] |
---|
[6e390b1] | 628 | #@brief Devuelve el último sector usable del disco o de una partición. |
---|
[73488c9] | 629 | #@param int_ndisk nº de orden del disco |
---|
| 630 | #@param int_npart nº de orden de la partición (opcional) |
---|
| 631 | #@return Último sector usable. |
---|
| 632 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 633 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponde con un dispositivo. |
---|
| 634 | #@note Requisitos: sfdisk, sgdisk |
---|
| 635 | #@version 1.0.4 - Primera versión compatible con OpenGnSys. |
---|
| 636 | #@author Universidad de Huelva |
---|
| 637 | #@date 2012/06/03 |
---|
[680f79f] | 638 | #@version 1.1.0 - Usar "hdparm" y "partx" en vez de "sfdisk". |
---|
| 639 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 640 | #@date 2016-05-20 |
---|
[73488c9] | 641 | #*/ ## |
---|
| 642 | function ogGetLastSector () |
---|
| 643 | { |
---|
| 644 | # Variables locales |
---|
[680f79f] | 645 | local DISK PART LASTSECTOR |
---|
[73488c9] | 646 | # Si se solicita, mostrar ayuda. |
---|
| 647 | if [ "$*" == "help" ]; then |
---|
| 648 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk [int_npart]" \ |
---|
| 649 | "$FUNCNAME 1 => 488392064" \ |
---|
| 650 | "$FUNCNAME 1 1 => 102400062" |
---|
| 651 | return |
---|
| 652 | fi |
---|
[680f79f] | 653 | |
---|
| 654 | # Obtener último sector. |
---|
[73488c9] | 655 | case $# in |
---|
[680f79f] | 656 | 1) # Para un disco. |
---|
| 657 | DISK=$(ogDiskToDev $1) || return $? |
---|
[df35dea] | 658 | LASTSECTOR=$[$(hdparm -N $DISK | awk -F"[=/]" '/=/ {print $2}') - 1] |
---|
[73488c9] | 659 | ;; |
---|
[680f79f] | 660 | 2) # Para una partición. |
---|
[73488c9] | 661 | PART=$(ogDiskToDev $1 $2) || return $? |
---|
[680f79f] | 662 | LASTSECTOR=$(partx -gso END $PART) |
---|
[73488c9] | 663 | ;; |
---|
[680f79f] | 664 | *) # Error si se reciben más parámetros. |
---|
| 665 | ogRaiseError $OG_ERR_FORMAT |
---|
[73488c9] | 666 | return $? ;; |
---|
| 667 | esac |
---|
| 668 | echo $LASTSECTOR |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | |
---|
| 672 | #/** |
---|
[42669ebf] | 673 | # ogGetPartitionActive int_ndisk |
---|
[a5df9b9] | 674 | #@brief Muestra que particion de un disco esta marcada como de activa. |
---|
[b9e1a8c] | 675 | #@param int_ndisk nº de orden del disco |
---|
| 676 | #@return int_npart Nº de partición activa |
---|
[a5df9b9] | 677 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 678 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
| 679 | #@note Requisitos: parted |
---|
[59f9ad2] | 680 | #@todo Queda definir formato para atributos (arranque, oculta, ...). |
---|
[afc1e74] | 681 | #@version 0.9 - Primera version compatible con OpenGnSys. |
---|
[a5df9b9] | 682 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[985bef0] | 683 | #@date 2009/09/17 |
---|
[1e7eaab] | 684 | #*/ ## |
---|
[42669ebf] | 685 | function ogGetPartitionActive () |
---|
| 686 | { |
---|
[59f9ad2] | 687 | # Variables locales |
---|
[a5df9b9] | 688 | local DISK |
---|
| 689 | |
---|
[1e7eaab] | 690 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 691 | if [ "$*" == "help" ]; then |
---|
| 692 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1 => 1" |
---|
| 693 | return |
---|
| 694 | fi |
---|
[1e7eaab] | 695 | # Error si no se recibe 1 parámetro. |
---|
[aae34f6] | 696 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[a5df9b9] | 697 | |
---|
[1e7eaab] | 698 | # Comprobar que el disco existe y listar su partición activa. |
---|
[a5df9b9] | 699 | DISK="$(ogDiskToDev $1)" || return $? |
---|
[9ca55ab] | 700 | LANG=C parted -sm $DISK print 2>/dev/null | awk -F: '$7~/boot/ {print $1}' |
---|
[a5df9b9] | 701 | } |
---|
| 702 | |
---|
| 703 | |
---|
| 704 | #/** |
---|
[42669ebf] | 705 | # ogGetPartitionId int_ndisk int_npartition |
---|
[7dada73] | 706 | #@brief Devuelve el mnemónico con el tipo de partición. |
---|
[42669ebf] | 707 | #@param int_ndisk nº de orden del disco |
---|
| 708 | #@param int_npartition nº de orden de la partición |
---|
[9f57de01] | 709 | #@return Identificador de tipo de partición. |
---|
[326cec3] | 710 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
[7dada73] | 711 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponde con un dispositivo. |
---|
[a5df9b9] | 712 | #@note Requisitos: sfdisk |
---|
[7dada73] | 713 | #@version 0.9 - Primera versión compatible con OpenGnSys. |
---|
[9f57de01] | 714 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[0d6e7222] | 715 | #@date 2009-03-25 |
---|
[7dada73] | 716 | #@version 1.0.2 - Detectar partición vacía. |
---|
| 717 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[0d6e7222] | 718 | #@date 2011-12-23 |
---|
| 719 | #@version 1.0.6 - Soportar LVM. |
---|
| 720 | #@author Universidad de Huelva |
---|
| 721 | #@date 2014-09-04 |
---|
| 722 | #@version 1.1.0 - Soportar pool de volúmenes ZFS. |
---|
| 723 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 724 | #@date 2014-11-14 |
---|
[1e7eaab] | 725 | #*/ ## |
---|
[42669ebf] | 726 | function ogGetPartitionId () |
---|
| 727 | { |
---|
[59f9ad2] | 728 | # Variables locales. |
---|
[680f79f] | 729 | local DISK ID |
---|
[2e15649] | 730 | |
---|
[1e7eaab] | 731 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 732 | if [ "$*" == "help" ]; then |
---|
| 733 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 734 | "$FUNCNAME 1 1 => 7" |
---|
| 735 | return |
---|
| 736 | fi |
---|
[1e7eaab] | 737 | # Error si no se reciben 2 parámetros. |
---|
[aae34f6] | 738 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[2e15649] | 739 | |
---|
[680f79f] | 740 | # Detectar y mostrar el id. de tipo de partición. |
---|
[2e15649] | 741 | DISK=$(ogDiskToDev $1) || return $? |
---|
[8baebd4] | 742 | case "$(ogGetPartitionTableType $1)" in |
---|
| 743 | GPT) ID=$(sgdisk -p $DISK 2>/dev/null | awk -v p="$2" '{if ($1==p) print $6;}') || ogRaiseError $OG_ERR_NOTFOUND "$1,$2" || return $? |
---|
[f12cc5d] | 744 | [ "$ID" == "8301" -a "$1 $2" == "$(ogFindCache)" ] && ID=CA00 |
---|
[8baebd4] | 745 | ;; |
---|
| 746 | MSDOS) ID=$(sfdisk --id $DISK $2 2>/dev/null) || ogRaiseError $OG_ERR_NOTFOUND "$1,$2" || return $? ;; |
---|
[0d6e7222] | 747 | LVM) ID=10000 ;; |
---|
| 748 | ZPOOL) ID=10010 ;; |
---|
[8baebd4] | 749 | esac |
---|
[7dada73] | 750 | echo $ID |
---|
[9f29ba6] | 751 | } |
---|
| 752 | |
---|
[a5df9b9] | 753 | |
---|
| 754 | #/** |
---|
[42669ebf] | 755 | # ogGetPartitionSize int_ndisk int_npartition |
---|
[a5df9b9] | 756 | #@brief Muestra el tamano en KB de una particion determinada. |
---|
[42669ebf] | 757 | #@param int_ndisk nº de orden del disco |
---|
| 758 | #@param int_npartition nº de orden de la partición |
---|
| 759 | #@return int_partsize - Tamaño en KB de la partición. |
---|
[a5df9b9] | 760 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 761 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
| 762 | #@note Requisitos: sfdisk, awk |
---|
[985bef0] | 763 | #@version 0.1 - Integracion para Opengnsys - EAC: SizePartition () en ATA.lib |
---|
| 764 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
| 765 | #@date 2008/10/27 |
---|
[afc1e74] | 766 | #@version 0.9 - Primera version para OpenGnSys |
---|
[a5df9b9] | 767 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 768 | #@date 2009/07/24 |
---|
[c01bee2] | 769 | #@version 1.1.0 - Sustituir "sfdisk" por "partx". |
---|
| 770 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[025bd24] | 771 | #@date 2016/05/04 |
---|
[1e7eaab] | 772 | #*/ ## |
---|
[42669ebf] | 773 | function ogGetPartitionSize () |
---|
| 774 | { |
---|
[59f9ad2] | 775 | # Variables locales. |
---|
[c01bee2] | 776 | local PART |
---|
[a5df9b9] | 777 | |
---|
[1e7eaab] | 778 | # Si se solicita, mostrar ayuda. |
---|
[aae34f6] | 779 | if [ "$*" == "help" ]; then |
---|
| 780 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 781 | "$FUNCNAME 1 1 => 10000000" |
---|
| 782 | return |
---|
| 783 | fi |
---|
[1e7eaab] | 784 | # Error si no se reciben 2 parámetros. |
---|
[aae34f6] | 785 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[a5df9b9] | 786 | |
---|
[c01bee2] | 787 | # Devolver tamaño de partición o del sistema de archivos (para ZFS). |
---|
[a5df9b9] | 788 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
[025bd24] | 789 | partx -gbo SIZE $PART 2>/dev/null | awk '{print int($1/1024)}' |
---|
[c01bee2] | 790 | [ ${PIPESTATUS[0]} -ne 0 ] && ogGetFsSize $1 $2 |
---|
[fa93b79] | 791 | return 0 |
---|
[a5df9b9] | 792 | } |
---|
| 793 | |
---|
| 794 | |
---|
[b094c59] | 795 | #/** |
---|
[73488c9] | 796 | # ogGetPartitionsNumber int_ndisk |
---|
| 797 | #@brief Detecta el numero de particiones del disco duro indicado. |
---|
| 798 | #@param int_ndisk nº de orden del disco |
---|
| 799 | #@return Devuelve el numero paritiones del disco duro indicado |
---|
| 800 | #@warning Salidas de errores no determinada |
---|
| 801 | #@attention Requisitos: parted |
---|
| 802 | #@note Notas sin especificar |
---|
| 803 | #@version 0.1 - Integracion para Opengnsys - EAC: DetectNumberPartition () en ATA.lib |
---|
| 804 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 805 | #@date Date: 27/10/2008 |
---|
| 806 | #@version 1.0 - Uso de sfdisk Primera version para OpenGnSys |
---|
| 807 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[0d6e7222] | 808 | #@date 2009-07-24 |
---|
[73488c9] | 809 | #@version 1.0.4 - Uso de /proc/partitions para detectar el numero de particiones |
---|
| 810 | #@author Universidad de Huelva |
---|
[0d6e7222] | 811 | #@date 2012-03-28 |
---|
[95e9664] | 812 | #@version 1.0.6 - Soportar LVM. |
---|
| 813 | #@author Universidad de Huelva |
---|
[0d6e7222] | 814 | #@date 2014-09-04 |
---|
[12d6d5b] | 815 | #@version 1.1.0 - Soportar ZFS y sustituir "sfdisk" por "partx". |
---|
[0d6e7222] | 816 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
[12d6d5b] | 817 | #@date 2016-04-28 |
---|
[73488c9] | 818 | #*/ ## |
---|
| 819 | function ogGetPartitionsNumber () |
---|
| 820 | { |
---|
| 821 | # Variables locales. |
---|
| 822 | local DISK |
---|
| 823 | # Si se solicita, mostrar ayuda. |
---|
| 824 | if [ "$*" == "help" ]; then |
---|
| 825 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
| 826 | "$FUNCNAME 1 => 3" |
---|
| 827 | return |
---|
| 828 | fi |
---|
| 829 | # Error si no se recibe 1 parámetro. |
---|
| 830 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 831 | |
---|
| 832 | # Contar el nº de veces que aparece el disco en su lista de particiones. |
---|
| 833 | DISK=$(ogDiskToDev $1) 2>/dev/null |
---|
[5de6fb0] | 834 | case "$(ogGetPartitionTableType $1)" in |
---|
| 835 | GPT) grep -c "${DISK#/dev/}." /proc/partitions ;; |
---|
[12d6d5b] | 836 | MSDOS) partx -gso NR $DISK 2>/dev/null | awk -v p=0 '{p=$1} END {print p}' ;; |
---|
[95e9664] | 837 | LVM) lvs --noheadings $DISK 2>/dev/null | wc -l ;; |
---|
[9ca55ab] | 838 | ZPOOL) zpool list &>/dev/null || modprobe zfs |
---|
| 839 | zpool import -f -R /mnt -N -a 2>/dev/null |
---|
[0d6e7222] | 840 | zfs list -Hp -o name,canmount,mountpoint -r $(blkid -s LABEL -o value $DISK) | \ |
---|
| 841 | awk '$2=="on" && $3!="none" {c++} |
---|
| 842 | END {print c}' |
---|
| 843 | ;; |
---|
[5de6fb0] | 844 | esac |
---|
[73488c9] | 845 | } |
---|
| 846 | |
---|
| 847 | |
---|
| 848 | #/** |
---|
[60fc799] | 849 | # ogGetPartitionTableType int_ndisk |
---|
| 850 | #@brief Devuelve el tipo de tabla de particiones del disco (GPT o MSDOS) |
---|
| 851 | #@param int_ndisk nº de orden del disco |
---|
| 852 | #@return str_tabletype - Tipo de tabla de paritiones |
---|
| 853 | #@warning Salidas de errores no determinada |
---|
[6e390b1] | 854 | #@note tabletype = { MSDOS, GPT } |
---|
[28aef0b] | 855 | #@note Requisitos: blkid, parted, vgs |
---|
[60fc799] | 856 | #@version 1.0.4 - Primera versión para OpenGnSys |
---|
| 857 | #@author Universidad de Huelva |
---|
| 858 | #@date 2012/03/01 |
---|
[95e9664] | 859 | #@version 1.0.6 - Soportar LVM. |
---|
| 860 | #@author Universidad de Huelva |
---|
[0d6e7222] | 861 | #@date 2014-09-04 |
---|
[880b7fa] | 862 | #@version 1.1.0 - Mejorar rendimiento y soportar ZFS. |
---|
[0d6e7222] | 863 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 864 | #@date 2014-11-14 |
---|
[6e390b1] | 865 | #*/ ## |
---|
[60fc799] | 866 | function ogGetPartitionTableType () |
---|
| 867 | { |
---|
| 868 | # Variables locales. |
---|
[95e9664] | 869 | local DISK TYPE |
---|
[60fc799] | 870 | |
---|
| 871 | # Si se solicita, mostrar ayuda. |
---|
| 872 | if [ "$*" == "help" ]; then |
---|
| 873 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
[6e390b1] | 874 | "$FUNCNAME 1 => MSDOS" |
---|
[60fc799] | 875 | return |
---|
| 876 | fi |
---|
| 877 | # Error si no se recibe 1 parámetro. |
---|
| 878 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 879 | |
---|
| 880 | # Sustituye n de disco por su dispositivo. |
---|
[95e9664] | 881 | DISK=$(ogDiskToDev $1) || return $? |
---|
| 882 | |
---|
| 883 | # Comprobar tabla de particiones. |
---|
[28aef0b] | 884 | if [ -b $DISK ]; then |
---|
| 885 | TYPE=$(parted -sm $DISK print 2>/dev/null | awk -F: -v D=$DISK '{ if($1 == D) print toupper($6)}') |
---|
| 886 | [ -z "$TYPE" ] && TYPE=$(parted -sm $DISK print 2>/dev/null | awk -F: -v D=$DISK '{ if($1 == D) print toupper($6)}') |
---|
| 887 | fi |
---|
[0d6e7222] | 888 | # Comprobar si es volumen lógico. |
---|
[95e9664] | 889 | [ -d $DISK ] && vgs $DISK &>/dev/null && TYPE="LVM" |
---|
[0d6e7222] | 890 | # Comprobar si es pool de ZFS. |
---|
[9ca55ab] | 891 | [ -z "$TYPE" -o "$TYPE" == "UNKNOWN" ] && [ -n "$(blkid -s TYPE $DISK | grep zfs)" ] && TYPE="ZPOOL" |
---|
[95e9664] | 892 | |
---|
| 893 | # Mostrar salida. |
---|
| 894 | [ -n "$TYPE" ] && echo "$TYPE" |
---|
[60fc799] | 895 | } |
---|
| 896 | |
---|
| 897 | |
---|
| 898 | #/** |
---|
[344d6e7] | 899 | # ogGetPartitionType int_ndisk int_npartition |
---|
[5804229] | 900 | #@brief Devuelve el mnemonico con el tipo de partición. |
---|
| 901 | #@param int_ndisk nº de orden del disco |
---|
| 902 | #@param int_npartition nº de orden de la partición |
---|
| 903 | #@return Mnemonico |
---|
[be48687] | 904 | #@note Mnemonico: valor devuelto por ogIdToType. |
---|
[5804229] | 905 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
[824b0dd] | 906 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
| 907 | #@version 0.1 - Integracion para Opengnsys - EAC: TypeFS() en ATA.lib |
---|
[5804229] | 908 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 909 | #@date 2008-10-27 |
---|
| 910 | #@version 0.9 - Primera adaptacion para OpenGnSys. |
---|
| 911 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 912 | #@date 2009-07-21 |
---|
| 913 | #@version 1.0.3 - Código trasladado de antigua función ogGetFsType. |
---|
| 914 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 915 | #@date 2011-12-01 |
---|
[be48687] | 916 | #@version 1.0.5 - Usar función ogIdToType para hacer la conversión id. a tipo. |
---|
| 917 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 918 | #@date 2013-09-19 |
---|
[344d6e7] | 919 | #*/ ## |
---|
| 920 | function ogGetPartitionType () |
---|
| 921 | { |
---|
[5804229] | 922 | # Variables locales. |
---|
| 923 | local ID TYPE |
---|
| 924 | |
---|
| 925 | # Si se solicita, mostrar ayuda. |
---|
| 926 | if [ "$*" == "help" ]; then |
---|
| 927 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 928 | "$FUNCNAME 1 1 => NTFS" |
---|
| 929 | return |
---|
| 930 | fi |
---|
| 931 | # Error si no se reciben 2 parámetros. |
---|
| 932 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 933 | |
---|
| 934 | # Detectar id. de tipo de partición y codificar al mnemonico. |
---|
| 935 | ID=$(ogGetPartitionId "$1" "$2") || return $? |
---|
[b663655] | 936 | TYPE=$(ogIdToType "$ID") |
---|
[5804229] | 937 | echo "$TYPE" |
---|
[344d6e7] | 938 | } |
---|
| 939 | |
---|
| 940 | |
---|
| 941 | #/** |
---|
[b09d0fa] | 942 | # ogHidePartition int_ndisk int_npartition |
---|
| 943 | #@brief Oculta un apartición visible. |
---|
| 944 | #@param int_ndisk nº de orden del disco |
---|
| 945 | #@param int_npartition nº de orden de la partición |
---|
| 946 | #@return (nada) |
---|
| 947 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
[053993f] | 948 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
[b09d0fa] | 949 | #@exception OG_ERR_PARTITION tipo de partición no reconocido. |
---|
| 950 | #@version 1.0 - Versión en pruebas. |
---|
| 951 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 952 | #@date 2010/01/12 |
---|
| 953 | #*/ ## |
---|
| 954 | function ogHidePartition () |
---|
| 955 | { |
---|
| 956 | # Variables locales. |
---|
| 957 | local PART TYPE NEWTYPE |
---|
| 958 | # Si se solicita, mostrar ayuda. |
---|
| 959 | if [ "$*" == "help" ]; then |
---|
| 960 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 961 | "$FUNCNAME 1 1" |
---|
| 962 | return |
---|
| 963 | fi |
---|
| 964 | # Error si no se reciben 2 parámetros. |
---|
| 965 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 966 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
| 967 | |
---|
| 968 | # Obtener tipo de partición. |
---|
| 969 | TYPE=$(ogGetPartitionType "$1" "$2") |
---|
| 970 | case "$TYPE" in |
---|
| 971 | NTFS) NEWTYPE="HNTFS" ;; |
---|
| 972 | FAT32) NEWTYPE="HFAT32" ;; |
---|
| 973 | FAT16) NEWTYPE="HFAT16" ;; |
---|
| 974 | FAT12) NEWTYPE="HFAT12" ;; |
---|
| 975 | *) ogRaiseError $OG_ERR_PARTITION "$TYPE" |
---|
| 976 | return $? ;; |
---|
| 977 | esac |
---|
| 978 | # Cambiar tipo de partición. |
---|
[ec6de25] | 979 | ogSetPartitionType $1 $2 $NEWTYPE |
---|
[b09d0fa] | 980 | } |
---|
| 981 | |
---|
| 982 | |
---|
| 983 | #/** |
---|
[afc1e74] | 984 | # ogIdToType int_idpart |
---|
| 985 | #@brief Devuelve el identificador correspondiente a un tipo de partición. |
---|
| 986 | #@param int_idpart identificador de tipo de partición. |
---|
| 987 | #@return str_parttype mnemónico de tipo de partición. |
---|
| 988 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 989 | #@version 1.0.5 - Primera version para OpenGnSys |
---|
| 990 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 991 | #@date 2013-02-07 |
---|
| 992 | #*/ ## |
---|
| 993 | function ogIdToType () |
---|
| 994 | { |
---|
| 995 | # Variables locales |
---|
| 996 | local ID TYPE |
---|
| 997 | |
---|
| 998 | # Si se solicita, mostrar ayuda. |
---|
| 999 | if [ "$*" == "help" ]; then |
---|
| 1000 | ogHelp "$FUNCNAME" "$FUNCNAME int_idpart" \ |
---|
| 1001 | "$FUNCNAME 83 => LINUX" |
---|
| 1002 | return |
---|
| 1003 | fi |
---|
| 1004 | # Error si no se recibe 1 parámetro. |
---|
| 1005 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1006 | |
---|
| 1007 | # Obtener valor hexadecimal de 4 caracteres rellenado con 0 por delante. |
---|
| 1008 | ID=$(printf "%4s" "$1" | tr ' ' '0') |
---|
| 1009 | case "${ID,,}" in |
---|
| 1010 | 0000) TYPE="EMPTY" ;; |
---|
| 1011 | 0001) TYPE="FAT12" ;; |
---|
| 1012 | 0005|000f) TYPE="EXTENDED" ;; |
---|
| 1013 | 0006|000e) TYPE="FAT16" ;; |
---|
| 1014 | 0007) TYPE="NTFS" ;; |
---|
| 1015 | 000b|000c) TYPE="FAT32" ;; |
---|
| 1016 | 0011) TYPE="HFAT12" ;; |
---|
| 1017 | 0012) TYPE="COMPAQDIAG" ;; |
---|
| 1018 | 0016|001e) TYPE="HFAT16" ;; |
---|
| 1019 | 0017) TYPE="HNTFS" ;; |
---|
| 1020 | 001b|001c) TYPE="HFAT32" ;; |
---|
| 1021 | 0042) TYPE="WIN-DYNAMIC" ;; |
---|
| 1022 | 0082|8200) TYPE="LINUX-SWAP" ;; |
---|
| 1023 | 0083|8300) TYPE="LINUX" ;; |
---|
| 1024 | 008e|8E00) TYPE="LINUX-LVM" ;; |
---|
[b663655] | 1025 | 00a5|a503) TYPE="FREEBSD" ;; |
---|
[afc1e74] | 1026 | 00a6) TYPE="OPENBSD" ;; |
---|
| 1027 | 00a7) TYPE="CACHE" ;; # (compatibilidad con Brutalix) |
---|
| 1028 | 00af|af00) TYPE="HFS" ;; |
---|
| 1029 | 00be|be00) TYPE="SOLARIS-BOOT" ;; |
---|
| 1030 | 00bf|bf0[0145]) TYPE="SOLARIS" ;; |
---|
| 1031 | 00ca|ca00) TYPE="CACHE" ;; |
---|
| 1032 | 00da) TYPE="DATA" ;; |
---|
| 1033 | 00ee) TYPE="GPT" ;; |
---|
| 1034 | 00ef|ef00) TYPE="EFI" ;; |
---|
| 1035 | 00fb) TYPE="VMFS" ;; |
---|
| 1036 | 00fd|fd00) TYPE="LINUX-RAID" ;; |
---|
| 1037 | 0700) TYPE="WINDOWS" ;; |
---|
| 1038 | 0c01) TYPE="WIN-RESERV" ;; |
---|
| 1039 | 7f00) TYPE="CHROMEOS-KRN" ;; |
---|
| 1040 | 7f01) TYPE="CHROMEOS" ;; |
---|
| 1041 | 7f02) TYPE="CHROMEOS-RESERV" ;; |
---|
| 1042 | 8301) TYPE="LINUX-RESERV" ;; |
---|
| 1043 | a500) TYPE="FREEBSD-DISK" ;; |
---|
| 1044 | a501) TYPE="FREEBSD-BOOT" ;; |
---|
| 1045 | a502) TYPE="FREEBSD-SWAP" ;; |
---|
[b663655] | 1046 | ab00) TYPE="HFS-BOOT" ;; |
---|
[afc1e74] | 1047 | af01) TYPE="HFS-RAID" ;; |
---|
| 1048 | bf02) TYPE="SOLARIS-SWAP" ;; |
---|
| 1049 | bf03) TYPE="SOLARIS-DISK" ;; |
---|
| 1050 | ef01) TYPE="MBR" ;; |
---|
| 1051 | ef02) TYPE="BIOS-BOOT" ;; |
---|
| 1052 | *) TYPE="UNKNOWN" ;; |
---|
| 1053 | esac |
---|
| 1054 | echo "$TYPE" |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | |
---|
[858b1b0] | 1058 | # ogIsDiskLocked int_ndisk |
---|
| 1059 | #@brief Comprueba si un disco está bloqueado por una operación de uso exclusivo. |
---|
| 1060 | #@param int_ndisk nº de orden del disco |
---|
| 1061 | #@return Código de salida: 0 - bloqueado, 1 - sin bloquear o error. |
---|
| 1062 | #@note Los ficheros de bloqueo se localizan en \c /var/lock/dev, siendo \c dev el dispositivo de la partición o de su disco, sustituyendo el carácter "/" por "-". |
---|
| 1063 | #@version 1.1.0 - Primera versión para OpenGnSys. |
---|
| 1064 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1065 | #@date 2016-04-08 |
---|
| 1066 | #*/ ## |
---|
| 1067 | function ogIsDiskLocked () |
---|
| 1068 | { |
---|
| 1069 | # Variables locales |
---|
| 1070 | local DISK LOCKFILE |
---|
| 1071 | |
---|
| 1072 | # Si se solicita, mostrar ayuda. |
---|
| 1073 | if [ "$*" == "help" ]; then |
---|
| 1074 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
| 1075 | "if $FUNCNAME 1; then ... ; fi" |
---|
| 1076 | return |
---|
| 1077 | fi |
---|
| 1078 | # Falso, en caso de error. |
---|
| 1079 | [ $# == 1 ] || return 1 |
---|
| 1080 | DISK="$(ogDiskToDev $1 2>/dev/null)" || return 1 |
---|
| 1081 | |
---|
| 1082 | # Comprobar existencia de fichero de bloqueo para el disco. |
---|
| 1083 | LOCKFILE="/var/lock/lock${DISK//\//-}" |
---|
| 1084 | test -f $LOCKFILE |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | |
---|
[afc1e74] | 1088 | #/** |
---|
[73c8417] | 1089 | # ogListPartitions int_ndisk |
---|
[a5df9b9] | 1090 | #@brief Lista las particiones definidas en un disco. |
---|
[42669ebf] | 1091 | #@param int_ndisk nº de orden del disco |
---|
| 1092 | #@return str_parttype:int_partsize ... |
---|
[a5df9b9] | 1093 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 1094 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
| 1095 | #@note Requisitos: \c parted \c awk |
---|
[73c8417] | 1096 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
[59f9ad2] | 1097 | #@attention Las tuplas de valores están separadas por espacios. |
---|
[afc1e74] | 1098 | #@version 0.9 - Primera versión para OpenGnSys |
---|
[a5df9b9] | 1099 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1100 | #@date 2009/07/24 |
---|
[1e7eaab] | 1101 | #*/ ## |
---|
[42669ebf] | 1102 | function ogListPartitions () |
---|
| 1103 | { |
---|
[59f9ad2] | 1104 | # Variables locales. |
---|
[55ad138c] | 1105 | local DISK PART NPARTS TYPE SIZE |
---|
[aae34f6] | 1106 | |
---|
[42669ebf] | 1107 | # Si se solicita, mostrar ayuda. |
---|
[1a7130a] | 1108 | if [ "$*" == "help" ]; then |
---|
[aae34f6] | 1109 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
[73c8417] | 1110 | "$FUNCNAME 1 => NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
[aae34f6] | 1111 | return |
---|
| 1112 | fi |
---|
[42669ebf] | 1113 | # Error si no se recibe 1 parámetro. |
---|
[5dbb046] | 1114 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $? |
---|
[a5df9b9] | 1115 | |
---|
[42669ebf] | 1116 | # Procesar la salida de \c parted . |
---|
[b094c59] | 1117 | DISK="$(ogDiskToDev $1)" || return $? |
---|
[3543b3e] | 1118 | NPARTS=$(ogGetPartitionsNumber $1) |
---|
| 1119 | for (( PART = 1; PART <= NPARTS; PART++ )); do |
---|
[5804229] | 1120 | TYPE=$(ogGetPartitionType $1 $PART 2>/dev/null) |
---|
[3543b3e] | 1121 | if [ $? -eq 0 ]; then |
---|
[55ad138c] | 1122 | SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null) |
---|
| 1123 | echo -n "$TYPE:$SIZE " |
---|
[3543b3e] | 1124 | else |
---|
| 1125 | echo -n "EMPTY:0 " |
---|
| 1126 | fi |
---|
[a5df9b9] | 1127 | done |
---|
| 1128 | echo |
---|
| 1129 | } |
---|
| 1130 | |
---|
[326cec3] | 1131 | |
---|
| 1132 | #/** |
---|
[55ad138c] | 1133 | # ogListPrimaryPartitions int_ndisk |
---|
[942dfd7] | 1134 | #@brief Metafunción que lista las particiones primarias no vacías de un disco. |
---|
[42669ebf] | 1135 | #@param int_ndisk nº de orden del disco |
---|
[55ad138c] | 1136 | #@see ogListPartitions |
---|
[1e7eaab] | 1137 | #*/ ## |
---|
[42669ebf] | 1138 | function ogListPrimaryPartitions () |
---|
| 1139 | { |
---|
[55ad138c] | 1140 | # Variables locales. |
---|
[942dfd7] | 1141 | local PTTYPE PARTS |
---|
[55ad138c] | 1142 | |
---|
[942dfd7] | 1143 | PTTYPE=$(ogGetPartitionTableType $1) || return $? |
---|
[55ad138c] | 1144 | PARTS=$(ogListPartitions "$@") || return $? |
---|
[942dfd7] | 1145 | case "$PTTYPE" in |
---|
| 1146 | GPT) echo $PARTS | sed 's/\( EMPTY:0\)*$//' ;; |
---|
| 1147 | MSDOS) echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//' ;; |
---|
| 1148 | esac |
---|
[55ad138c] | 1149 | } |
---|
| 1150 | |
---|
| 1151 | |
---|
| 1152 | #/** |
---|
| 1153 | # ogListLogicalPartitions int_ndisk |
---|
[942dfd7] | 1154 | #@brief Metafunción que lista las particiones lógicas de una tabla tipo MSDOS. |
---|
[42669ebf] | 1155 | #@param int_ndisk nº de orden del disco |
---|
[55ad138c] | 1156 | #@see ogListPartitions |
---|
[1e7eaab] | 1157 | #*/ ## |
---|
[b061ad0] | 1158 | function ogListLogicalPartitions () |
---|
| 1159 | { |
---|
[55ad138c] | 1160 | # Variables locales. |
---|
[942dfd7] | 1161 | local PTTYPE PARTS |
---|
[55ad138c] | 1162 | |
---|
[942dfd7] | 1163 | PTTYPE=$(ogGetPartitionTableType $1) || return $? |
---|
| 1164 | [ "$PTTYPE" == "MSDOS" ] || ogRaiseError $OG_ERR_PARTITION "" || return $? |
---|
[55ad138c] | 1165 | PARTS=$(ogListPartitions "$@") || return $? |
---|
| 1166 | echo $PARTS | cut -sf5- -d" " |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | |
---|
| 1170 | #/** |
---|
[01d4253] | 1171 | # ogLockDisk int_ndisk |
---|
| 1172 | #@brief Genera un fichero de bloqueo para un disco en uso exlusivo. |
---|
| 1173 | #@param int_ndisk nº de orden del disco |
---|
| 1174 | #@return (nada) |
---|
| 1175 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 1176 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
| 1177 | #@note El fichero de bloqueo se localiza en \c /var/lock/disk, siendo \c disk el dispositivo del disco, sustituyendo el carácter "/" por "-". |
---|
| 1178 | #@version 1.1.0 - Primera versión para OpenGnSys. |
---|
| 1179 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1180 | #@date 2016-04-07 |
---|
| 1181 | #*/ ## |
---|
| 1182 | function ogLockDisk () |
---|
| 1183 | { |
---|
| 1184 | # Variables locales |
---|
| 1185 | local DISK LOCKFILE |
---|
| 1186 | |
---|
| 1187 | # Si se solicita, mostrar ayuda. |
---|
| 1188 | if [ "$*" == "help" ]; then |
---|
| 1189 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
| 1190 | "$FUNCNAME 1" |
---|
| 1191 | return |
---|
| 1192 | fi |
---|
| 1193 | # Error si no se recibe 1 parámetro. |
---|
| 1194 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1195 | |
---|
| 1196 | # Obtener partición. |
---|
| 1197 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 1198 | |
---|
| 1199 | # Crear archivo de bloqueo exclusivo. |
---|
| 1200 | LOCKFILE="/var/lock/lock${DISK//\//-}" |
---|
| 1201 | touch $LOCKFILE |
---|
| 1202 | } |
---|
| 1203 | |
---|
| 1204 | |
---|
| 1205 | #/** |
---|
[42669ebf] | 1206 | # ogSetPartitionActive int_ndisk int_npartition |
---|
[89403cd] | 1207 | #@brief Establece cual es la partición activa de un disco. |
---|
[42669ebf] | 1208 | #@param int_ndisk nº de orden del disco |
---|
| 1209 | #@param int_npartition nº de orden de la partición |
---|
| 1210 | #@return (nada). |
---|
[326cec3] | 1211 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 1212 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
| 1213 | #@note Requisitos: parted |
---|
[985bef0] | 1214 | #@version 0.1 - Integracion para Opengnsys - EAC: SetPartitionActive() en ATA.lib |
---|
| 1215 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
| 1216 | #@date 2008/10/27 |
---|
[afc1e74] | 1217 | #@version 0.9 - Primera version compatible con OpenGnSys. |
---|
[326cec3] | 1218 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1219 | #@date 2009/09/17 |
---|
[1e7eaab] | 1220 | #*/ ## |
---|
[42669ebf] | 1221 | function ogSetPartitionActive () |
---|
| 1222 | { |
---|
[326cec3] | 1223 | # Variables locales |
---|
| 1224 | local DISK PART |
---|
| 1225 | |
---|
[1e7eaab] | 1226 | # Si se solicita, mostrar ayuda. |
---|
[326cec3] | 1227 | if [ "$*" == "help" ]; then |
---|
| 1228 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 1229 | "$FUNCNAME 1 1" |
---|
| 1230 | return |
---|
| 1231 | fi |
---|
[1e7eaab] | 1232 | # Error si no se reciben 2 parámetros. |
---|
[326cec3] | 1233 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1234 | |
---|
[1e7eaab] | 1235 | # Comprobar que el disco existe y activar la partición indicada. |
---|
[326cec3] | 1236 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 1237 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
| 1238 | parted -s $DISK set $2 boot on 2>/dev/null |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | |
---|
[1553fc7] | 1242 | #/** |
---|
[ec6de25] | 1243 | # ogSetPartitionId int_ndisk int_npartition hex_partid |
---|
[5af5d5f] | 1244 | #@brief Cambia el identificador de la partición. |
---|
| 1245 | #@param int_ndisk nº de orden del disco |
---|
| 1246 | #@param int_npartition nº de orden de la partición |
---|
[ec6de25] | 1247 | #@param hex_partid identificador de tipo de partición |
---|
[5af5d5f] | 1248 | #@return (nada) |
---|
[ec6de25] | 1249 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 1250 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
| 1251 | #@exception OG_ERR_OUTOFLIMIT Valor no válido. |
---|
| 1252 | #@exception OG_ERR_PARTITION Error al cambiar el id. de partición. |
---|
[5af5d5f] | 1253 | #@attention Requisitos: fdisk, sgdisk |
---|
| 1254 | #@version 0.1 - Integracion para Opengnsys - SetPartitionType() en ATA.lib |
---|
| 1255 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 1256 | #@date 2008/10/27 |
---|
| 1257 | #@version 1.0.4 - Soporte para discos GPT. |
---|
| 1258 | #@author Universidad de Huelva |
---|
| 1259 | #@date 2012/03/13 |
---|
[ec6de25] | 1260 | #@version 1.0.5 - Utiliza el id. de tipo de partición (no el mnemónico) |
---|
| 1261 | #@author Universidad de Huelva |
---|
[0a693d3] | 1262 | #@date 2012/05/14 |
---|
[5af5d5f] | 1263 | #*/ ## |
---|
[6e390b1] | 1264 | function ogSetPartitionId () |
---|
| 1265 | { |
---|
[5af5d5f] | 1266 | # Variables locales |
---|
| 1267 | local DISK PART PTTYPE ID |
---|
| 1268 | |
---|
| 1269 | # Si se solicita, mostrar ayuda. |
---|
| 1270 | if [ "$*" == "help" ]; then |
---|
[8ca8f5e] | 1271 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition hex_partid" \ |
---|
| 1272 | "$FUNCNAME 1 1 7" |
---|
[5af5d5f] | 1273 | return |
---|
| 1274 | fi |
---|
| 1275 | # Error si no se reciben 3 parámetros. |
---|
| 1276 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1277 | |
---|
[ec6de25] | 1278 | # Sustituye nº de disco y nº partición por su dispositivo. |
---|
| 1279 | DISK=$(ogDiskToDev $1) || return $? |
---|
| 1280 | PART=$(ogDiskToDev $1 $2) || return $? |
---|
| 1281 | # Error si el id. de partición no es hexadecimal. |
---|
| 1282 | ID="${3^^}" |
---|
| 1283 | [[ "$ID" =~ ^[0-9A-F]+$ ]] || ogRaiseError $OG_ERR_OUTOFLIMIT "$3" || return $? |
---|
[5af5d5f] | 1284 | |
---|
| 1285 | # Elección del tipo de partición. |
---|
| 1286 | PTTYPE=$(ogGetPartitionTableType $1) |
---|
| 1287 | case "$PTTYPE" in |
---|
[0a693d3] | 1288 | GPT) sgdisk -t$2:$ID $DISK 2>/dev/null ;; |
---|
[1f2f1e2] | 1289 | MSDOS) echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK | grep Syncing &>/dev/null ;; |
---|
[ec6de25] | 1290 | *) ogRaiseError $OG_ERR_OUTOFLIMIT "$1,$PTTYPE" |
---|
| 1291 | return $? ;; |
---|
[5af5d5f] | 1292 | esac |
---|
[1f2f1e2] | 1293 | |
---|
| 1294 | # MSDOS) Correcto si fdisk sin error o con error pero realiza Syncing |
---|
| 1295 | if [ "${PIPESTATUS[1]}" == "0" -o $? -eq 0 ]; then |
---|
[ec6de25] | 1296 | partprobe $DISK 2>/dev/null |
---|
[1f2f1e2] | 1297 | return 0 |
---|
[ec6de25] | 1298 | else |
---|
| 1299 | ogRaiseError $OG_ERR_PARTITION "$1,$2,$3" |
---|
| 1300 | return $? |
---|
| 1301 | fi |
---|
[5af5d5f] | 1302 | } |
---|
| 1303 | |
---|
| 1304 | |
---|
| 1305 | #/** |
---|
[42669ebf] | 1306 | # ogSetPartitionSize int_ndisk int_npartition int_size |
---|
[2ecd096] | 1307 | #@brief Muestra el tamano en KB de una particion determinada. |
---|
[5af5d5f] | 1308 | #@param int_ndisk nº de orden del disco |
---|
[42669ebf] | 1309 | #@param int_npartition nº de orden de la partición |
---|
| 1310 | #@param int_size tamaño de la partición (en KB) |
---|
[2ecd096] | 1311 | #@return (nada) |
---|
| 1312 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 1313 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
| 1314 | #@note Requisitos: sfdisk, awk |
---|
| 1315 | #@todo Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición. |
---|
[afc1e74] | 1316 | #@version 0.9 - Primera versión para OpenGnSys |
---|
[2ecd096] | 1317 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1318 | #@date 2009/07/24 |
---|
[1e7eaab] | 1319 | #*/ ## |
---|
[42669ebf] | 1320 | function ogSetPartitionSize () |
---|
| 1321 | { |
---|
[2ecd096] | 1322 | # Variables locales. |
---|
| 1323 | local DISK PART SIZE |
---|
| 1324 | |
---|
[1e7eaab] | 1325 | # Si se solicita, mostrar ayuda. |
---|
[2ecd096] | 1326 | if [ "$*" == "help" ]; then |
---|
[311532f] | 1327 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition int_size" \ |
---|
[2ecd096] | 1328 | "$FUNCNAME 1 1 10000000" |
---|
| 1329 | return |
---|
| 1330 | fi |
---|
[1e7eaab] | 1331 | # Error si no se reciben 3 parámetros. |
---|
[2ecd096] | 1332 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1333 | |
---|
[1e7eaab] | 1334 | # Obtener el tamaño de la partición. |
---|
[2ecd096] | 1335 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 1336 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
| 1337 | # Convertir tamaño en KB a sectores de 512 B. |
---|
| 1338 | SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
[3915005] | 1339 | # Redefinir el tamaño de la partición. |
---|
[1c04494] | 1340 | sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
[942dfd7] | 1341 | partprobe $DISK 2>/dev/null |
---|
[2ecd096] | 1342 | } |
---|
| 1343 | |
---|
[5af5d5f] | 1344 | |
---|
[b09d0fa] | 1345 | #/** |
---|
[ec6de25] | 1346 | # ogSetPartitionType int_ndisk int_npartition str_type |
---|
| 1347 | #@brief Cambia el identificador de la partición. |
---|
| 1348 | #@param int_ndisk nº de orden del disco |
---|
| 1349 | #@param int_npartition nº de orden de la partición |
---|
[8ca8f5e] | 1350 | #@param str_type mnemónico de tipo de partición |
---|
[ec6de25] | 1351 | #@return (nada) |
---|
| 1352 | #@attention Requisitos: fdisk, sgdisk |
---|
| 1353 | #@version 0.1 - Integracion para Opengnsys - SetPartitionType() en ATA.lib |
---|
| 1354 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 1355 | #@date 2008/10/27 |
---|
| 1356 | #@version 1.0.4 - Soporte para discos GPT. |
---|
| 1357 | #@author Universidad de Huelva |
---|
| 1358 | #@date 2012/03/13 |
---|
| 1359 | #@version 1.0.5 - Renombrada de ogSetPartitionId. |
---|
| 1360 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1361 | #@date 2013/03/07 |
---|
| 1362 | #*/ ## |
---|
| 1363 | function ogSetPartitionType () |
---|
| 1364 | { |
---|
| 1365 | # Variables locales |
---|
| 1366 | local DISK PART PTTYPE ID |
---|
| 1367 | |
---|
| 1368 | # Si se solicita, mostrar ayuda. |
---|
| 1369 | if [ "$*" == "help" ]; then |
---|
| 1370 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition str_type" \ |
---|
| 1371 | "$FUNCNAME 1 1 NTFS" |
---|
| 1372 | return |
---|
| 1373 | fi |
---|
| 1374 | # Error si no se reciben 3 parámetros. |
---|
| 1375 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1376 | |
---|
| 1377 | # Sustituye nº de disco por su dispositivo. |
---|
| 1378 | DISK=`ogDiskToDev $1` || return $? |
---|
| 1379 | PART=`ogDiskToDev $1 $2` || return $? |
---|
| 1380 | |
---|
| 1381 | # Elección del tipo de partición. |
---|
| 1382 | PTTYPE=$(ogGetPartitionTableType $1) |
---|
| 1383 | ID=$(ogTypeToId "$3" "$PTTYPE") |
---|
| 1384 | [ -n "$ID" ] || ogRaiseError $OG_ERR_FORMAT "$3,$PTTYPE" || return $? |
---|
| 1385 | ogSetPartitionId $1 $2 $ID |
---|
| 1386 | } |
---|
| 1387 | |
---|
| 1388 | |
---|
| 1389 | #/** |
---|
[afc1e74] | 1390 | # ogTypeToId str_parttype [str_tabletype] |
---|
| 1391 | #@brief Devuelve el identificador correspondiente a un tipo de partición. |
---|
| 1392 | #@param str_parttype mnemónico de tipo de partición. |
---|
| 1393 | #@param str_tabletype mnemónico de tipo de tabla de particiones (MSDOS por defecto). |
---|
| 1394 | #@return int_idpart identificador de tipo de partición. |
---|
| 1395 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 1396 | #@note tabletype = { MSDOS, GPT }, (MSDOS, por defecto) |
---|
| 1397 | #@version 0.1 - Integracion para Opengnsys - EAC: TypeFS () en ATA.lib |
---|
| 1398 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
| 1399 | #@date 2008/10/27 |
---|
| 1400 | #@version 0.9 - Primera version para OpenGnSys |
---|
| 1401 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
| 1402 | #@date 2009-12-14 |
---|
| 1403 | #@version 1.0.4 - Soportar discos GPT (sustituye a ogFsToId). |
---|
| 1404 | #@author Universidad de Huelva |
---|
| 1405 | #@date 2012/03/30 |
---|
| 1406 | #*/ ## |
---|
| 1407 | function ogTypeToId () |
---|
| 1408 | { |
---|
| 1409 | # Variables locales |
---|
| 1410 | local PTTYPE ID |
---|
| 1411 | |
---|
| 1412 | # Si se solicita, mostrar ayuda. |
---|
| 1413 | if [ "$*" == "help" ]; then |
---|
| 1414 | ogHelp "$FUNCNAME" "$FUNCNAME str_parttype [str_tabletype]" \ |
---|
| 1415 | "$FUNCNAME LINUX => 83" \ |
---|
| 1416 | "$FUNCNAME LINUX MSDOS => 83" |
---|
| 1417 | return |
---|
| 1418 | fi |
---|
| 1419 | # Error si no se reciben 1 o 2 parámetros. |
---|
| 1420 | [ $# -lt 1 -o $# -gt 2 ] && (ogRaiseError $OG_ERR_FORMAT; return $?) |
---|
| 1421 | |
---|
| 1422 | # Asociar id. de partición para su mnemónico. |
---|
| 1423 | PTTYPE=${2:-"MSDOS"} |
---|
| 1424 | case "$PTTYPE" in |
---|
| 1425 | GPT) # Se incluyen mnemónicos compatibles con tablas MSDOS. |
---|
| 1426 | case "$1" in |
---|
| 1427 | EMPTY) ID=0 ;; |
---|
| 1428 | WINDOWS|NTFS|EXFAT|FAT32|FAT16|FAT12|HNTFS|HFAT32|HFAT16|HFAT12) |
---|
| 1429 | ID=0700 ;; |
---|
| 1430 | WIN-RESERV) ID=0C01 ;; |
---|
| 1431 | CHROMEOS-KRN) ID=7F00 ;; |
---|
| 1432 | CHROMEOS) ID=7F01 ;; |
---|
| 1433 | CHROMEOS-RESERV) ID=7F02 ;; |
---|
| 1434 | LINUX-SWAP) ID=8200 ;; |
---|
| 1435 | LINUX|EXT[234]|REISERFS|REISER4|XFS|JFS) |
---|
| 1436 | ID=8300 ;; |
---|
| 1437 | LINUX-RESERV) ID=8301 ;; |
---|
| 1438 | LINUX-LVM) ID=8E00 ;; |
---|
| 1439 | FREEBSD-DISK) ID=A500 ;; |
---|
| 1440 | FREEBSD-BOOT) ID=A501 ;; |
---|
| 1441 | FREEBSD-SWAP) ID=A502 ;; |
---|
| 1442 | FREEBSD) ID=A503 ;; |
---|
[b663655] | 1443 | HFS-BOOT) ID=AB00 ;; |
---|
[afc1e74] | 1444 | HFS|HFS+) ID=AF00 ;; |
---|
[1cbf9e0] | 1445 | HFSPLUS) ID=AF00 ;; |
---|
[afc1e74] | 1446 | HFS-RAID) ID=AF01 ;; |
---|
| 1447 | SOLARIS-BOOT) ID=BE00 ;; |
---|
| 1448 | SOLARIS) ID=BF00 ;; |
---|
| 1449 | SOLARIS-SWAP) ID=BF02 ;; |
---|
| 1450 | SOLARIS-DISK) ID=BF03 ;; |
---|
| 1451 | CACHE) ID=CA00;; |
---|
| 1452 | EFI) ID=EF00 ;; |
---|
| 1453 | LINUX-RAID) ID=FD00 ;; |
---|
| 1454 | *) ID="" ;; |
---|
| 1455 | esac |
---|
| 1456 | ;; |
---|
| 1457 | MSDOS) |
---|
| 1458 | case "$1" in |
---|
| 1459 | EMPTY) ID=0 ;; |
---|
| 1460 | FAT12) ID=1 ;; |
---|
| 1461 | EXTENDED) ID=5 ;; |
---|
| 1462 | FAT16) ID=6 ;; |
---|
| 1463 | WINDOWS|NTFS|EXFAT) |
---|
| 1464 | ID=7 ;; |
---|
| 1465 | FAT32) ID=b ;; |
---|
| 1466 | HFAT12) ID=11 ;; |
---|
| 1467 | HFAT16) ID=16 ;; |
---|
| 1468 | HNTFS) ID=17 ;; |
---|
| 1469 | HFAT32) ID=1b ;; |
---|
| 1470 | LINUX-SWAP) ID=82 ;; |
---|
| 1471 | LINUX|EXT[234]|REISERFS|REISER4|XFS|JFS) |
---|
| 1472 | ID=83 ;; |
---|
| 1473 | LINUX-LVM) ID=8e ;; |
---|
| 1474 | FREEBSD) ID=a5 ;; |
---|
| 1475 | OPENBSD) ID=a6 ;; |
---|
| 1476 | HFS|HFS+) ID=af ;; |
---|
| 1477 | SOLARIS-BOOT) ID=be ;; |
---|
| 1478 | SOLARIS) ID=bf ;; |
---|
| 1479 | CACHE) ID=ca ;; |
---|
| 1480 | DATA) ID=da ;; |
---|
| 1481 | GPT) ID=ee ;; |
---|
| 1482 | EFI) ID=ef ;; |
---|
| 1483 | VMFS) ID=fb ;; |
---|
| 1484 | LINUX-RAID) ID=fd ;; |
---|
| 1485 | *) ID="" ;; |
---|
| 1486 | esac |
---|
| 1487 | ;; |
---|
| 1488 | esac |
---|
| 1489 | echo $ID |
---|
| 1490 | } |
---|
| 1491 | |
---|
| 1492 | |
---|
| 1493 | #/** |
---|
[b09d0fa] | 1494 | # ogUnhidePartition int_ndisk int_npartition |
---|
| 1495 | #@brief Hace visible una partición oculta. |
---|
| 1496 | #@param int_ndisk nº de orden del disco |
---|
| 1497 | #@param int_npartition nº de orden de la partición |
---|
| 1498 | #@return (nada) |
---|
| 1499 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
[053993f] | 1500 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
[b09d0fa] | 1501 | #@exception OG_ERR_PARTITION tipo de partición no reconocido. |
---|
| 1502 | #@version 1.0 - Versión en pruebas. |
---|
| 1503 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1504 | #@date 2010/01/12 |
---|
| 1505 | #*/ ## |
---|
| 1506 | function ogUnhidePartition () |
---|
| 1507 | { |
---|
| 1508 | # Variables locales. |
---|
| 1509 | local PART TYPE NEWTYPE |
---|
| 1510 | # Si se solicita, mostrar ayuda. |
---|
| 1511 | if [ "$*" == "help" ]; then |
---|
| 1512 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
| 1513 | "$FUNCNAME 1 1" |
---|
| 1514 | return |
---|
| 1515 | fi |
---|
| 1516 | # Error si no se reciben 2 parámetros. |
---|
| 1517 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1518 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
| 1519 | |
---|
| 1520 | # Obtener tipo de partición. |
---|
| 1521 | TYPE=$(ogGetPartitionType "$1" "$2") |
---|
| 1522 | case "$TYPE" in |
---|
| 1523 | HNTFS) NEWTYPE="NTFS" ;; |
---|
| 1524 | HFAT32) NEWTYPE="FAT32" ;; |
---|
| 1525 | HFAT16) NEWTYPE="FAT16" ;; |
---|
| 1526 | HFAT12) NEWTYPE="FAT12" ;; |
---|
| 1527 | *) ogRaiseError $OG_ERR_PARTITION "$TYPE" |
---|
| 1528 | return $? ;; |
---|
| 1529 | esac |
---|
| 1530 | # Cambiar tipo de partición. |
---|
[ec6de25] | 1531 | ogSetPartitionType $1 $2 $NEWTYPE |
---|
[b09d0fa] | 1532 | } |
---|
| 1533 | |
---|
[2ecd096] | 1534 | |
---|
| 1535 | #/** |
---|
[01d4253] | 1536 | # ogUnlockDisk int_ndisk |
---|
| 1537 | #@brief Elimina el fichero de bloqueo para un disco. |
---|
| 1538 | #@param int_ndisk nº de orden del disco |
---|
| 1539 | #@return (nada) |
---|
| 1540 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
| 1541 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
| 1542 | #@note El fichero de bloqueo se localiza en \c /var/lock/disk, siendo \c disk el dispositivo del disco, sustituyendo el carácter "/" por "-". |
---|
| 1543 | #@version 1.1.0 - Primera versión para OpenGnSys. |
---|
| 1544 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 1545 | #@date 2016-04-08 |
---|
| 1546 | #*/ ## |
---|
| 1547 | function ogUnlockDisk () |
---|
| 1548 | { |
---|
| 1549 | # Variables locales |
---|
| 1550 | local DISK LOCKFILE |
---|
| 1551 | |
---|
| 1552 | # Si se solicita, mostrar ayuda. |
---|
| 1553 | if [ "$*" == "help" ]; then |
---|
| 1554 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
| 1555 | "$FUNCNAME 1" |
---|
| 1556 | return |
---|
| 1557 | fi |
---|
| 1558 | # Error si no se recibe 1 parámetro. |
---|
| 1559 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
| 1560 | |
---|
| 1561 | # Obtener partición. |
---|
| 1562 | DISK="$(ogDiskToDev $1)" || return $? |
---|
| 1563 | |
---|
| 1564 | # Borrar archivo de bloqueo exclusivo. |
---|
| 1565 | LOCKFILE="/var/lock/lock${DISK//\//-}" |
---|
| 1566 | rm -f $LOCKFILE |
---|
| 1567 | } |
---|
| 1568 | |
---|
| 1569 | |
---|
| 1570 | #/** |
---|
[6cdca0c] | 1571 | # ogUpdatePartitionTable |
---|
[1553fc7] | 1572 | #@brief Fuerza al kernel releer la tabla de particiones de los discos duros |
---|
[42669ebf] | 1573 | #@param no requiere |
---|
[1553fc7] | 1574 | #@return informacion propia de la herramienta |
---|
| 1575 | #@note Requisitos: \c partprobe |
---|
| 1576 | #@warning pendiente estructurar la funcion a opengnsys |
---|
[985bef0] | 1577 | #@version 0.1 - Integracion para Opengnsys - EAC: UpdatePartitionTable() en ATA.lib |
---|
| 1578 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 1579 | #@date 27/10/2008 |
---|
[3915005] | 1580 | #*/ ## |
---|
[42669ebf] | 1581 | function ogUpdatePartitionTable () |
---|
| 1582 | { |
---|
[3915005] | 1583 | local i |
---|
[c6087b9] | 1584 | for i in `ogDiskToDev` |
---|
| 1585 | do |
---|
| 1586 | partprobe $i |
---|
| 1587 | done |
---|
[1553fc7] | 1588 | } |
---|