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