Forgot your password?
typodupeerror
User Journal

hazem's Journal: Linux/Windows Auto Rebuilder

Journal by hazem

October 19th, 2004

Hello!

After posting about creating an auto-re-imaging system using Linux to keep Windows running nicely, I got a lot of responses. (The article was "Spyware/Adware Prevention In Large Deployments?"). I mailed the following to several people and posted it as a reply to my own comments.

http://slashdot.org/comments.pl?sid=126181&cid=10561793

I just wrote up this document, and put the files inline within it. It should be clear how to snip them to make the individual files.

I'd release this under the GPL, but darn, it just doesn't seem like there's enough there to bother. I mean... can you really GPL some config scripts?

I found it helpful to configure the Linux stuff on one computer, then using a bootable Linux CD (I didn't want the local box slowed down by unnecessary services like networking), I put it on a server, called lin.tgz. I then booted on another machine with the bootable cd, and applied it to the /dev/hda2. If that was mounted to /lin, you'd then need to do a "chroot /lin" and then run /sbin/lilo to get lilo installed.

Good luck!

Hazem
hazem-dli at google's mail service

Linux Rebuilder
Write-up version 0.1, 19 October 2004, 4:17AM

This set of tools helps automate the process of keeping a Windows box with a consistent image. It works similarly to "Deep Freeze" by storing an image of the Windows system and all its software on a Linux partition. The computer boots into Linux, which restores this image to the Windows partition (overwriting whatever the user did before). It then reboots into Windows.

** Installing/Setup **
        The scripts as I have written them use tar/gzip to make the image of the Windows partition. This is because I was working on Win98 boxes that use FAT32 (which Linux can easily read and write). Linux does not yet reliably write NTFS, so to use this on an NTFS based Windows system, such as Windows 2000, or Windows XP, the scripts will need to be rewritten using dd/gzip rather than tar/gzip.

Here are the basic steps:
1) Install Windows on your computer. If you are using one drive, partition that drive in half (or, if you know how much space you'll need, just a little more than that - make sure you leave enough room on the remaining partition for your linux install (as little as 50MB, and room for the compressed image of your windows image). Install all your applications and customize the Windows "image" so that it is exactly the way you want it to be each time you reboot.
2) Install some Linux version on the other half. Keep it small, since you won't need networking, X, or much else.
3) Create a /rebuilder directory and place the following files in that directory: getimage, putimage, rebuilder, win_reboot
4) Modify /etc/rc.local to point to /rebuilder/rebuilder
5) Modify /etc/lilo.conf to match the menu options in my lilo.conf. Run lilo.
6) Create a /images directory to store the image.

For FAT32 systems using tar/gzip, you'll need to add an entry to your /etc/fstab to mount /dev/hda1 to /win.

** Useful Points **
There are two main keys to why this thing works pretty well. First, lilo can invoke the same kernel with different options. The menu options I place in lilo.conf do this. The other key is contained in the win_reboot file. By invoking lilo with the -R option followed by a boot label, (eg. "lilo -R Windows"), lilo will override its default boot option on the next reboot.

There are two other nice features that work nicely. The first one is that while the kernel is loading, the keyboard cannot interrupt the process. This is great for keeping someone from hijacking the system. The second is that by putting the line "password=""" in lilo.conf will password protect the boot options that do not have a "bypass" in them. This allows the user to do some things, like boot directly into Windows, or even rebuild the Windows partition, but not make a new image of the Windows partition.

If you're going to do a dd/gzip option, you'll want to wipe your Windows partition's empty space. From the documentation for g4u, there is a link to a program called nulfile, which will fill up the empty space with 0's. http://www.feyrer.de/g4u/

(If you like imaging, check out g4u, since it has many options that are similar to Norton Ghost. It may even work to replace what I've done here, but I'm not sure.)

** How it Works **

There are several lilo boot options that come up when the computer boots. They are fairly self explanatory.

auto_rebuild: default - it goes to Linux and if rebuild is set, it rebuilds window and then reboots directly into Windows
Windows: boots directly into Windows
rebuild_win: allows someone to force a rebuild of Windows without rebooting directly into it
get_image: takes an image of what is currently on the Windows drive. This will become the new image that rebuilds are based on
auto_on: turns ON autotmatic rebuilds
auto_off: turns OFF automatic rebuilds

Some options are password protected. The password for these options is set by running lilo. It will prompt for the password and store it in a hashed form. One can put the actual password in plaintext in the lilo.conf, but I prefer a bit of secrecy here!

