Load Lat15-Terminus16 console font if no framebuffer is available.
[grml-autoconfig.git] / bin / mkpersistenthome
1 #!/bin/bash
2 # Filename:      mkpersistenthome
3 # Purpose:       mkpersistenthome - create grml home directory image
4 # Authors:       grml-team (grml.org), (c) Klaus Knopper Feb 2003, (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 # Latest change: Fre Mär 16 19:59:53 CET 2007 [mika]
8 ################################################################################
9
10 export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
11
12 if [ "`id -u`" != "0" ] ; then
13    exec sudo "$0" "$@"
14 else
15    export HOME='/home/grml'
16 fi
17
18 TMP=$(mktemp)
19 DIRECTORY=$(mktemp -d /mnt/XXXXXX)
20 DIALOG="dialog"
21
22 bailout(){
23   [ -n "$DIRECTORY" ] && umount "$DIRECTORY" 2>/dev/null
24   [ -d "$DIRECTORY" ] && rmdir "$DIRECTORY"
25   rm -f "$TMP" "$TMP.done" "$TMP.err"
26   exit 0
27 }
28
29 trap bailout 1 2 3 15
30
31 HOMEKB="$(du -sk $HOME 2>/dev/null | awk '{print $1}')"
32
33 # Language-dependent messages
34 case "$LANGUAGE" in
35 de*|at*|ch*)
36 TITLE1="Permanentes grml Heimverzeichnis anlegen"
37 MESSAGE1="Dieses Skript richtet ein permanentes Heimverzeichnis für den User \"grml\" auf Ihrer Festplatte oder einem Wechselmedium wie Memorystick, Compactflash oder ZIP-Medium ein. Dadurch ist es möglich, Daten und persönliche Konfigurationen über einen Reboot hinaus zu speichern, sofern sie im persönlichen Heimverzeichnis des Benutzers abgelegt werden. Per Bootoption, z.B. \"home=/dev/sda1\" für die erste Partition einer SCSI-Festplatte oder eines USB-Memorystick, können Sie beim Systemstart das permanente Heimverzeichnis wieder aktivieren. Die Bootoption \"home=scan\" durchsucht automatisch alle Partitionen und erkannten Speichermedien nach einem persistenten grml-Heimverzeichnis.
38
39 Möchten Sie das Heimverzeichnis des Benutzers \"grml\" permanent speichern?"
40 MESSAGE2="Bitte wählen Sie die Partition, auf der Sie das Heimverzeichnis (oder ein Image davon) anlegen möchten."
41 MESSAGE3="Möchten Sie die KOMPLETTE Partition verwenden und mit den Linux-Dateisystem formatieren?
42
43 NEIN=Image-Datei auf bestehendem Dateisystem anlegen."
44 SUCCESS="FERTIG!
45
46 Die grml-Konfiguration wurde erfolgreich gespeichert. Ihre Konfigurationsdateien werden beim nächsten grml-Bootvorgang in die Ramdisk restauriert, wenn Sie im Bootscreen \"grml floppyconfig\" (Diskette) bzw. \"grml myconfig=/mnt/verzeichnisname\" angeben."
47 ERROR="Leider konnte die grml-Konfiguration NICHT gespeichert werden:"
48 MESSAGE_NO_PARTS="Keine passende Partition gefunden. Zur Erinnerung: NTFS-Partitonen können mit Linux nicht beschrieben werden."
49 ;;
50 *)
51 TITLE1="Create persistent grml home directory"
52 MESSAGE1="This script creates a persistent home directory for the \"grml\" account on your harddisk or on a changeable medium like memory stick, compact flash or zip media. Using this features makes it possible to store personal data and config files permanently over a reboot, if they are stored inside the home directory of the \"grml\" user. The boot option \"home=/dev/sda1\", for the first partition of a SCSI harddisk or USB memory stick as example, activates the persistent home directory at system startup. You can also let grml scan all autodetected storage devices using the boot option \"home=scan\".
53
54 Do you want to create a persistent home directory for the \"grml\" user?"
55
56 MESSAGE2="Please select partition for creating persistent homedir (or image):"
57 MESSAGE3="Do you want to use the ENTIRE Partition, and would like to format it with the Linux filesystem?
58
59 NO=Just create an image in the existing filesystem."
60 SUCCESS="SUCCESS!
61
62 Creation of grml configuration floppy was successful. Your configuration files will be reinstalled to the ramdisk on next grml boot if you specify \"grml floppyconf\" (floppy disk), or \"grml myconfig=/mnt/directoryname\" at the boot prompt."
63 ERROR="The grml configuration could NOT be saved:"
64 MESSAGE_NO_PARTS="No suitable partitions could be found. Remember that NTFS-Partitions are not writable with linux."
65 ;;
66 esac
67
68 $DIALOG --cr-wrap --clear --title "$TITLE1" --yesno "$MESSAGE1" 18 75 || bailout
69
70 # Partition selector
71 PARTITIONS=""
72 count=0
73 for i in `fdisk -l | grep -v NTFS | awk '/^\/dev\// { print $1 }'` ; do
74   PARTITIONS="$PARTITIONS ${i} [Disk/Partition] off"
75   echo "PARTITIONS=$PARTITIONS ${i} [Disk/Partition] off" >> /tmp/part
76 done
77 [ -z "$PARTITIONS" ] && { $DIALOG --cr-wrap --clear --title "$TITLE1" --msgbox "$ERROR $MESSAGE_NO_PARTS" 10 75; bailout; }
78
79 PARTITION=""
80 while [ -z "$PARTITION" -o -z "$DIRECTORY" -o ! -e "$DIRECTORY" ]; do
81   rm -f "$TMP"
82   $DIALOG --cr-wrap --clear --title "$TITLE1" --radiolist "$MESSAGE2" 18 75 9 $PARTITIONS 2>"$TMP" || bailout
83   PARTITION="$(<$TMP)"
84 done
85
86 DEVICE=""
87 if $DIALOG --cr-wrap --clear --title "$TITLE1" --defaultno --yesno "$MESSAGE3" 15 75; then
88   DEVICE="$PARTITION"
89   FILEINFO="$PARTITION"
90 else
91   FILEINFO="grml.img on $PARTITION"
92   DEVICE="$DIRECTORY/grml.img"
93   mount | grep -q "$DIRECTORY" || mount -r $PARTITION "$DIRECTORY" 2>"$TMP.err"
94   [ "$?" != "0" ] && { $DIALOG --cr-wrap --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75; bailout; }
95   mount | grep -q "$DIRECTORY.*ntfs" && { $DIALOG --cr-wrap --title "$TITLE1" --msgbox "$ERROR NTFS" 10 75; bailout; }
96   mount -o remount,rw $PARTITION "$DIRECTORY"
97 fi
98
99 # More language-dependent messages
100 case "$LANGUAGE" in
101  de*|at*|ch*)
102   MESSAGE4="Sind Sie ABSOLUT SICHER, dass Sie die Partition $PARTITION formatieren möchten? ALLE DATEN WERDEN DADURCH GELÖSCHT!"
103   MESSAGE5="Möchten Sie Ihr Heimverzeichnis mit AES256 (=Advanced Encryption Standard 256bit, s.a. http://csrc.nist.gov/encryption/aes/) verschlüsselt speichern? Hierzu ist die Eingabe eines sehr langen Passwortes beim Einrichten sowie beim Einbinden des Verzeichnisses beim Systemstart erforderlich. NEIN=unverschlüsselt"
104   MESSAGE6="Bitte geben Sie die gewünschte Größe Ihres persistenten Heimverzeichnisses in MB an (aktuell belegt: $HOMEKB Kilobyte, verfügbar:"
105   MESSAGE7="Formatiere $FILEINFO mit dem ext2 Dateisystem und kopiere Daten..."
106   MESSAGE8="Lege Datenbereich für Linux an..."
107   SUCCESS="$FILEINFO wurde erfolgreich mit dem Linux ext2 Dateisystem formatiert, und Ihre Heimverzeichnis-Daten wurden darauf transferiert.
108
109 Sie können jetzt Ihren Computer neu starten, und beim grml boot:-Prompt durch Eingabe von \"grml home=$PARTITION\" oder \"grml home=scan\" Ihr permanentes Heimverzeichnis aktivieren. Falls Sie außerdem (mit dem save-config Skript) die Systemeinstellungen gesichert haben, können Sie diese Option mit der Angabe von \"myconf=$PARTITION\" kombinieren."
110  ;;
111  *)
112   MESSAGE4="Are you positively SURE that you want to format partition $PARTITION? ALL DATA WILL BE LOST!"
113   MESSAGE5="Do you want to save your home directory encrypted with AES256 (Advanced Encryption Standard, see http://csrc.nist.gov/encryption/aes/)? If yes, you will have to specify a very long password at homedir creation and boot time."
114   MESSAGE6="Please enter the desired size of your persistent homedir in MB (currently used: $HOMEKB kB, available:"
115   MESSAGE7="Formatting $FILEINFO with ext2 filesystem and copying data..."
116   MESSAGE8="Preparing for Linux filesystem..."
117   SUCCESS="$FILEINFO has been succeessfully formatted with the Linux ext2 filesystem, and your home directory has been transferred to it.
118
119 You may now reboot your computer and type \"grml home=$PARTITION\" or \"grml home=scan\" at the grml boot: prompt to take advantage of your new persistent home directory. If you have also saved the system configuration (using the save-config script), you can use this in combination with \"myconf=$PARTITION\"."
120  ;;
121 esac
122
123 gauge(){
124   rm -f "$TMP.done"
125   status=0
126   while [ ! -e "$TMP.done" ]; do
127     echo "$status" ; status="`expr \( 100 - $status \) / 4 + $status`"; sleep 2;
128   done | $DIALOG --title "$TITLE1" --gauge "$1" 8 75 0
129 }
130
131 # Stop status bar
132 killgauge(){
133   touch "$TMP.done" ; wait ; rm -f "$TMP.done"
134 }
135
136 if [ "$DEVICE" = "$PARTITION" ]; then
137   $DIALOG --cr-wrap --clear --title "$TITLE1" --defaultno --yesno "$MESSAGE4" 6 75 || bailout
138 else
139   AMOUNT=0
140   AVAIL="$(df -m $DIRECTORY/. | awk '/^\/dev\//{print $4}')"
141
142   until [ "$AMOUNT" -ge 1 -a "$AMOUNT" -le "$AVAIL" ] 2>/dev/null; do
143     $DIALOG --cr-wrap --clear --title "$TITLE1" --inputbox "$MESSAGE6 $AVAIL MB)" 10 62 "30" 2>"$TMP" || bailout
144     AMOUNT="$(<$TMP)"
145   done
146
147   gauge "$MESSAGE8" &
148   dd if=/dev/urandom of="$DEVICE" bs=1M count="$AMOUNT" 1>/dev/null || { killgauge; sleep 2; bailout; }
149   killgauge
150 fi
151
152 # Normal loopback file or partition
153 gauge "$MESSAGE7" &
154 umount "$DEVICE" 2>/dev/null
155
156 # create filesystem
157 mke2fs -F -m0 "$DEVICE" 1>/dev/null 2>"$TMP.err" || { killgauge; $DIALOG --cr-wrap --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75; bailout; }
158
159 # mount for synch
160 LOOPOPT=""
161 [ -f "$DEVICE" ] && LOOPOPT="-o loop"
162 mkdir -p /tmp/grmlhome
163 mount -t ext2 $LOOPOPT "$DEVICE" /tmp/grmlhome 2>"$TMP.err" && rsync -Ha "$HOME/" /tmp/grmlhome 2>"$TMP.err" || { killgauge; umount /tmp/grmlhome 2>/dev/null; $DIALOG --cr-wrap --title "$TITLE1" --msgbox "$ERROR `cat $TMP.err`" 10 75; bailout; }
164
165 umount /tmp/grmlhome
166 umount "$DIRECTORY" 2>/dev/null
167 killgauge
168
169 $DIALOG --cr-wrap --title "$TITLE1" --msgbox "$SUCCESS" 17 65
170
171 bailout
172
173 ## END OF FILE #################################################################