Customizing Clonezilla’s custom-ocs

In a (very popular by search engine standards) previous post, I talked about moving from Norton Ghost to Clonezilla. Part of the move from Ghost to Clonezilla was creating a method of automatically determining the computer model I was imaging, and selecting the right image. This is possible by changing the custom-ocs file, and here are the changes I made to do it.

I made a number of minor changes to the stock custom-ocs file, but they were powerful enough to get the job done.

mount /dev/sdb2 /home/partimag/

This is often overlooked, but make sure that you mount the proper location for your image files! On the external hard drive I used for imaging, I have two partitions: the first is tiny and has the Clonezilla software, the second is huge and holds the images. Hence, I want the second one (/sdb2) mounted as the image store.

echo “Type c to create image; defaults to imaging computer if nothing typed.”
read -n 1 -t 7 createimg
echo
if [[ $createimg == “c” ]] && [[ -n “${createimg}” ]]
then
echo “Image will be created from current machine.”
else
echo “Hard drive will be overwritten with disk image.”
createimg=”g”
fi

This code determines if I want to create a new image from the current machine, or push an image onto the current machine. The “read –n 1 –t 7 createimg” line reads one character (-n 1) into the variable createimg, with a 7 second timeout (-t 7). The “if [[ $createimg == “c” ]] && [[ -n “${createimg}” ]]” line tests to see if createimg is equal to ‘c’ and not empty. If so, a reassurance that the image will be created from the current machine is displayed.

x=`sudo dmidecode -t 1 | grep -i Product | tr -d ‘\n’`
y=${x#*: }
y=${y// /}

Here is the most important section. This determines the model of the attached computer. It looks hairy (like a lot of bash scripts), but it’s not too bad. It pipes the dmidecode output of system info (-t 1) to grep, which pulls out the line containing “Product”, and then trims the newline. The next line (y=${x#*: }) removes everything before the colon, and the following line (y=${y// /}) removes all the spaces. You are left with a variable containing the Product Name as stored in the BIOS, without spaces. Very convenient.

echo The following model has been detected:
echo $y
echo Please press q to abort.
read -n 1 -t 5 abort
if [[ $abort == “q” ]] && [[ -n “${abort}” ]]
then
echo Aborting!
exit 1
else
echo Continuing…
fi

This displays the just-detected model information, and once pauses for input, this time for 5 seconds (-t 5). This way, if something terrible has gone wrong and the wrong model has been detected, or you made a mistake, you can abort. I set it up with timeouts like these so when I plug my external HD into a computer and boot from it, it will automatically push the image onto that machine without any further intervention from me. If you are nervous, then remove the timeouts and do things manually.

echo Running Clonezilla
case $y in
OptiPlex780*)
if [[ $createimg == “c” ]]
then
echo Now saving image…
echo y | /opt/drbl/sbin/ocs-sr -q2 -c -j2 -rm-win-swap-hib -z1 -i 2000 -p poweroff savedisk “Dell780” “sda”
else
echo Now ghosting machine…
echo y | /opt/drbl/sbin/ocs-sr -g auto -e1 auto -r -j2 -p reboot restoredisk “Dell780” “sda”
fi
exit 0
;;

*)
echo The computer could not be correctly identified!
exit 1
;;
esac

Here’s where each individual model is handled. In this sample case block, the only listed model is an OptiPlext780. If ‘c’ was pressed earlier to signal the creation of a new image from the current machine, then the line echo y | /opt/drbl/sbin/ocs-sr -q2 -c -j2 -rm-win-swap-hib -z1 -i 2000 -p poweroff savedisk “Dell780” “sda” does that for us. Going through, it echoes ‘y’ (so we don’t have to hit it manually) to the Clonezilla script that creates an image using partclone, with confirmation, with the hidden data between the MBR and 1st partition, removing the swap and hibernation files, with gzip compression, with the image split into 2000 MB pieces, power off after completion, save the entire disk, call it “Dell780”, and save device “sda”. If c was not pressed, and the image is put onto the machine, then it’s almost the exact same thing, with a few different parameters (“restoredisk” instead of “savedisk”, for example).

The default block is run if none of the model numbers match. Always a good sanity check.

If you had several different image types for each model, you could add additional prompts with read and case in each model block. The functionality is limited only by your needs and technical expertise.

Ever since I set up this custom-ocs file, it has run beautifully, needing only tweaks to add new models as necessary. It makes imaging a breeze.