1) Basically, the computer boots up to lilo, which then launches the "auto_rebuild" option.
2) From /etc/rc.local, the script /rebuild/rebuild is run. It uses a case/switch structure to see what kernel option was passed (read from /proc/cmdline).
3) If it was "auto_rebuild", it checks to see if the file /rebuild/auto_rebuild_on exists. If so, the Windows image file is applied to the Windows partition
4) /rebuild/win_reboot is invoked, which reboots the computer and lilo will go immediately into Windows

** Updating the Windows Software **

To install new software/updates to Windows, do the following:
1) do a "rebuild_win" go get to a known state
2) to an "auto_off" so that work isn't lost while rebooting during
3) updates and installs
4) boot direclty into Windows to install software and updates as needed
5) when the computer is reconfigured, reboot and choose "get_image"
6) reboot and select "auto_on". The computer will now re-image itself on reboot with the new settings.

** Cautionary Tales **

The only caveat is not to install anything that tweaks the partitions, such as the boot locker on Foolproof. Other foolproof stuff CAN be installed, though. So, you may consider installing Foolproof without bootlock, and then set the rebuilder for manual-only rebuilds.

The only other caveat is that you will want to turn off any auto update features in any of the software (Windows, AV, Spyware, etc). Each time the system is rebuilt, these softwares will try to re-update.

A nice improvement would be to allow the system to have the option of creating multiple Windows images to allow roll-backs, particularly if there was an error while creating the image. As it stands now, if it crashes while doing a "getimage", your only rescue is that the Windows partition should still be in good shape, and you can try again.

** Removing the Rebuilder **

First, to remove this system (or just totally disable it), boot up onto a Windows bootable floppy (or Windows install CD) that has fdisk on it. Just type: fdisk /mbr, and the menu system will be gone, and the system will boot into Windows.

If it then tries to boot into Linux instead, boot up on the same disk, use fdisk, and make sure the "active partition" is the Windows one.

This still leaves the Linux stuff in there, but it just gets bypassed. To get rid of that, you'll need to use a partitioning tool that will allow you to delete the Linux partition and then expand the Windows partition. I believe Partition Magic will do this.

** The Files **
File: /etc/lilo.conf
Notes: If your system is not set up on hda, you'll need to make more changes to this. It expects Windows on hda1, and your Linux installed on hda2

---- start lilo.conf ----
# LILO configuration file
# generated by 'liloconfig'
#
# Start LILO global section
boot = /dev/hda
#compact # faster, but won't work on all systems.
password=""
prompt
timeout = 50
# Normal VGA console
vga = normal

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = AutoRebuild
    append="auto_rebuild"
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
    bypass
# Linux bootable partition config ends

# DOS bootable partition config begins
other = /dev/hda1
    label = Windows
    table = /dev/hda
    bypass
# DOS bootable partition config ends

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = Rebuild
    append="rebuild_win"
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
    bypass
# Linux bootable partition config ends

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = Get_Win
    append="get_image"
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = Auto-on
    append="auto_on"
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = Auto-off
    append="auto_off"
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends

# Linux bootable partition config begins
image = /boot/vmlinuz
    root = /dev/hda2
    label = Linux
    read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends

---- end lilo.conf ----

File: /etc/rc.local
Notes: This might have more stuff for your system, so you're really only concerned about the last line that invokes /rebuilder/rebuilder

---- start rc.local ----
#!/bin/sh
#
# /etc/rc.d/rc.local: Local system initialization script.
#
# Put any local setup commands in here:

cat /proc/cmdline

#invoke the rebuilder script /rebuild/rebuilder

---- end rc.local ----

File: /rebuild/getimage
Notes: This is the script that gets the Windows partition. It is written for Windows98, which uses FAT32. Since Linux can reliably read/write FAT32, I do some clean-up stuff, such as deleting temp files. You can't do this win Windows XP, and you will need to change from using tar/gzip to using dd/gzip

---- start getimage ----
#!/bin/bash

echo Creating new image file

echo

# if an image already exists, provide 10 seconds before the image is
# over-written

if [ -e /image/win98image.tgz ]
then
      echo You have 10 seconds to hit ctrl-c and stop the regetting process
      sleep 10
fi

#
# Skip these rm statements if you are using NTFS based systems
#
#swapfile is big and doesn't need to be backed up
rm /win/Windows/win386.swp

