Knoppix recombine

Script Download

#! /bin/bash
#----------------------------------------------------------------------
# kn-recombine (V0.1-3)
# Description: Build new compressed filesystem Image
# of flash disk Installation
# Author: Werner P. Schulz <Diese E-Mail-Adresse ist vor Spambots gesch├╝tzt! Zur Anzeige muss JavaScript eingeschaltet sein!>
# Created at: Do 01 Sep 2011 10:24:19 CET
# License: This file is licensed under the GPL v3.
#----------------------------------------------------------------------
# Last change: So 01 Jul 2012 16:40:07 UTC [wps]
whichpossible() {
# list only possible partitions
if ! [ -n "${PARTLIST[*]}" ]; then
  if [ "$WREC" = "CD" ]; then
    sfdisk -l 2> /dev/null | sed -n '/sd.[0-9]/p'| sed 's/\/dev\///' | \
      sed '/'$INST_PART'\|Empty\|Extend\|Hidden\|swap/d'| awk '{print $1}' \
      > /tmp/partition.lst
  elif [ "$WREC" = "DVD" ]; then
    sfdisk -l 2> /dev/null | sed -n '/sd.[0-9]/p'| sed 's/\/dev\///' | \
      sed '/'$INST_PART'\|Empty\|FAT32\|Extend\|Hidden\|swap/d'| awk '{print $1}' \
      > /tmp/partition.lst
  else
    sfdisk -l 2> /dev/null | sed -n '/sd.[0-9]/p'| sed 's/\/dev\///' | \
      sed '/Empty\|Extend\|Hidden\|swap/d'| awk '{print $1}' \
      > /tmp/partition.lst
  fi
fi
}
partselect() {
# list your partitions
whichpossible	
for i in $(cat /tmp/partition.lst | awk '{print $1}'); do
  part="${i##}"
  PARTLIST[$((count++))]="$part"
  PARTLIST[$((count++))]="$(awk '{printf("%8d",($1 / 2048) "MB")}' \
    /sys/block/$(expr substr $i 1 3)/$i/size 2>/dev/null) \
    $(blkid -o list /dev/$i | sed -n '3p' | awk '{print $2}')"
done
SEL=`dialog --title "Select Partition" --aspect 12 --colors --menu \
  "$1" 0 0 0 "${PARTLIST[@]}" 3>&1 1>&2 2>&3`
[ $? = 1 ] && exit 1
}
remove_old() {
# if exist old recombine stuff remove it
mount | grep -q "dev/$SEL" || mount /media/$SEL
STOR="/media/$SEL"
if [ -d $STOR/knx/recombine ]; then
  TM="\nBe aware, old recombinded stuff on \
\n\Zb$STOR/knx\ZB will be removed! \n\nShall we go on?"
  dialog --title "$TL" --aspect 12 --colors --defaultno --yesno "$TM" 0 0
    [ $? = 1 ] && exit 1
  dialog --title "$TL" --aspect 12 --colors --infobox \
    "\nDelete old stuff." 0 0
  setterm -cursor off
  tput cup  14 1
  rm -vR $STOR/knx/recombine | sed 's/^.\{27\}//' \
    | awk '{printf "%-78s\r", substr($1, 1, 78)}'
  setterm -cursor on
fi
[ -e $STOR/knx/KNOPPIX ] && rm -R $STOR/knx/KNOPPIX
HD_FREE=$(df $STOR | tail -n1 | awk '{print $4}')
}
#--------------------------------------------------------------------
fkrsync() {
# Rsync UNIONFS and build new KNOPPIX
TL="$1"
# are we within a flash disk installation
if ! [ -e /mnt-system/ldlinux.sys ]; then
  dialog --title "$TL" --infobox \
    "\nYou have no Flash disk installation!" 0 0
  exit 1
fi
dialog --title "$TL" --aspect 12 --infobox \
  "\nCalculate needed free space." 0 0
UFS="$(du -s /UNIONFS/ | awk '{print $1}')"
MIN_FREE="$(expr $(expr $UFS + $(du /mnt-system/KNOPPIX/KNOPPIX \
  | awk '{print $1}')))"
# is CD or DVD
if [ $(du -s /mnt-system/KNOPPIX/KNOPPIX | awk '{print $1}') -lt 800000 ]
then
  WREC="CD"
else
  WREC="DVD"
fi
# which partition contains flash install
INST_PART="$(df -h | sed -n '/mnt-system/p' | sed 's/\/dev\///' | \
  awk '{print $1}')"
# which partiton for recombined stuff
TM="\nWhich partition (size MB) do you want to use \
\nfor \Zbrsync UNIONFS\ZB and \Zbbuild new KNOPPIX\ZB? \
\nYou need at least "$(expr $MIN_FREE / 1024)" MB free space."
PARTLIST=()
count=0
partselect "$TM"
remove_old
if [ $MIN_FREE -ge $HD_FREE ]; then
  dialog --aspect 12 --colors --title "$TL" --yesno \
    "\nThere is not enough free space within \
\nselected partition for 'recombine KNOPPIX'. \
\n\nSelect another partition?" 0 0
  [ $? = 1 ] && exit 1
  TM="\nWhich other partition do you want to use? \
\n\nYou need at least "$(expr $MIN_FREE / 1024)" MB free space."
  count=0
  partselect "$TM"
  remove_old
  if [ $MIN_FREE -ge $HD_FREE ]; then
    dialog --aspect 12 --colors --title "$TL" --infobox \
    "\nThere is also not enough free space \
\nwithin selected partition." 0 0
    exit 1
  fi
fi
# preliminaries completed
START=$(date +'%s')
xscreensaver-command -exit
dialog --title "$TL" --aspect 12 --colors --infobox \
  "\nStart with rsync of UNIONFS." 0 0
mkdir -p $STOR/knx/recombine
TSTART=$(date +'%s')
# recombine persistent memory with decompressed filesystem Image
setterm -cursor off
tput cup 15 1
rsync -vaH /UNIONFS/ $STOR/knx/recombine \
  --exclude=/var/log/* --exclude=/var/tmp/* \
  --exclude=/var/backups/*.gz --exclude=/var/cache/apt/archives/* \
  --exclude=/var/lib/apt/lists/* | awk '{printf "%-78s\r", substr($1, 1, 78)}'
setterm -cursor on
# calculate transfer rate of UNIONFS
TRSYNC=`echo "$UFS / $(expr $(expr $(date +'%s') - $TSTART)) /1024" | bc -l`
dialog --title "$TL" --aspect 12 --colors --infobox \
"\nBuild new compressed 'KNOPPIX'
\n\Z3This will take a very long time!\Zn" 0 0
# build new compressed filesystem Image
TSTART=$(date +'%s')
genisoimage -R -V KNOPPIX_FS -quiet \
  -hide-rr-moved -cache-inodes \
  $STOR/knx/recombine | create_compressed_fs -q -B 131072 -L 9 \
  -f $STOR/knoppix_tmp - $STOR/knx/KNOPPIX 2>&1 \
  | sed  -n 's/^.\{4\}/      /; s/, \[ra.*//; 12,${p;n;n;n;n;n;n;n;n;n;}'
# calculate used times to build new KNOPPIX
TK="\nTime to build new KNOPPIX: $(expr $(expr $(date +'%s') - $TSTART) / \
  60) min. and $(expr $(expr $(date +'%s') - $START) % 60) sec."
TT="\nRsync rate from /UNIONFS/: $(expr substr $TRSYNC 1 4) MB/sec. \
\n$TK\nFinished! Total used time: $(expr $(expr $(date +'%s') - $START) / \
  60) min. and $(expr $(expr $(date +'%s') - $START) % 60) sec."
su knoppix -c "xscreensaver -nosplash &"
if [ $(du $STOR/knx/KNOPPIX | awk '{print $1}') -ge 4194304 ]; then
  dialog --aspect 12 --colors --title "$TL" --infobox \
  "$TT\n\nThe \Zbnew KNOPPIX\ZB exceeds the size of 4 GB. \
\n\nDeinstall some packages within your flash-disk \
\ninstallation and try recombine once more." 0 0
else
  dialog --aspect 12 --colors --title "$TL" --infobox \
  "$TT\n\nYou can find your new KNOPPIX filesystem \
\nat \Zb$STOR/knx/KNOPPIX\ZB. \
\n\nShutdown and unplug the flash disk. \
\nStart your computer with \Zbanother Knoppix\ZB (CD/DVD, \
\nHD or Flash disk installation). Replugin your flash disk, \
\ncall the script \Zbkn-recombine\ZB once more \
\nand select: 'Write new KNOPPIX to flash disk'." 0 0
fi
}
#--------------------------------------------------------------------
fkwrite() {
# Write compressed, new KNOPPIX to flash disk installation
TL="$1"
# look for source
TM="\n\Z2Which partition contains the \
\nrecombined \Zbnew KNOPPIX?\ZB"
PARTLIST=()
WREC="HD"
count=0
partselect "$TM"
PART=$SEL
mount | grep -q "dev/$PART" || mount /media/$PART
STOR="/media/$PART"
if ! [ -e $STOR/knx/KNOPPIX ]; then
  dialog --title "$TL" --aspect 12 --colors --yesno \
    "\nThere is no \ZbKNOPPIX\ZB at '$STOR/knx/'. \
\n\nSelect another partition?" 0 0
  [ $? = 1 ] && exit 1
  TM="\nWhich other partition do you select \
\nto look for new recombined \Zbnew KNOPPIX?\ZB"
  count=0
  partselect "$TM"	
  PART=$SEL
  mount | grep -q "dev/$PART" || mount /media/$PART
  STOR="/media/$PART"
  if ! [ -e $STOR/knx/KNOPPIX ]; then
     dialog --aspect 12 --colors --title "$TL" --infobox \
       "\nThere is also not \
\nnew \ZbKNOPPIX\ZB at '$STOR/knx/'." 0 0
    echo "Abbruch"; exit 1
  fi
fi
if [ $(du $STOR/knx/KNOPPIX | awk '{print $1}') -ge 4194304 ]; then
  dialog --aspect 12 --colors --title "$TL" --infobox \
    "\n$STOR/knx/KNOPPIX exceeds \
\nthe size of 4 GB. You cannot exchange \
\nold KNOPPIX in your flash disk \
\ninstallation with it." 0 0
  exit 1
fi
# look for target
PARTLIST=()
count=0
TM="\n\Z5Which partition contains your \
\n\Zbprevious\ZB flash disk Installation?\Zn"
partselect "$TM"
while [ $SEL == $PART ]; do
  TM="\nDo not select the partition with recombined KNOPPIX!"
  partselect "$TM"
done  
mount | grep -q "dev/$SEL" || mount /media/$SEL
FLASH="/media/$SEL"
if ! [ -e $FLASH/KNOPPIX/KNOPPIX ]; then
  dialog --title "$TL" --aspect 12 --colors --infobox \
"\nThere is no \Zbflash disk Installation\ZB at '$FLASH'." 0 0
  exit 1
fi
TM="\nNow we remove previous KNOPPIX and also persistent memory. \
\n\nShall we go on?"
dialog --title "$TL" --aspect 12 --colors --defaultno --yesno "$TM" 0 0
[ $? = 1 ] &&  exit 1
# preliminaries completed
START=$(date +'%s')
xscreensaver-command -exit > /dev/null
rm $FLASH/KNOPPIX/KNOPPIX
if [ -e $FLASH/KNOPPIX/knoppix-data.img ];then
  rm $FLASH/KNOPPIX/knoppix-data.img
  TM="\nOld KNOPPIX and 'not encrypted' \
\npersistent memory removed."
elif [ -e $FLASH/KNOPPIX/knoppix-data.aes ];then
  rm $FLASH/KNOPPIX/knoppix-data.aes
  TM="\nOld KNOPPIX and 'encrypted' \
\npersistent memory removed."
else
  TM="\nOnly old KNOPPIX removed; \
\npersistent memory not found."
fi
TM="$TM\n\nCopy new KNOPPIX to flash disk."
dialog --title "$TL" --aspect 12 --infobox "$TM" 0 0
TSTART=$(date +'%s')
setterm -cursor off
tput cup 19 1
rsync -h --progress $STOR/knx/KNOPPIX $FLASH/KNOPPIX/
setterm -cursor on
# calculate transfer rate of new KNOPPIX
TRANS=`echo "$(du $STOR/knx/KNOPPIX \
  | awk '{print $1}') / $(expr $(expr $(date +'%s') - $TSTART)) \
  /1024" | bc -l`
# calculate new sha1sums
dialog --title "$TL" --aspect 12 --colors --infobox \
  "\nCalculate checksums" 0 0
cd $FLASH
find -type f -not -name sha1sums -not -name boot.cat -not -name \
  isolinux.bin -exec sha1sum '{}' \; > KNOPPIX/sha1sums
TT="\nTransfer rate to $FLASH/KNOPPIX/: $(expr substr $TRANS 1 4) MB/sec. \
\nFinished! Used time: $(expr $(expr $(date +'%s') - $START) / 60) min. \
and $(expr $(expr $(date +'%s') - $START) % 60) sec."
su knoppix -c "xscreensaver -nosplash &"
dialog --aspect 12 --colors --title "$TL" --msgbox \
"$TT\n\nReboot your flash disk Installation as usual \
\nand let Knoppix create new persistent memory." 0 0
}
# ------------------------------- main  -------------------------------
TL="Recombine Knoppix flash disk Installation"
if [ "$(id -u)" != 0 ] ; then
  dialog --title "$TL" --aspect 12 \
    --infobox "\nYou must be root to use this script!" 0 0
  exit 1
fi
W1="Rsync UNIONFS and build new KNOPPIX"
W2="Write new KNOPPIX to flash disk"
WAHL=`dialog --title "$TL" --aspect 12 --menu \
"\nWhat do you want to do?" 0 0 0 \
  "$W1" "" "$W2" "" 3>&1 1>&2 2>&3`
case "$WAHL" in
  $W1) fkrsync "$W1";;
  $W2) fkwrite "$W2";;
  *) break
esac

 Top