summaryrefslogtreecommitdiffstats
path: root/bin/chroot_tester.sh
blob: dae4cacc1799e79e5b68696c3c37836e814399bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash

# Copyright 2012 Holger Levsen <holger@layer-acht.org>
# released under the GPLv=2

# $1 = base distro
# $2 = extra component
# $3 = upgrade distro

if [ "$1" == "" ] ; then
	echo "need at least one distribution to act on"
	echo '# $1 = base distro'
	echo '# $2 = extra component (gnome, kde, xfce, lxce)'
	echo '# $3 = upgrade distro'
	exit 1
fi

SLEEP=$(shuf -i 1-10 -n 1)
echo "Sleeping $SLEEP seconds to randomize start times and parallel runs."
sleep $SLEEP

set -x
set -e
export LANG=C
export MIRROR=http://ftp.de.debian.org/debian
export http_proxy="http://localhost:3128"

export SCRIPT_HEADER="#!/bin/bash
set -x
set -e
export DEBIAN_FRONTEND=noninteractive
export LANG=C
export http_proxy=$http_proxy"

export CHROOT_TARGET=$(mktemp -d -p /chroots/ $1.XXXXXXXXX)
export TMPFILE=$(mktemp -u)
export CTMPFILE=$CHROOT_TARGET/$TMPFILE

cleanup_all() {
	# FIXME: test if it starts with /chroots/
	if [ "$CHROOT_TARGET" = "" ] ; then
		echo "HALP. CHROOT_TARGET = $CHROOT_TARGET"
		exit 1
	fi
	sudo umount -l $CHROOT_TARGET/proc || true
	sudo umount -l $CHROOT_TARGET/run/lock || true
	sudo umount -l $CHROOT_TARGET/run/shm || true
	sudo umount -l $CHROOT_TARGET/run || true
	sudo rm -rf $CHROOT_TARGET
}

execute_ctmpfile() {
	chmod +x $CTMPFILE
	sudo chroot $CHROOT_TARGET $TMPFILE
	rm $CTMPFILE
}

prepare_bootstrap() {
cat >> $CTMPFILE <<-EOF
$SCRIPT_HEADER
mount /proc -t proc /proc
echo -e '#!/bin/sh\nexit 101' > /usr/sbin/policy-rc.d
chmod +x /usr/sbin/policy-rc.d
EOF
}

prepare_install_packages() {
cat >> $CTMPFILE <<-EOF
$SCRIPT_HEADER
apt-get update
apt-get -y install $1
EOF
}

prepare_upgrade2() {
cat >> $CTMPFILE <<-EOF
echo "deb $MIRROR $1 main contrib non-free" > /etc/apt/sources.list
$SCRIPT_HEADER
apt-get update
#apt-get -y install apt
apt-get -y upgrade
apt-get -y dist-upgrade
apt-get -y dist-upgrade
apt-get -y autoremove
EOF
}

bootstrap() {
	echo "Bootstraping $1 into $CHROOT_TARGET now."
	sudo debootstrap $1 $CHROOT_TARGET $MIRROR
	prepare_bootstrap
	execute_ctmpfile 
}

install_packages() {
	echo "Installing extra packages for $1 now."
	prepare_install_packages $2
	execute_ctmpfile 
}

upgrade2() {
	echo "Upgrading to $1 now."
	prepare_upgrade2 $1
	execute_ctmpfile 
}

trap cleanup_all INT TERM EXIT

case $1 in
	squeeze)bootstrap squeeze;;
	wheezy)	bootstrap wheezy;;
	sid)	bootstrap sid;;
	*)	echo "unsupported distro." ; exit 1 ;;
esac

if [ "$2" != "" ] ; then
	case $2 in
		none)	;;
		gnome)	install_packages gnome gnome ;;
		kde)	install_packages kde kde-plasma-desktop ;;
		xfce)	install_packages xfce xfce4 ;;
		lxde)	install_packages lxde lxde ;;
		*)	echo "unsupported component." ; exit 1 ;;
	esac
fi

if [ "$3" != "" ] ; then
	case $3 in
		squeeze)upgrade2 squeeze;;
		wheezy)	upgrade2 wheezy;;
		sid)	upgrade2 sid;;
		*)	echo "unsupported distro." ; exit 1 ;;
	esac
fi

cleanup_all
trap - INT TERM EXIT