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