Sunday, December 21, 2008

ClamAV Live CD being updated every hour! (+ script)

So, I haven't been doing my duty of updating the Live CD as much as I should have lately. Tonight, I set off to automate updating everything so I just have to set a cron to do everything for me. I have been asked numerous times to supply some kind of script to do this so the person asking didn't have to re-download the whole iso over again. Here is the script (if you have any suggestions on how to do something better, feel free to suggest away):

START=$(date +%s)

cd ~/Projects/clamavlivecd

mkdir iso
mount ClamAVLiveCD2.0.iso iso/ -o loop

cp -R iso/ image/

echo "Decompressing SquashFS...\n"
cp iso/casper/filesystem.squashfs ./
unsquashfs filesystem.squashfs

echo "Setting up Live CD chroot...\n"
cp /etc/resolv.conf squashfs-root/etc/resolv.conf

chroot squashfs-root/ mount /proc
chroot squashfs-root/ mount /sys
chroot squashfs-root/ mount -t devpts none /dev/pts

echo "Refreshing and updating ClamAV virus definitions...\n"
chroot squashfs-root/ freshclam

#cleanup chroot
echo "Cleaning up chroot...\n"
chroot squashfs-root/ rm -rf /tmp/*
chroot squashfs-root/ rm /etc/resolv.conf
chroot squashfs-root/ umount -l -f /proc
chroot squashfs-root/ umount -l -f /sys
chroot squashfs-root/ umount /dev/pts

echo "Removing old SquashFS filesystem...\n"
rm image/casper/filesystem.squashfs

echo "Creating new SquashFS filesystem...\n"
sudo mksquashfs squashfs-root image/casper/filesystem.squashfs

echo "Finding and creating MD5 hash sums of files in image...\n"
cd image
find . -type f -print0 | xargs -0 md5sum > md5sum.txt

echo "Removing old old image and creating new old image...\n"
cd ..
rm ClamAVLiveCD2.0_old.iso
mv ClamAVLiveCD2.0.iso ClamAVLiveCD2.0_old.iso

echo "Creating new image...\n"
sudo mkisofs -r -V "ClamAV Live CD 2.0" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/ -no-emul-boot -boot-load-size 4 -boot-info-table -o ClamAVLiveCD2.0.iso image/

#cleanup working directory

echo "Cleaning up working directory...\n"
umount iso/

rm -rf squashfs-root
rm -rf image
rm -rf iso
rm filesystem.squashfs

echo "Getting MD5 and SHA1 sum of image...\n"
echo "MD5: " > clamavlivecd.sums
md5sum ClamAVLiveCD2.0.iso >> clamavlivecd.sums
echo "SHA1: " >> clamavlivecd.sums
sha1sum ClamAVLiveCD2.0.iso >> clamavlivecd.sums

END=$(date +%s)

echo "\nDone at `date`. The whole process took $(($END - $START)) seconds!\n"

Well, now I should stop worrying about not having time for this project! yay