#also cleanup some other files
rm /win/Windows/temp/*
rm -rf /win/Windows/Temporary\ Internet\ Files/*
rm -rf /win/Windows/*.tmp
rm -rf /win/Windows/History
rm /win/*.1st
rm /win/*.00*
rm /win/*.prv

#get the boot sector of the partition
dd if=/dev/hda1 of=/image/bootsector.img bs=512 count=1

#create tgz archive of the Windows partition
#
# for NTFS, you would change this to something like:
# dd if=/dev/hda1 bs=8192|gzip -c > /image/winXPimage.gz
# just make sure your image file name matches what is in putimage

tar cvzf /image/win98image.tgz /win

---- end getimage ----

File: /rebuild/putimage
Notes: This is the script that puts the image back on the Windows partition. It's the complement to getimage. Again, it is written for Windows98, which uses FAT32. Since Linux can reliably read/write FAT32, I do some clean-up stuff, such as deleting all the files on the Windows partition. You can't do this win Windows XP, and you will need to change from using tar/gzip to using dd/gzip

---- start putimage ----

#!/bin/bash

echo Rebuilding Windows drive
echo

# if there is a command line arugment ("no sleep"), we go right into
# rebuilding. Otherwise, we give a 10 sec grace period!

if [ -z "$1" ]
then
      echo You have 10 seconds to hit ctrl-c and stop the rebuild process
      sleep 10
fi

if [ -e /image/win98image.tgz ]
then
        # you can't do this if you're using NTFS - just skip it, since it's not necessary
        #delete existing Windows
        rm -rf /win/*

        # with NTFS, this can be skipped too, since it's redundant
        #restore the boot sector of the partition
        dd of=/dev/hda1 if=/image/bootsector.img bs=512 count=1

        # for NTFS, you'd need to change this to (based on getimage above)
        # gunzip -c /images/winXPimage.gz|dd of=/dev/hda1 bs=8192

#restore the Windows files
        (cd /;tar xzvf /image/win98image.tgz)

        echo Windows drive rebuilt
else
        echo
        echo WARNING!
        echo There was no image to rebuild from.
        echo Try using "getimage"
fi

---- end putimage ----

File: /rebuild/rebuilder
Notes: This script is called from /etc/rc.local. It reads the /proc/cmdline to see if any of its options are there, and if so, takes the appropriate action

---- start reubilder ----

#!/bin/bash

# this is the main script for the Windows image rebuilder
# it is evoked from /etc/init.d/rc.local
#
# it looks at the kernel options set by lilo to determine the action

# look to see if we should rebuild Windows
if grep -q auto_rebuild /proc/cmdline
then
      # we should automatically rebuild Windows
      if [ -e /rebuild/auto_rebuild_on ]
      then
        #echo I would rebuild Windows because the auto_rebuild_on exists /rebuild/putimage nosleep
      else
        echo Did nothing: Auto-Rebuild currently turned off.. booting Windows
                sleep 10
      fi /rebuild/win_reboot
elif grep -q rebuild_win /proc/cmdline
then
        # we SHOULD rebuild Windows
        #echo rebuild Windows /rebuild/putimage
        echo Rebooting to Windows in 10 seconds. Press ctrl-c to abort.
        sleep 10 /rebuild/win_reboot
elif grep -q get_image /proc/cmdline
then
        # we should get the current Windows drive as the new image
        #echo get new image /rebuild/getimage

elif grep -q auto_on /proc/cmdline
then
        # we should set the auto-rebuild to ON
        echo CAUTION: Set Auto-Rebuild to ON
        touch /rebuild/auto_rebuild_on

elif grep -q auto_off /proc/cmdline
then
        # we should set the auto-rebuild to OFF
        echo Set Auto-Rebuild to OFF
        rm /rebuild/auto_rebuild_on
else
        # we don't do anything special - just go into Linux
        echo booting into Linux.
fi

---- end rebuilder ----

File: /rebuild/win_reboot
Notes: This is called to make lilo bypass its default and go directly into Windows.

---- start win_reboot ----
#!/bin/sh
#
# This overrides lilo's default boot option and forces the next reboot
# into the Windows drive

lilo -R Windows /sbin/init 6
---- end win_reboot ----

This discussion has been archived. No new comments can be posted.

Linux/Windows Auto Rebuilder

Comments Filter:

No amount of genius can overcome a preoccupation with detail.

Working...