~ubuntu-branches/ubuntu/gutsy/debianutils/gutsy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/sh
# mkboot: make the system bootable
# Debian GNU/Linux
# Copyright 1996-1997 Guy Maor <maor@debian.org>
# This is free software; see the GNU General Public License version 2
# or later for copying conditions.  There is NO warranty.

set -e

PATH=$PATH:/sbin:/usr/sbin

# basic devfsd support
if [ -e /dev/.devfsd ]; then
	FLOPPY=/dev/floppy/0
else 
	FLOPPY=/dev/fd0
fi

# root partition
if mount | grep -q "^proc " && [ -e /proc/cmdline ]; then
	rootpart=$(grep -o "root=[^ ]*" /proc/cmdline | cut -d \= -f 2)
else
	which rdev >/dev/null && rootpart=$(rdev | cut -d ' ' -f 1)
fi
# temporary directory
tmpdir=${TMPDIR-/tmp}

# check whether ELILO is installed
elilocheck() {
    printf "\nChecking for ELILO..."
    if [ -f /etc/elilo.conf ] && [ -x /usr/sbin/elilo ]; then
	echo "Yes"
	return 0
    fi
    echo "No"
    return 1
}

# check whether SILO is installed
silocheck() {
    printf "\nChecking for SILO..."
    if [ -f /etc/silo.conf ] && [ -x /sbin/silo ]; then
       echo "Yes"
       return 0
    fi
    echo "No"
    return 1
}

# check whether GRUB is installed
grubcheck () {
    if ! which update-grub >/dev/null && ! which grub-install >/dev/null; then return 1; fi
}

# check whether Yaboot is installed
yabootcheck () {
    [ -f /etc/yaboot.conf ] && [ -x /usr/sbin/ybin ] && return 0 || return 1
}

# check whether Arcboot is installed
arcbootcheck () {
    [ -f /etc/arcboot.conf ] && [ -x /usr/sbin/arcboot ] && return 0 || return 1
}

