📄 Viewing: mkbootdisk

#!/bin/bash

# mkbootdisk
#
# Written by Erik Troan <ewt@redhat.com>
#
# Copyright 1998-2010 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

pause=yes
unset kernel
device=/dev/fd0
unset verbose
unset kernelargs
unset mountopts
unset isoimage
unset realdev
size=1440
failed=0

MOUNTDIR=`mktemp -d /tmp/mkbootdisk.XXXXXX`
PATH=/sbin:$PATH
export PATH

VERSION=1.5.5

usage () {
    cat >&2 <<EOF
usage: `basename $0` [--version] [--noprompt] [--device <devicefile>] 
       [--verbose -v] [--iso] [--kernelargs <args>] [--size <size>]  <kernel>
       (ex: `basename $0` --device /dev/fd1 2.0.31)
EOF
    exit $1
}

while [ $# -gt 0 ]; do
    case $1 in
	--device)
	    shift
	    device=$1
	    ;;
	--kernelargs)
	    shift
	    kernelargs=$1
	    ;;
	--help)
	    rm -rf $MOUNTDIR
	    usage 0
	    ;;
	--iso)
	    isoimage=1
	    ;;
	--size)
	    shift
	    size=$1
	    ;;
	--noprompt)
	    unset pause
	    ;;
	-v)
	    verbose=true
	    ;;
	--verbose)
	    verbose=true
	    ;;
	--version)
	    echo "mkbootdisk: version $VERSION"
	    rm -rf $MOUNTDIR
	    exit 0
	    ;;
	*)
	    if [ -z "$kernel" ]; then
		kernel=$1
	    else
		rm -rf $MOUNTDIR
		usage
	    fi
	    ;;
    esac

    shift
done

[ -z "$kernel" ] && {
    rm -rf $MOUNTDIR
    usage 1
}

[ "$(uname -i)" == "x86_64" ] && [ -z $isoimage ] && {
    echo "Image for x86_64 architecture does not fit on 1.44MB floppy." >&2
    echo "Use \"--iso --device image.iso\" options to make a CD image." >&2
    exit 1
}

[ -d /lib/modules/$kernel ] || {
    echo "/lib/modules/$kernel is not a directory." >&2
    rm -rf $MOUNTDIR
    exit 1
}

[ -f /boot/vmlinuz-$kernel ] || {
    echo "/boot/vmlinuz-$kernel does not exist." >&2
    rm -rf $MOUNTDIR
    exit 1
}


rootdev=`awk '$1 ~ /^[^#]/ && $2 == "/" { print $1 ; exit }' /etc/fstab`

case $rootdev in #(
LABEL=* )
    rootlabel=$(expr $rootdev : 'LABEL=\(.*\)')

    # whee, now we have to look through every partition looking for
    # the thing called $rootlabel, which could be raid. Ick.

    # md partitions first
    list=$(awk 'NR>=3 {i=($4~/^md/); a[i]=a[i]" "$4} END{print a[1] a[0]}' \
		/proc/partitions)

    rootdev=""
    for dev in $list; do
	if tune2fs -l /dev/$dev >/dev/null 2>/dev/null; then
	    label=$(tune2fs -l /dev/$dev 2>/dev/null | 
		awk '/Filesystem volume name/ {print $4}')
	    if [ "$label" = $rootlabel ]; then
		rootdev=/dev/$dev
		break
	    fi
	fi
    done
    ;;
esac

if [ -z "$kernelargs" -a -x /sbin/grubby -a -f /boot/grub/grub.conf ]; then
    # sed gross... this grep's for the args= line and removes the args=" and "$
    defkernel=$(grubby --default-kernel)
    if [ -n "$defkernel" ]; then
	kernelargs=$(grubby --info "$defkernel" | sed -n 's/"$//;s/^args="//p')
    fi
fi

[ -z "$rootdev" ] && {
    echo 'Cannot find root partition in /etc/fstab.' >&2
    rm -rf $MOUNTDIR
    exit 1
}

rm -rf $MOUNTDIR
mkdir $MOUNTDIR || {
    echo "Failed to create $MOUNTDIR" >&2
    exit 1
}
[ -d $MOUNTDIR ] || {
    echo "$MOUNTDIR is not a directory!" >&2
    rm -rf $MOUNTDIR
    exit 1
}

