Newer
Older
Digital_Repository / OARiNZ / DIY / LiveCD / Makefile
#!/usr/bin/make -f


SHELL=/bin/sh


# Version and filename info.
VERSION=3.0
INSTANT_EPRINTS=Instant_EPrints_${VERSION}_i386
UBUNTU=Ubuntu_6.10_i386

# Directories.
LIVECD=livecd
OLD_SQUASH=old-squashfs
NEW_SQUASH=new-squashfs

# Miscellaneous files.
REMOVE_FILES=${NEW_SQUASH}/etc/resolv.conf ${NEW_SQUASH}/etc/apt/apt.conf \
	${NEW_SQUASH}/Makefile.chroot

# Commands locations.
ECHO=/bin/echo
SUDO=/usr/bin/sudo
RM=/bin/rm
CP=/bin/cp
MOUNT=/bin/mount
UMOUNT=/bin/umount
MKDIR=/bin/mkdir
SED=/bin/sed
DD=/bin/dd


.PHONY: filesystem squashfs iso cleanup


# Build ISO image.

iso: ${INSTANT_EPRINTS}.iso

${INSTANT_EPRINTS}.iso: ${LIVECD}/casper/filesystem.squashfs
	@${ECHO}
	@${ECHO} "=== BUILDING ISO IMAGE ==="

	@# Update live CD MD5 hash. We do a /bin/sh -c to run the find command
	@# so that we can use output redirection. Otherwise we'll get pe${RM}ission
	@# errors.
	@${ECHO}
	@${ECHO} "=== Updating live CD MD5 hash (takes about 1.5 minutes)."
	@cd ${LIVECD}
	${SUDO} ${RM} -f md5sum.txt
	@${ECHO} "find . -type f -print0 | xargs -0 md5sum > md5sum.txt"
	@${SUDO} /bin/sh -c '/usr/bin/find . -type f -print0 | xargs -0 md5sum > md5sum.txt'
	@cd ..

	@# Create live CD ISO.
	@${ECHO}
	@${ECHO} "=== Building final ISO image (takes about 40 seconds)."
	${SUDO} /usr/bin/mkisofs \
		-o $@ \
		-b isolinux/isolinux.bin \
		-c isolinux/boot.cat \
		-no-emul-boot \
		-boot-load-size 4 \
		-boot-info-table \
		-r \
		-V "Instant EPrints 3.0 i386" \
		-cache-inodes \
		-J \
		-input-charset iso8859-1 \
		-allow-leading-dots \
		${LIVECD}

	@${ECHO}
	@${ECHO} "=== Done!"


# Build the new squashfs for the ISO.

squashfs: ${LIVECD}/casper/filesystem.squashfs

${LIVECD}/casper/filesystem.squashfs: ${INSTANT_EPRINTS}_work.ext2
	@${ECHO}
	@${ECHO} "=== BUILDING NEW SQUASHFS ==="

	@# Mount squashfs source filesystem and set up work environment.
	@${ECHO}
	@${ECHO} "=== Initialising."
	@${SUDO} ${MOUNT} -o loop $< ${NEW_SQUASH}
	${SUDO} ${CP} /etc/resolv.conf ${NEW_SQUASH}/etc
	${SUDO} ${MOUNT} -t proc -o bind /proc ${NEW_SQUASH}/proc

	@# Copy in cached .deb files to save on downloads and set up apt
	@# sources, etc. An appropriate sources.list can be generated
	@# using the utility at http://www.ubuntu-nl.org/source-o-matic/.
	@${ECHO}
	@${ECHO} "=== Setting up apt (takes about 10 seconds)."
	${SUDO} ${CP} -a apt-cache/. ${NEW_SQUASH}/var/cache/apt/archives
	${SUDO} ${CP} /etc/apt/apt.conf ${NEW_SQUASH}/etc/apt
	${SUDO} ${CP} -b --suffix=.old sources.list ${NEW_SQUASH}/etc/apt

	@# Chroot to the squashfs source filesystem and reconfigure
	@# installed packages.
	@${ECHO}
	@${ECHO} "=== Reconfiguring installed packages (takes about xx minutes)."
	@${SUDO} ${CP} Makefile.chroot	${NEW_SQUASH}
	time ${SUDO} chroot ${NEW_SQUASH} /usr/bin/env HOME=/root LC_ALL=C \
		make -f Makefile.chroot

	@# Clean up.
	@${ECHO}
	@${ECHO} "=== Cleaning up."
	${SUDO} ${UMOUNT} ${NEW_SQUASH}/proc
	${SUDO} ${RM} -f ${REMOVE_FILES}
	${SUDO} /bin/mv ${NEW_SQUASH}/etc/apt/sources.list.old \
		${NEW_SQUASH}/etc/apt/sources.list
	${SUDO} ${RM} -rf ${NEW_SQUASH}/tmp/*

	@# Update manifest and disk name.
	@${ECHO}
	@${ECHO} "=== Updating manifest and disk name."
	${SED} -ie 's/DISKNAME  .*/DISKNAME  Instant EPrints 3.0 - i386/' \
		${LIVECD}/README.diskdefines
	${SUDO} chroot ${NEW_SQUASH} \
		dpkg-query -W --showformat='${Package} ${Version}\n' \
		> ${LIVECD}/casper/filesystem.manifest
	${SUDO} ${CP} ${LIVECD}/casper/filesystem.manifest \
		${LIVECD}/casper/filesystem.manifest-desktop
	${SED} -ie '/ubiquity/d' ${LIVECD}/casper/filesystem.manifest-desktop

	@# Clear free space on the squashfs source filesystem.
	@${ECHO}
	@${ECHO} "=== Clearing free space on new squashfs."
	-${SUDO} ${DD} if=/dev/zero of=${NEW_SQUASH}/dummyfile
	${SUDO} ${RM} ${NEW_SQUASH}/dummyfile

	@# Regenerate the live CD squashfs.
	@${ECHO}
	@${ECHO} "=== Building new squashfs (takes about 10 minutes)."
	${SUDO} ${RM} -f $@
	${SUDO} mksquashfs ${NEW_SQUASH} $@

	@# Unmount the squashfs source filesystem.
	@${SUDO} ${UMOUNT} ${NEW_SQUASH}

	@${ECHO}
	@${ECHO} "=== Done!"