# check whether LILO is installed
lilocheck () {
    printf "\nChecking for LILO..."
    if [ $(whoami) != root ] ; then
	echo "Only root can check for LILO"
        return 1;
    fi
    if [ ! -f /etc/lilo.conf ] || [ ! -x /sbin/lilo ] ; then
	echo "No"
	return 1;
    fi
    bootpart=$(perl -ne 'print $1 if /^\s*boot\s*=\s*(\S*)/' /etc/lilo.conf)
    if [ -z "$bootpart" ] ; then
	# lilo defaults to current root when 'boot=' is not present
	bootpart=$rootpart
    fi
    if [ ${bootpart#/dev/md} != $bootpart ] ||
       [ ${bootpart#/dev/evms/md} != $bootpart ]; then
        echo " - on software RAID device $bootpart - assuming LILO is installed and working."
    	return 0;
    fi
    if ! dd if=$bootpart ibs=16 count=1 2>&- | grep -q LILO ; then
	printf "\nYes, but I couldn't find a LILO signature on $bootpart\n"
	echo "Check your /etc/lilo.conf, or run /sbin/lilo by hand."
	return 1;
    fi
    echo "Yes, on $bootpart"
    return 0;
}

# check whether PALO is installed
palocheck() {
    printf "\nChecking for PALO..."
    if [ -f /etc/palo.conf ] && [ -x /sbin/palo ]; then
       echo "Yes"
       return 0
    fi
    echo "No"
    return 1
}

# make a lilo boot disk
makelilo () {
(
    b=$tmpdir/boot$$
    trap "set +e; cd /; umount $FLOPPY; rmdir $b" EXIT
    mkdir $b
    mke2fs -q $FLOPPY 
    mount -t ext2 $FLOPPY $b
    if [ -e /boot/boot.b ]; then
        cp /boot/boot.b $b/boot.b
    fi
    cp $1 $b/vmlinuz
    # if a symbolic link, look for the real file
    kfile=`readlink -f $1`
    # see if we need an "initrd=" line in lilo.conf
    kdir=`dirname $kfile`
    if [ $kdir = . ]; then kdir=$PWD   # full path
      elif [ $kdir = boot ]; then kdir=/boot; kfile="/$kfile"; fi
    # make sure directory exists
    if [ -d $kdir ]; then
        # extract version
        case $kfile in
            *-*) kvers=${kfile#*-} ;;
    	    *)   kvers="" ;;
        esac
        # look for matching initrd.img
	if [ "$kvers" = "" ]; then ifile="$kdir"/initrd.img
	else ifile="$kdir"/initrd.img-$kvers ; fi
	#
	if [ ! -f $ifile ]; then ifile=""; fi
    else
        kdir="" ; ifile=""
    fi
    # kfile is now the full path to the kernel image, and
    # ifile is now the initrd.img that matches the kernel, or null
    #
    echo "Kernel is at $kfile in $kdir"
    if [ "$ifile" != "" ]; then echo "Matching initrd image is $ifile";fi
    #
    lilo_conf="# floppy lilo.conf
	boot = $FLOPPY
	install = boot.b
	map = map"
    state=G
    usesinitrd=n
    # examine /etc/lilo.conf
    while read line
    do
        case $line in
	    "#"* | "" ) continue ;;   # skip comments and blank lines
	    *boot*=* | *root*=* | *install*=* ) continue ;;
	    *map*=* | *message*=* ) continue    # skip these files as well
	                ;;
	    *image*=* ) if [ $state = L ]; then
			    break
			fi   # done
			#
	                case $line in
			    *$1 )  kimg=`echo ${line#*=}`
			              state=L   # found correct kernel
			              ;;
			    *linu*) kimg=`echo ${line#*=}`
				    # see if this is the right Linux kernel
				    if [ `ls -l $kimg |
				         awk '{print $NF}'` = $kfile ]; then
			                state=L   # found right kernel
				    else
				        state=I   # Ignore wrong kernel
				    fi
			               ;;
			    * ) state=I   # Ignore this stanza
				       ;;
	                esac
			#
			if [ $state = L ]; then
	                     lilo_conf="$lilo_conf
			               # kernel-specific:
				       $line"
			fi
			;;
	    *other*=* ) state=I  # ignore non-Linux stanza
	                ;;
	    * ) if [ $state != I ]; then
	           lilo_conf=`printf "$lilo_conf\n$line\n"`
		fi
		#
		case $line in
		    *initrd*=* )  lcinitrd=`ls -l ${line#*=} |
		                            awk '{print $NF}'`
		                  if [ $lcinitrd = "$ifile" ]; then
				      usesinitrd=y
				  fi
		                  ;;
		esac
		;;
	esac
    done < /etc/lilo.conf
    #
    lilo_conf=`printf "$lilo_conf\nroot = $rootpart\n"`
    #
    if [ $state = L ]; then
        echo "Found kernel image $kimg in existing /etc/lilo.conf"
	if [ "$liloOK" != n ] && [ $usesinitrd = y ] && [ "$ifile" != "" ]; then
	    echo "   and the correct initrd.img as well."
	    problems=n
	elif [ "$liloOK" != n ] && [ $usesinitrd = n ] && [ "$ifile" = "" ]; then
	    problems=n   # no initrd needed
	elif [ "$liloOK" != n ] && [ $usesinitrd = n ] && [ "$ifile" != "" ]; then
	    echo "   but it does not invoke $ifile ."
	    problems=y
	elif [ "$liloOK" = n ] && [ $usesinitrd = y ]; then
	    echo "   and the correct initrd.img as well,"
	    echo "   but there were problems in installing from it."
	    problems=y
	else
	    echo "but it does not mention the required initrd.img ."
	    problems=y
	fi
    else
	echo "Could not find the requested kernel in your"
	echo "   current /etc/lilo.conf ."
	problems=y
    fi
    #
    if [ $problems = n ]; then
        echo "Your current /etc/lilo.conf looks good, and can be used"
        echo "   as the basis for the boot-floppy lilo.conf."
    else
        echo "The mkboot script can probably do better."
    fi
    sleep 8
    #
    echo
    echo "Here is the proposed lilo.conf:"
    echo
    echo "$lilo_conf"
    echo
    echo "You can install the boot-loader from this best guess,"
    echo "or you can try to install from a \`vanilla\' lilo.conf ."
    echo
    reply=""
    while [ "$reply" = "" ]
    do
        printf "Which do you choose? (Enter B for best, V for vanilla): "
        read reply
    done
    #
    if [ $reply = B ] || [ $reply = b ]; then
	echo "Installing the best-guess lilo.conf..."
        cd $b
        echo "$lilo_conf" > lilo.conf
    else
	echo "Installing the vanilla lilo.conf..."
        cd $b
        cat > lilo.conf <<- EOF
	    lba32
	    boot = $FLOPPY
	    install = boot.b
	    map = map
	    compact
	    prompt
	    timeout = 50
	    read-only
	    image = vmlinuz
	    label = linux
	    root = $rootpart
	EOF
    fi
    #
    lilo -C lilo.conf
    cat <<EOF
