Cybersecurity
DevOps Cloud (ADM)
IT Operations Cloud
ZEN imaging is great, but out of the box it's hardly the panacea that it can be.
Like many others, we have built our imaging using a non-monolithic image system that layers pieces on top of each other to create a finished product. All of this is done from the ZENworks Image Maintenance mode, whereas we are running commands from a Bash prompt in the imaging environment.
Our images are built from:
We worked to write some scripts that would automate the process of layering in the images but the first issue was figuring out how to get them down to the machine.
This can be accomplished by adding the following lines to the /srv/tftp/boot/settings.txt file:
cd /bin
tftp $PROXYADDR -c get addon/addons
bash /bin/addons
The settings.txt file is actually a script file that is run at the end of the ZENworks imaging environment initialization. What we've asked it to do is to download a file called addons that we're storing in /srv/tftp/addon directory that will be the file that prepares our environment.
The addons file looks like this:
#!/bin/bash
#######################################################################
#-- Imaging environment preparation script
#######################################################################
## Download additional scripts
cd /bin
## Auto image script
tftp $PROXYADDR -c get addon/autoImage
chmod x /bin/autoImage
## Download standard MBR image
if [ ! -e "/imgfiles" ]; then
mkdir /imgfiles
fi
cd /imgfiles
tftp $PROXYADDR -c get addon/mbr.img
This script will download all of the other scripts that we need for our environment. Additionally, it is downloading a standard MBR image that we use during the imaging process. We always image partition to partition as opposed to the entire image to work around the problem of different drive sizes. With that, the problem occurred that when a brand new hard drive is used, ZEN does not install a new master boot record so the drive ends up being unusable. We resolve this problem by rewriting the master boot record every time a drive is imaged.
This is a stripped down version of the script that we use, but over the course of these articles we will enhance it with additional functionality.
Now, for our imaging script which we will put in /srv/tftp/addon/autoImage
#!/bin/bash
########################################################################-- Standard baseline imaging script
#
#-- This script is used to pull down a baseline image to a PC.
#######################################################################
##--- [BEGIN environment configuration]
basepath=//$STORAGEADDR/images/
baseimg=xpsp3base.zmg
baseapp=addons/baseapp.zmg
driverbase=addons/driverimg/
##--- [END environment configuration]
partsize="0"
sysname=""
## Help display
function syntax {
echo "Correct command syntax is:"
echo " autoImage [partsize:SIZE] [sysname:NAME]"
echo
echo " partsize - Specify the size of the boot partition to be created in megabytes."
echo " If not specified, the entire drive will be used. ** 1 GB = 1000 MB"
echo
echo " sysname - Specify the Windows name to use for this system. If not specified,"
echo " the serial number for the machine will be used."
}
## Parse command line variables
for var in "$@"
do
validcmd=0
cmd=${var%%:*}
if [[ $var == *:* ]]; then
val=${var#*:}
else
val=""
fi
if $cmd == partsize; then
if "$val" -ne ""; then
partsize=$val
validcmd=1
fi
fi
if $cmd == sysname; then
if "$val" != ""; then
sysname=$val
validcmd=1
fi
fi
if $cmd == help; then
syntax
exit
fi
if $validcmd == 0; then
echo "Invalid command."
echo
syntax
exit
fi
done
## Check to make sure that this is being run on purpose
while true; do
read -p "Are you sure you want to image this computer? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
## Get computer model number information (without spaces)
## This is used to determine the name of the driver image add-on file
cpumodel=`/usr/sbin/dmidecode -s system-product-name | tr -d ' '`
## Get computer serial number from DMI
serialnumber=`/usr/sbin/dmidecode -s system-serial-number | tr -d ' '`
## Use the system's serial number if no name was specified on the
## command line
if "$sysname" != ""; then
serialnumber=$sysname
fi
## Clear out all existing partitions and create one
img pd-all
## Install clean MBR
dd if=/imgfiles/mbr.img of=/dev/sda count=1 bs=446
if [ $partsize == 0 ]; then
img pc1 ntfs
else
img pc1 ntfs $partsize
fi
## Clear set the workstation name to the system's serial number
zisedit computername=$serialnumber
## Download the base image
img rp $PROXYADDR "$basepath$baseimg" a1:p1
## Download the base apps image
img rp $PROXYADDR "$basepath$baseapp"
## Download the driver image
img rp $PROXYADDR "$basepath$driverbase$cpumodel.zmg"
## Set first partition to be active
img pa1
## Reboot and complete the process
reboot -f
This script will give you the option to specify the workstation name for the system as well as a partition size in the event that you need to image a machine without using the entire drive.
The system takes the workstation's model number from DMI to determine the name of the driver image file. It also strips out all spaces for simplicity. For example, if the model number in DMI is "Latitude D630", the driver image file name will be LatitudeD630.zmg
The script will then layer the three images onto the machine and reboot. After the reboot, it is up to my post-imaging scripts to prep the system and install all of the required applications.
The master boot record image file (mbr.img) is available below, but you can make your own if you prefer.
The way that I prefer to create the master boot record image file is to boot with some sort of Linux environment. You can use the imaging environment, however you will need some method of copying the file from the imaging environment, though if you have an FTP server available, that's easily accomplished.
From Linux, type the following:
dd if=/dev/sda of=mbr.img bs=446 count=1
This will back up just the master boot record and will not overwrite the partition table.
If you wish to wipe out the partition table as well, start with a drive with no partitions and change the bs=446 to bs=512.
In the next article, we'll talk about automating the process of backing up and restoring images from existing machines.