Create Ubuntu LiveCD from Squash

#!/bin/bash
 
# last version
# http://dl.dropbox.com/u/4170695/scripts/mkTSClient.sh
 
# http://ubuntuforums.org/showpost.php?p=5175091&postcount=27
 
# version: 2009-09-09
 
# todo
# configure locales
# configure hostname
# clean up "fglrx"
# check install_lirc
# umount all from chroot envirement, rm: Entfernen von Verzeichnis „$HOME/xbmc-XXX-livecd-hardy-i386/chroot/lib/modules/2.6.24-21-generic/volatile“ nicht möglich: Device or resource busy
 
# parameter
# architecture: i386, amd64
ARCH=i386
# distribution: hardy, intrepid, jaunty, karmic
DISTRIB_CODENAME=karmic
 
# check
if [ ${USER} != "root" ]; then
	echo "Please run this script as root"
	echo "sudo $0"
	exit 0
fi
 
if [ -z $1 ]; then
	echo "Please specify a image to build: slideshow, xbmc, ts"
	exit 0
else
	IMAGE=$1
fi
 
# functions
function state() {
	echo "STATE: $1"
	if [ $1 != 0 ]; then
		echo "ERROR";		
		exit
	else
		echo "OK";
	fi
}
 
function check_host_packages() {
	echo "check_host_packages..."
 
# add repository
cat <<EOF> /etc/apt/sources.list.d/${DISTRIB_CODENAME}.list
deb http://archive.ubuntu.com/ubuntu/ ${DISTRIB_CODENAME} main
EOF
 
# update
apt-get update 1>/dev/null
 
# install debootstrap
apt-get install -y --force-yes debootstrap squashfs-tools 1>/dev/null
 
# remove repository
rm /etc/apt/sources.list.d/${DISTRIB_CODENAME}.list
apt-get update 1>/dev/null
}
 
function mk_run() {
cat <<EOF>> /etc/init.d/xrun
#!/bin/sh
 
case "\$1" in
   start)
      if [ -f /cdrom/run ]; then
         /cdrom/run &
      else
         echo "[W] /cdrom/run not found, exit to bash"
      fi
      ;;
   stop)
      ;;
   *)
      ;;
esac
 
exit 0
EOF
 
