Toad's sandbox

From MEPIS Documentation Wiki

Jump to: navigation, search

This is yours to fart around in all you like!

WEYHEY!!!

This is fun. Toad 12:03, 18 April 2008 (EDT)

Remember the basic rule: farts in the sandbox are OK, but turds are not allowed. Jerry bond 12:17, 18 April 2008 (EDT)

Oy, what are you doing in my sandbox??? Anyway, I'm wearing my nappies, so don't worry ;) Toad 02:47, 19 April 2008 (EDT)

Hm, whenever I paste a script in here it ends up looking horrendous - wonder how you do it... Toad 02:48, 19 April 2008 (EDT)

Obviously not with spaces :( Toad 03:34, 19 April 2008 (EDT) - ah, go it - code pre and pre code in <>

#!/bin/bash

# This script may be freely distributed and modified as long as it is properly attributed, i.e., contains the following sentence:  
# I got most of the commands from this site: http://www.mepis.org/docs/en/index.php/Remaster


# We would be grateful for any feedback and especially improvements.

### root check ###
if [[ $UID != "0" ]]
then
  echo "You need to be root to execute this script"
  su -c "$0 $1"
  exit
fi

### if script is called with "--build" or "-b" argument it will skip creating the chroot environment ###
if [[ $1 = "--build" || $1 = "-b" ]]; then 
  path=$PWD
  if [[ ! -d $path/remaster ]]; then 
    answer="no"
    while [[ $answer != "y" && $answer != "yes" ]]; do 
      echo -e "Enter the path to the remaster directory (i.e. /home/username)\n"
      read path
      echo
      if [[ -d $path/remaster ]]; then
        answer="yes"
      else 
        echo -e "\"remaster\" directory not found in that path, please try again \n"
        answer="no"
      fi
    done
  fi
  build=true
  rem=$path/remaster
  cd $rem
fi 

### Execute create environment chunk only if $build = false ###
if [[ ! $build ]]; then

#####################
### get host path ### 
#####################

### set the current directory as path, if path/remaster exists then it prompts for other path ###
startpath=$PWD
if [[ -e $startpath/remaster ]]; then 
  answer="no"
  while [[ $answer != "y" && $answer != "yes" ]]; do 
    echo -e "Enter the host path (i.e. /home/username) in which you want to remaster your project: \n"
    read path
    echo
    if [[ -e $path ]]; then 
      echo -e "Is this correct (y/n)? \n"
      read answer
      echo
      if [[ -e $path/remaster ]]; then
        echo -e "\"$path/remaster\" present, please enter another directory or terminate this script (CTRL-C) and remove $path/remaster \n" 
        answer="no"
      fi
    else 
      echo -e "\"$path\" doesn't exist, create (y/n)? \n"
      read a
      echo 
      case $a in 
        no|n);;
        yes|y) mkdir -p $path
          if [[ $? -ne 0 ]]; then 
            echo -e "Error: the folder was not created, please try again \n"
            answer="no"
          else 
            echo -e "The path will be \"$path\" \n" 
	    answer="yes"
          fi ;;
        esac
    fi
  done
else
  path=$startpath
  echo -e "The project will be created in \"$path/remaster\" directory \n"
fi

#######################
### get CD/iso path ###
#######################

if [[ ! -e $1 ]]; then 
  answer="no"
  while [[ $answer != "y" && $answer != "yes" ]]; do
    echo -e "Please enter the path of your optical drive with the MEPIS CD (i.e. /dev/hdc)" 
    echo -e "or enter the complete path to a MEPIS iso on your hard disk (i.e. /path_to_iso/mepis.iso): \n"
    read CD
    echo
    if [[ -e $CD ]]; then
      echo -e "Is this correct (y/n)? \n"
      read answer
      echo
    else
      echo -e "Path or file doesn't exist, please try again \n" 
    fi
  done
else 
  CD=$1
  echo -e  "This script will remaster \"$CD\"  \n"
fi

########################################
### creating remastering environment ###
########################################

mkdir $path/remaster
rem=$path/remaster
cd $rem
echo -e "Okay, we have added a \"remaster\" subdirectory to your host path. From now on we are working in this \"$rem\" environment and its subdirectories only. \n"

echo "creating directory structure for this operation"
mkdir iso squashfs new-iso new-squashfs
echo "(.../remaster/iso) directory to mount CD in"
echo "(.../remaster/squashfs) directory for old squashfs"
echo "(.../remaster/new-squashfs) directory for new squashfs"
echo -e "(.../remaster/new-iso) directory for new iso \n"

echo "mounting original cd to iso directory"
cd $startpath
mount -o loop $CD $rem/iso
cd $rem

### finds the biggest file in ISO, which is most likely the squash file ###
squash=`find $rem/iso -type f -printf "%k %p\n" | sort -rn | head -1 | cut -d " " -f 2`

echo "mounting original squashfs to .../remaster/squashfs"
mount -t squashfs -o loop $squash squashfs

