]> git.mxchange.org Git - secure-linux-project.git/blob - encrypt/stick.sh
Initial import from 0.5a version.
[secure-linux-project.git] / encrypt / stick.sh
1 #!/bin/sh
2 ##############################################
3 # Script for Secure Linux Project            #
4 # Copyright(c) 2005, 2006 by Roland Haeder   #
5 ##############################################
6 # Purpose: Creates the USB sticks and makes  #
7 #          *binary* backups of your old once #
8 ##############################################
9 # This software is licensed under the GNU    #
10 # General Public License Version 2 or either #
11 # and comes with ABSOLUTELY NO WARRANTY      #
12 # neither implied nor explicit.              #
13 ##############################################
14
15 . ./.settings.sh || exit 3
16
17 if test -b $STICK_DEVICE; then
18         # Stick is a real device
19         echo "WARNING! REAL DEVICE $STICK_DEVICE ENTERED! PLEASE MAKE BACKUP BEFORE CONTINUE!"
20         echo "Please insert first stick now ($MASTER_USER) before continue."
21         echo
22         echo "Press RETURN to continue or CTRL+C to abort here."
23         read dummy
24         DEVICE="$STICK_DEVICE"
25         echo -n "$0: Preparing backups... "
26         BACKUP="$BASEDIR/backups/"
27         mkdir  $BACKUP > /dev/null 2>&1
28         for user in $USERS; do
29                 mkdir $BACKUP/$user > /dev/null 2>&1
30         done
31         echo "done"
32  else
33         # Stick is an image
34         DEVICE=/dev/loop5
35         losetup -d /dev/loop6 > /dev/null 2>&1
36         losetup -d $DEVICE > /dev/null 2>&1
37         if test -e $STICK_DEVICE; then
38                 # Use existing image
39                 echo "$0: Using image $STICK_DEVICE."
40                 losetup $DEVICE $STICK_DEVICE
41          else
42                 if test $STICK_SIZE -gt 0; then
43                         # Create image
44                         echo "$0: Creating testing image $STICK_DEVICE."
45                         dd if=/dev/zero of=$STICK_DEVICE bs=1k count=$STICK_SIZE
46                         losetup $DEVICE $STICK_DEVICE
47                  else
48                         # Invalid size / cannot detect!
49                         echo "$0: Please provide a size for image $STICK_DEVICE!"
50                         exit 3
51                 fi
52         fi
53 fi
54
55 # Clean an existing .md5sums file
56 if test ! -e "$BASEDIR/initrd/.md5sums"; then
57         echo -n "" > $BASEDIR/initrd/.md5sums
58 else
59         mv $BASEDIR/initrd/.md5sums $BASEDIR/.md5sums
60 fi
61
62 if test -b $STICK_DEVICE; then
63         if test ! -e "$BASEDIR/.backuped"; then
64                 # Now let's setup all sticks
65                 echo "$0: Will now do a binary backup of your sticks."
66                 for user in $USERS; do
67                         rmmod usb-storage > /dev/null 2>&1
68                         echo "Please insert USB stick for $user and press RETURN to continue."
69                         read dummy
70                         insmod -kv $USB_STORAGE delay_use=0 > /dev/null 2>&1
71                         sleep 1
72                         for ((FAILED=1, TRY=1; ($FAILED != 0) && (TRY <= 15); TRY++)) do
73                                 # Auto-detect size here
74                                 SSIZE=`fdisk -s $DEVICE`
75                                 # DEBUG: echo "Code=$? (Size=$(($SSIZE/1024)) MB)"
76                                 if test "$?" == "0"; then
77                                         FAILED=0
78                                 fi
79                                 sleep 1
80                         done
81                         if test "$FAILED" == "0"; then
82                                 echo -n "$0: Binary backup of $DEVICE for $user... "
83                                 dd if=$DEVICE of=$BACKUP/$user/stick.img bs=1k count=$SSIZE $CONVERT > /dev/null 2>&1
84                                 CODE="$?"
85                                 if test "$CODE" == "0"; then
86                                         echo "done"
87                                         echo -n "$0: Compressing... "
88                                         bzip2 -9f $BACKUP/$user/stick.img >/dev/null 2>&1
89                                         echo "done"
90                                  else
91                                         echo "failed. (Code=$CODE)"
92                                         exit 6
93                                 fi
94                          else
95                                 echo "$0: Too many attemnts to read from $DEVICE!"
96                                 exit 5
97                         fi
98                 done
99                 echo "$0: Binary backup complete."
100                 echo -n "" > "$BASEDIR/.backuped"
101         fi
102
103         echo "$0: Running make_part() on all sticks now."
104         for user in $USERS; do
105                 if test ! -e "$BASEDIR/.stick_$user"; then
106                         # Create filename for the image
107                         FILE="$BASEDIR/setup/images/key-$user.img"
108
109                         rmmod usb-storage > /dev/null 2>&1
110                         echo "Please insert USB stick for $user and press RETURN to continue."
111                         echo
112                         echo "WARNING! You will loose data after this point!"
113                         read dummy
114                         insmod $USB_STORAGE delay_use=0 > /dev/null 2>&1
115                         for ((FAILED=1, TRY=1; ($FAILED != 0) && ($TRY <= 15); TRY++)) do
116                                 # Auto-detect size here
117                                 #echo "$0: Scanning..."
118                                 SSIZE=`fdisk -s $DEVICE`
119                                 CODE="$?"
120                                 #echo "$0: Code=$CODE"
121                                 if test "$CODE" == "0"; then
122                                         FAILED=0
123                                         break
124                                 fi
125                                 #echo "$0: Sleeping..."
126                                 sleep 1
127                         done
128
129                         if test "$FAILED" == "0"; then
130                                 echo "$0: Slept $TRY seconds."
131                                 # Total size for the parted command (again)
132                                 SIZE="$(($SSIZE/1024))"
133                                 SIZE_TARGET=`stat --format="%s" $FILE`
134
135                                 # Continue with creating new partitions...
136                                 echo "$0: Creating partition on $DEVICE..."
137                                 make_part
138                                 analyse_stick
139
140                                 # Write image
141                                 write_image
142                          else
143                                 echo "$0: Too many attemnts to read from $DEVICE!"
144                                 exit 5
145                         fi
146                         echo -n "" > "$BASEDIR/.stick_$user"
147                 else
148                         echo "$0: Skipping stick for $user."
149                 fi
150         done
151
152         # Do we need to update STICK_START?
153         if test "$STICK_START" == "xxx"; then
154                 echo "$0: Fatal error. Please remove one of the .stick_user files to setup STICK_START!"
155                 exit 2
156         fi
157  else
158         # Total size for the parted command
159         SIZE="$(($SSIZE/1024))"
160
161         # For testing purposes only first user
162         echo "$0: Using $MASTER_USER for testing."
163
164         # Create "partitions" on the image
165         make_part
166
167         # Analyse the stick
168         analyse_stick
169
170         # Set user for the first user
171         user="$MASTER_USER"
172
173         # Create filename for the image
174         FILE="$BASEDIR/setup/images/key-$user.img"
175
176         # Use MASTER_USER for testing purposes
177         echo "$0: Using image $FILE."
178         SIZE_TARGET=`stat --format="%s" $FILE`
179
180         # Write image now
181         write_image
182 fi
183
184 if test -e "$BASEDIR/.md5sums"; then
185         cat $BASEDIR/.md5sums $BASEDIR/initrd/.md5sums >> $BASEDIR/.dummy
186         rm $VERBOSE $BASEDIR/.md5sums
187         mv $VERBOSE $BASEDIR/.dummy $BASEDIR/initrd/.md5sums
188 fi
189
190 # Creating image/stick now completed
191 echo
192 echo "$0: All done now."
193 echo
194 echo "Now it's time for executing finish.sh to finish setup."