* Use 'Architecture: all' in debian/control so we can use the
[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: Mon Nov 06 23:48:26 CET 2006 [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 [ "$UID" != 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 [[ $DEBUG -gt 0 ]]; 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'
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 - delete grml-installation from /mnt/external1:
86   $PROGRAMNAME uninstall /mnt/external1
87
88 For more information take a look at http://wiki.grml.org/doku.php?id=usb
89 Report bugs, send wishes and feedback to the grml team:
90 http://grml.org/bugs/ - contact (at) grml.org
91 "
92 }
93
94 notice(){
95   echo "
96 Installing grml to $DEVICE should have been successful!
97
98 Tips:
99
100   Use the usb cheatcode on bootprompt if you experience any problems with
101   booting grml via usb/firewire, so boot with 'grml scandelay' on bootprompt.
102   This adds a 'sleep' while trying to access the usb-stick and the module
103   should have enough time to access it. Using scandeleay=seconds is possible
104   as well, use 'grml scandelay=20' to wait 20 seconds before trying to access
105   the external device with grml on it.
106
107   Also make sure you have tested all available modes of USB-boot in
108   the BIOS of your computer. Often found as USB-HDD, USB-FDD and
109   USB-ZIP in the bootmenu.
110
111   Take a look at http://wiki.grml.org/doku.php?id=usb for more information."
112 }
113
114 info(){
115   echo "${BLUE}$PROGRAMNAME ${VERSION}${NORMAL} - install grml-system to usb-device"
116   echo
117 }
118
119 vfat_warning(){
120   echo "Warning: make sure that your usb-device is preformated with vfat.
121 If not please run it e.g. via:  mkfs.vfat -F 16 -v /dev/sda1"
122   echo
123 }
124
125 mount_device(){
126   if grep ${DEVICE} /proc/mounts ; then
127     echo echo 1>&2 "${WHITE}${DEVICE} already mounted${NORMAL}"
128   else
129     echo -n "Mounting ${DEVICE}: "
130     debugit "debug: mount $DEVICE"
131     mount $DEVICE && echo "${WHITE}done${NORMAL}" || bailout
132   fi
133 }
134
135 mount_iso(){
136   echo -n "Mounting ${ISO} to ${TMPMNT}: "
137   debugit "debug: mount -o loop ${ISO} ${TMPMNT}"
138   if mount -o loop "${ISO}" ${TMPMNT} ; then
139     echo "${WHITE}done${NORMAL}"
140   else
141     echo 1>&2 "${RED}Problem? You got an error saying 'mount: could not find any free loop device'?
142 Possible solution: losetup -d /dev/loop/0${NORMAL}"
143     exit 3
144   fi
145 }
146
147 unmount(){
148   echo -n "Unmounting ${DEVICE} and ${TMPMNT}."
149   umount ${TMPMNT}
150   umount ${DEVICE}
151 }
152
153 copyit(){
154   echo -n "Installing data from ${ISO} to ${DEVICE} (will take some time): "
155   debugit "debug: cp -dR --preserve=mode,timestamps ${TMPMNT}/* ${DEVICE}"
156   debugit "debug: mv ${DEVICE}/boot/isolinux/* ${DEVICE}/"
157   if cp -dR --preserve=mode,timestamps ${TMPMNT}/* ${DEVICE} ; then
158     echo "# filelist of $PROGRAMNAME on $(date) using $ISO on ${DEVICE}:" > $DEVICE/grml2hd.filelist
159     find  ${TMPMNT} -type f | grep -v isolinux | sed 's#^/mnt/test/##' | tr A-Z a-z >> $DEVICE/grml2hd.filelist && \
160     find  ${DEVICE}/boot/isolinux -type f | sed 's#.*isolinux/##'      | tr A-Z a-z >> $DEVICE/grml2hd.filelist && \
161     mv    ${DEVICE}/boot/isolinux/* ${DEVICE}/    && \
162     rmdir ${DEVICE}/boot/isolinux                 && \
163     rmdir ${DEVICE}/boot/
164     sync && echo "${WHITE}done${NORMAL}"
165   else
166     unmount
167     return 1
168   fi
169 }
170
171 run_syslinux(){
172   DEV=$(echo ${DEVICE%/})
173   TMPDEV=$(grep ${DEV} /etc/fstab | awk '{print $1}')
174   SYSDEV=$(echo ${TMPDEV%/})
175   echo -n "Running syslinux on ${SYSDEV}: "
176   debugit "debug: syslinux ${SYSDEV}"
177   syslinux ${SYSDEV} && echo "${WHITE}done${NORMAL}" || echo 1>&2 "${RED}Problem when running syslinux?
178 Try to call it manually via 'syslinux /dev/sda1' if you installed grml to /dev/sda1'${NORMAL}"
179 }
180
181 # }}}
182
183 # main program {{{
184 trap bailout 1 2 3 15
185
186 if [[ $# != 2 ]]; then
187   usage ; exit 4
188 fi
189
190 if [ -n "$UNINSTALL" ] ; then
191   echo "${BLUE}$PROGRAMNAME - ${VERSION}"
192   echo "${BLUE}Uninstalling grml from ${DEVICE} based on ${DEVICE}/grml2hd.filelist.${NORMAL}"
193   if mount_device ; then
194    if [ -f ${DEVICE}/grml2hd.filelist ] ; then
195     for file in `cat $DEVICE/grml2hd.filelist | grep -v '^#'` ; do
196       echo -n "removing ${file} on ${DEVICE}: "
197       rm ${DEVICE}/${file} && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
198     done
199     echo -n "removing directory grml/images/ on ${DEVICE}: "
200     rmdir ${DEVICE}/grml/images/ && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
201     echo -n "removing directory grml/ on ${DEVICE}: "
202     rmdir ${DEVICE}/grml && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
203     echo -n "removing filelist grml2hd.filelist on ${DEVICE}: "
204     rm ${DEVICE}/grml2hd.filelist && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
205     echo -n "Unmounting ${DEVICE}:"
206     umount $DEVICE && echo "${WHITE}done${NORMAL}" || echo "${RED}error${NORMAL}"
207    else
208     echo 1>&2 "${RED}File ${DEVICE}/grml2hd.filelist not found. I have nothing to delete therefor. Exiting.${NORMAL}"
209    fi
210   fi
211 else
212   info
213   vfat_warning && \
214   mount_iso    && \
215   mount_device && \
216   copyit       && \
217   unmount      && \
218   run_syslinux && \
219   notice
220 fi
221 # }}}
222
223 ## END OF FILE #################################################################
224 # vim:foldmethod=marker