1 | #!/bin/bash |
---|
2 | # create-image.sh - Scirpt de ejemplo para crear una imagen de un sistema de archivos. |
---|
3 | # (puede usarse como base para el programa de creación de imágenes usado por OpenGNSys Admin). |
---|
4 | |
---|
5 | TIME1=$SECONDS |
---|
6 | PROG="$(basename $0)" |
---|
7 | if [ $# -ne 4 ]; then |
---|
8 | ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" |
---|
9 | exit $? |
---|
10 | fi |
---|
11 | |
---|
12 | # Si el repositorio es CACHE comprobamos que exista |
---|
13 | [ $3 == "REPO" -o $3 == "repo" ] || `ogFindCache >/dev/null` || ogRaiseError $OG_ERR_NOTFOUND "CACHE"|| exit $? |
---|
14 | |
---|
15 | |
---|
16 | # Obtener información de los parámetros de entrada. |
---|
17 | PART=$(ogDiskToDev "$1" "$2") || exit $? |
---|
18 | IMGDIR=$(ogGetParentPath "$3" "/$4") |
---|
19 | # Si no existe, crear subdirectorio de la imagen. |
---|
20 | if [ $? != 0 ]; then |
---|
21 | echo "[5] Crear subdirectorio de la imagen \"$3 $(dirname "$4")." |
---|
22 | ogMakeDir "$3" $(dirname "/$4") |
---|
23 | IMGDIR=$(ogGetParentPath "$3" "/$4") || exit $? |
---|
24 | fi |
---|
25 | IMGFILE=$IMGDIR/$(basename "/$4").img |
---|
26 | # Renombrar el fichero de imagen si ya existe. |
---|
27 | if [ -f "$IMGFILE" ]; then |
---|
28 | echo "[10] Renombrar \"$IMGFILE\" por \"$IMGFILE.ant\"." |
---|
29 | mv "$IMGFILE" "$IMGFILE.ant" |
---|
30 | fi |
---|
31 | # Mostrar información. |
---|
32 | echo "[15] $PROG: Origen=$PART, Destino=$IMGFILE" |
---|
33 | |
---|
34 | # Obtener tamaño de la partición. |
---|
35 | SIZE=$(ogGetPartitionSize "$1" "$2") |
---|
36 | # Reducir el sistema de archvios. |
---|
37 | echo "[20]: Reducir sistema de archivos." |
---|
38 | REDSIZE=$(ogReduceFs $1 $2) || REDSIZE=$[SIZE+1] |
---|
39 | if [ $REDSIZE -lt $SIZE ]; then |
---|
40 | echo "[25] Redimensionar partición a $REDSIZE KB." |
---|
41 | ogSetPartitionSize $1 $2 $REDSIZE |
---|
42 | fi |
---|
43 | # Crear la imagen. |
---|
44 | echo "[40] Crear imagen." |
---|
45 | #ejemplo test herramienta de clonacion |
---|
46 | #ogCreateImage "$@" partclone lzop |
---|
47 | ogCreateImage "$@" |
---|
48 | EXITCODE=$? |
---|
49 | # Restaurar tamaño. |
---|
50 | if [ $REDSIZE -lt $SIZE ]; then |
---|
51 | echo "[85] Redimensionar partición a $SIZE KB." |
---|
52 | ogSetPartitionSize $1 $2 $SIZE |
---|
53 | echo "[90] Extender sistema de archivos." |
---|
54 | ogExtendFs $1 $2 |
---|
55 | fi |
---|
56 | |
---|
57 | # Creamos fichero torrent. si REPO tracker=ipREPO, si CACHE tracker=ipLocal |
---|
58 | [ "$3" == "REPO" ] && IPTORRENT=`mount | grep ":/opt/opengnsys/images" | cut -f1 -d:` \ |
---|
59 | || IPTORRENT=`ogGetIpAddress` |
---|
60 | echo "[95] Crear torrent $3 $4 $IPTORRENT" |
---|
61 | ogCreateTorrent "$3" "$4" "$IPTORRENT" |
---|
62 | |
---|
63 | TIME=$[SECONDS-TIME1] |
---|
64 | echo "[100] Duración de la operación $[TIME/60]m $[TIME%60]s" |
---|
65 | exit $EXITCODE |
---|
66 | |
---|