#!/bin/sh
# usbslackDVD.sh - Make a bootable USB-stick (or USB-HD) from Slackware
# copyleft Fabio Zorba 2012-2013
# rev.03 - 2013-03-13
# Note: Slackware14 kernel needs LILO version 23.2 to boot
 
SLACK_INSTALL_PATH="SlackDVD"
USB_TEMP_MOUNTPOINT="../USB_SLACK_TEMP_MOUNTPOINT"
 
if [ $# -eq 2 ] ; then
 
 ROOT_DEV=/dev/$1
 
  #-------- check device ---------
  case "$1" in
 
   sda) BOOT_DEVICE="/dev/sda$2" ;;
   sdb) BOOT_DEVICE="/dev/sdb$2" ;;
   sdc) BOOT_DEVICE="/dev/sdc$2" ;;
   sdd) BOOT_DEVICE="/dev/sdd$2" ;;
   sde) BOOT_DEVICE="/dev/sde$2" ;;
   sdf) BOOT_DEVICE="/dev/sdf$2" ;;
   sdg) BOOT_DEVICE="/dev/sdg$2" ;;
   sdh) BOOT_DEVICE="/dev/sdh$2" ;;
 
   *) BOOT_DEVICE="not_in_list" ;;
 
  esac
 
  if [ "a$BOOT_DEVICE" = "anot_in_list" ] ; then
   echo "Error device $ROOT_DEV, valid names are: sda,sdb,sdc,sdd,sde,sdf,sdh ... exit"
   exit
  fi
 
 echo "
---------------------------------------------------
You selected $BOOT_DEVICE device to install SLACK filesystem.
 
Warning ! All data in selected device may be LOST
 
"
 echo -n "type 'okay' to proceed : "
 
 read NNN
 
 if [ "a$NNN" = "aokay" ] ; then
 
  EXIST_DEVICE=`ls $BOOT_DEVICE`
  if [ "a$EXIST_DEVICE" == "a" ] ; then
   echo "Error device $BOOT_DEVICE not exists ... exit"
   exit
  fi
 
  #Check if Slackware DVD is present in system
 
  DVD_MOUNTPOINT=`mount | grep iso9660 | cut -d" " -f3`
 
  if [ ! -d $DVD_MOUNTPOINT/slackware ] ; then
   echo "
Error Slackware DVD not found or not mounted ... exit
try to mount by hand:
 mount /dev/sr0 /mnt/cdrom
or
 mount /dev/hda /mnt/cdrom
"
   exit
  fi
 
  USB_MOUNTPOINT=`mount | grep $BOOT_DEVICE | cut -d" " -f3`
 
  if [ "a$USB_MOUNTPOINT" == "a" ] || [ ! -d $USB_MOUNTPOINT ] ; then
 
   echo "Format the partition $BOOT_DEVICE ? [ENTER=no]"
   echo -n "... or type 'okay' to proceed with formatting: "
 
   read NNN
 
   if [ "a$NNN" = "aokay" ] ; then
    mkfs.ext2 $BOOT_DEVICE
   fi
 
   mkdir $USB_TEMP_MOUNTPOINT
 
   mount $BOOT_DEVICE $USB_TEMP_MOUNTPOINT
 
   IS_USB_MOUNTED=`mount | grep $USB_TEMP_MOUNTPOINT`
 
   if [ "a$IS_USB_MOUNTED" = "a" ] ; then
    echo "Error: $ROOT_DEV device not mounted in $USB_TEMP_MOUNTPOINT ... exit"
    exit
   fi
 
  else
    echo "$BOOT_DEVICE already mounted in $USB_MOUNTPOINT, I will install inside this one ..."
    USB_TEMP_MOUNTPOINT=$USB_MOUNTPOINT
  fi
 
  echo "Make usb-disk devices"
 
  mkdir -p $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH
  mkdir -p $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/boot
  mkdir -p $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/dev
  mkdir -p $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/etc
 
  echo "Copying files ..."
 
  cp -f $DVD_MOUNTPOINT/kernels/hugesmp.s/bzImage  $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/boot
  cp -f $DVD_MOUNTPOINT/isolinux/initrd.img        $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/boot
 
  #Add Slackware packages to root of partition
  echo "Copy Slackware packages to $BOOT_DEVICE ? [ENTER=no]"
  echo -n "type 'yes' to begin copying files: "
 
  read NNN
 
  if [ "a$NNN" = "ayes" ] ; then
    echo "Copying Slackware packages ..."
    cp -Rf $DVD_MOUNTPOINT/slackware  $USB_TEMP_MOUNTPOINT/
  fi
 
  #Make usb devices in /dev because Lilo needs them
  START=0
  MINOR=0
 
  for DISK in "a" "b" "c" "d" "e" "f" "g" "h" 
  do
   MINOR=$START
 
   for i in 0 1 2 3 4 5 6
   do
    if [ $i -eq 0 ] ; then
     mknod $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/dev/sd$DISK   b 8 $MINOR
    else
     mknod $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/dev/sd$DISK$i b 8 $MINOR
    fi
 
    let MINOR=$MINOR+1
   done
 
    let START=$START+16
  done
 
  #Uncomment below if you get errors type:
  #Warning: '/proc/partitions' does not match '/dev' directory structure.
 
  cp -Rpdf /dev/* $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/dev/
 
  echo    "LILO will be installed to $ROOT_DEV by default ..."
  echo
  echo -n "You may select another device [enter=default] : "
 
  read ELSE_ROOT_DEV
 
  if [ "a$ELSE_ROOT_DEV" != "a" ] ; then
   ROOT_DEV=$ELSE_ROOT_DEV
  fi
 
  echo
  echo    "flushing disk cache before install Lilo ... please wait ..."
 
  echo "
# LILO configuration file
# generated by Zoros
#
# Start LILO global section
# Append any additional kernel parameters:
 
boot = $ROOT_DEV
 
prompt
compact
lba32
vga = normal
timeout = 50
 
# Linux bootable partition config begins
 
image =  /boot/bzImage
  initrd=/boot/initrd.img
  label = SlackDVD
  read-write
 
# Linux bootable partition config ends
 
" > $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH/etc/lilo.conf
 
  lilo -P ignore -r $USB_TEMP_MOUNTPOINT/$SLACK_INSTALL_PATH
 
  if [ "a$USB_MOUNTPOINT" == "a" ] ; then
    umount $USB_TEMP_MOUNTPOINT
    rmdir  $USB_TEMP_MOUNTPOINT
  fi
 
 fi
 
else
 
 echo "
This script make a bootable USB-Stick (or HD) from Slackware DVD ...
Pay attention: all data in selected device may be lost.
 
Usage: $0 device [sdb,sdb,sdc...] n.partition [1,2,3...]
"
 
fi
Retrieved from "http://www.zoros.org/wiki/index.php?title=UsbslackDVDboot.sh_source_code"