aboutsummaryrefslogtreecommitdiff
blob: ad271a2b7e567c9c5d39551215cb91130c9d9ffa (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash

# Gentoaster VM build tool
# Licensed under GPL v3, see COPYING file
# Usage: ./create_image.sh <configuration file path>
# Prerequisites: extlinux, qemu-img, sfdisk

# Parse the configuration we took as an arg
source parse_config.sh $1

# Generate a few helper variables using the configuration file

IMAGE_NAME="${BUILD_ID}.image"
ROOT_MEGABYTES=$(( ${IMAGE_MEGABYTES} - ( ${BOOT_MEGABYTES} + ${SWAP_MEGABYTES} + 1 ) ))
IMAGE_BYTES=$(( ${IMAGE_MEGABYTES} * 1024 * 1024 ))
IMAGES_OUTPUT_PATH=`pwd`
IMAGE_WORK_PATH="/root/gentoo/${BUILD_ID}"
LOG_FILE="${IMAGE_WORK_PATH}/log.txt"
TOOL_RES_PATH=`pwd`/res

CPU_CORES=`grep -c processor /proc/cpuinfo`
STAGE3_URL="http://distfiles.gentoo.org/releases/x86/current-stage3/stage3-i486-20110607.tar.bz2"
PORTAGE_URL="http://distfiles.gentoo.org/snapshots/portage-latest.tar.bz2"
BINHOST_URL="http://tinderbox.dev.gentoo.org/default-linux/x86"

echo "Creating build working directory"
mkdir -p ${IMAGE_WORK_PATH}
cd ${IMAGE_WORK_PATH}

echo "Starting log at ${LOG_FILE}..."
echo "" > ${LOG_FILE}

# Create disk iamge
BYTES_PER_CYLINDER=$(( 512*63*255 ))
CYLINDERS=$(( ${IMAGE_BYTES}/${BYTES_PER_CYLINDER} ))
REAL_IMAGE_BYTES=$(( (${CYLINDERS}+1)*${BYTES_PER_CYLINDER} ))
echo "Creating image ${IMAGE_NAME}, size ${REAL_IMAGE_BYTES} bytes"
qemu-img create -f raw ${IMAGE_NAME} ${REAL_IMAGE_BYTES} &>> ${LOG_FILE}

# Clean up old mounts
cleanup_mounts() {
	echo "Cleaning up old mounts"
	umount -d rootfs/dev/pts &>> ${LOG_FILE}
	umount -d rootfs/dev/shm &>> ${LOG_FILE}
	umount -d rootfs/dev &>> ${LOG_FILE}
	umount -d rootfs/proc &>> ${LOG_FILE}
	umount -d rootfs/boot &>> ${LOG_FILE}
	umount -d rootfs &>> ${LOG_FILE}
}
cleanup_mounts

# Create partition table
echo "Writing partition table"
echo -e "\x55\xaa" | dd bs=1 count=2 seek=510 of=${IMAGE_NAME} conv=notrunc &>> ${LOG_FILE}
LOOP_DEV_IMAGE=`losetup -f`
losetup --sizelimit ${REAL_IMAGE_BYTES} ${LOOP_DEV_IMAGE} ${IMAGE_NAME} &>> ${LOG_FILE}

sfdisk ${LOOP_DEV_IMAGE} -H64 -S32 &>> ${LOG_FILE} << EOF
1,${BOOT_MEGABYTES},83,*
$(( 1+ ${BOOT_MEGABYTES} )),${SWAP_MEGABYTES},82,-
$(( 1+ ${BOOT_MEGABYTES} + ${SWAP_MEGABYTES} )),${ROOT_MEGABYTES},83,-
EOF
sleep 2
losetup -d ${LOOP_DEV_IMAGE}

# Mounting new root and boot
echo "Creating filesystems"

LOOP_DEV_BOOT=`losetup -f`
losetup -o $(( 512 * 2048 )) --sizelimit $(( ${BOOT_MEGABYTES} * 1024 * 1024 )) ${LOOP_DEV_BOOT} ${IMAGE_NAME}
mkfs -t ext2 ${LOOP_DEV_BOOT} &>> ${LOG_FILE}
sleep 2
losetup -d ${LOOP_DEV_BOOT}

LOOP_DEV_ROOT=`losetup -f`
losetup -o $(( ( 512 * 2048 )  + ( ${BOOT_MEGABYTES} * 1024 * 1024 ) + ( ${SWAP_MEGABYTES} * 1024 * 1024 ) )) --sizelimit $(( ${ROOT_MEGABYTES} * 1024 * 1024 )) ${LOOP_DEV_ROOT} ${IMAGE_NAME}
mkfs -t ext3 ${LOOP_DEV_ROOT} &>> ${LOG_FILE}
sleep 2
losetup -d ${LOOP_DEV_ROOT}

LOOP_DEV_SWAP=`losetup -f`
losetup -o $(( ( 512 * 2048 ) + ( ${BOOT_MEGABYTES} * 1024 * 1024 ) )) --sizelimit $(( ${SWAP_MEGABYTES} * 1024 * 1024 )) ${LOOP_DEV_SWAP} ${IMAGE_NAME}
mkswap ${LOOP_DEV_SWAP} &>> ${LOG_FILE}
sleep 2 
losetup -d ${LOOP_DEV_SWAP}

echo "Mounting fileystems"

mkdir -p rootfs
mount -o loop,offset=$(( ( 512 * 2048 )  + ( ${BOOT_MEGABYTES} * 1024 * 1024 ) + ( ${SWAP_MEGABYTES} * 1024 * 1024 ) )) ${IMAGE_NAME} rootfs
mkdir -p rootfs/boot
mount -o loop,offset=$(( 512 * 2048 )) ${IMAGE_NAME} rootfs/boot
cd rootfs

# Setup Gentoo

if [ ! -f ${TOOL_RES_PATH}/stage3.tar.bz2 ];
then
	echo "Downloading Stage 3"
	wget ${STAGE3_URL} -O ${TOOL_RES_PATH}/stage3.tar.bz2 &>> ${LOG_FILE}
fi

echo "Extracting Stage 3"
tar jxf ${TOOL_RES_PATH}/stage3.tar.bz2 &>> ${LOG_FILE}

if [ ! -f ${TOOL_RES_PATH}/portage-latest.tar.bz2 ];
then
	echo "Downloading Portage snapshot"
	wget ${PORTAGE_URL} -O ${TOOL_RES_PATH}/portage-latest.tar.bz2 &>> ${LOG_FILE}
fi

echo "Extracting Portage snapshot"
cd usr
tar jxf ${TOOL_RES_PATH}/portage-latest.tar.bz2 &>> ${LOG_FILE}

echo "Setting up chroot"
cd ..
mount -t proc /proc proc
mount --rbind /dev dev
cp -L /etc/resolv.conf etc/resolv.conf

echo "Setting up make.conf"
mkdir -p usr/portage/packages
echo 'PORTAGE_BINHOST="${BINHOST_URL}"' >> etc/make.conf
echo 'PKGDIR="/usr/portage/packages"' >> etc/make.conf
echo 'FEATURES="${FEATURES}"' >> etc/make.conf
echo 'USE="${USE_FLAGS}"' >> etc/make.conf
if [[ ${OUTPUT_FORMAT} = "vbox" ]]
then
	echo 'INPUT_DEVICES="virtualbox evdev"' >> etc/make.conf
	echo 'VIDEO_CARDS="virtualbox"' >> etc/make.conf
fi

echo "Setting up package.use"
mkdir -p etc/portage
echo ${PACKAGE_USE} >> etc/portage/package.use

echo "Setting up package.accept_keywords"
echo ${PACKAGE_ACCEPT_KEYWORDS} >> etc/portage/package.accept_keywords

echo "Entering chroot"
linux32 chroot . env-update &>> ${LOG_FILE}

echo "Loading profile"
linux32 chroot . /bin/bash /etc/profile &>> ${LOG_FILE}

echo "Setting timezone to ${TIMEZONE}"
linux32 chroot . cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime &>> ${LOG_FILE}

echo "Setting hostname to ${HOSTNAME}"
linux32 chroot . /bin/bash -c "echo 127.0.0.1 ${HOSTNAME}.local ${HOSTNAME} localhost > /etc/hosts" &>> ${LOG_FILE}

echo "Copying new fstab"
cp ${TOOL_RES_PATH}/fstab etc/fstab &>> ${LOG_FILE}

echo "Setting up networking"
echo 'config_eth0=( "dhcp" )' > etc/conf.d/net
ln -s etc/init.d/net.lo etc/init.d/net.eth0
linux32 chroot . rc-update add net.eth0 default

echo "Downloading/installing kernel sources"
linux32 chroot . emerge gentoo-sources &>> ${LOG_FILE}

echo "Copying kernel configuration"
cp ${TOOL_RES_PATH}/kernelconfig usr/src/linux/.config

echo "Building kernel"
linux32 chroot . make -C /usr/src/linux -j$(( ${CPU_CORES}+1 )) &>> ${LOG_FILE}

echo "Installing kernel"
linux32 chroot . make -C /usr/src/linux modules_install &>> ${LOG_FILE}
linux32 chroot . cp /usr/src/linux/arch/i386/boot/bzImage /boot/kernel &>> ${LOG_FILE}

echo "Setting root password"
linux32 chroot . /bin/bash -c "echo 'root:${ROOT_PASSWORD}' | chpasswd" &>> ${LOG_FILE}

echo "Installing packages list"
linux32 chroot . emerge ${PACKAGES_LIST} &>> ${LOG_FILE}

echo "Adding default user"
linux32 chroot . useradd -g users -G lp,wheel,audio,cdrom,portage -m ${DEFAULT_USERNAME}
linux32 chroot . /bin/bash -c "echo '${DEFAULT_USERNAME}:${DEFAULT_PASSWORD}' | chpasswd" &>> ${LOG_FILE}

if [[ ${OUTPUT_FORMAT} = "vbox" ]]
then
	echo "Installing VirtualBox additions/drivers"
	linux32 chroot . emerge xf86-video-virtualbox xf86-input-virtualbox virtualbox-guest-additions &>> ${LOG_FILE}
	linux32 chroot . rc-update add virtualbox-guest-additions default &>> ${LOG_FILE}
	linux32 chroot . mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak &>> ${LOG_FILE}
	linux32 chroot . usermod -a vboxguest ${DEFAULT_USERNAME}
fi

echo "Installing extlinux"
extlinux --heads 255 --sectors 63 --install boot
dd if=/usr/lib/extlinux/mbr.bin of=../${IMAGE_NAME} conv=notrunc
cp ${TOOL_RES_PATH}/extlinux.conf boot/
cd  ..
cleanup_mounts

case "${OUTPUT_FORMAT}" in
	"raw" )
		echo "Already in raw format, not converting"
		IMAGE_OUT="${IMAGE_NAME}.image"
		;;
	"vbox" )
		echo "Converting image from RAW to VDI"
		qemu-img convert -O vdi ${IMAGE_NAME} ${BUILD_ID}.vdi
		rm -rf ${IMAGE_NAME}
		IMAGE_OUT="${IMAGE_NAME}.vdi"
		;;
	"vmware" )
		echo "Converting image from RAW to VMDK"
		qemu-img convert -O vmdk ${IMAGE_NAME} ${BUILD_ID}.vmdk
		rm -rf ${IMAGE_NAME}
		IMAGE_OUT="${IMAGE_NAME}.vmdk"
		;;
esac
mv ${IMAGE_OUT} ${IMAGES_OUTPUT_PATH}/${IMAGE_OUT}
mv ${LOG_FILE} ${IMAGES_OUTPUT_PATH}/${BUILD_ID}.log
rm -rf ${IMAGE_WORK_PATH}

echo "Image build completed!"
echo "Your image is here: ${IMAGES_OUTPUT_PATH}/${IMAGE_OUT}"
echo "Your log file is here: ${IMAGES_OUTPUT_PATH}/${BUILD_ID}.log"