Minggu, 16 Desember 2007

MCNLive Toronto to USB

Aku lagi belajar beragam cara membuat Linux bisa diboot dari USB. Salah satu caranya aku temukan di distro MCNLive Toronto USB. Script-nya terdapat di direktori
/usr/local/bin/mkmcnlive


Coba perhatikan baik-baik isi script-nya agar bisa mengerti:
#!/bin/sh
# MCNLive To flash drive install, without formatting
# (c) 2005, 2006 Chris, mcnlive@googlemail.com
# (c) www.mandrivaclub.nl
# Requires Zenity
# Licence GPL 2.0 or later

#### Check for root ####
if [ "$UID" != "0" ]; then
echo "This script must be run as root."
exit
fi
#### Constants, Functions ####
#
## Constants
DIALOG=zenity
ERROR="LiveUSB: Error Occurred"
USB="$DEVICE"1
DATA="$DEVICE"2
MOUNT="$USB"
MOUNT2="$DATA"
TMP="/tmp/mkmcnlive.tmp"
echo " " > $TMP

goodbye() {
[ -n "$MOUNT" ] && umount -dlf "$MOUNT" 2>/dev/null
[ -n "$MOUNT2" ] && umount -dlf "$MOUNT2" 2>/dev/null
umount -dl /mnt/usbstick 2>/dev/null
exit 0
}

#### Report error and exit
error() {
$DIALOG --error --text="$1" --title="$ERROR"
}

#### Command
cmd() {
echo "> $1 " >> "$TMP"
$1 2>"$TMP"
OK=$?
RESULT="$(<$TMP)"
echo "OK = $OK" >>"$TMP"
if [ "$OK" != "0" ]; then # Haarlem, we have a problem
if [ "$3" = "WARN" ]; then
#$DIALOG --title "WARNING" --sorry "$2"
$DIALOG --warning --text="$2"
elif [ "$3" = "STOP" ]; then # Bad error - stop
error "ERROR:$2: $RESULT" # Send to error func
goodbye
fi
fi
}

trap goodbye 1 2 3 15

###### MAIN SCRIPT ######
#
### Setup

echo "Starting mkmcnlive"
echo "Dialog = $DIALOG"

TITLE1="Create MCNLive on a Flash Drive "

$DIALOG --question --title="$TITLE1" --width=600 --height=800 --text=" This wizard creates a bootable USB Flash Drive on your chosen usb device. Before proceeding please notice:

0. All detected usb devices, flash drives, external harddrives and *internal sata disks* are shown. Before proceeding, make sure which device is the correct one. Use the Mandriva Control Center or the command line: fdisk -l.
1. Your BIOS must support USB-HDD booting. When using this wizard: - don't boot with the copy2ram cheatcode, -plug in your usb flash drive after you booted sucessfully.
2. Your flash drive needs a capacity of at least 512 MB.
3. It already has valid FAT formatted partition(s), partition 1 needs at least 480 MB available space. This wizard does not repartition or format your device. You can do this in the Mandriva Control Center. Be sure that the boot flag is set on the first partition.
4. Files will be copied to the first partition, syslinux bootloader installed, a valid MBR written.

*** No responsibility for data loss or hardware damage! ***
*** Press 'Cancel' if you want to stop. ***

Please verify that your usb flash drive is plugged in and press 'OK' to proceed."

if [ "$?" = "1" ]; then
error "Exiting. You chose not to continue."
exit 0
fi

#### Find the usb devices
DEVICES=""
for stick in /dev/sd[a-h]
do
if [ -e "$stick" ]; then
DEVICES="$DEVICES $stick"
echo " Found the following USB devices: $stick"
else
## Nothing found
error "Error: No suitable USB devices found :-("
exit 0
fi
done

#### PAGE 2: User selects the usb device
TITLE2="Select USB Device"
MESSAGE2="Please select a USB device to prepare.
Make sure *not* to select an internal SATA harddisk, or an external large harddisk."

DEVICE=""
while [ "$DEVICE" = "" ];
do
$DIALOG --list --title="$TITLE2" --text="$MESSAGE2" \
--column="USB Device" $DEVICES 1>"$TMP"
if [ "$?" = "1" ]; then
error "Exiting. You chose not to continue."
exit 0
fi
DEVICE="$(<$TMP)"
done

## Unmount if mounted
USB="$DEVICE"1
DATA="$DEVICE"2
(
cmd "umount -dlf $USB" 2>/dev/null

# installing MBR
cmd "dd if=/usr/local/bin/mbr.bin of=$DEVICE" "Problem writing MBR" "STOP"

# echo " Setting up boot loader on $DEVICE"
cmd "/usr/bin/syslinux $USB" "Problem installing bootloader" "STOP"
sync

# Copying the files
IMAGE="/initrd/cdrom"
cmd "mkdir -p /mnt/usbstick"
cmd "mount -t vfat $USB /mnt/usbstick"

cmd "cp $IMAGE/isolinux/* /mnt/usbstick"
cmd "rm /mnt/usbstick/isolinux.cfg"
cmd "rm /mnt/usbstick/isolinux.bin"
cmd "cp /usr/local/bin/splash.msg /mnt/usbstick"
cmd "cp /usr/local/bin/syslinux.cfg /mnt/usbstick"
cmd "cp $IMAGE/livecd.sqfs /mnt/usbstick" "Problem copying files. Exiting." "STOP"
sync

umount /mnt/usbstick

) | $DIALOG --progress --percentage=0 --title="Creating MCNLive USB" --text="Creating LiveUSB, this can take some minutes ...
Please be patient. When the OK button is active again, click on it."

#### Finished ####
TITLE4="FINISHED"
MESSAGE4="MCNLive successfully created on $DEVICE. You can unplug it now.
When rebooting, please check your BIOS settings while the flash drive is inserted!
Press 'OK' to finish."
$DIALOG --info --title="$TITLE4" --text="$MESSAGE4"

exit 0


Untuk sementara aku tulis dulu aja, belum sempat menganalisa.

Tidak ada komentar: