Wednesday, February 1, 2012

Making bootable Slax Usb in Ubuntu

It is a problem to make a bootable USB flash disk in Ubuntu as I see in the Slax Forums. Slax can be installed in a Fat formatted USB disk or a CD-Rom. However, for the need for compactness, people generally chooses the first option in order to use an "install once and use anywhere" operating system.

Unfortunately, if you are using an Ubuntu OS and you want to make a Slax Bootable USB, problem arises. Since the USB must be formatted as FAT, you can not change the owners and permissions of files and directories. So that, we can not set a file executable. And simply writing a

usbdisk/boot> bash bootinst.sh

command cannot handle the problem. But the solution is easy.

First, download the Slax tar file for USB disks. Untar it. You have two folders. One of them is 'slax' and the other is 'boot'. Copy the 'boot' folder in your harddisk. Write

harddisk/some folder/boot> chmod -R a+x *

So, the copied boot folder is now fully executable. Suppose that  the 'some folder' is '/usr/local/bin'. Goto usb disk. Change directory to 'boot'. Edit the bootinst.sh

usbdisk> vim bootinst.sh


And change the content of the bootinst.sh file as following:



#!/bin/bash

set -e
TARGET=""
MBR=""

# Find out which partition or disk are we using
MYMNT=$(cd -P $(dirname $0) ; pwd)
while [ "$MYMNT" != "" -a "$MYMNT" != "." -a "$MYMNT" != "/" ]; do
   TARGET=$(egrep "[^[:space:]]+[[:space:]]+$MYMNT[[:space:]]+" /proc/mounts | cut -d " " -f 1)
   if [ "$TARGET" != "" ]; then break; fi
   MYMNT=$(dirname "$MYMNT")
done

if [ "$TARGET" = "" ]; then
   echo "Can't find device to install to."
   echo "Make sure you run this script from a mounted device."
   exit 1
fi

if [ "$(cat /proc/mounts | grep "^$TARGET" | grep noexec)" ]; then
   echo "The disk $TARGET is mounted with noexec parameter, trying to remount..."
   mount -o remount,exec "$TARGET"
fi

MBR=$(echo "$TARGET" | sed -r "s/[0-9]+\$//g")
NUM=${TARGET:${#MBR}}
cd "$MYMNT"

clear
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
echo "                        Welcome to Slax boot installer                         "
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
echo
echo "This installer will setup disk $TARGET to boot only Slax."
if [ "$MBR" != "$TARGET" ]; then
   echo
   echo "Warning! Master boot record (MBR) of $MBR will be overwritten."
   echo "If you use $MBR to boot any existing operating system, it will not work"
   echo "anymore. Only Slax will boot from this device. Be careful!"
fi
echo
echo "Press any key to continue, or Ctrl+C to abort..."
read junk
clear

echo "Flushing filesystem buffers, this may take a while..."
sync

# setup MBR if the device is not in superfloppy format
if [ "$MBR" != "$TARGET" ]; then
   echo "Setting up MBR on $MBR..."
   /usr/local/bin/syslinux/lilo -S /dev/null -M $MBR ext # this must be here to support -A for extended partitions
   echo "Activating partition $TARGET..."
   /usr/local/bin/syslinux/lilo -S /dev/null -A $MBR $NUM
   echo "Updating MBR on $MBR..." # this must be here because LILO mbr is bad. mbr.bin is from syslinux
   cat /usr/local/bin/syslinux/mbr.bin > $MBR
fi

echo "Setting up boot record for $TARGET..."
  /usr/local/bin/syslinux/syslinux -d boot/syslinux $TARGET

echo "Disk $TARGET should be bootable now. Installation finished."

echo
echo "Read the information above and then press any key to exit..."
read junk 
 
 
Save the file. Exit to shell. Type

usbdisk/boot> sudo bash bootinst.sh


By now, bootinst.sh runs the needed commands from /usr/local/bin not from /usbdisk/boot.


Happy installs...