Drop grml-postfix
[grml-scripts.git] / usr_sbin / mkdosswapfile
1 #!/bin/sh
2 # Filename:      mkdosswapfile
3 # Purpose:       create GRML swapfile on an existing DOS partition
4 # Authors:       (c) Klaus Knopper Mar 2001, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 PATH="/bin:/sbin:/usr/bin:/usr/sbin"
10 export PATH
11
12 # XDIALOG_HIGH_DIALOG_COMPAT=1
13 # export XDIALOG_HIGH_DIALOG_COMPAT
14
15 [ "`id -u`" != "0" ] && exec sudo "$0" "$@"
16
17 #TMP="/tmp/mkdosswapfile.tmp$$"
18 TMP=$(mktemp)
19
20 bailout(){
21   rm -f "$TMP"
22   exit 0
23 }
24
25 DIALOG="dialog"
26 # [ -n "$DISPLAY" ] && [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog"
27
28 trap bailout 1 2 3 15
29
30 # LANGUAGE etc.
31 [ -r /etc/default/locale ] && . /etc/default/locale
32
33 DOSPARTITIONS=""
34
35 # Find all DOS partitions
36 if [ -f /proc/partitions ]
37 then
38 partitions=""
39 for p in $(awk 'BEGIN{old="__start"}{if($0==old){exit}else{old=$0;if($4&&$4!="name"){print $4}}}' /proc/partitions); do
40 case $p in
41  hd?|sd?) partitions="$partitions /dev/$p"; ;;
42  *) ;;
43 esac
44 done
45 if [ -n "$partitions" ]
46 then
47 foundp="$(LANG=C LC_ALL=C fdisk -l $partitions 2>/dev/null)"
48 for p in `echo "$foundp" | awk '/^\/dev\//{if(/FAT/){print $1}}'`
49 do
50 d="/mnt/${p##*/}"
51 if mount -o ro -t vfat $p $d 2>/dev/null; then
52 [ ! -f $d/grml.swp ] && DOSPARTITIONS="$DOSPARTITIONS $p"
53 umount $d
54 fi
55 done
56 fi
57 fi
58
59 if [ -n "$DOSPARTITIONS" ]; then
60 echo -n "\ec"
61 for p in $DOSPARTITIONS; do
62 # Language-dependent Messages
63 case "$LANGUAGE" in
64 de|at|ch)
65 MESSAGE1="Moechten Sie eine SWAP-Datei 'grml.swp' fuer GRML auf Ihrer bestehenden DOS-Partition $p anlegen? Eine solche SWAP-Datei ermoeglicht es Ihnen, trotz geringem Hauptspeicher Programmpakete wie KDE zu benutzen. Sie koennen diese Datei nach Beendigung Ihrer GRML-Session gefahrlos wieder loeschen."
66 MESSAGE2="Bitte geben Sie an, wieviel MB Sie als SWAP verwenden wollen. Empfohlen: 60 - 128. Frei: "
67 MESSAGE3="Erzeuge swapfile 'grml.swp' auf $p..."
68 ERROR1="Leider ist nicht genug Platz auf dieser Partition ($p), es sollten mindestens 60 MB frei sein."
69 SUCCESS="Das Einrichten des Swapfiles 'grml.swp' auf $p war erfolgreich."
70 ;;
71 es)
72 MESSAGE1="¿Quiere crear un fichero de memoria virtual (swap) 'grml.swp' en su partición DOS existente $p? Un fichero swap le permite utilizar grandes aplicaciones como KDE incluso si su ordenador tiene poca memoria. Puede borrar tranquilamente el fichero swap una vez haya finalizado su sesión con GRML."
73 MESSAGE2="Por favor, especifique la cantidad de espacio en disco que quiere utilizar como SWAP. Recomendado: 60 - 128. Libre: "
74 MESSAGE3="Creando archivo de memoria virtual 'grml.swp' en $p..."
75 ERROR1="Lo siento, no hay suficiente espacio libre en $p. Son necesarios al menos 60 MB."
76 SUCCESS="Archivo swap 'grml.swp' en $p creado satisfactoriamente."
77 ;;
78 *)
79 MESSAGE1="Do you want to create a swapfile 'grml.swp' on your existing DOS partition $p? A swapfile allows you to use huge application packages like KDE even if your computer is low on memory. You can safely delete the swapfile after finishing your GRML session."
80 MESSAGE2="Please specify the amount of diskspace that you want to use as SWAP. Recommended: 60 - 128. Free: "
81 MESSAGE3="Creating swapfile 'grml.swp' on $p..."
82 ERROR1="Sorry, not enough free space on $p. At least 60 MB required."
83 SUCCESS="Swapfile 'grml.swp' on $p successfully created."
84 ;;
85 esac
86
87 d="/mnt/${p##*/}"
88 f="$d/grml.swp"
89 if mount -o umask=000,rw -t vfat $p $d; then
90 if $DIALOG --yesno "$MESSAGE1" 11 62; then
91 AVAIL=$(df -m $d | awk '/^\/dev\//{print $4}')
92 if [ "$AVAIL" -lt 60 ]; then
93 $DIALOG --msgbox "$ERROR1" 10 45
94 umount $d
95 else
96 rm -f "$TMP"
97 $DIALOG --inputbox "$MESSAGE2 $AVAIL" 8 62 "60" 2>"$TMP" || { umount "$d" ; bailout; }
98 IN="`cat $TMP`" 
99 [ "$IN" -ge 60 -a "$IN" -le "$AVAIL" ] 2>/dev/null || IN="60"
100 echo "$MESSAGE3"
101 dd if=/dev/zero of="$f" bs=1000k count="$IN" && \
102 mkswap -v1 "$f" && swapon -v "$f" 2>/dev/null && \
103 echo "$f swap swap defaults 0 0" >>/etc/fstab
104 [ "$?" = "0" ] && { sleep 2 ; $DIALOG --msgbox "$SUCCESS" 10 45; } || umount "$d" 2>/dev/null
105 mount -o remount,ro $d
106 fi
107 else
108 umount "$d"
109 fi
110 fi
111 done
112 else
113 case "$LANGUAGE" in
114 de|at|ch)
115 ERROR2="Leider sind auf Ihrem System keine geeigneten DOS-Partitionen zum Einrichten eines Swapfile vorhanden."
116 ;;
117 es)
118 ERROR2="Lo siento, no se han encontrado particiones disponibles de tipo DOS para el fichero swap de memoria virtual."
119 ;;
120 *)
121 ERROR2="Sorry, no DOS partitions available for swapfile."
122 ;;
123 esac
124
125 $DIALOG --msgbox "$ERROR2" 10 45
126 fi
127
128 bailout
129
130 ## END OF FILE #################################################################