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