adjust grml2usb version in script itself
[grml2usb.git] / grml2usb
1 #!/bin/sh
2 # Filename:      grml2usb
3 # Purpose:       install grml-system to usb-device
4 # Authors:       grml-team (grml.org), (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: Mit Apr 04 00:02:50 CEST 2007 [mika]
8 ################################################################################
9
10 # colors {{{
11   CRE="\r\e[K"
12   NORMAL="\e[0;39m"
13   RED="\e[1;31m"
14   GREEN="\e[1;32m"
15   YELLOW="\e[1;33m"
16   BLUE="\e[1;34m"
17   MAGENTA="\e[1;35m"
18   CYAN="\e[1;36m"
19   WHITE="\e[1;37m"
20 # }}}
21
22 # usercheck {{{
23   if ! [ -x "$(which syslinux)" ] ; then
24     echo 1>&2 "${RED}Error: syslinux is not available. Please install it before running this script.${NORMAL}" ; exit 2
25   fi
26
27   if [ "$(id -u)" != 0 ];  then
28     echo 1>&2 "${RED}Error: please run this script with uid 0 (root).${NORMAL}" ; exit 1
29   fi
30 # }}}
31
32 # set variables  {{{
33   if [ -n "$DEBUG" ]; then
34 #    set -x
35     debugit(){
36       echo $*
37     }
38   else
39     debugit(){
40       echo $* >/dev/null
41     }
42   fi
43
44   if [ "$1" = "uninstall" ] ; then
45      UNINSTALL=1
46   fi
47
48   LANG='C'
49   LANGUAGE='C'
50   LC_ALL='C'
51   PROGRAMNAME=${0##*/}
52   VERSION='0.7.5'
53   ISO="$1"
54   DEVICE="$2"
55   [ -n "$TMPMNT" ] || TMPMNT='/mnt/test'
56   [ -d "$TMPMNT" ] || mkdir -p $TMPMNT
57 # }}}
58
59 # helper functions {{{
60 bailout(){
61   echo
62   echo "Exiting - umounting /mnt/test and $DEVICE"
63   umount /mnt/test 2>/dev/null
64   umount $DEVICE 2>/dev/null
65   exit 2
66 }
67
68 usage()
69 {
70   echo 1>&2 "${BLUE}$PROGRAMNAME $VERSION${NORMAL} - install grml-system to usb-device
71
72 This program installs a grml-iso to an usb-device to be able to boot
73 from usb. Make sure you have a grml-iso, syslinux (just run
74 'apt-get install syslinux' on debian-systems) and root access.
75
76 Usage:
77   $PROGRAMNAME grml.iso /mount/point
78
79   Notice: /mount/point is the mountpoint set up for your USB device in /etc/fstab
80           /mount/point should not be mounted at the time running $PROGRAMNAME
81
82 Usage example - install grml-small to usb-device on /mnt/external1:
83   $PROGRAMNAME grml_small_0.2.iso /mnt/external1
84
85 Usage example - install currently running grml to usb-device on /mnt/external1:
86   $PROGRAMNAME /cdrom /mnt/external1
87
88 Usage example - delete grml-installation from /mnt/external1:
89   $PROGRAMNAME uninstall /mnt/external1
90
91 For more information take a look at http://wiki.grml.org/doku.php?id=usb
92 Report bugs, send wishes and feedback to the grml team:
93 http://grml.org/bugs/ - contact (at) grml.org
94 "
95 }
96
97 notice(){
98   echo "
99 Installing grml to $DEVICE should have been successful!
100
101 Tips:
102
103   Use the usb cheatcode on bootprompt if you experience any problems with
104   booting grml via usb/firewire, so boot with 'grml scandelay' on bootprompt.
105   This adds a 'sleep' while trying to access the usb-stick and the module
106   should have enough time to access it. Using scandeleay=seconds is possible
107   as well, use 'grml scandelay=20' to wait 20 seconds before trying to access
108   the external device with grml on it.
109
110   Also make sure you have tested all available modes of USB-boot in
111   the BIOS of your computer. Often found as USB-HDD, USB-FDD and
112   USB-ZIP in the bootmenu.
113
114   Take a look at http://wiki.grml.org/doku.php?id=usb for more information."
115 }
116
117 info(){
118   echo "${BLUE}$PROGRAMNAME ${VERSION}${NORMAL} - install grml-system to usb-device"
119   echo
120 }
121
122 vfat_warning(){
123   echo "Warning: make sure that your usb-device is preformated with vfat.
124 If not please run it e.g. via:  mkfs.vfat -F 16 -v /dev/sda1"
125   echo
126 }
127
128 mount_device(){
129   if grep ${DEVICE} /proc/mounts ; then
130     echo echo 1>&2 "${WHITE}${DEVICE} already mounted${NORMAL}"
131   else
132     echo -n "Mounting ${DEVICE}: "
133     debugit "debug: mount $DEVICE"
134     mount $DEVICE && echo "${WHITE}done${NORMAL}" || bailout
135   fi
136 }
137
138 mount_iso(){
139   echo -n "Mounting ${ISO} to ${TMPMNT}: "
140   local mount_opts_="-o loop"
141   if [ -d $ISO ]; then
142       mount_opts_="--bind"
143   elif [ -b $ISO ]; then
144       mount_opts_=""
145   fi
146   debugit "debug: mount $mount_opts_ ${ISO} ${TMPMNT}"
147   if mount $mount_opts_ "${ISO}" ${TMPMNT} ; then
148     echo "${WHITE}done${NORMAL}"
149   else
150     echo 1>&2 "${RED}Problem? You got an error saying 'mount: could not find any free loop device'?
151 Possible solution: losetup -d /dev/loop/0${NORMAL}"
152     exit 3
153   fi
154 }
155
156 unmount(){
157   echo -n "Unmounting ${DEVICE} and ${TMPMNT}."
158   umount ${TMPMNT}
159   umount ${DEVICE}
160 }
161
162 copyit(){
163   echo -n "Installing data from ${ISO} to ${DEVICE} (will take some time): "
164   debugit "debug: cp -dR --preserve=mode,timestamps ${TMPMNT}/* ${DEVICE}"
165   debugit "debug: mv ${DEVICE}/boot/isolinux/* ${DEVICE}/"
166   if cp -dR --preserve=mode,timestamps ${TMPMNT}/* ${DEVICE} ; then
167     echo "# filelist of $PROGRAMNAME on $(date) using $ISO on ${DEVICE}:" > $DEVICE/grml2hd.filelist
168     find  ${TMPMNT} -type f | grep -v isolinux | sed 's#^/mnt/test/##' | tr A-Z a-z >> $DEVICE/grml2hd.filelist && \
169     find  ${DEVICE}/boot/isolinux -type f | sed 's#.*isolinux/##'      | tr A-Z a-z >> $DEVICE/grml2hd.filelist && \
170     mv    ${DEVICE}/boot/isolinux/* ${DEVICE}/    && \
171     rmdir ${DEVICE}/boot/isolinux                 && \
172     rmdir ${DEVICE}/boot/
173     sync && echo "${WHITE}done${NORMAL}"
174   else
175     unmount
176     return 1
177   fi
178 }
179
180 run_syslinux(){
181   DEV=$(echo ${DEVICE%/})
182   TMPDEV=$(grep ${DEV} /etc/fstab | awk '{print $1}')
183   SYSDEV=$(echo ${TMPDEV%/})
184   echo -n "Running syslinux on ${SYSDEV}: "
185   debugit "debug: syslinux ${SYSDEV}"
186   syslinux ${SYSDEV} && echo "${WHITE}done${NORMAL}" || echo 1>&2 "${RED}Problem when running syslinux?
187 Try to call it manually via 'syslinux /dev/sda1' if you installed grml to /dev/sda1'${NORMAL}"
188 }
189
190 # }}}
191
192 # main program {{{
193 trap bailout 1 2 3 15
194
195 if [ "$#" != 2 ]; then
196   usage ; exit 4
197 fi
198
199 if [ -n "$UNINSTALL" ] ; then
200   echo "${BLUE}$PROGRAMNAME - ${VERSION}"
201   echo "${BLUE}Uninstalling grml from ${DEVICE} based on ${DEVICE}/grml2hd.filelist.${NORMAL}"
202   if mount_device ; then
203    if [ -f ${DEVICE}/grml2hd.filelist ] ; then
204     for file in `cat $DEVICE/grml2hd.filelist | grep -v '^#'` ; do
205       echo -n "removing ${file} on ${DEVICE}: "
206       rm ${DEVICE}/${file} && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
207     done
208     echo -n "removing directory grml/images/ on ${DEVICE}: "
209     rmdir ${DEVICE}/grml/images/ && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
210     echo -n "removing directory grml/ on ${DEVICE}: "
211     rmdir ${DEVICE}/grml && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
212     echo -n "removing filelist grml2hd.filelist on ${DEVICE}: "
213     rm ${DEVICE}/grml2hd.filelist && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
214     echo -n "Unmounting ${DEVICE}:"
215     umount $DEVICE && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
216    else
217     echo 1>&2 "${RED}File ${DEVICE}/grml2hd.filelist not found. I have nothing to delete therefor. Exiting.${NORMAL}"
218    fi
219   fi
220 else
221   info
222   vfat_warning && \
223   mount_iso    && \
224   mount_device && \
225   copyit       && \
226   unmount      && \
227   run_syslinux && \
228   notice
229 fi
230 # }}}
231
232 ## END OF FILE #################################################################
233 # vim:foldmethod=marker