If you need to modify the floppy's lilo.conf, run the following:

    	mount $FLOPPY /mnt
    	cd /mnt
    	vi lilo.conf		# edit the file
    	lilo -C lilo.conf	# run lilo on floppy
    	cd
    	umount $FLOPPY
EOF
)
}


# make a simple boot disk
makesimple () {
(
    dd if=$1 of=$FLOPPY
    rdev $FLOPPY $rootpart
    rdev -R $FLOPPY 1
)
}    



# make a boot disk
makedisk () {
    kernel=${1:-/vmlinuz}
    if [ ! -r $kernel ] ; then
	echo "Error: Can't read $kernel."
	exit 1
    fi

    boottype="lilo"
    if [ $(whoami) != root ] ; then
	echo "Since you don't have root permissions, I can't put LILO on the diskette."
	echo "I will make a non-LILO diskette instead, but it won't be as useful.  You"
	echo "can hit <Ctrl-C> to cancel."
	boottype="simple"
    fi

    printf "\nInsert a floppy diskette into your boot drive, and press <Return>. "
    read input
    diskok=0
    while [ "$diskok" != 1 ] ; do
	printf "\nCreating a $boottype bootdisk...\n"
	make$boottype $kernel
	if [ $? -eq 0 ] ; then
	    diskok=1
	else
	    printf "\nThere was a problem creating the boot diskette.  Please make sure that\n"
	    echo "you inserted the diskette into the correct drive and that the diskette"
	    echo "is not write-protected."
	    printf "\nWould you like to try again? (y/n) "
	    read input
	    if [ "$input" != "y" ] ; then
		return 1
	    fi
	fi
    done
    echo "...Success."
    return 0
}

usage="$0 [-r rootpartition] [-i] [-d device] [kernel]"

while getopts "r:id:h-" opt ; do
    case "$opt" in
	r) rootpart="$OPTARG" ;;
	i) installkernel=1 ;;
	d) FLOPPY="$OPTARG" ;;
	h) echo $usage ; exit 0 ;;
	-) break ;;
	*) echo $usage 1>&2 ; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

if [ "$installkernel" ] ; then
    echo "In order to use the new kernel image you have just installed, you"
    echo "will need to reboot the machine.  First, however, you will need to"
    echo "either make a bootable floppy diskette, re-run LILO, or have GRUB"
    echo "installed."

    if elilocheck; then
	printf "\nShould I run /usr/sbin/elilo? (y/N) "
	read input
	if [ "$input" = "y" ] ; then
	    /usr/sbin/elilo && exit 0
            echo "There was a problem running /usr/sbin/elilo."
	fi
    fi

    if grubcheck; then
        printf "\nGRUB is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in menu.lst to $1"
        exit 0
    elif yabootcheck; then
        printf "\nYaboot is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in /etc/yaboot.conf to $1"
        exit 0
    elif arcbootcheck; then
        printf "\nArcboot is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in /etc/arcboot.conf to $1"
        exit 0
    fi

    if lilocheck; then
        liloOK=y
	printf "\nShould I run /sbin/lilo? (y/N) "
	read input
	if [ "$input" = "y" ] ; then
	    /sbin/lilo && exit 0
            echo "There was a problem running /sbin/lilo."
            liloOK=n
	fi
    fi

    if silocheck; then
        printf "\nSILO is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in silo.conf to $1"
        exit 0
    fi

    if palocheck; then
        printf "\nPALO is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in palo.conf to $1"
        exit 0
    fi

    printf "\nShould I make a bootdisk? (y/N) "
    read input
    if [ "$input" = "y" ] ; then
	makedisk $1 && exit 0
    fi

    printf "\nWARNING: Your system is probably unbootable now.  After correcting any\n"
    echo "problems, rerun this script with the command \`mkboot -i'."
    exit 1
fi

makedisk $1