1 | #!/bin/bash |
---|
2 | #/** |
---|
3 | #@file FileSystem.lib |
---|
4 | #@brief Librería o clase FileSystem |
---|
5 | #@class FileSystem |
---|
6 | #@brief Funciones para gestión de sistemas de archivos. |
---|
7 | #@version 1.0.5 |
---|
8 | #@warning License: GNU GPLv3+ |
---|
9 | #*/ |
---|
10 | |
---|
11 | |
---|
12 | #/** |
---|
13 | # ogCheckFs int_ndisk int_nfilesys |
---|
14 | #@brief Comprueba el estado de un sistema de archivos. |
---|
15 | #@param int_ndisk nº de orden del disco |
---|
16 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
17 | #@return (nada) |
---|
18 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
19 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
20 | #@exception OG_ERR_PARTITION Partición desconocida o no accesible. |
---|
21 | #@note Requisitos: *fsck* |
---|
22 | #@warning No se comprueban sistemas de archivos montados o bloqueados. |
---|
23 | #@todo Definir salidas. |
---|
24 | #@version 0.9 - Primera adaptación para OpenGnSys. |
---|
25 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
26 | #@date 2009-10-07 |
---|
27 | #@version 1.0.2 - Ignorar códigos de salida de comprobación (no erróneos). |
---|
28 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
29 | #@date 2011-09-23 |
---|
30 | #@version 1.0.4 - Soportar HFS/HFS+. |
---|
31 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
32 | #@date 2012-05-21 |
---|
33 | #@version 1.0.5 - Desmontar antes de comprobar. |
---|
34 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
35 | #@date 2012-09-05 |
---|
36 | #*/ ## |
---|
37 | function ogCheckFs () |
---|
38 | { |
---|
39 | # Variables locales. |
---|
40 | local PART TYPE PROG PARAMS CODES ERRCODE |
---|
41 | # Si se solicita, mostrar ayuda. |
---|
42 | if [ "$*" == "help" ]; then |
---|
43 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
44 | "$FUNCNAME 1 1" |
---|
45 | return |
---|
46 | fi |
---|
47 | |
---|
48 | # Error si no se reciben 2 parámetros. |
---|
49 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
50 | # Obtener partición. |
---|
51 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
52 | |
---|
53 | TYPE=$(ogGetFsType $1 $2) |
---|
54 | case "$TYPE" in |
---|
55 | EXT[234]) PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;; |
---|
56 | REISERFS) PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;; |
---|
57 | REISER4) PROG="fsck.reiser4"; PARAMS="-ay" ;; |
---|
58 | JFS) PROG="fsck.jfs"; CODES=(1 2) ;; |
---|
59 | XFS) PROG="fsck.xfs" ;; |
---|
60 | NTFS) PROG="ntfsfix" ;; |
---|
61 | FAT32) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;; |
---|
62 | FAT16) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;; |
---|
63 | FAT12) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;; |
---|
64 | HFS) PROG="fsck.hfs" ;; |
---|
65 | HFSPLUS) PROG="fsck.hfsplus" ;; |
---|
66 | *) ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" |
---|
67 | return $? ;; |
---|
68 | esac |
---|
69 | # Error si el sistema de archivos esta montado o bloqueado. |
---|
70 | ogUnmount $1 $2 |
---|
71 | if ogIsMounted $1 $2; then |
---|
72 | ogRaiseError $OG_ERR_PARTITION "$1 $2" # Indicar nuevo error |
---|
73 | return $? |
---|
74 | fi |
---|
75 | if ogIsLocked $1 $2; then |
---|
76 | ogRaiseError $OG_ERR_LOCKED "$1 $2" |
---|
77 | return $? |
---|
78 | fi |
---|
79 | # Comprobar en modo uso exclusivo. |
---|
80 | ogLock $1 $2 |
---|
81 | trap "ogUnlock $1 $2" 1 2 3 6 9 |
---|
82 | eval $PROG $PARAMS $PART |
---|
83 | ERRCODE=$? |
---|
84 | case $ERRCODE in |
---|
85 | 0|${CODES[*]}) |
---|
86 | ERRCODE=0 ;; |
---|
87 | 127) ogRaiseError $OG_ERR_NOTEXEC "$PROG" |
---|
88 | ERRCODE=$OG_ERR_NOTEXEC ;; |
---|
89 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
90 | ERRCODE=$OG_ERR_PARTITION ;; |
---|
91 | esac |
---|
92 | ogUnlock $1 $2 |
---|
93 | return $ERRCODE |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | #/** |
---|
98 | # ogExtendFs int_ndisk int_nfilesys |
---|
99 | #@brief Extiende un sistema de archivos al tamaño de su partición. |
---|
100 | #@param int_ndisk nº de orden del disco |
---|
101 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
102 | #@return (nada) |
---|
103 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
104 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
105 | #@exception OG_ERR_PARTITION Partición desconocida o no accesible. |
---|
106 | #@note Requisitos: *resize* |
---|
107 | #@version 0.1 - Integracion para Opengnsys - EAC: EnlargeFileSystem() en ATA.lib |
---|
108 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
109 | #@date 2008-10-27 |
---|
110 | #@version 0.9 - Primera adaptacion para OpenGnSys. |
---|
111 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
112 | #@date 2009-09-23 |
---|
113 | #@version 1.0.5 - Soporte para BTRFS. |
---|
114 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
115 | #@date 2012-06-28 |
---|
116 | #*/ ## |
---|
117 | function ogExtendFs () |
---|
118 | { |
---|
119 | # Variables locales. |
---|
120 | local PART TYPE PROG PARAMS ERRCODE |
---|
121 | |
---|
122 | # Si se solicita, mostrar ayuda. |
---|
123 | if [ "$*" == "help" ]; then |
---|
124 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
125 | "$FUNCNAME 1 1" |
---|
126 | return |
---|
127 | fi |
---|
128 | # Error si no se reciben 2 parámetros. |
---|
129 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
130 | |
---|
131 | # Obtener partición. |
---|
132 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
133 | |
---|
134 | # Redimensionar al tamano máximo según el tipo de partición. |
---|
135 | TYPE=$(ogGetFsType $1 $2) |
---|
136 | case "$TYPE" in |
---|
137 | EXT[234]) PROG="resize2fs"; PARAMS="-f" ;; |
---|
138 | REISERFS) PROG="resize_reiserfs"; PARAMS="-f" ;; |
---|
139 | BTRFS) PROG="btrfs"; PARAMS="filesystem resize max" ;; |
---|
140 | NTFS) PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;; |
---|
141 | # FAT32|FAT16) # Usar "fatresize" |
---|
142 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE" |
---|
143 | return $? ;; |
---|
144 | esac |
---|
145 | # Error si el sistema de archivos está montado o bloqueado. |
---|
146 | ogUnmount $1 $2 2>/dev/null |
---|
147 | if ogIsMounted $1 $2; then |
---|
148 | ogRaiseError $OG_ERR_PARTITION "$1 $2" # Indicar nuevo error |
---|
149 | return $? |
---|
150 | fi |
---|
151 | if ogIsLocked $1 $2; then |
---|
152 | ogRaiseError $OG_ERR_LOCKED "$1 $2" |
---|
153 | return $? |
---|
154 | fi |
---|
155 | # Redimensionar en modo uso exclusivo. |
---|
156 | ogLock $1 $2 |
---|
157 | trap "ogUnlock $1 $2" 1 2 3 6 9 |
---|
158 | eval $PROG $PARAMS $PART &>/dev/null |
---|
159 | ERRCODE=$? |
---|
160 | case $ERRCODE in |
---|
161 | 0) ;; |
---|
162 | 127) ogRaiseError $OG_ERR_NOTEXEC "$PROG" |
---|
163 | ERRCODE=$OG_ERR_NOTEXEC ;; |
---|
164 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
165 | ERRCODE=$OG_ERR_PARTITION ;; |
---|
166 | esac |
---|
167 | ogUnlock $1 $2 |
---|
168 | return $ERRCODE |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | #/** |
---|
173 | # ogFormat int_ndisk int_nfilesys | CACHE |
---|
174 | #@see ogFormatFs ogFormatCache |
---|
175 | #*/ ## |
---|
176 | function ogFormat () |
---|
177 | { |
---|
178 | case "$*" in |
---|
179 | CACHE|cache) ogFormatCache ;; |
---|
180 | *) ogFormatFs "$@" ;; |
---|
181 | esac |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | #/** |
---|
186 | # ogFormatFs int_ndisk int_nfilesys [type_fstype] [str_label] |
---|
187 | #@brief Formatea un sistema de ficheros según el tipo de su partición. |
---|
188 | #@param int_ndisk nº de orden del disco |
---|
189 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
190 | #@param type_fstype mnemónico de sistema de ficheros a formatear |
---|
191 | #@param str_label etiqueta de volumen (opcional) |
---|
192 | #@return (por determinar) |
---|
193 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
194 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
195 | #@exception OG_ERR_PARTITION Partición no accesible o desconocida. |
---|
196 | #@note Requisitos: mkfs* |
---|
197 | #@warning No formatea particiones montadas ni bloqueadas. |
---|
198 | #@todo Definir salidas. |
---|
199 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
200 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
201 | #@date 2009-10-08 |
---|
202 | #@version 1.0.4 - Solucionado error cuando no se detecta tipo de sistema de ficheros pero si se indica. |
---|
203 | #@author Universidad de Huelva |
---|
204 | #@date 2012-04-11 |
---|
205 | #@version 1.0.5 - Comprobar errores al inicio y desmontar antes de formatear. |
---|
206 | #@author Universidad de Huelva |
---|
207 | #@date 2012-12-14 |
---|
208 | #*/ ## |
---|
209 | function ogFormatFs () |
---|
210 | { |
---|
211 | # Variables locales |
---|
212 | local PART ID TYPE LABEL PROG PARAMS ERRCODE |
---|
213 | |
---|
214 | # Si se solicita, mostrar ayuda. |
---|
215 | if [ "$*" == "help" ]; then |
---|
216 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys [str_label]" \ |
---|
217 | "$FUNCNAME 1 1" \ |
---|
218 | "$FUNCNAME 1 1 EXT4" \ |
---|
219 | "$FUNCNAME 1 1 \"DATA\"" \ |
---|
220 | "$FUNCNAME 1 1 EXT4 \"DATA\"" |
---|
221 | return |
---|
222 | fi |
---|
223 | # Error si no se reciben entre 2 y 4 parámetros. |
---|
224 | [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
225 | # Obtener fichero de dispositivo. |
---|
226 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
227 | # Error si la partición está montada o bloqueada. |
---|
228 | if ogIsMounted $1 $2; then |
---|
229 | ogRaiseError $OG_ERR_PARTITION "$1 $2" # Indicar nuevo error |
---|
230 | return $? |
---|
231 | fi |
---|
232 | if ogIsLocked $1 $2; then |
---|
233 | ogRaiseError $OG_ERR_LOCKED "$1 $2" |
---|
234 | return $? |
---|
235 | fi |
---|
236 | # Obtener tipo de sisitema de archivos. |
---|
237 | TYPE="$(ogGetFsType $1 $2)" |
---|
238 | if [ -z "$TYPE" ]; then |
---|
239 | # Si no se indico ningun tipo de sistema de ficheros se retorna |
---|
240 | if [ -z "$3" ]; then |
---|
241 | return $? |
---|
242 | else |
---|
243 | # Si no se detecto, se asigna el indicado |
---|
244 | TYPE="$3" |
---|
245 | fi |
---|
246 | fi |
---|
247 | |
---|
248 | # Elegir tipo de formato segun el tipo de particion. |
---|
249 | case "$3" in |
---|
250 | EXT2) ID=83; PROG="mkfs.ext2" ;; |
---|
251 | EXT3) ID=83; PROG="mkfs.ext3" ;; |
---|
252 | EXT4) ID=83; PROG="mkfs.ext4" ;; |
---|
253 | BTRFS) ID=83; PROG="mkfs.btrfs" ;; |
---|
254 | REISERFS) ID=83; PROG="mkfs.reiserfs"; PARAMS="-f" ;; |
---|
255 | REISER4) ID=83; PROG="mkfs.reiser4" ;; |
---|
256 | XFS) ID=83; PROG="mkfs.xfs"; PARAMS="-f" ;; |
---|
257 | JFS) ID=83; PROG="mkfs.jfs"; PARAMS="<<<\"y\"";; |
---|
258 | NTFS) ID=7; PROG="mkntfs"; PARAMS="-f" ;; |
---|
259 | HNTFS) ID=17; PROG="mkntfs"; PARAMS="-f" ;; |
---|
260 | FAT32) ID=b; PROG="mkdosfs"; PARAMS="-F 32" ;; |
---|
261 | HFAT32) ID=1b; PROG="mkdosfs"; PARAMS="-F 32" ;; |
---|
262 | FAT16) ID=6; PROG="mkdosfs"; PARAMS="-F 16" ;; |
---|
263 | HFAT16) ID=16; PROG="mkdosfs"; PARAMS="-F 16" ;; |
---|
264 | FAT12) ID=1; PROG="mkdosfs"; PARAMS="-F 12" ;; |
---|
265 | HFAT12) ID=11; PROG="mkdosfs"; PARAMS="-F 12" ;; |
---|
266 | HFS) ID=af; PROG="mkfs.hfs" ;; |
---|
267 | HFSPLUS) ID=af; PROG="mkfs.hfsplus" ;; |
---|
268 | UFS) ID=bf; PROG="mkfs.ufs"; PARAMS="-O 2" ;; |
---|
269 | *) LABEL="$3" ;; |
---|
270 | esac |
---|
271 | # Si no se indica explícitamente, detectar el tipo de sistema de archivos. |
---|
272 | if [ -z "$PROG" ]; then |
---|
273 | case "$TYPE" in |
---|
274 | EXT2) PROG="mkfs.ext2" ;; |
---|
275 | EXT3) PROG="mkfs.ext3" ;; |
---|
276 | EXT4) PROG="mkfs.ext4" ;; |
---|
277 | BTRFS) PROG="mkfs.btrfs" ;; |
---|
278 | REISERFS) PROG="mkfs.reiserfs"; PARAMS="-f" ;; |
---|
279 | REISER4) PROG="mkfs.reiser4" ;; |
---|
280 | XFS) PROG="mkfs.xfs"; PARAMS="-f" ;; |
---|
281 | JFS) PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;; |
---|
282 | LINUX-SWAP) PROG="mkswap" ;; |
---|
283 | NTFS) PROG="mkntfs"; PARAMS="-f" ;; |
---|
284 | FAT32) PROG="mkdosfs"; PARAMS="-F 32" ;; |
---|
285 | FAT16) PROG="mkdosfs"; PARAMS="-F 16" ;; |
---|
286 | FAT12) PROG="mkdosfs"; PARAMS="-F 12" ;; |
---|
287 | HFS) PROG="mkfs.hfs" ;; |
---|
288 | HFSPLUS) PROG="mkfs.hfsplus" ;; |
---|
289 | UFS) PROG="mkfs.ufs"; PARAMS="-O 2" ;; |
---|
290 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE" |
---|
291 | return $? ;; |
---|
292 | esac |
---|
293 | else |
---|
294 | [ $TYPE == "$3" -o $ID == "$(ogGetPartitionId $1 $2)" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $? |
---|
295 | fi |
---|
296 | # Comprobar consistencia entre id. de partición y tipo de sistema de archivos. |
---|
297 | [ -n "$PROG" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $? |
---|
298 | |
---|
299 | # Etiquetas de particion. |
---|
300 | if [ -z "$LABEL" ]; then |
---|
301 | [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $? |
---|
302 | [ -n "$4" ] && PARAMS="$PARAMS -L $4" |
---|
303 | else |
---|
304 | PARAMS="$PARAMS -L $LABEL" |
---|
305 | fi |
---|
306 | |
---|
307 | # Formatear en modo uso exclusivo (desmontar siempre). |
---|
308 | ogLock $1 $2 |
---|
309 | trap "ogUnlock $1 $2" 1 2 3 6 9 |
---|
310 | umount $PART |
---|
311 | eval $PROG $PARAMS $PART 2>/dev/null |
---|
312 | ERRCODE=$? |
---|
313 | case $ERRCODE in |
---|
314 | 0) ;; |
---|
315 | 127) ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;; |
---|
316 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2" ;; |
---|
317 | esac |
---|
318 | ogUnlock $1 $2 |
---|
319 | return $ERRCODE |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | #/** |
---|
324 | # ogGetFsSize int_ndisk int_npartition [str_unit] |
---|
325 | #@brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB |
---|
326 | #@param int_ndisk nº de orden del disco |
---|
327 | #@param int_npartition nº de orden de la partición |
---|
328 | #@param str_unit unidad (opcional, por defecto: KB) |
---|
329 | #@return float_size - Tamaño del sistema de archivos |
---|
330 | #@note str_unit = { KB, MB, GB, TB } |
---|
331 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
332 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
333 | #@version 0.1 - Integracion para Opengnsys - EAC: SizeFileSystem() en FileSystem.lib |
---|
334 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
335 | #@date 2008-10-27 |
---|
336 | #@version 1.0.4 - Adaptación de las salidas. |
---|
337 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
338 | #@date 2012-06-18 |
---|
339 | #*/ ## |
---|
340 | function ogGetFsSize () |
---|
341 | { |
---|
342 | # Variables locales. |
---|
343 | local MNTDIR UNIT VALUE FACTOR SIZE |
---|
344 | # Si se solicita, mostrar ayuda. |
---|
345 | if [ "$*" == "help" ]; then |
---|
346 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_unit]" \ |
---|
347 | "$FUNCNAME 1 1 => 15624188" \ |
---|
348 | "$FUNCNAME 1 1 KB => 15624188" |
---|
349 | return |
---|
350 | fi |
---|
351 | # Error si no se reciben 2 o 3 parámetros. |
---|
352 | [ $# == 2 ] || [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
353 | # Obtener unidad y factor de medida. |
---|
354 | UNIT="$3" |
---|
355 | UNIT=${UNIT:-"KB"} |
---|
356 | case "$UNIT" in |
---|
357 | [kK]B) |
---|
358 | FACTOR=1 ;; |
---|
359 | MB) FACTOR=1024 ;; |
---|
360 | GB) FACTOR=$[1024*1024] ;; |
---|
361 | TB) FACTOR=$[1024*1024*1024] ;; |
---|
362 | *) ogRaiseError $OG_ERR_FORMAT "$3 != { KB, MB, GB, TB }" |
---|
363 | return $? ;; |
---|
364 | esac |
---|
365 | |
---|
366 | # Obtener el tamaño del sistema de archivo (si no está formateado; tamaño = 0). |
---|
367 | MNTDIR="$(ogMount $1 $2 2>/dev/null)" |
---|
368 | if [ -n "$MNTDIR" ]; then |
---|
369 | VALUE=$(df -BK "$MNTDIR" | awk '{getline; print $2}') |
---|
370 | SIZE=$(echo "$VALUE $FACTOR" | awk '{printf "%f\n", $1/$2}') |
---|
371 | else |
---|
372 | SIZE=0 |
---|
373 | fi |
---|
374 | # Devolver el tamaño (quitar decimales si son 0). |
---|
375 | echo ${SIZE%.0*} |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | #/** |
---|
380 | # ogGetFsType int_ndisk int_nfilesys |
---|
381 | #@brief Devuelve el mnemonico con el tipo de sistema de archivos. |
---|
382 | #@param int_ndisk nº de orden del disco |
---|
383 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
384 | #@return Mnemonico |
---|
385 | #@note Mnemonico: { EXT2, EXT3, EXT4, REISERFS, XFS, JFS, FAT16, FAT32, NTFS, CACHE } |
---|
386 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
387 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
388 | #@version 0.1 - Integracion para Opengnsys - EAC: TypeFS() en ATA.lib |
---|
389 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
390 | #@date 2008-10-27 |
---|
391 | #@version 0.9 - Primera adaptacion para OpenGnSys. |
---|
392 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
393 | #@date 2009-07-21 |
---|
394 | #@version 1.0.2 - Obtención de datos reales de sistemas de ficheros. |
---|
395 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
396 | #@date 2011-12-02 |
---|
397 | #@version 1.0.5 - Usar "mount" en vez de "parted". |
---|
398 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
399 | #@date 2012-09-04 |
---|
400 | #*/ ## |
---|
401 | function ogGetFsType () |
---|
402 | { |
---|
403 | # Variables locales. |
---|
404 | local PART ID TYPE |
---|
405 | # Si se solicita, mostrar ayuda. |
---|
406 | if [ "$*" == "help" ]; then |
---|
407 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
408 | "$FUNCNAME 1 1 => NTFS" |
---|
409 | return |
---|
410 | fi |
---|
411 | # Error si no se reciben 2 parámetros. |
---|
412 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
413 | |
---|
414 | # Detectar id. de tipo de partición. |
---|
415 | PART=$(ogDiskToDev "$1" "$2") || return $? |
---|
416 | ID=$(ogGetPartitionId "$1" "$2") |
---|
417 | [ "$ID" == "a7" ] && ID="ca" # Traducir antiguo id. de partición de caché. |
---|
418 | TYPE="" |
---|
419 | case "$ID" in |
---|
420 | ca|CA00) # Detectar caché local (revisar detección en tablas GPT). |
---|
421 | ogIsFormated $1 $2 2>/dev/null && TYPE="CACHE" |
---|
422 | ;; |
---|
423 | *) # Detectar sistema de ficheros. |
---|
424 | ogMount $1 $2 &>/dev/null |
---|
425 | TYPE=$(mount | awk -v p=$PART '{ if ($1==p) { print toupper($5); } }') |
---|
426 | # Detectar otros sistemas de archivos (FUSE, VFAT). |
---|
427 | case "$TYPE" in |
---|
428 | FUSEBLK) TYPE="NTFS" ;; # usar "file -Ls" para detectar |
---|
429 | VFAT) TYPE="FAT32" ;; # usar "file -Ls" para detectar |
---|
430 | esac |
---|
431 | ;; |
---|
432 | esac |
---|
433 | |
---|
434 | [ -n "$TYPE" ] && echo "$TYPE" |
---|
435 | } |
---|
436 | |
---|
437 | |
---|
438 | #/** |
---|
439 | # ogGetMountPoint int_ndisk int_nfilesys |
---|
440 | #@brief Devuelve el punto de montaje de un sistema de archivos. |
---|
441 | #@param int_ndisk nº de orden del disco |
---|
442 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
443 | #@return Punto de montaje |
---|
444 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
445 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
446 | #@note Requisitos: \c mount* \c awk |
---|
447 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
448 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
449 | #@date 2009-10-15 |
---|
450 | #*/ ## |
---|
451 | function ogGetMountPoint () |
---|
452 | { |
---|
453 | # Variables locales |
---|
454 | local PART |
---|
455 | # Si se solicita, mostrar ayuda. |
---|
456 | if [ "$*" == "help" ]; then |
---|
457 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
458 | "$FUNCNAME 1 1 => /mnt/sda1" |
---|
459 | return |
---|
460 | fi |
---|
461 | # Error si no se reciben 2 parámetros. |
---|
462 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
463 | # Obtener partición. |
---|
464 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
465 | |
---|
466 | mount | awk -v P=$PART '{if ($1==P) {print $3}}' |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | #/** |
---|
471 | # ogIsFormated int_ndisk int_nfilesys |
---|
472 | #@brief Comprueba si un sistema de archivos está formateado. |
---|
473 | #@param int_ndisk nº de orden del disco o volumen. |
---|
474 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
475 | #@return Código de salida: 0 - formateado, 1 - sin formato o error. |
---|
476 | #@version 0.91 - Adaptación inicial para comprobar que existe caché. |
---|
477 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
478 | #@date 2010-03-18 |
---|
479 | #@version 1.0.1 - Devolver falso en caso de error. |
---|
480 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
481 | #@date 2011-05-18 |
---|
482 | #@version 1.0.5 - Dejar de usar "parted". |
---|
483 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
484 | #@date 2012-09-04 |
---|
485 | #*/ ## |
---|
486 | function ogIsFormated () |
---|
487 | { |
---|
488 | # Variables locales |
---|
489 | local DISK |
---|
490 | if [ "$*" == "help" ]; then |
---|
491 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
492 | "if $FUNCNAME 1 1; then ... ; fi" |
---|
493 | return |
---|
494 | fi |
---|
495 | # Falso, en caso de error. |
---|
496 | [ $# == 2 ] || return 1 |
---|
497 | |
---|
498 | test -n "$(ogMount "$1" "$2")" |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | #/** |
---|
503 | # ogIsMounted int_ndisk int_nfilesys |
---|
504 | #@brief Comprueba si un sistema de archivos está montado. |
---|
505 | #@param int_ndisk nº de orden del disco |
---|
506 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
507 | #@return Código de salida: 0 - montado, 1 - sin montar o error. |
---|
508 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
509 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
510 | #@date 2009-10-15 |
---|
511 | #@version 1.0.1 - Devolver falso en caso de error. |
---|
512 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
513 | #@date 2011-05-18 |
---|
514 | #*/ ## |
---|
515 | function ogIsMounted () |
---|
516 | { |
---|
517 | # Si se solicita, mostrar ayuda. |
---|
518 | if [ "$*" == "help" ]; then |
---|
519 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
520 | "if $FUNCNAME 1 1; then ... ; fi" |
---|
521 | return |
---|
522 | fi |
---|
523 | # Falso, en caso de error. |
---|
524 | [ $# == 2 ] || return 1 |
---|
525 | |
---|
526 | test -n "$(ogGetMountPoint $1 $2)" |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | #/** |
---|
531 | # ogIsLocked int_ndisk int_npartition |
---|
532 | #@brief Comprueba si una partición está bloqueada por una operación de uso exclusivo. |
---|
533 | #@param int_ndisk nº de orden del disco |
---|
534 | #@param int_npartition nº de orden de la partición |
---|
535 | #@return Código de salida: 0 - bloqueado, 1 - sin bloquear o error. |
---|
536 | #@note El fichero de bloqueo se localiza en \c /var/lock/part, siendo \c part el dispositivo de la partición, sustituyendo el carácter "/" por "-". |
---|
537 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
538 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
539 | #@date 2009-09-03 |
---|
540 | #@version 1.0.1 - Devolver falso en caso de error. |
---|
541 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
542 | #@date 2011-05-18 |
---|
543 | #*/ ## |
---|
544 | function ogIsLocked () |
---|
545 | { |
---|
546 | # Variables locales |
---|
547 | local PART LOCKFILE |
---|
548 | |
---|
549 | # Si se solicita, mostrar ayuda. |
---|
550 | if [ "$*" == "help" ]; then |
---|
551 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
552 | "if $FUNCNAME 1 1; then ... ; fi" |
---|
553 | return |
---|
554 | fi |
---|
555 | # Falso, en caso de error. |
---|
556 | [ $# == 2 ] || return 1 |
---|
557 | |
---|
558 | # Obtener partición. |
---|
559 | PART="$(ogDiskToDev $1 $2)" || return 1 |
---|
560 | |
---|
561 | # Comprobar existencia del fichero de bloqueo. |
---|
562 | LOCKFILE="/var/lock/lock${PART//\//-}" |
---|
563 | test -f $LOCKFILE |
---|
564 | } |
---|
565 | |
---|
566 | |
---|
567 | #/** |
---|
568 | # ogLock int_ndisk int_npartition |
---|
569 | #@see ogLockPartition |
---|
570 | #*/ |
---|
571 | function ogLock () |
---|
572 | { |
---|
573 | ogLockPartition "$@" |
---|
574 | } |
---|
575 | |
---|
576 | #/** |
---|
577 | # ogLockPartition int_ndisk int_npartition |
---|
578 | #@brief Genera un fichero de bloqueo para una partición en uso exlusivo. |
---|
579 | #@param int_ndisk nº de orden del disco |
---|
580 | #@param int_npartition nº de orden de la partición |
---|
581 | #@return (nada) |
---|
582 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
583 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
584 | #@note El fichero de bloqueo se localiza en \c /var/lock/part, siendo \c part el dispositivo de la partición, sustituyendo el carácter "/" por "-". |
---|
585 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
586 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
587 | #@date 2009-09-03 |
---|
588 | #*/ ## |
---|
589 | function ogLockPartition () |
---|
590 | { |
---|
591 | # Variables locales |
---|
592 | local PART LOCKFILE |
---|
593 | |
---|
594 | # Si se solicita, mostrar ayuda. |
---|
595 | if [ "$*" == "help" ]; then |
---|
596 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
597 | "$FUNCNAME 1 1" |
---|
598 | return |
---|
599 | fi |
---|
600 | # Error si no se reciben 2 parámetros. |
---|
601 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
602 | |
---|
603 | # Obtener partición. |
---|
604 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
605 | |
---|
606 | # Crear archivo de bloqueo exclusivo. |
---|
607 | LOCKFILE="/var/lock/lock${PART//\//-}" |
---|
608 | touch $LOCKFILE |
---|
609 | } |
---|
610 | |
---|
611 | |
---|
612 | #/** |
---|
613 | # ogMount int_ndisk int_nfilesys |
---|
614 | #@see ogMountFs ogMountCache ogMountCdrom |
---|
615 | #*/ ## |
---|
616 | function ogMount () |
---|
617 | { |
---|
618 | case "$*" in |
---|
619 | CACHE|cache) |
---|
620 | ogMountCache ;; |
---|
621 | CDROM|cdrom) |
---|
622 | ogMountCdrom ;; |
---|
623 | *) ogMountFs "$@" ;; |
---|
624 | esac |
---|
625 | } |
---|
626 | |
---|
627 | |
---|
628 | #/** |
---|
629 | # ogMountFs int_ndisk int_nfilesys |
---|
630 | #@brief Monta un sistema de archivos. |
---|
631 | #@param int_ndisk nº de orden del disco |
---|
632 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
633 | #@return Punto de montaje |
---|
634 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
635 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
636 | #@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar. |
---|
637 | #@version 0.1 - Integracion para Opengnsys - EAC: MountPartition() en FileSystem.lib |
---|
638 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
639 | #@date 2008-10-27 |
---|
640 | #@version 0.9 - Primera version para OpenGnSys. |
---|
641 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
642 | #@date 2009-09-28 |
---|
643 | #@version 1.0.5 - Independiente del tipo de sistema de ficheros. |
---|
644 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
645 | #@date 2012-09-04 |
---|
646 | #*/ ## |
---|
647 | function ogMountFs () |
---|
648 | { |
---|
649 | # Variables locales |
---|
650 | local PART MNTDIR |
---|
651 | |
---|
652 | # Si se solicita, mostrar ayuda. |
---|
653 | if [ "$*" == "help" ]; then |
---|
654 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
655 | "$FUNCNAME 1 1 => /mnt/sda1" |
---|
656 | return |
---|
657 | fi |
---|
658 | # Error si no se reciben 2 parámetros. |
---|
659 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
660 | |
---|
661 | # Obtener partición. |
---|
662 | PART="$(ogDiskToDev "$1" "$2")" || return $? |
---|
663 | |
---|
664 | # Comprobar si el sistema de archivos ya está montada. |
---|
665 | MNTDIR="$(ogGetMountPoint $1 $2)" |
---|
666 | # Si no, montarlo en un directorio de sistema. |
---|
667 | if [ -z "$MNTDIR" ]; then |
---|
668 | # Error si la particion esta bloqueada. |
---|
669 | if ogIsLocked $1 $2; then |
---|
670 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2" |
---|
671 | return $? |
---|
672 | fi |
---|
673 | # Crear punto de montaje o enlace simbólico para caché local. |
---|
674 | MNTDIR=${PART/dev/mnt} |
---|
675 | if [ "$(ogFindCache)" == "$1 $2" -a -n "$OGCAC" ]; then |
---|
676 | mkdir -p $OGCAC |
---|
677 | ln -fs $OGCAC $MNTDIR |
---|
678 | else |
---|
679 | mkdir -p $MNTDIR |
---|
680 | fi |
---|
681 | # Montar sistema de archivos. |
---|
682 | mount $PART $MNTDIR &>/dev/null || \ |
---|
683 | mount $PART $MNTDIR -o force,remove_hiberfile &>/dev/null || \ |
---|
684 | ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $? |
---|
685 | fi |
---|
686 | echo "$MNTDIR" |
---|
687 | } |
---|
688 | |
---|
689 | |
---|
690 | ##### PRUEBAS |
---|
691 | # Montar CDROM |
---|
692 | function ogMountCdrom () |
---|
693 | { |
---|
694 | local DEV MNTDIR |
---|
695 | DEV="/dev/cdrom" # Por defecto |
---|
696 | MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}') |
---|
697 | if [ -z "$MNTDIR" ]; then |
---|
698 | MNTDIR=${DEV/dev/mnt} |
---|
699 | mkdir -p $MNTDIR |
---|
700 | mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $? |
---|
701 | fi |
---|
702 | echo $MNTDIR |
---|
703 | } |
---|
704 | |
---|
705 | |
---|
706 | #/** |
---|
707 | # ogReduceFs int_ndisk int_nfilesys |
---|
708 | #@brief Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre. |
---|
709 | #@param int_ndisk nº de orden del disco |
---|
710 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
711 | #@return int_tamañoKB - tamaño en KB |
---|
712 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
713 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
714 | #@exception OG_ERR_PARTITION Partición desconocida o no accesible. |
---|
715 | #@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys |
---|
716 | #@warning El sistema de archivos se amplía al mínimo + 10%. |
---|
717 | #@note Requisitos: *resize* |
---|
718 | #@version 0.1 - Integracion para Opengnsys - EAC: ReduceFileSystem() en ATA.lib |
---|
719 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
720 | #@date 2008-10-27 |
---|
721 | #@version 0.9 - Primera version para OpenGnSys. |
---|
722 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
723 | #@date 2009-09-23 |
---|
724 | #@version 0.9.2 - Añadir un 10% al tamaño mínimo requerido. |
---|
725 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
726 | #@date 2010-09-27 |
---|
727 | #@version 1.0 - Deteccion automatica del tamaño minimo adecuado |
---|
728 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
729 | #@date 2011-02-24 |
---|
730 | #*/ ## |
---|
731 | function ogReduceFs () |
---|
732 | { |
---|
733 | # Variables locales |
---|
734 | local PART BLKS SIZE |
---|
735 | |
---|
736 | # Si se solicita, mostrar ayuda. |
---|
737 | if [ "$*" == "help" ]; then |
---|
738 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \ |
---|
739 | "$FUNCNAME 1 1" |
---|
740 | return |
---|
741 | fi |
---|
742 | # Error si no se reciben 2 parámetros. |
---|
743 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
744 | |
---|
745 | # Obtener partición. |
---|
746 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
747 | |
---|
748 | # Redimensionar según el tipo de particion. |
---|
749 | case "$(ogGetFsType $1 $2)" in |
---|
750 | EXT[234]) |
---|
751 | ogUnmount $1 $2 2>/dev/null |
---|
752 | # Ext2/3/4: Tamaño de los bloques del sistema de archivos |
---|
753 | BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}') |
---|
754 | # Traduce el num. en sectores de 512B a tamano en MB. |
---|
755 | #SIZE=$(resize2fs -P $PART 2>/dev/null | \ |
---|
756 | # awk -v B=$BLKS '/minimum size/ {print int($7*1.1*B/2048)}') |
---|
757 | #resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
758 | resize2fs -fpM $PART &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
759 | ;; |
---|
760 | # BTRFS) # Usar "btrfs" |
---|
761 | # ;; |
---|
762 | # REISERFS) # Usar "resize_reiserfs" |
---|
763 | # ;; |
---|
764 | NTFS) |
---|
765 | ogDeleteFile $1 $2 pagefile.sys |
---|
766 | ogDeleteFile $1 $2 hiberfil.sys |
---|
767 | ogUnmount $1 $2 2>/dev/null |
---|
768 | ## NTFS: Obtiene tamaño mínimo en MB. |
---|
769 | #SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print int($8*1.1)}') |
---|
770 | #ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
771 | #ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
772 | SIZE=$(ogReduceFsCheck $1 $2) |
---|
773 | [ "$SIZE" == 0 ] && return 1 |
---|
774 | ntfsresize -fs "${SIZE}M" $PART <<<"y" || ogRaiseError $OG_ERR_PARTITION "error reduciendo $1,$2" || return $? |
---|
775 | ;; |
---|
776 | # FAT32|FAT16) # Usar "fatresize" |
---|
777 | # ;; |
---|
778 | *) ogRaiseError $OG_ERR_PARTITION "$1,$2" |
---|
779 | return $? ;; |
---|
780 | esac |
---|
781 | ogGetFsSize $1 $2 |
---|
782 | } |
---|
783 | |
---|
784 | |
---|
785 | function ogReduceFsCheck () |
---|
786 | { |
---|
787 | #IMPORTANTE: retorna el valor en MB que podrá reducir el FS de una particion ntfs |
---|
788 | #valor devuelto 0, y codigo error 1. No se puede reducir, probar a reiniciar windows y chkdsk |
---|
789 | |
---|
790 | |
---|
791 | local PART RC MODE SIZE SIZEDATA |
---|
792 | [ $# == 2 ] && MODE=STAGE1 |
---|
793 | [ $# == 3 ] && MODE=STAGE2 |
---|
794 | [ -z $MODE ] && return |
---|
795 | |
---|
796 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
797 | ogUnmount $1 $2 &>/dev/null |
---|
798 | |
---|
799 | |
---|
800 | case $MODE in |
---|
801 | STAGE1) |
---|
802 | # echo "primera etapa $*" |
---|
803 | ntfsresize -fi $PART &>/dev/null |
---|
804 | RC=`echo $?` |
---|
805 | # echo "RC es" $RC |
---|
806 | if [ "$RC" -eq "1" ] |
---|
807 | then |
---|
808 | echo "0" |
---|
809 | return 1 |
---|
810 | fi |
---|
811 | SIZEDATA=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}') |
---|
812 | # echo "salida" $? |
---|
813 | # echo $SIZEDATA |
---|
814 | ogReduceFsCheck $1 $2 $SIZEDATA |
---|
815 | return 0 |
---|
816 | ;; |
---|
817 | STAGE2) |
---|
818 | # echo "segunda etapa $*" |
---|
819 | SIZEDATA=$3 |
---|
820 | ntfsresize -fns "${SIZEDATA}M" $PART &>/tmp/ntfsresize.txt |
---|
821 | RC=$? |
---|
822 | if [ "$RC" == "0" ] |
---|
823 | then |
---|
824 | SIZE=$SIZEDATA |
---|
825 | echo $SIZE |
---|
826 | else |
---|
827 | SIZEEXTRA=$(cat /tmp/ntfsresize.txt | awk '/Needed relocations :/ {print $0}' | awk -F"(" '{print $2}' | awk '{print $1+500}') |
---|
828 | SIZE=$(expr $SIZEDATA + $SIZEEXTRA) |
---|
829 | ogReduceFsCheck $1 $2 $SIZE |
---|
830 | return 0 |
---|
831 | fi |
---|
832 | ;; |
---|
833 | *) |
---|
834 | return |
---|
835 | ;; |
---|
836 | esac |
---|
837 | } |
---|
838 | |
---|
839 | |
---|
840 | |
---|
841 | #/** |
---|
842 | # ogUnlock int_ndisk int_npartition |
---|
843 | #@see ogUnlockPartition |
---|
844 | #*/ ## |
---|
845 | function ogUnlock () |
---|
846 | { |
---|
847 | ogUnlockPartition "$@" |
---|
848 | } |
---|
849 | |
---|
850 | #/** |
---|
851 | # ogUnlockPartition int_ndisk int_npartition |
---|
852 | #@brief Elimina el fichero de bloqueo para una particion. |
---|
853 | #@param int_ndisk nº de orden del disco |
---|
854 | #@param int_npartition nº de orden de la partición |
---|
855 | #@return (nada) |
---|
856 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
857 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
858 | #@note El fichero de bloqueo se localiza en \c /var/lock/part, siendo \c part el dispositivo de la partición, sustituyendo el carácter "/" por "-". |
---|
859 | #@version 0.9 - Primera versión para OpenGnSys. |
---|
860 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
861 | #@date 2009-09-03 |
---|
862 | #*/ ## |
---|
863 | function ogUnlockPartition () |
---|
864 | { |
---|
865 | # Variables locales |
---|
866 | local PART LOCKFILE |
---|
867 | |
---|
868 | # Si se solicita, mostrar ayuda. |
---|
869 | if [ "$*" == "help" ]; then |
---|
870 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
871 | "$FUNCNAME 1 1" |
---|
872 | return |
---|
873 | fi |
---|
874 | # Error si no se reciben 2 parámetros. |
---|
875 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
876 | |
---|
877 | # Obtener partición. |
---|
878 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
879 | |
---|
880 | # Borrar archivo de bloqueo exclusivo. |
---|
881 | LOCKFILE="/var/lock/lock${PART//\//-}" |
---|
882 | rm -f $LOCKFILE |
---|
883 | } |
---|
884 | |
---|
885 | |
---|
886 | #/** |
---|
887 | # ogUnmount int_ndisk int_npartition |
---|
888 | #@see ogUnmountFs |
---|
889 | #*/ ## |
---|
890 | function ogUnmount () |
---|
891 | { |
---|
892 | ogUnmountFs "$@" |
---|
893 | } |
---|
894 | |
---|
895 | #/** |
---|
896 | # ogUnmountFs int_ndisk int_nfilesys |
---|
897 | #@brief Desmonta un sistema de archivos. |
---|
898 | #@param int_ndisk nº de orden del disco |
---|
899 | #@param int_nfilesys nº de orden del sistema de archivos |
---|
900 | #@return Nada |
---|
901 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
902 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
903 | #@warning La partición no está previamente montada o no se puede desmontar. |
---|
904 | #@version 0.1 - Integracion para Opengnsys - EAC: UmountPartition() en FileSystem.lib |
---|
905 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
906 | #@date 2008-10-27 |
---|
907 | #@version 0.9 - Primera version para OpenGnSys. |
---|
908 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
909 | #@date 2009-09-28 |
---|
910 | #*/ ## |
---|
911 | function ogUnmountFs () |
---|
912 | { |
---|
913 | # Variables locales |
---|
914 | local PART MNTDIR |
---|
915 | |
---|
916 | # Si se solicita, mostrar ayuda. |
---|
917 | if [ "$*" == "help" ]; then |
---|
918 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1" |
---|
919 | return |
---|
920 | fi |
---|
921 | # Error si no se reciben 2 parámetros. |
---|
922 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
923 | |
---|
924 | # Obtener partición y punto de montaje. |
---|
925 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
926 | MNTDIR="$(ogGetMountPoint $1 $2)" |
---|
927 | |
---|
928 | # Si está montada, desmontarla. |
---|
929 | if [ -n "$MNTDIR" ]; then |
---|
930 | # Error si la particion está bloqueada. |
---|
931 | if ogIsLocked $1 $2; then |
---|
932 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2" |
---|
933 | return $? |
---|
934 | fi |
---|
935 | # Desmontar y borrar punto de montaje. |
---|
936 | umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\"" |
---|
937 | rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null |
---|
938 | else |
---|
939 | ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\"" |
---|
940 | fi |
---|
941 | } |
---|
942 | |
---|
943 | |
---|
944 | #/** |
---|
945 | # ogUnmountAll int_ndisk |
---|
946 | #@brief Desmonta todos los sistema de archivos de un disco, excepto el caché local. |
---|
947 | #@param int_ndisk nº de orden del disco |
---|
948 | #@return Nada |
---|
949 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
950 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
951 | #@warning No se desmonta la partición marcada como caché local. |
---|
952 | #@version 0.9 - Versión para OpenGnSys. |
---|
953 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
954 | #@date 2009/10/07 |
---|
955 | #*/ ## |
---|
956 | function ogUnmountAll () |
---|
957 | { |
---|
958 | # Variables locales |
---|
959 | local DISK PART |
---|
960 | # Si se solicita, mostrar ayuda. |
---|
961 | if [ "$*" == "help" ]; then |
---|
962 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1" |
---|
963 | return |
---|
964 | fi |
---|
965 | # Error si no se recibe 1 parámetro. |
---|
966 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
967 | |
---|
968 | # Obtener partición y punto de montaje. |
---|
969 | DISK="$(ogDiskToDev $1)" || return $? |
---|
970 | for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do |
---|
971 | case "$(ogGetFsType $1 $PART)" in |
---|
972 | CACHE) ;; |
---|
973 | *) ogUnmount $1 $PART 2>/dev/null ;; |
---|
974 | esac |
---|
975 | done |
---|
976 | } |
---|
977 | |
---|
978 | |
---|
979 | function ogGetFreeSize () { |
---|
980 | if [ $# = 0 ] |
---|
981 | then |
---|
982 | echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red |
---|
983 | echo "devuelve int_size : int_data : int_free" red |
---|
984 | return |
---|
985 | fi |
---|
986 | if [ $# -ge 2 ] |
---|
987 | then |
---|
988 | particion=`ogMount $1 $2 ` #1>/dev/null 2>&1 |
---|
989 | if [ -z $3 ] |
---|
990 | then |
---|
991 | unit=kB # s B kB MB GB TB % |
---|
992 | else |
---|
993 | unit=$3 |
---|
994 | fi |
---|
995 | case $unit in |
---|
996 | kB) |
---|
997 | factor="1.024"; |
---|
998 | #valor=`df | grep $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d:%d:%d", size,used,free}'` |
---|
999 | valor=`df | grep $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'` |
---|
1000 | ;; |
---|
1001 | MB) |
---|
1002 | factor="1.024/1000"; |
---|
1003 | valor=`df | grep $particion | awk -F" " '{size=$2*1.024/1000; used=$3*1.024/1000; free=$4*1.024/1000; printf "%d:%d:%d", size,used,free}'` |
---|
1004 | ;; |
---|
1005 | GB) |
---|
1006 | factor="1.024/1000000"; |
---|
1007 | valor=`df | grep $particion | awk -F" " '{size=$2*1.024/1000000; used=$3*1.024/1000000; free=$4*1.024/1000000; printf "%f:%f:%f", size,used,free}'` |
---|
1008 | ;; |
---|
1009 | esac |
---|
1010 | #echo $valor |
---|
1011 | #NumberRound $valor |
---|
1012 | #valor=`NumberRound $valor`; |
---|
1013 | ogUnmount $1 $2 1>/dev/null 2>&1 |
---|
1014 | echo $valor |
---|
1015 | |
---|
1016 | fi |
---|
1017 | } |
---|
1018 | |
---|