${INSTANT_EPRINTS}_work.ext2: ${INSTANT_EPRINTS}.ext2
	@${ECHO}
	@${ECHO} "=== Making working copy of original filesystem (takes about 2.5 minutes)."
	time ${SUDO} ${CP} $< $@


# Build source filesystem.

filesystem: ${INSTANT_EPRINTS}.ext2

${INSTANT_EPRINTS}.ext2: ${UBUNTU}.iso
	@${ECHO}
	@${ECHO} "=== BUILDING SOURCE FILESYSTEM ==="

	@# Make a copy of the original ISO.
	@${ECHO}
	@${ECHO} "=== Copying original ISO (takes about 30 seconds)."
	@${SUDO} ${MOUNT} -t iso9660 -o loop $< /cdrom
	@${MKDIR} -p ${LIVECD}
	${SUDO} ${RM} -rf ${LIVECD}/*
	${CP} -a /cdrom/. ${LIVECD}
	/bin/chmod -R u+w ${LIVECD}
	@${SUDO} ${UMOUNT} /cdrom

	@# Remove unnecessary Windows cruft.
	@${ECHO}
	@${ECHO} "=== Removing "`/usr/bin/du -smh ${LIVECD}/programs | /usr/bin/cut -d'M' -f1`"MiB of unnecessary Windows crud."
	${RM} -rf ${LIVECD}/programs

	@# Mount original compressed filesystem.
	@${MKDIR} -p ${OLD_SQUASH}
	@${SUDO} ${MOUNT} -t squashfs -o loop,ro \
		${LIVECD}/casper/filesystem.squashfs ${OLD_SQUASH}

	@# Create image for new compressed filesystem. Add on an extra 500MiB
	@# to allow for the temporary files that we're going to copy in (mostly
	@# cached .deb files.
	@${ECHO}
	@${ECHO} "=== Creating new 2.5 GiB blank file system (takes about 1.5 minutes)."
	${SUDO} ${DD} if=/dev/zero \
		of=$@ bs=1M count=2500
	${SUDO} mke2fs -F -q $@

	@# Mount new compressed filesystem.
	@${MKDIR} -p ${NEW_SQUASH}
	@${SUDO} ${MOUNT} -o loop $@ ${NEW_SQUASH}

	@# Copy the old compressed filesystem.
	@${ECHO}
	@${ECHO} "=== Copying original compressed filesystem (takes about 2.5 minutes)."
	${SUDO} ${CP} -a ${OLD_SQUASH}/. ${NEW_SQUASH}

	@# Unmount all compressed filesystems.
	@${SUDO} ${UMOUNT} ${OLD_SQUASH}
	@${SUDO} ${UMOUNT} ${NEW_SQUASH}
	@${ECHO}
	@${ECHO} "=== Done!"


# Unmount everything that might be mounted. Useful if the make dies partway
# through.

cleanup:
	@${ECHO}
	@${ECHO} "=== Cleaning up mounted items."
	-${SUDO} ${UMOUNT} ${NEW_SQUASH}/proc
	-${SUDO} ${UMOUNT} ${NEW_SQUASH}
	-${SUDO} ${UMOUNT} ${OLD_SQUASH}
	-${SUDO} ${UMOUNT} /cdrom