OpenGnsys  1.1.1
FileSystem.lib
Ir a la documentación de este archivo.
1 #!/bin/bash
2 #
12 # ##
40 function ogCheckFs ()
41 {
42 # Variables locales.
43 local PART TYPE PROG PARAMS CODES ERRCODE
44 # Si se solicita, mostrar ayuda.
45 if [ "$*" == "help" ]; then
46  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
47  "$FUNCNAME 1 1"
48  return
49 fi
50 
51 # Error si no se reciben 2 parámetros.
52 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
53 # Obtener partición.
54 PART="$(ogDiskToDev $1 $2)" || return $?
55 
56 TYPE=$(ogGetFsType $1 $2)
57 case "$TYPE" in
58  EXT[234]|CACHE) PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;;
59  BTRFS) PROG="btrfsck"; CODES=(1) ;;
60  REISERFS) PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;;
61  REISER4) PROG="fsck.reiser4"; PARAMS="-ay" ;;
62  JFS) PROG="fsck.jfs"; CODES=(1 2) ;;
63  XFS) PROG="xfs_repair" ;;
64  F2FS) PROG="fsck.f2fs" ;;
65  NTFS) PROG="ntfsfix" ;;
66  EXFAT) PROG="fsck.exfat" ;;
67  FAT32) PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
68  FAT16) PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
69  FAT12) PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
70  HFS) PROG="fsck.hfs"; PARAMS="-f" ;;
71  HFSPLUS) PROG="fsck.hfs"; PARAMS="-f" ;;
72  UFS) PROG="fsck.ufs" ;;
73  ZFS) PROG="fsck.zfs" ;;
74  *) ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
75  return $? ;;
76 esac
77 # Error si el sistema de archivos esta montado o bloqueado.
78 ogUnmount $1 $2
79 if ogIsMounted $1 $2; then
80  ogRaiseError $OG_ERR_PARTITION "$1 $2" # Indicar nuevo error
81  return $?
82 fi
83 if ogIsLocked $1 $2; then
84  ogRaiseError $OG_ERR_LOCKED "$1 $2"
85  return $?
86 fi
87 # Comprobar en modo uso exclusivo.
88 ogLock $1 $2
89 trap "ogUnlock $1 $2" 1 2 3 6 9
90 eval $PROG $PARAMS $PART
91 ERRCODE=$?
92 case $ERRCODE in
93  0|${CODES[*]})
94  ERRCODE=0 ;;
95  127) ogRaiseError $OG_ERR_NOTEXEC "$PROG"
96  ERRCODE=$OG_ERR_NOTEXEC ;;
97  *) ogRaiseError $OG_ERR_PARTITION "$1 $2"
98  ERRCODE=$OG_ERR_PARTITION ;;
99 esac
100 ogUnlock $1 $2
101 return $ERRCODE
102 }
103 
104 
105 # ##
125 function ogExtendFs ()
126 {
127 # Variables locales.
128 local PART TYPE PROG PARAMS ERRCODE DOMOUNT
129 
130 # Si se solicita, mostrar ayuda.
131 if [ "$*" == "help" ]; then
132  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
133  "$FUNCNAME 1 1"
134  return
135 fi
136 # Error si no se reciben 2 parámetros.
137 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
138 
139 # Obtener partición.
140 PART="$(ogDiskToDev $1 $2)" || return $?
141 
142 # Redimensionar al tamano máximo según el tipo de partición.
143 TYPE=$(ogGetFsType $1 $2)
144 case "$TYPE" in
145  EXT[234]) PROG="resize2fs"; PARAMS="-f" ;;
146  BTRFS) PROG="btrfs"; PARAMS="filesystem resize max"
147  DOMOUNT=1 # Debe estar montado.
148  ;;
149  REISERFS|REISER4)
150  PROG="resize_reiserfs"; PARAMS="-f" ;;
151  F2FS) ;; # No se reduce (por el momento).
152  JFS) ;; # No se reduce (por el momento).
153  NILFS2) ;; # No se reduce (probar "nilfs-resize").
154  XFS) ;; # No se reduce (por el momento).
155  NTFS) PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
156  EXFAT) ;; # No se reduce (por el momento).
157  FAT32|FAT16) ;; # No se reduce (probar "fatresize").
158  HFS|HFSPLUS) ;; # No se reduce (por el momento).
159  UFS) ;; # No se reduce (por el momento).
160  *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
161  return $? ;;
162 esac
163 # Salida normal si no se va a aplicar la operación.
164 [ -z "$PROG" ] && return
165 # Error si el sistema de archivos no se queda en el estado de montaje adecuado.
166 if [ "$DOMOUNT" ]; then
167  PART=$(ogMount $1 $2) || return $? # Indicar nuevo error
168 else
169  ogUnmount $1 $2 2>/dev/null
170  if ogIsMounted $1 $2; then
171  ogRaiseError $OG_ERR_PARTITION "$1 $2" # Indicar nuevo error
172  return $?
173  fi
174 fi
175 # Error si el sistema de archivos está bloqueado.
176 if ogIsLocked $1 $2; then
177  ogRaiseError $OG_ERR_LOCKED "$1 $2"
178  return $?
179 fi
180 # Redimensionar en modo uso exclusivo.
181 ogLock $1 $2
182 trap "ogUnlock $1 $2" 1 2 3 6 9
183 eval $PROG $PARAMS $PART &>/dev/null
184 ERRCODE=$?
185 case $ERRCODE in
186  0) ;;
187  127) ogRaiseError $OG_ERR_NOTEXEC "$PROG"
188  ERRCODE=$OG_ERR_NOTEXEC ;;
189  *) ogRaiseError $OG_ERR_PARTITION "$1 $2"
190  ERRCODE=$OG_ERR_PARTITION ;;
191 esac
192 ogUnlock $1 $2
193 return $ERRCODE
194 }
195 
196 
197 # ##
201 function ogFormat ()
202 {
203 case "$*" in
204  CACHE|cache) ogFormatCache ;;
205  *) ogFormatFs "$@" ;;
206 esac
207 }
208 
209 
210 # ##
237 function ogFormatFs ()
238 {
239 # Variables locales
240 local PART ID TYPE LABEL PROG PARAMS LABELPARAM ERRCODE
241 
242 # Si se solicita, mostrar ayuda.
243 if [ "$*" == "help" ]; then
244  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys [str_label]" \
245  "$FUNCNAME 1 1" \
246  "$FUNCNAME 1 1 EXT4" \
247  "$FUNCNAME 1 1 \"DATA\"" \
248  "$FUNCNAME 1 1 EXT4 \"DATA\""
249  return
250 fi
251 # Error si no se reciben entre 2 y 4 parámetros.
252 [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
253 # Obtener fichero de dispositivo.
254 PART="$(ogDiskToDev $1 $2)" || return $?
255 # Error si la partición está montada o bloqueada.
256 if ogIsMounted $1 $2; then
257  ogRaiseError $OG_ERR_DONTFORMAT "$MSG_MOUNT: $1 $2"
258  return $?
259 fi
260 if ogIsLocked $1 $2; then
261  ogRaiseError $OG_ERR_LOCKED "$1 $2"
262  return $?
263 fi
264 # Si no se indica el tipo de sisitema de archivos, intentar obtenerlo.
265 TYPE="${3:-$(ogGetFsType $1 $2)}"
266 # Error, si no especifica el tipo de sistema de archivos a formatear.
267 [ -n "$TYPE" ] || ogRaiseError $OG_ERR_FORMAT "$1 $2 ..." || return $?
268 
269 # Elegir tipo de formato.
270 case "$TYPE" in
271  EXT2) PROG="mkfs.ext2"; PARAMS="-F" ;;
272  EXT3) PROG="mkfs.ext3"; PARAMS="-F" ;;
273  EXT4) PROG="mkfs.ext4"; PARAMS="-F" ;;
274  BTRFS) PROG="mkfs.btrfs"; PARAMS="-f" ;;
275  REISERFS) PROG="mkfs.reiserfs"; PARAMS="-f"; LABELPARAM="-l" ;;
276  REISER4) PROG="mkfs.reiser4"; PARAMS="-f <<<\"y\"" ;;
277  XFS) PROG="mkfs.xfs"; PARAMS="-f" ;;
278  JFS) PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;;
279  F2FS) PROG="mkfs.f2fs"; LABELPARAM="-l" ;;
280  NILFS2) PROG="mkfs.nilfs2"; PARAMS="-f" ;;
281  LINUX-SWAP) PROG="mkswap" ;;
282  NTFS) PROG="mkntfs"; PARAMS="-f" ;;
283  EXFAT) PROG="mkfs.exfat"; LABELPARAM="-n" ;;
284  FAT32) PROG="mkdosfs"; PARAMS="-F 32"; LABELPARAM="-n" ;;
285  FAT16) PROG="mkdosfs"; PARAMS="-F 16"; LABELPARAM="-n" ;;
286  FAT12) PROG="mkdosfs"; PARAMS="-F 12"; LABELPARAM="-n" ;;
287  HFS) PROG="mkfs.hfs" ;;
288  HFSPLUS) PROG="mkfs.hfsplus"; LABELPARAM="-v" ;;
289  UFS) PROG="mkfs.ufs"; PARAMS="-O 2" ;;
290  *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
291  return $? ;;
292 esac
293 
294 # Etiquetas de particion.
295 if [ -z "$LABEL" ]; then
296  [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
297  [ -n "$4" ] && PARAMS="$PARAMS ${LABELPARAM:-"-L"} $4"
298 else
299  PARAMS="$PARAMS ${LABELPARAM:-"-L"} $LABEL"
300 fi
301 
302 # Formatear en modo uso exclusivo (desmontar siempre).
303 ogLock $1 $2
304 trap "ogUnlock $1 $2" 1 2 3 6 9
305 umount $PART 2>/dev/null
306 eval $PROG $PARAMS $PART 2>/dev/null
307 ERRCODE=$?
308 case $ERRCODE in
309  0) ;;
310  127) ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
311  *) ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
312 esac
313 ogUnlock $1 $2
314 return $ERRCODE
315 }
316 
317 
318 # ##
335 function ogGetFsSize ()
336 {
337 # Variables locales.
338 local MNTDIR UNIT VALUE FACTOR SIZE
339 # Si se solicita, mostrar ayuda.
340 if [ "$*" == "help" ]; then
341  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_unit]" \
342  "$FUNCNAME 1 1 => 15624188" \
343  "$FUNCNAME 1 1 KB => 15624188"
344  return
345 fi
346 # Error si no se reciben 2 o 3 parámetros.
347 [ $# == 2 ] || [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
348 # Obtener unidad y factor de medida.
349 UNIT="$3"
350 UNIT=${UNIT:-"KB"}
351 case "$UNIT" in
352  [kK]B)
353  FACTOR=1 ;;
354  MB) FACTOR=1024 ;;
355  GB) FACTOR=$[1024*1024] ;;
356  TB) FACTOR=$[1024*1024*1024] ;;
357  *) ogRaiseError $OG_ERR_FORMAT "$3 != { KB, MB, GB, TB }"
358  return $? ;;
359 esac
360 
361 # Obtener el tamaño del sistema de archivo (si no está formateado; tamaño = 0).
362 MNTDIR="$(ogMount $1 $2 2>/dev/null)"
363 if [ -n "$MNTDIR" ]; then
364  VALUE=$(df -BK "$MNTDIR" | awk '{getline; print $2}')
365  SIZE=$(echo "$VALUE $FACTOR" | awk '{printf "%f\n", $1/$2}')
366 else
367  SIZE=0
368 fi
369 # Devolver el tamaño (quitar decimales si son 0).
370 echo ${SIZE%.0*}
371 }
372 
373 
374 # ##
399 function ogGetFsType ()
400 {
401 # Variables locales.
402 local PART TYPE
403 # Si se solicita, mostrar ayuda.
404 if [ "$*" == "help" ]; then
405  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
406  "$FUNCNAME 1 1 => NTFS"
407  return
408 fi
409 # Error si no se reciben 2 parámetros.
410 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
411 
412 # Detectar tipo de sistema de archivo (independientemente del tipo de partición).
413 PART=$(ogDiskToDev "$1" "$2") || return $?
414 if [[ "$PART" =~ ^/ ]]; then
415  TYPE=$(blkid -o export $PART | awk -F= '$1~/^TYPE/ { print toupper($2) }')
416 else
417  zfs mount $PART 2>/dev/null
418  TYPE=$(mount | awk "\$1==\"$PART\" { print toupper(\$5) }")
419 fi
420 
421 # Componer valores correctos.
422 case "$TYPE" in
423  EXT4) # Comprobar si es caché o Ext4.
424  if [ "$1 $2" == "$(ogFindCache)" ]; then
425  ogIsFormated $1 $2 2>/dev/null && TYPE="CACHE"
426  fi
427  ;;
428  VFAT) TYPE="$(blkid -po export $PART | awk -F= '$1~/^VERSION$/ { print toupper($2) }')" ;;
429  SWAP) TYPE="LINUX-SWAP" ;;
430  LVM*) TYPE="LINUX-LVM" ;;
431  *RAID*) TYPE="LINUX-RAID" ;;
432  ZFS_MEMBER) TYPE="ZVOL" ;;
433  *_MEMBER) TYPE="${TYPE/_MEMBER/}" ;;
434 esac
435 
436 [ -n "$TYPE" ] && echo "$TYPE"
437 }
438 
439 
440 # ##
456 function ogGetMountPoint ()
457 {
458 # Variables locales
459 local PART
460 # Si se solicita, mostrar ayuda.
461 if [ "$*" == "help" ]; then
462  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
463  "$FUNCNAME 1 1 => /mnt/sda1"
464  return
465 fi
466 # Error si no se reciben 2 parámetros.
467 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
468 # Obtener partición.
469 PART="$(ogDiskToDev $1 $2)" || return $?
470 
471 # Devolver punto de montaje.
472 findmnt -n -o TARGET $PART
473 }
474 
475 
476 # ##
495 function ogIsFormated ()
496 {
497 # Variables locales
498 local PART
499 if [ "$*" == "help" ]; then
500  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
501  "if $FUNCNAME 1 1; then ... ; fi"
502  return
503 fi
504 # Falso, en caso de error.
505 [ $# == 2 ] || return 1
506 PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
507 
508 # Revisar tipo de sistema de ficheros.
509 if [[ "$PART" =~ ^/ ]]; then
510  # Sistemas de ficheros genéricos.
511  test -n "$(blkid -s TYPE $PART | egrep -vi "swap|_member")"
512 else
513  # ZFS.
514  test "$(zfs list -Hp -o canmount $PART 2>/dev/null)" = "on"
515 fi
516 }
517 
518 
519 #
523 function ogIsLocked ()
524 {
526 }
527 
528 # ##
546 {
547 # Variables locales
548 local DISK PART LOCKDISK LOCKPART
549 
550 # Si se solicita, mostrar ayuda.
551 if [ "$*" == "help" ]; then
552  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
553  "if $FUNCNAME 1 1; then ... ; fi"
554  return
555 fi
556 # Falso, en caso de error.
557 [ $# == 2 ] || return 1
558 PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
559 DISK="$(ogDiskToDev $1)"
560 
561 # Comprobar existencia de fichero de bloqueo de la partición o de su disco.
562 LOCKDISK="/var/lock/lock${DISK//\//-}"
563 LOCKPART="/var/lock/lock${PART//\//-}"
564 test -f $LOCKDISK -o -f $LOCKPART
565 }
566 
567 
568 # ##
581 function ogIsMounted ()
582 {
583 # Si se solicita, mostrar ayuda.
584 if [ "$*" == "help" ]; then
585  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
586  "if $FUNCNAME 1 1; then ... ; fi"
587  return
588 fi
589 # Falso, en caso de error.
590 [ $# == 2 ] || return 1
591 
592 test -n "$(ogGetMountPoint $1 $2)"
593 }
594 
595 
596 # ##
606 
607 function ogIsReadonly ()
608 {
609 # Variables locales
610 local PART
611 
612 # Si se solicita, mostrar ayuda.
613 if [ "$*" == "help" ]; then
614  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
615  "if $FUNCNAME 1 1; then ... ; fi"
616  return
617 fi
618 # Falso, en caso de error.
619 [ $# == 2 ] || return 1
620 PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
621 
622 test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^ro$/ {print}')"
623 }
624 
625 
626 # ##
636 function ogIsWritable ()
637 {
638 # Variables locales
639 local PART
640 
641 # Si se solicita, mostrar ayuda.
642 if [ "$*" == "help" ]; then
643  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
644  "if $FUNCNAME 1 1; then ... ; fi"
645  return
646 fi
647 # Falso, en caso de error.
648 [ $# == 2 ] || return 1
649 PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
650 
651 test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^rw$/ {print}')"
652 }
653 
654 
655 #
659 function ogLock ()
660 {
661 ogLockPartition "$@"
662 }
663 
664 # ##
677 function ogLockPartition ()
678 {
679 # Variables locales
680 local PART LOCKFILE
681 
682 # Si se solicita, mostrar ayuda.
683 if [ "$*" == "help" ]; then
684  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
685  "$FUNCNAME 1 1"
686  return
687 fi
688 # Error si no se reciben 2 parámetros.
689 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
690 
691 # Obtener partición.
692 PART="$(ogDiskToDev $1 $2)" || return $?
693 
694 # Crear archivo de bloqueo exclusivo.
695 LOCKFILE="/var/lock/lock${PART//\//-}"
696 touch $LOCKFILE
697 }
698 
699 
700 # ##
704 function ogMount ()
705 {
706 case "$*" in
707  CACHE|cache)
708  ogMountCache ;;
709  CDROM|cdrom)
710  ogMountCdrom ;;
711  *) ogMountFs "$@" ;;
712 esac
713 }
714 
715 
716 # ##
722 function ogMountFirstFs ()
723 {
724 # Variables locales
725 local PART NPARTS MNTDIR
726 
727 # Error si no se recibe 1 parámetro.
728 [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
729 
730 # Obtener nº de particiones del disco.
731 NPARTS=$(ogGetPartitionsNumber "$1") || return $?
732 for (( PART = 1; PART <= NPARTS; PART++ )); do
733  MNTDIR=$(ogMount $1 $PART 2>/dev/null)
734  if [ -n "$MNTDIR" ]; then
735  echo "$MNTDIR"
736  return 0
737  fi
738 done
739 ogRaiseError $OG_ERR_NOTFOUND "$1"
740 return $OG_ERR_NOTFOUND
741 }
742 
743 
744 # ##
766 function ogMountFs ()
767 {
768 # Variables locales
769 local PART MNTDIR
770 
771 # Si se solicita, mostrar ayuda.
772 if [ "$*" == "help" ]; then
773  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
774  "$FUNCNAME 1 1 => /mnt/sda1"
775  return
776 fi
777 # Error si no se reciben 2 parámetros.
778 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
779 
780 # Obtener partición.
781 PART="$(ogDiskToDev "$1" "$2")" || return $?
782 
783 # Comprobar si el sistema de archivos ya está montada.
784 MNTDIR="$(ogGetMountPoint $1 $2)"
785 # Si no, montarlo en un directorio de sistema.
786 if [ -z "$MNTDIR" ]; then
787  # Error si la particion esta bloqueada.
788  if ogIsLocked $1 $2; then
789  ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
790  return $?
791  fi
792  # El camino de un dispositivo normal comienza por el carácter "/".
793  if [[ "$PART" =~ ^/ ]]; then
794  # Crear punto de montaje o enlace simbólico para caché local.
795  MNTDIR=${PART/dev/mnt}
796  DEBUG="no"
797  if [ "$(ogFindCache)" == "$1 $2" -a -n "$OGCAC" ]; then
798  mkdir -p $OGCAC
799  ln -fs $OGCAC $MNTDIR
800  else
801  mkdir -p $MNTDIR
802  fi
803  unset DEBUG
804  # Montar sistema de archivos.
805  mount $PART $MNTDIR &>/dev/null || \
806  mount $PART $MNTDIR -o force,remove_hiberfile &>/dev/null
807  case $? in
808  0) # Correcto.
809  ;;
810  14) # Intentar limpiar hibernación NTFS y montar.
811  ntfsfix -d $PART &>/dev/null && mount $PART $MNTDIR &>/dev/null || \
812  ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
813  ;;
814  *) # Probar montaje de solo lectura.
815  mount $PART $MNTDIR -o ro &>/dev/null || \
816  ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
817  ;;
818  esac
819  # Aviso de montaje de solo lectura.
820  if ogIsReadonly $1 $2; then
821  ogEcho warning "$FUNCNAME: $MSG_MOUNTREADONLY: \"$1, $2\""
822  fi
823  else
824  # Montar sistema de archivos ZFS (un ZPOOL no comienza por "/").
825  zfs mount $PART 2>/dev/null
826  fi
827 fi
828 echo "$MNTDIR"
829 }
830 
831 
832 # ##
842 function ogMountCdrom ()
843 {
844 local DEV MNTDIR
845 # Si se solicita, mostrar ayuda.
846 if [ "$*" == "help" ]; then
847  ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME"
848  return
849 fi
850 # Error si se reciben parámetros.
851 [ $# == 0 ] || ogRaiseError $OG_ERR_FORMAT || return $?
852 DEV="/dev/cdrom" # Por defecto
853 MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
854 if [ -z "$MNTDIR" ]; then
855  MNTDIR=${DEV/dev/mnt}
856  mkdir -p $MNTDIR
857  mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
858 fi
859 echo $MNTDIR
860 }
861 
862 
863 # ##
894 function ogReduceFs ()
895 {
896 # Variables locales
897 local PART BLKS SIZE MAXSIZE EXTRASIZE=0 RETVAL
898 
899 # Si se solicita, mostrar ayuda.
900 if [ "$*" == "help" ]; then
901  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
902  "$FUNCNAME 1 1"
903  return
904 fi
905 # Error si no se reciben 2 parámetros.
906 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
907 
908 # Obtener partición.
909 PART="$(readlink -f "$(ogDiskToDev $1 $2)")" || return $?
910 
911 # Redimensionar según el tipo de particion.
912 case "$(ogGetFsType $1 $2)" in
913  EXT[234])
914  ogUnmount $1 $2 &>/dev/null
915  resize2fs -fpM $PART &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
916  ;;
917 
918  BTRFS)
919  MNTDIR=$(ogMount $1 $2)
920  # Calcular tamaño ocupado + 10%, redondeado + 1 (incluyendo letra de unidad).
921  SIZE=$(btrfs filesystem show $MNTDIR | awk -v P=$PART '{ d=""; "readlink -f "$8" 2>/dev/null"|getline d; if(d==P) printf("%d%s", $6*1.1+1, substr($6,match($6,/[A-Z]/),1)) }')
922  btrfs filesystem resize ${SIZE} $MNTDIR &>/dev/null
923  ;;
924  REISERFS|REISER4)
925  # Calcular tamaño ocupado + 10%.
926  MNTDIR=$(ogMount $1 $2)
927  SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
928  ogUnmount $1 $2 2>/dev/null
929  resize_reiserfs -s${SIZE}K $PART <<<"y"
930  ;;
931 
932  F2FS) ;; # No se reduce (por el momento).
933  JFS) ;; # No se reduce (por el momento).
934  NILFS2) ;; # No se reduce (probar "nilfs-resize").
935  XFS) ;; # No se reduce (por el momento).
936 
937  NTFS)
938  # Calcular tamaño ocupado + 10%.
939  ogUnmount $1 $2 &>/dev/null
940  read -e MAXSIZE SIZE <<<$(ntfsresize -fi $PART | \
941  awk '/device size/ {d=$4}
942  /resize at/ {r=int($5*1.1/1024+1)*1024}
943  END { print d,r}')
944  # Error si no puede obtenerse el tamaño máximo del volumen.
945  [ -n "$MAXSIZE" -a -n "$SIZE" ] || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
946  # Simular la redimensión y comprobar si es necesario ampliarala.
947  RETVAL=1
948  while [ $RETVAL != 0 -a $[ SIZE+=EXTRASIZE ] -lt $MAXSIZE ]; do
949  # Obtener espacio de relocalización y devolver código de salida
950  # (ntfsresize devuelve 0 si no necesita relocalizar).
951  EXTRASIZE=$(ntfsresize -fns $SIZE $PART 2>/dev/null | \
952  awk '/Needed relocations/ {print int($4*1.1/1024+1)*1024}'
953  exit ${PIPESTATUS[0]})
954  RETVAL=$?
955  done
956  # Redimensionar solo si hace falta.
957  if [ $SIZE -lt $MAXSIZE ]; then
958  ntfsresize -fs $SIZE $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
959  fi
960  ;;
961 
962  EXFAT) ;; # No se reduce (por el momento).
963  FAT32|FAT16) # Se deja comentado por no haber un método seguro para extender el SF.
964  # Calcular tamaño ocupado + 10%.
965  #ogUnmount $1 $2 &>/dev/null
966  #SIZE=$(fatresize --info $PART | awk -F: '/Min size/ {printf("%d", $2*1.1)}')
967  #[ "$SIZE" ] && fatresize --size $SIZE $PART &>/dev/null
968  ;;
969  HFS|HFSPLUS) ;; # No se reduce (por el momento).
970  UFS) ;; # No se reduce (por el momento).
971 
972  *) ogRaiseError $OG_ERR_PARTITION "$1,$2"
973  return $? ;;
974 esac
975 
976 # Devuelve tamaño del sistema de ficheros.
977 ogGetFsSize $1 $2
978 }
979 
980 
981 # ##
985 function ogUnlock ()
986 {
988 }
989 
990 # ##
1004 {
1005 # Variables locales
1006 local PART LOCKFILE
1007 
1008 # Si se solicita, mostrar ayuda.
1009 if [ "$*" == "help" ]; then
1010  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
1011  "$FUNCNAME 1 1"
1012  return
1013 fi
1014 # Error si no se reciben 2 parámetros.
1015 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1016 
1017 # Obtener partición.
1018 PART="$(ogDiskToDev $1 $2)" || return $?
1019 
1020 # Borrar archivo de bloqueo exclusivo.
1021 LOCKFILE="/var/lock/lock${PART//\//-}"
1022 rm -f $LOCKFILE
1023 }
1024 
1025 
1026 # ##
1030 function ogUnmount ()
1031 {
1032 ogUnmountFs "$@"
1033 }
1034 
1035 # ##
1051 function ogUnmountFs ()
1052 {
1053 # Variables locales
1054 local PART MNTDIR
1055 
1056 # Si se solicita, mostrar ayuda.
1057 if [ "$*" == "help" ]; then
1058  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
1059  return
1060 fi
1061 # Error si no se reciben 2 parámetros.
1062 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1063 
1064 # Obtener partición y punto de montaje.
1065 PART="$(ogDiskToDev $1 $2)" || return $?
1066 MNTDIR="$(ogGetMountPoint $1 $2)"
1067 
1068 # Si está montada, desmontarla.
1069 if [ -n "$MNTDIR" ]; then
1070  # Error si la particion está bloqueada.
1071  if ogIsLocked $1 $2; then
1072  ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
1073  return $?
1074  fi
1075  # Desmontar y borrar punto de montaje.
1076  umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
1077  rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
1078 else
1079  ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
1080 fi
1081 }
1082 
1083 
1084 # ##
1096 function ogUnmountAll ()
1097 {
1098 # Variables locales
1099 local DISK PART
1100 # Si se solicita, mostrar ayuda.
1101 if [ "$*" == "help" ]; then
1102  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1103  return
1104 fi
1105 # Error si no se recibe 1 parámetro.
1106 [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1107 
1108 # Obtener partición y punto de montaje.
1109 DISK="$(ogDiskToDev $1)" || return $?
1110 for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
1111  case "$(ogGetFsType $1 $PART)" in
1112  CACHE) ;;
1113  *) ogUnmount $1 $PART 2>/dev/null ;;
1114  esac
1115 done
1116 }
1117 
1118 # ##
1129 function ogUnsetDirtyBit ()
1130 {
1131 # Variables locales
1132 local PART
1133 # Si se solicita, mostrar ayuda.
1134 if [ "$*" == "help" ]; then
1135  ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1136  return
1137 fi
1138 # Error si no se reciben 2 parámetros.
1139 [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1140 
1141 # Obtener partición y punto de montaje.
1142 case "$(ogGetFsType $1 $2)" in
1143  NTFS)
1144  ogUnmount $1 $2 2>/dev/null
1145  PART="$(ogDiskToDev $1 $2)" || return $?
1146  ntfsfix -d $PART ;;
1147  *) ;;
1148 esac
1149 }
1150 
1151 
1152 # ##
1165 
1166 function ogGetFreeSize ()
1167 {
1168 local particion unit factor valor
1169 if [ $# = 0 ]
1170 then
1171  echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
1172  echo "devuelve int_size : int_data : int_free" red
1173 return
1174 fi
1175 if [ $# -ge 2 ]
1176 then
1177  particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
1178  if [ -z $3 ]
1179  then
1180  unit=kB # s B kB MB GB TB %
1181  else
1182  unit=$3
1183  fi
1184  case $unit in
1185  kB)
1186  factor="1.024";
1187  #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}'`
1188  valor=`df | grep $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
1189  ;;
1190  MB)
1191  factor="1.024/1000";
1192  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}'`
1193  ;;
1194  GB)
1195  factor="1.024/1000000";
1196  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}'`
1197  ;;
1198  esac
1199  #echo $valor
1200  #NumberRound $valor
1201  #valor=`NumberRound $valor`;
1202  echo $valor
1203 fi
1204 }
1205 
function ogDiskToDev()
Devuelve la equivalencia entre el nº de orden del dispositivo (dicso o partición) y el nombre de fich...
Definition: Disk.lib:484
function ogUnmountFs()
Desmonta un sistema de archivos. #.
function ogLock()
Definition: FileSystem.lib:659
function ogIsReadonly()
Comprueba si un sistema de archivos está montado solo de lectura. #.
Definition: FileSystem.lib:607
function ogFormatCache()
Formatea el sistema de ficheros para la caché local. #.
Definition: Cache.lib:275
function ogGetFsType()
Devuelve el mnemonico con el tipo de sistema de archivos. #.
Definition: FileSystem.lib:399
function ogReduceFs()
Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre. #.
Definition: FileSystem.lib:894
function ogCheckFs()
Comprueba el estado de un sistema de archivos. #.
Definition: FileSystem.lib:40
function ogRaiseError()
Devuelve el mensaje y el código de error correspondiente. #.
Definition: System.lib:188
function ogIsPartitionLocked()
Comprueba si una partición o su disco están bloqueados por una operación de uso exclusivo. #.
Definition: FileSystem.lib:545
function mount()
Definition: ToolsGNU.c:121
function ogHelp()
Muestra mensaje de ayuda para una función determinda. #.
Definition: System.lib:153
function ogFormat()
Definition: FileSystem.lib:201
function ogUnsetDirtyBit()
Inhabilita el Dirty Bit del sistema de ficheros NTFS para evitar un CHKDSK en el primer arranque #...
function ogGetFsSize()
Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida...
Definition: FileSystem.lib:335
function ogIsWritable()
Comprueba si un sistema de archivos está montado de lectura y escritura. #.
Definition: FileSystem.lib:636
function ogIsMounted()
Comprueba si un sistema de archivos está montado. #.
Definition: FileSystem.lib:581
function ogMountFs()
Monta un sistema de archivos. #.
Definition: FileSystem.lib:766
function ogMountFirstFs()
Monta el primer sistema de archivos disponible en el disco. #.
Definition: FileSystem.lib:722
function ogExtendFs()
Extiende un sistema de archivos al tamaño de su partición. #.
Definition: FileSystem.lib:125
function ogMountCache()
Monta la partición Cache y exporta la variable $OGCAC #.
Definition: Cache.lib:395
function ogGetFreeSize()
muestra informacion del tamaño total, datos y libre. #
function umount()
Definition: ToolsGNU.c:143
function ogUnmountAll()
Desmonta todos los sistema de archivos de un disco, excepto el caché local. #.
function ogIsLocked()
Definition: FileSystem.lib:523
function ogUnlockPartition()
Elimina el fichero de bloqueo para una particion. #.
function ogGetMountPoint()
Devuelve el punto de montaje de un sistema de archivos. #.
Definition: FileSystem.lib:456
function ogMount()
Definition: FileSystem.lib:704
function ogUnmount()
function ogFormatFs()
Formatea un sistema de ficheros según el tipo de su partición. #.
Definition: FileSystem.lib:237
function ogLockPartition()
Genera un fichero de bloqueo para una partición en uso exlusivo. #.
Definition: FileSystem.lib:677
function ogMountCdrom()
Monta dispositivo óptico por defecto #.
Definition: FileSystem.lib:842
function ogGetPartitionsNumber()
Detecta el numero de particiones del disco duro indicado. #.
Definition: Disk.lib:915
function ogIsFormated()
Comprueba si un sistema de archivos está formateado. #.
Definition: FileSystem.lib:495
function ogUnlock()
Definition: FileSystem.lib:985
function ogEcho()
Muestra mensajes en consola y lo registra en fichero de incidencias. #.
Definition: System.lib:34
function awk()
Definition: ToolsGNU.c:89