if [ -n "$isoimage" ]; then
    cfgfile=$MOUNTDIR/isolinux/isolinux.cfg
    bootmsg=$MOUNTDIR/isolinux/boot.msg

    # create an iso image; the directory is all we need
    [ -n "$verbose" ] && echo -n "Installing isolinux... "
    mkdir $MOUNTDIR/isolinux
    cp /usr/share/syslinux/isolinux.bin $MOUNTDIR/isolinux
    [ -n "$verbose" ] && echo done
else
    cfgfile=$MOUNTDIR/syslinux.cfg
    bootmsg=$MOUNTDIR/boot.msg

    # we need to create an msdos filesystem for syslinux

    if [ -b "$device" ]; then
	if [ -n "$pause" ]; then
	    echo "Insert a disk in $device. Any information on the disk will be lost."
	    echo -n "Press <Enter> to continue or ^C to abort: "
	    read aline
	fi

	# this is copying some bits from below.  oh well
	realdev=$device
	device=`/bin/mktemp /tmp/mkbootdisk.XXXXXX`
	dd if=/dev/zero of=$device bs=1024 count=$size 2> /dev/null
	mountopts=loop
    elif [ -f "$device" ]; then
	echo "$device exists but is not a block device."
	rmdir $MOUNTDIR
	exit 1
    else 
	dd if=/dev/zero of=$device bs=1024 count=$size 2> /dev/null
	mountopts=loop
    fi

    [ -n "$verbose" ] && echo -n "Formatting $device... "
    mkdosfs -r 16 -I $device > /dev/null || {
	echo "Failed to format $device" >&2
	rmdir $MOUNTDIR
	[ -n "$realdev" ] && rm -f "$device"
	exit 1
    }
    
    syslinux $device
    [ -n "$verbose" ] && echo "done."

    mount -o "$mountopts" -t vfat $device $MOUNTDIR || {
	rmdir $MOUNTDIR
	[ -n "$realdev" ] && rm -f "$device"
	exit 1
    }
fi


if [ -n "$isoimage" ]; then
    BOOTDESTDIR=$MOUNTDIR/isolinux
else
    BOOTDESTDIR=$MOUNTDIR
fi

[ -n "$verbose" ] && echo -n "Copying /boot/vmlinuz-$kernel... "
cp -p /boot/vmlinuz-$kernel $BOOTDESTDIR/vmlinuz || failed=1
[ -n "$verbose" ] && echo "done."

# "initramfs.img" would not fit into the 8.3 scheme...
for f in /boot/initr{amfs,d}-$kernel.img; do
  [ -f $f ] || continue
  [ -n "$verbose" ] && echo -n "Copying $f... "
  cp -p $f $BOOTDESTDIR/initrd.img || failed=1
  [ -n "$verbose" ] && echo "done."
  break
done

[ -n "$verbose" ] && echo -n "Configuring bootloader... "

[ -f $BOOTDESTDIR/initrd.img ] && INITRDARG="initrd=initrd.img"

if [ $(echo $rootdev | cut -b 6-9) = "loop" ]; then
    rootdev=$(ls -l $rootdev | awk '{ printf("%02x%02x\n", $5, $6); }')
fi

cat > $cfgfile <<EOF || failed=1
default linux
prompt 1
display boot.msg
timeout 100
label linux
	kernel vmlinuz
	append $INITRDARG $kernelargs root=$rootdev
EOF

chmod 644 $cfgfile

title=""
[ -f /etc/redhat-release ] && title="$(sed 's/ release.*$//' /etc/redhat-release)"
if [ -z "$title" -o $(echo -n "$title Boot CD" | wc -c) -ge 33 ]; then
    title="Red Hat Linux"
fi


cat >> $bootmsg <<EOF || failed=1

Press <return> (or wait 10 seconds) to boot your $title system from
$rootdev. You may override the default linux kernel parameters by typing
"linux <params>", followed by <return> if you like.

EOF

[ -n "$verbose" ] && echo "done."

if [ -n "$isoimage" ]; then
    mkisofs \
	-A "$title Boot CD" \
	-V "$title Boot CD" \
	-J -R -T -quiet \
	-o $device \
	-b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
	   -boot-load-size 4 -boot-info-table $MOUNTDIR
    rm -rf $MOUNTDIR
else
    umount $MOUNTDIR
    rmdir $MOUNTDIR
fi

if [ -n "$realdev" ] && [ $failed -eq 0 ]; then
    dd if="$device" of="$realdev" bs=72k 
    rm -f "$device"
fi

if [ $failed -eq 1 ]; then
  exit 1
else
  exit 0
fi

🌑 DarkStealth — WP Plugin Edition

Directory: /usr/sbin