chmod a+x /etc/init.d/xrun
update-rc.d xrun defaults 99
}
 
 
 
 
function mk_slideshow() {
	apt-get install -y xserver-xorg xorg qiv wget
	#1680x1050
 
	#echo "deb http://archive.ubuntu.com/ubuntu hardy main universe multiverse restricted" > /etc/apt/sources.list
	#apt-get update 1>/dev/null
 
	wget http://panticz.de/sites/default/files/xserver-xorg-video-ati-6.6.3_hardy.tar_.bz2 -P /tmp/
	tar xjf /tmp/xserver-xorg-video-ati-6.6.3_hardy.tar_.bz2 -C /tmp
	cp /tmp/usr/lib/xorg/modules/multimedia/* /usr/lib/xorg/modules/multimedia/
	cp /tmp/usr/lib/xorg/modules/drivers/* /usr/lib/xorg/modules/drivers/
}
 
function mk_xbmc() {
	echo "mk_xbmc..."
 
	# set root password
	echo "root:xbmc" | chpasswd
 
	# install debconf
	apt-get install debconf-utils
 
	# install lirc
	install_lirc
 
	# install Xorg
	apt-get update #1>/dev/null
	apt-get install -y --force-yes xorg
	#? xserver-xorg
 
	# install tools
	apt-get install -y --force-yes lirc sshfs openssh-server alsa alsa-utils wget hdparm
	apt-get install -y --force-yes xinit x11-xserver-utils	#?
 
	# install xbmc
	install_xbmc
 
	# install fglrx (AMD proprietary Xorg driver)
	install_fglrx
 
	# install cpupowerd (xbmc settings)
	install_cpupowerd_base
 
	# sensores
	###apt-get install -y lm-sensors
 
	# fix ssh login
	echo "UseDNS no" >> /etc/ssh/sshd_config
}
 
function install_lirc() {
# preconfigure lirc for ATI/NVidia X10 RF remote
debconf-set-selections <<\EOF
lirc lirc/remote select ATI/NVidia X10 RF (userspace)
lirc lirc/transmitter select None
EOF
 
	# fix lirc
	echo "blacklist ati_remote" >> /etc/modprobe.d/blacklist
 
	# fix ati remote
	echo "ati_remote" >> /etc/modules
}
 
function install_xbmc() {
# add xbmc repository
cat <<EOF> /etc/apt/sources.list.d/xbmc.list
deb http://ppa.launchpad.net/team-xbmc-svn/ubuntu ${DISTRIB_CODENAME} main
deb-src http://ppa.launchpad.net/team-xbmc-svn/ubuntu ${DISTRIB_CODENAME} main
 
deb http://ppa.launchpad.net/xbmc-addons/ubuntu ${DISTRIB_CODENAME} main
deb-src http://ppa.launchpad.net/xbmc-addons/ubuntu ${DISTRIB_CODENAME} main
 
#deb http://ppa.launchpad.net/team-xbmc-intrepid/ppa/ubuntu intrepid main
#deb-src http://ppa.launchpad.net/team-xbmc-intrepid/ppa/ubuntu intrepid main
EOF
 
# install xbmc
apt-get update
apt-get install -y --force-yes xbmc
 
#?
apt-get install -f -y
}
 
function install_cpupowerd() {
	install_cpupowerd_base
 
cat <<EOF> /etc/cpupowerd.conf
1000 0.8500
1800 1.1500
2000 1.1500
2200 1.2000 
EOF
 
cat <<EOF> /etc/rc.local
#!/bin/sh -e
/usr/sbin/cpupowerd -d -c /etc/cpupowerd.conf
exit 0
EOF
}
 
function install_cpupowerd_base() {
	wget http://www.panticz.de/sites/default/files/cpupowerd.bz2 -P /tmp
	bzip2 -d /tmp/cpupowerd.bz2
	mv /tmp/cpupowerd /usr/sbin/
	chmod +x /usr/sbin/cpupowerd
 
cat <<EOF>> /etc/modules
msr
powernow-k8
cpufreq_userspace
EOF
}
 
function install_cpuload() {
	# configure wallpaper
	rm /usr/share/lxde/pcmanfm/main.lxde
	wget http://www.panticz.de/sts/main.lxde -O /usr/share/lxde/pcmanfm/main.lxde
 
	# configure panel
	rm /usr/share/lxpanel/profile/LXDE/panels/panel
	wget http://www.panticz.de/sts/panel -O /usr/share/lxpanel/profile/LXDE/panels/panel
 
	# get cpuload scripts
	wget http://www.panticz.de/sts/cpu_load_v2.tar.bz2 -P /tmp
	tar xjf /tmp/cpu_load_v2.tar.bz2 -C /usr/sbin/
 
	# create application starter
	wget http://www.panticz.de/sts/cpu_load_cyclic.desktop -P /usr/share/applications/
	wget http://www.panticz.de/sts/cpu_load_100.desktop -P /usr/share/applications/
	wget http://www.panticz.de/sts/logout.desktop -P /usr/share/applications/
}
 
function cp_install_fglrx() {
	####cp /home/pako/fglrx/intrepid/fglrx.ko ${DIR}/chroot/tmp/
	####cp /home/pako/fglrx/intrepid/*.deb ${DIR}/chroot/tmp/
 
	echo "-- cp_install_fglrx 1 --"
	DIR=/home/pako/develop/fglrx/intrepid
	apt-get install -f
	dpkg -i ${DIR}/*.deb
	apt-get install -f
	mkdir -p /lib/modules/$(uname -r)/updates/dkms
	cp ${DIR}/fglrx.ko /lib/modules/$(uname -r)/updates/dkms/
	depmod -a $(ls /lib/modules)
 
	#mkdir /root/ati
	#wget --no-check-certificate https://www2.ati.com/drivers/linux/ati-driver-installer-9-1-x86.x86_64.run -P /tmp/root
	#cd /root/ati
	#echo "-------------4----------------"
	#chmod 777 /tmp/root/ati-driver-installer-9-1-x86.x86_64.run
	#/tmp/root/ati-driver-installer-9-1-x86.x86_64.run --buildandinstallpkg Ubuntu/intrepid
}
 
function install_fglrx() {
	apt-get install -f -y
	apt-get install -y --force-yes build-essential cdbs fakeroot dh-make debhelper debconf libstdc++5 dkms
	apt-get install -f -y
	apt-get install -y --force-yes linux-headers-$(uname -r)
	apt-get install -f -y
	#apt-get install -y --force-yes linux-restricted-modules-generic	# no more avaiable under karmic ?
	#apt-get install -f -y
 
	install_fglrx-installer
}
 
function install_fglrx-installer() {
	#	apt-get install -y --force-yes xorg-driver-fglrx linux-restricted-modules-generic
 
	# “fglrx-installer” packages
	# https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179
	mkdir /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/fglrx-amdcccle_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/fglrx-kernel-source_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/fglrx-modaliases_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/libamdxvba1_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/xorg-driver-fglrx_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	wget https://launchpad.net/ubuntu/+source/fglrx-installer/2:8.660-0ubuntu1/+build/1204179/+files/xorg-driver-fglrx-dev_8.660-0ubuntu1_i386.deb -P /tmp/fglrx-installer
	dpkg -i /tmp/fglrx-installer/*.deb
 
	#?
	depmod -a $(ls /lib/modules)
}
 
function install_fglrx_from_amd() {
	##mkdir -p /lib/modules/2.6.27-11-generic/updates/dkms
	##cp /tmp/fglrx.ko /lib/modules/2.6.27-11-generic/updates/dkms/
	##depmod -a
	##dpkg -i /tmp/*.deb
 
	# NEW #
 
	#wget --no-check-certificate https://www2.ati.com/drivers/linux/ati-driver-installer-9-5-x86.x86_64.run -P /tmp/root
	wget http://192.168.1.9/ubuntu/install/ati-driver-installer-9-5-x86.x86_64.run -P /tmp
	echo "-- fglrx1 --"
	read
 
	cd /tmp
	chmod +x /tmp/ati-driver-installer-*.run
	echo "-- fglrx2 --"
	read
 
	/tmp/ati-driver-installer-*.run --buildandinstallpkg Ubuntu/intrepid
	echo "-- fglrx3 --"
	read
}
 
function mk_ts() {
# dep
# preconfigure
#debconf-set-selections <<\EOF
#postfix postfix/mailname string localdomain
#postfix postfix/main_mailer_type select Internet Site
#EOF
 
 
	apt-get -y install hwinfo bonnie++ stress x86info memtester nfs-common wget psmisc bc pciutils perl lshw openssh-server
	apt-get -y install smartmontools --no-install-recommends
	# disable openssh-server
 
	# update pci ids
	wget http://pciids.sourceforge.net/pci.ids -O /usr/share/misc/pci.ids
 
	# install additional applications
	mk3ware
	mkCtcs
 
	# mkTSGui
 
	# ts_gui
	# glxgears -fullscreen
}
 
function mkTSGui() {
	apt-get install -y --force-yes xserver-xorg xorg
	apt-get install -y --force-yes xorg-driver-fglrx linux-restricted-modules-generic
}
 
function mkCtcs() {
	if [ $(uname -m) == "x86_64" ]; then
		wget http://www.panticz.de/sites/default/files/ctcs-1.3.0_64.tar.bz2 -P /tmp
	else
		wget http://www.panticz.de/sites/default/files/ctcs-1.3.0_32.tar.bz2 -P /tmp
	fi
 
	tar xjf /tmp/ctcs-*.tar.bz2 -C /tmp
	cd /tmp/ctcs-1.3.0/
	mkdir /usr/share/ctcs
	cp -a /tmp/ctcs-1.3.0/* /usr/share/ctcs
 
	#todo
	#fix burnin (nice stress -c $(cat /proc/cpuinfo | grep processor | wc -l) -t 900)
	#compile stress
	#http://www.panticz.de/?q=node/230
}
 
function mk3ware() {
	# 3ware CLI for 7006-2 / 8006-2
	if [ $(uname -m) == "x86_64" ]; then
		wget http://www.3ware.com/download/Escalade9690SA-Series/9.5.0.1/tw_cli-linux-x86_64-9.5.0.1.tgz -P /tmp
	else
		wget http://www.3ware.com/download/Escalade9690SA-Series/9.5.0.1/tw_cli-linux-x86-9.5.0.1.tgz -P /tmp
	fi
	tar xzf /tmp/tw_cli-linux-*.tgz -C /usr/sbin/
}
 
function mk_small() {
	rm /etc/event.d/tty6
	rm /etc/event.d/tty5
	rm /etc/event.d/tty4
	rm /etc/event.d/tty3
	rm /etc/event.d/tty2
}
 
function mk_super_small() {
	mk_small
 
	#enable my again# rm /etc/event.d/tty*
 
	update-rc.d -f sysklogd remove
	update-rc.d -f klogd remove
 
	rm -r /usr/share/doc
	rm -r /usr/share/man
	rm -r /usr/share/i18n
 
	# /usr/share/locale
	#test# update-rc.d -f nvidia-kernel remove
	#test# update-rc.d -f xserver-xorg-input-wacom remove
	#test# update-rc.d -f rmnologin remove
}
 
function check_dirs() {
	echo "check_dirs..."
	if [ -d ${DIR}/chroot ]; then
		echo "${DIR}/chroot already exists, please delete first"
		exit 0;
	else
		mkdir -p ${DIR}/chroot
		mkdir -p ${DIR}/image/casper
	fi
}
 
function mk_bootstrap() {
	echo -n "mk_bootstrap..."
	debootstrap --arch ${ARCH} ${DISTRIB_CODENAME} ${DIR}/chroot http://archive.ubuntu.com/ubuntu/ 1>/dev/null
 
	state $?
}
 
 
function create_sources() {
echo "create_sources..."
cat <<EOF> ${DIR}/chroot/etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME} main universe multiverse restricted
deb http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME}-updates main universe multiverse restricted
deb http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME}-security main universe multiverse restricted
#deb http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME}-proposed main universe multiverse restricted
#deb http://archive.ubuntu.com/ubuntu ${DISTRIB_CODENAME}-backports main universe multiverse restricted
EOF
}
 
function create_bashrc() {
echo "create_bashrc..."
cat <<EOF>> ${DIR}/chroot/root/.bashrc
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
EOF
 
echo 'LANG="en_US"' > ${DIR}/chroot/etc/default/locale
}
 
function create_chroot() {
	echo "create_chroot..."
}
 
 
function configure_locale() {
	echo "configure_locale..."
	locale-gen en_US.UTF-8
}
 
function update_chroot() {
	echo "update_chroot..."
 
debconf-set-selections <<\EOF
# lilo (hardy)
console-setup console-setup/charmap select UTF-8
lilo lilo/new-config select
 
# grub2 (karmic)
grub-pc grub2/linux_cmdline select 
grub-pc grub2/linux_cmdline_default select quiet splash
EOF
 
	apt-get update #1>/dev/null
	apt-get dist-upgrade -y
}
 
function install_default_apps() {
	echo "install_default_apps..."
 
	apt-get install -y casper linux-generic
}
 
function install_lxde() {
	echo "install_lxde..."
 
	#apt-get install -y lxde
#	aptitude install -y --without-recommends lxde
	#-core
	apt-get install -y lxde
	apt-get install -y xserver-xorg-core xinit
	apt-get install -y xterm gnome-system-monitor
	apt-get remove -y xscreensaver
 
# disable screansaver
#cat <<EOF> /etc/xdg/lxsession/LXDE/autostart
#@lxde-settings
#@lxpanel --profile LXDE
#@pcmanfm -d
#EOF
}
 
function mk_firefox() {
	# firefox os
	apt-get install -y firefox xserver-xorg xorg
 
	# todo
	# run as unpriviliged user "ubuntu"
	# add flash
 
 
	echo firefox > /root/.xsession
 
	# override /etc/init.d/xrun
cat <<EOF> /etc/init.d/xrun
#!/bin/sh
 
case "\$1" in
   start)
         startx &
      ;;
   stop)
      ;;
   *)
      ;;
esac
 
exit 0
EOF
}
 
 
function install_extra_apps() {
	echo "install_extra_apps for ${IMAGE}..."
	case ${IMAGE} in
		slideshow)
			mk_run
			mk_slideshow
			mk_super_small
		;;
		xbmc)
			mk_run
			mk_xbmc
			#mk_super_small
		;;
		ts)
			mk_run
			mk_ts
			mk_small
		;;
		pm)
			mk_run
			apt-get install -y stress wget powernowd cpufrequtils cpufreqd sysfsutils
			#install_lxde
			# dep install_cpupowerd
			install_cpuload
			mk_small
		;;
		firefox)
			mk_run "/root/run"
			mk_firefox
		;;
		*)
			mk_run
			#mk_super_small
			#install_lastest_kernel
		;;
	esac
}
 
function install_lastest_kernel() {
echo "install karmic kernel..."
rm /boot/vmlinuz-2.6.**-**-generic
rm /boot/initrd.img-2.6.**-**-generic
 
mv /etc/apt/sources.list /etc/apt/sources.list.org
 
cat <<EOF> /etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu karmic main universe multiverse restricted
EOF
 
apt-get install -f
apt-get update
apt-get install -f
apt-get install -y linux-generic
mv /etc/apt/sources.list.org /etc/apt/sources.list
apt-get update
}
 
function exit_chroot() {
	echo "exit_chroot..."
	apt-get clean
	rm -rf /tmp/*
	rm /etc/resolv.conf
	umount -l -f /proc
	umount -l -f /sys
	umount /dev/pts
}
 
function configure_resolv() {
	cp /etc/resolv.conf ${DIR}/chroot/etc/resolv.conf
}
 
function mount_chroot_sys() {
	mount /proc
	mount /sys
	mount -t devpts none /dev/pts
}
 
function clean_bash_history() {
	FILE=${DIR}/chroot/root/.bash_history
	[ -f ${FILE} ] && rm ${FILE}
}
 
function get_squashfs() {
	EXCLUDE="-e ${DIR}/chroot/boot"
	# -e ${DIR}/chroot/var/lib/apt/lists -e ${DIR}/chroot/var/cache/apt"
 
	cp ${DIR}/chroot/boot/vmlinuz-2.6.**-**-generic ${DIR}/image/casper/vmlinuz
	cp ${DIR}/chroot/boot/initrd.img-2.6.**-**-generic ${DIR}/image/casper/initrd.gz
 
 
 
 
	#mksquashfs ${DIR}/chroot/ ${DIR}/image/casper/filesystem3.squashfs -nolzma -noDataCompression -noappend ${EXCLUDE}
	#mksquashfs ${DIR}/chroot/ ${DIR}/image/casper/filesystem2.squashfs -nolzma -noappend ${EXCLUDE}
	mksquashfs ${DIR}/chroot/ ${DIR}/image/casper/filesystem.squashfs -noappend ${EXCLUDE}
 
	chmod a+r ${DIR}/image/casper/*
}
 
if [ $# == 1 ]; then
	# main
	export DIR="/tmp/${IMAGE}-$(date  "+%Y-%m-%d_%H%M")-livecd-${DISTRIB_CODENAME}-${ARCH}"
	echo "create ${DIR}"
 
	check_host_packages
	check_dirs
	mk_bootstrap
	create_sources
	create_bashrc
	configure_resolv
 
	# cp fglrx
	case ${IMAGE} in
		xbmc)
			echo "copy..."
			#cp_install_fglrx
		;;
	esac
 
	# create_chroot
	echo "cp $0 ${DIR}/chroot/tmp/"
	cp $0 ${DIR}/chroot/tmp/
 
	echo "chroot ${DIR}/chroot/ /tmp/$0 ${IMAGE} ${DIR}"
	echo "/tmp/$0"
 
echo -------------------------------------------
	chroot ${DIR}/chroot/ sh -c "locale-gen en_US.UTF-8"
echo -------------------------------------------
	chroot ${DIR}/chroot/ sh -c "locale -a"
echo -------------------------------------------	
	chroot ${DIR}/chroot/ sh -c "/tmp/$0 ${IMAGE} ${DIR}"
echo -------------------------------------------
 
	# clean bash history
	clean_bash_history
 
	# get_squashfs
	get_squashfs
fi
 
if [ $# == 2 ]; then
	echo "---- CHROOT:$1 ----"
 
	##mount_chroot_sys
	mount_chroot_sys
 
	# read variables
	. /etc/lsb-release
 
	##configure_locale	
	# 2009-07-09 configure_locale
	#export LG_ALL=en_US
 
	##update_chroot
	update_chroot
 
	##install_default_apps
	install_default_apps
 
	##install_extra_apps
	install_extra_apps
 
	##exit_chroot
	exit_chroot
fi
 
exit
 
 
 
 
 
 
 
#######################################################
### TEST
#######################################################
 
echo "copy files"
 
#ssh root@192.168.2.1 "mv /var/lib/tftpboot/xbmc/casper/ /var/lib/tftpboot/xbmc/casper.$(date -I)"
#scp -a ${DIR}/image/casper root@192.168.2.1:/tftpboot/xbmc
 
 
# copy
switch($MODE) {
	case SLIDESHOW:
		scp ${DIR}/image/casper/* root@192.168.1.2:/tftpboot/slideshow/casper/
		scp ${DIR}/image/casper/* root@192.168.1.2:/tftpboot/slideshow2/casper/
 
	;;
	case XBMC:
DATE=$(date +%Y-%m-%d_%H-%M-%S)
ssh root@192.168.2.1 "mkdir /var/lib/tftpboot/xbmc/casper/${DATE}"
ssh root@192.168.2.1 "mv /var/lib/tftpboot/xbmc/casper/initrd.gz /var/lib/tftpboot/xbmc/casper/${DATE}"
ssh root@192.168.2.1 "mv /var/lib/tftpboot/xbmc/casper/filesystem.squashfs /var/lib/tftpboot/xbmc/casper/${DATE}"
ssh root@192.168.2.1 "mv /var/lib/tftpboot/xbmc/casper/vmlinuz /var/lib/tftpboot/xbmc/casper/${DATE}"
scp /tmp/xbmc-*/image/casper/* root@192.168.2.1:/var/lib/tftpboot/xbmc/casper/
 
	;;
	case TEST:
		cp ${DIR}/image/casper/* /tftpboot/test32/casper/
		cp ${DIR}/image/casper/* /tftpboot/test64/casper/
		scp ${DIR}/image/casper/* root@192.168.1.34:/tftpboot/test32/casper/
 
	;;
}
 
 
 
# configure tftpboot
cat <<EOF> /tftpboot/pxelinux.cfg/xbmc.conf
LABEL linux
   MENU LABEL XBMC 
   KERNEL /xbmc/casper/vmlinuz
   APPEND initrd=/xbmc/casper/initrd.gz boot=casper netboot=nfs nfsroot=192.168.2.1:/tftpboot/xbmc --
EOF
 
# configure nfs
echo "/tftpboot/test32	*(ro,sync,no_subtree_check)" >> /etc/exports

Do you like this page? Then support it. Please click the AD below and visit the sponsor. Thank you!