#!/bin/bash # # Custom clone script for my Windows XP virtual machine (or half clone # anyway, since all it does is refresh the image files that already # are defined in existing KVM machines - no new KVMs are created). # # If it isn't obvious already - the machines should be stopped # when doing this :-). # # Directory where virtual images to be manipulated reside. Override with # the -d option. # imagedir="/zooty/images" # Backing file for all images. Override with -b # baseimage="winxpsp3-acpi-base.img" # Default login user on the virtual machine (override with -u) # user="Tom" # Name of virtual image to update (1st positional arg - required) # name="" # Name of wallpaper file to use for background (2nd positional arg - required) # wallpaper="" while [ $# -gt 0 ] do arg="$1" shift case $arg in -d) imagedir="$1" shift;; -b) baseimage="$1" shift;; -u) user="$1" shift;; *) if [ -z "$name" ] then name="$arg" elif [ -z "$wallpaper" ] then wallpaper="$arg" else echo Invalid arg 1>&2 exit 2 fi;; esac done if [ -z "$name" ] then echo Missing virtual machine name 1>&2 exit 2 fi if [ -z "$wallpaper" ] then echo Missing wallpaper name 1>&2 exit 2 fi if [ -d "$imagedir" ] then : else echo $imagedir is not a directory 1>&2 exit 2 fi cd $imagedir if [ -f "$baseimage" ] then : else echo $baseimage does not exist 1>&2 exit 2 fi lowname=`echo $name | tr '[A-Z]' '[a-z]'` hiname=`echo $name | tr '[a-z]' '[A-Z]'` tmpdir="/tmp/ui$$" trap "rm -rf $tmpdir" EXIT mkdir -p $tmpdir # Make the .reg file we need to merge in order to change the computer name # of the Windows XP virtual machine. tmp="$tmpdir/name.reg" sed -e 's/@UPNAME@/'"$hiname"'/g' -e 's/@loname@/'"$lowname"'/g' > $tmp <<'EOF' [HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] "AltDefaultDomainName"="@UPNAME@" [HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName] "ComputerName"="@UPNAME@" [HKLM\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName] "ComputerName"="@UPNAME@" [HKLM\SYSTEM\ControlSet001\Services\Eventlog] "ComputerName"="@UPNAME@" [HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters] "NV Hostname"="@loname@" [HKLM\SYSTEM\ControlSet001\Services\Tcpip\Parameters] "Hostname"="@loname@" [HKLM\SYSTEM\ControlSet003\Control\ComputerName\ComputerName] "ComputerName"="@UPNAME@" [HKLM\SYSTEM\ControlSet003\Services\Eventlog] "ComputerName"="@UPNAME@" [HKLM\SYSTEM\ControlSet003\Services\Tcpip\Parameters] "NV Hostname"="@loname@" [HKLM\SYSTEM\ControlSet003\Services\Tcpip\Parameters] "Hostname"="@loname@" EOF # Remove the old image file (if any) and create a new one backed by # the base image file. rm -f $name.img qemu-img create -f qcow2 -o backing_file=$baseimage $name.img # Now update the registry in the new image to merge in the host name change. virt-win-reg --merge $name.img $tmp # Next we need to do the much more complex task of updating the wallpaper # image for the default login user (me). Start by grabbing the NTUSER.DAT # hive file. guestfish --ro -i -a $name.img download 'WIN:C:\Documents and Settings\'"$user"'\NTUSER.DAT' $tmpdir/NTUSER.DAT # Then update that hive to indicate that the wallpaper comes from the desired # standard wallpaper image bak="$tmpdir/bak.reg" sed -e 's/@JPEG@/'"$wallpaper"'/g' > $bak <<'EOF' [Control Panel\Desktop] "ConvertedWallpaper"="C:\\WINDOWS\\Web\\Wallpaper\\@JPEG@.jpg" [Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme] "Wallpaper"="%SystemRoot%\\Web\\Wallpaper\\@JPEG@.jpg" EOF hivexregedit --merge $tmpdir/NTUSER.DAT $bak guestfish -i -a $name.img upload $tmpdir/NTUSER.DAT 'WIN:C:\Documents and Settings\'"$user"'\NTUSER.DAT' # But wait! There's more, we not only need to fix the registry, but also # update the cached bitmap image of the jpg file. guestfish --ro -i -a $name.img download 'WIN:C:\WINDOWS\Web\Wallpaper\'"$wallpaper"'.jpg' $tmpdir/wall.jpg bmp="$tmpdir/Wallpaper1.bmp" convert $tmpdir/wall.jpg $bmp guestfish -i -a $name.img upload $bmp 'WIN:C:\Documents and Settings\'"$user"'\Local Settings\Application Data\Microsoft\Wallpaper1.bmp'