#!/bin/bash ############################################################################## # # # imgconvert, (C) 2007-2010 S. Georgaras # # # # This file is part of Clonezilla-SysRescCD. # # # # Clonezilla-SysRescCD is free software: you can redistribute it and/or # # modify it under the terms of the GNU General Public License as published # # by the Free Software Foundation, either version 2 of the License, or # # (at your option) any later version. # # # # Clonezilla-SysRescCD is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License along # # with Clonezilla-SysRescCD. If not, see . # # # ############################################################################## # DEBUG=true versionNumber='0.2' function checkPartitionName(){ # check partition if [ -z "$(echo "$1" | grep -e '[hs]d[a-z][0-9]')" ];then # printVersion echo error echo "Error: Partition name \"$1\" is not valid!!!" exit 1 fi } function validateImage(){ # set -x [ -d "$1"/parts ] && return 1 local sf=$(find "$1" -name "*-chs.sf" 2>/dev/null | wc -l) [ $sf -eq 0 ] && return 1 local pt=$(find "$1" -name "*-pt.sf" 2>/dev/null | wc -l) [ $pt -eq 0 ] && return 1 # modification to read unsplited images # thanks to Viking (24/11/2008) # local aa=$(find "$1" -name "*.aa" 2>/dev/null | wc -l) # [ $aa -eq 0 ] && return 1 ######################################### # accept only disk images # ######################################### # [ ! -e "$1"/disk ] && return 2 ######################################### return 0 } function testRoot(){ if [ "$(whoami)" != "root" ];then printVersion echo "Error: This script must be executed by root..." exit fi } function printVersion(){ echo "Clonezilla Live Image Conversion $(basename $0) v. $versionNumber - (C) 2009-2011 S. Georgaras " } function printHelp(){ printVersion # echo "Usage: $(basename $0) [image] [partition] # # Parameters are: # [image] Disk image to be converted to partition image # [partition] Partition name to convert. It must be a valid device name # # Available options: # o [image] Save new imag as [image] # p Save new partition instead of making a link to the old one # d Draft execution. Execute all checks but don't create the image # v Print version info and exit # h Print this screen and exit" echo "Usage: $(basename $0) [image] [partition] Parameters are: [image] Disk image to be converted to partition image [partition] Partition name to convert. It must be a valid device name Available options: o [image] Save new imag as [image] p Save new partition instead of making a link to the old one v Print version info and exit h Print this screen and exit" } # # # Script starts here # # # set -x while getopts hvpdo: opt do case "$opt" in p) # output partition name testRoot doCopy=1 ;; # d) # do draft (execute all checks but don't create the image) # testRoot # doDraft=1 # ;; o) # output image name testRoot imgOutName="$(echo "$OPTARG" | sed 's|^\./||')" ;; v) printVersion exit;; h) printHelp exit;; esac done shift $(( OPTIND - 1 )) if [ -z "$1" ] || [ -z "$2" ];then printHelp exit 1 fi testRoot printVersion # check input image echo 'Determining input image' isRelative=1 imgInName="$(echo "$1" | sed 's|^\./||')" imgInName="$(echo "$imgInName" | sed 's|/$||')" if [ "$(expr substr "$imgInName" 1 1)" = "/" ];then isRelative='' fi hasError=1 if test $isRelative; then # relative path? if [ -d ./"$imgInName" ];then # yes it is relativePath=1 hasError='' imgInName="$(pwd)"/"$imgInName" else # no, is it in /home/partimag? if [ -d /home/partimag/"$imgInName" ];then imgInName=/home/partimag/"$imgInName" hasError='' fi fi elif [ -d "$imgInName" ];then hasError='' fi if test $hasError; then echo "Error: Image \"$imgInName\" does not exist!!!" exit 1 fi echo " Input image: \"$imgInName\"" echo -n ' Validating image... ' validateImage "$imgInName" ans=$? if [ "$ans" = "1" ];then # printVersion echo error echo "Error: Image \"$imgInName\" is not a valid image!!!" exit 1 elif [ "$ans" = "2" ];then # printVersion echo error echo "Error: Image \"$imgInName\" is not a disk image!!!" exit 1 fi echo ok partInName="$2" # check input partition name echo 'Determining input partition' echo " Input partition: \"$partInName\"" echo -n ' Validating input partition... ' checkPartitionName "$partInName" if [ -z "$(grep "$partInName" "$imgInName"/parts)" ];then # printVersion echo 'error' echo "Error: Partition \"$partInName\" not found in image!!!" exit 1 fi echo ok echo 'Determining output image' isRelative=1 if [ -z "$imgOutName" ];then imgOutName="$imgInName"-cnv else imgOutName="$(echo "$imgOutName" | sed 's|^\./||')" imgOutName="$(echo "$imgOutName" | sed 's|/$||')" fi if [ "$(expr substr "$imgOutName" 1 1)" = "/" ];then isRelative='' fi # set -x hasError='' if test $isRelative; then if [ -e "$(pwd)/$imgOutName" ];then if [ -e /home/partimag/"$imgOutName" ];then hasError=1 else hasError='' imgOutName=/home/partimag/"$imgOutName" fi else imgOutName="$(pwd)/$imgOutName" hasError='' fi else if [ -e "$imgOutName" ];then hasError=1 else hasError='' fi fi # set -x echo " Output image: \"$imgOutName\"" echo -n ' Validating output image... ' if test $hasError; then echo error echo "Error: Output image already exists!!!" exit 1 else mkdir "$imgOutName" 2>/dev/null if [ -d "$imgOutName" ];then echo ok echo -n ' Checking permissions... ' touch "$imgOutName"/test.$$ 2>/dev/null || { echo error echo "Error: Output image is not writable!!!" rm -rf "$imgOutName" 2>/dev/null exit 1 } if ! test $doCopy; then ln -s "$imgOutName"/test.$$ "$imgOutName"/test.$$.$$ 2>/dev/null || { echo error echo "Error: File system does not supports links!!!" rm -rf "$imgOutName" 2>/dev/null exit 1 } fi echo ok rm -rf "$imgOutName"/* 2>/dev/null else hasError=1 echo error echo "Error: Image folder cannot be created!!!" fi fi # set +x echo 'Determining output partition' partOutName="$3" # check output partition name if [ -z "$partOutName" ];then partOutName="$partInName" echo " Output partition: \"$partOutName\"" echo ' Validating output partition... ok' else echo " Output partition: \"$partOutName\"" echo -n ' Validating output partition... ' checkPartitionName "$partOutName" if [ ! -z "$(echo "$partOutName" | grep -e '[0-9][0-9][0-9]$')" ];then echo error echo "Error: Partition name \"$partOutName\" is not valid!!!" rm -rf "$imgOutName" 2>/dev/null exit 1 fi echo ok fi hasError='' echo "Creating output image: $imgOutName" cd "$imgOutName" echo "$partOutName" > parts 2>/dev/null || hasError=1 for n in "$imgInName"/"$partInName"*;do if test $doCopy;then # copy files instead of linking echo -n " Copying files... " cp "$n" "$(echo "$n" | sed "s|"$imgInName"/||" | sed "s|"$partInName"|"$partOutName"|")" 2>/dev/null || { hasError=1 echo error } && echo 'done' else # link files echo -n " Linking files... " ln -s "$n" "$(echo "$n" | sed "s|"$imgInName"/||" | sed "s|"$partInName"|"$partOutName"|")" 2>/dev/null || { cd .. rm -rf "$imgOutName" echo "Error: Could not create output image" exit 1 } && echo 'done' fi done if test $hasError ;then echo "Error: Could not create output image" exit 1 fi echo -n " Fixing info files... " disk="$(echo "$partInName" | sed 's|[0-9]*$||')" newDisk="$(echo "$partOutName" | sed 's|[0-9]*$||')" cp "$imgInName"/"$disk"-chs.sf "$newDisk"-chs.sf 2>/dev/null || { cd .. rm -rf "$imgOutName" echo "Error: Could not create output image" exit 1 } && { ptFile="$(echo "$(basename "$imgInName"/"$disk"-pt.sf)" | sed "s|"$disk"|"$newDisk"|")" echo "# partition table of /dev/"$newDisk" unit: sectors " > "$ptFile" || { cd .. rm -rf "$imgOutName" echo "Error: Could not create output image" exit 1 } grep "$partInName" "$imgInName"/"$disk"-pt.sf | sed "s|"$partInName"|"$partOutName"|" >> "$ptFile" || { cd .. rm -rf "$imgOutName" echo "Error: Could not create output image" exit 1 } echo 'done ' }