echo "copying existing iso to .../remaster/new-iso (takes some time)"
squash_rel=${squash#$rem/iso/}
rsync -a iso/ new-iso --exclude=$squash_rel

echo -e "copying mounted squashfs to new-squashfs (takes some time) \n"
cp -a squashfs/* new-squashfs/

umount $rem/squashfs
umount $rem/iso
rm -r $rem/squashfs
rm -r $rem/iso

### mount /proc and /sys and set up networking ###
mount --bind /proc new-squashfs/proc
mount --bind /sys new-squashfs/sys
mount --bind /dev new-squashfs/dev
mount --bind /dev/pts new-squashfs/dev/pts
mount --bind /dev/shm new-squashfs/dev/shm
mount --bind /tmp new-squashfs/tmp

if [[ ! -f new-squashfs/etc/resolv.conf ]]; then
  touch new-squashfs/etc/resolv.conf
fi
mount --bind /etc/resolv.conf new-squashfs/etc/resolv.conf

### Template for commands to be executed in chroot at entering and exit        ###
### un-comment the next lines and add the commands you want executed in chroot ###
#echo '
#  ### These commands will run when entering chroot ###
#  echo -e "This command is executed when you enter chroot
#
#  ### restore original file ###
#  mv /etc/bash.bashrc_original /etc/bash.bashrc
#
#  ### starts a new session ###
#  su
#
#  ### Put here commands to run when exiting from chroot environment (e.g., clean-up commands) ### 
#  echo -e "Exiting chroot. \n"' >> new-squashfs/etc/bash.bashrc

### assume root in our new squashfs ###
echo -e "Chrooting into your / "
echo -e "You should now be in the environment you want to remaster. To check please type 'ls' - you should see a root directory tree."
echo -e "When done please type \"exit\" or press CTRL-D \n"
chroot new-squashfs

### clean-up at exit from chroot ###  
umount new-squashfs/tmp
umount new-squashfs/dev/shm
umount new-squashfs/dev/pts
umount new-squashfs/dev
umount new-squashfs/sys
umount new-squashfs/proc
umount new-squashfs/etc/resolv.conf

###clean bash history###
if [ -f $rem/new-squashfs/root/.bash_history ]; then 
  rm $rem/new-squashfs/root/.bash_history
fi

echo -e "Are you ready to start buiding the ISO (y/n)? \n" 
read answer
echo
case $answer in
  no|n) echo -e "OK, to remaster later on, run this script with \"build\" argument, like this \"$0 --build\" or \"$0 -b\" "
    echo
    exit;;
  yes|y) echo
    echo -e "...on we go...\n";;
esac

fi ### this ends check on $build

#################
### build ISO ###
#################

### amending version file on boot cd ###
### this gets you into your version file, lets you change and save it ###
echo -e "This is your current version file: \n"
echo "----------------------------------------------------"
cat $rem/new-iso/version
echo
echo -e "----------------------------------------------------\n"
echo -e "Would you like to amend your version file (y/n)? \n"
read answer
echo
case $answer in
   no|n) echo -e "OK, on we go...\n";;
   yes|y) chmod +w $rem/new-iso/version
       nano $rem/new-iso/version
       chmod -w $rem/new-iso/version
       echo ;;
esac

### Set ISO name ###
echo -e "The ISO file will be placed by default in \"$rem\" directory. \n"
echo -e "Is that OK (y/n)? \n"
read answer
echo
case $answer in
  no|n) answer="no"
    while [[ $answer != "y" && $answer != "yes" ]]; do 
      echo -e "Enter the path (i.e. /home/username) in which you want to place your ISO file: \n"
      read isopath
      echo
      if [[ -e $isopath ]]; then 
        echo -e "Is this correct (y/n)? \n"
        read answer
        echo
      else
        echo -e "\"$isopath\" doesn't exist, create (y/n)? \n"
        read a
        echo 
        case $a in 
          no|n);;
          yes|y) mkdir -p $isopath
            if [[ $? -ne 0 ]]; then 
              echo -e "Error: the folder was not created, please try again \n"
              answer="no"
            else 
              echo -e "The path will be \"$isopath\" \n" 
	      answer="yes"
            fi ;;
        esac
      fi
    done ;;
  yes|y) 
    isopath=$rem;;
esac

echo -e "Please enter the name of the ISO file (i.e. file_name.iso) \n"
read isoname
isoname=$isopath/$isoname
echo
if [[ -e $isoname ]]; then 
  answer="no"
  while [[ $answer != "y" && $answer != "yes" ]]; do
    echo -e "File exists, please enter another name for the ISO file (i.e. file_name.iso) \n"
    read isoname
    isoname=$isopath/$isoname
    echo
    if [[ ! -e $isoname ]]; then 
      echo -e "Is this correct (y/n)? \n"
      read answer
      echo
    fi
  done
fi

echo -e "Good. We are now creating your iso. Sit back and relax, this takes some time (some 20 minutes on an AMD +2500 for a 680MB iso). \n"

### create new squashfs in the new-iso ###
mksquashfs $rem/new-squashfs $rem/new-iso/mepis/mepis -noappend

cd $rem/new-iso

### make remastered ISO file ###
mkisofs -l -r -R -v -V "Mepis" -no-emul-boot -boot-load-size 5 -boot-info-table -b boot/grub/stage2_eltorito -c boot.catalog -hide-rr-moved -o $isoname .
if [[ $? -ne 0 ]]; then 
  echo
  echo "ISO building failed"
else 
  echo
  echo "Done. You will find your very own remastered home-made Linux here: $isoname"
fi
Personal tools