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
|
#!/bin/bash
IMAGE_BASENAME="${1}"
IMAGE_NAME="${IMAGE_BASENAME}.image"
IMAGE_OUT="${IMAGE_BASENAME}.vdi"
IMAGE_MEGABYTES="${2}"
BOOT_MEGABYTES=64
SWAP_MEGABYTES=128
ROOT_MEGABYTES=$(( ${IMAGE_MEGABYTES} - ( ${BOOT_MEGABYTES} + ${SWAP_MEGABYTES} + 1 ) ))
IMAGE_BYTES=$(( ${IMAGE_MEGABYTES} * 1024 * 1024 ))
TIMEZONE="Europe/London"
HOSTNAME="gentoo"
ROOT_PASSWORD="g3nt00"
DEFAULT_USERNAME="gentoo"
DEFAULT_PASSWORD="g3nt00"
LOG_FILE="`pwd`/log.txt"
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}
losetup -d ${LOOP_DEV_ROOT}
}
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 ${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"
echo "Mount root"
mkdir -p rootfs
mount -o loop,offset=$(( ( 512 * 2048 ) + ( ${BOOT_MEGABYTES} * 1024 * 1024 ) + ( ${SWAP_MEGABYTES} * 1024 * 1024 ) )) ${IMAGE_NAME} rootfs
echo "Mount boot"
mkdir -p rootfs/boot
mount -o loop,offset=$(( 512 * 2048 )) ${IMAGE_NAME} rootfs/boot
cd rootfs
# Setup Gentoo
echo "Downloading Stage 3"
#wget http://gentoo.virginmedia.com/releases/x86/current-stage3/stage3-i686-20110315.tar.bz2 -O stage3.tar.bz2 &>> ${LOG_FILE}
echo "Extracting Stage 3"
tar jxf ../stage3.tar.bz2 &>> ${LOG_FILE}
#rm stage3.tar.bz2
echo "Downloading Portage snapshot"
cd usr
#wget http://gentoo.virginmedia.com/snapshots/portage-latest.tar.bz2 &>> ${LOG_FILE}
echo "Extracting Portage snapshot"
tar jxf ../../portage-latest.tar.bz2 &>> ${LOG_FILE}
#rm portage-latest.tar.bz2
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 BINHOST"
echo 'PORTAGE_BINHOST="http://tinderbox.dev.gentoo.org/default-linux/x86"' >> etc/make.conf
echo 'PKGDIR="/usr/portage/packages"' >> etc/make.conf
echo 'FEATURES="parallel-fetch userfetch userpriv getbinpkg"' >> etc/make.conf
mkdir -p usr/portage/packages
#echo "Setting up make.conf"
#echo 'INPUT_DEVICES="virtualbox evdev"' >> etc/make.conf
#echo 'VIDEO_CARDS="virtualbox"' >> etc/make.conf
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}
linux32 chroot . bin/hostname ${HOSTNAME} &>> ${LOG_FILE}
echo "Copying new fstab"
cp ../fstab etc/fstab &>> ${LOG_FILE}
echo "Downloading/installing kernel sources"
linux32 chroot . emerge gentoo-sources &>> ${LOG_FILE}
echo "Copying kernel configuration"
cp ../kernelconfig usr/src/linux/.config
echo "Building kernel"
linux32 chroot . make -C /usr/src/linux -j8 &>> ${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 dhcpcd"
linux32 chroot . emerge dhcpcd &>> ${LOG_FILE}
#echo "Installing XML-Parser - BUG"
#linux32 chroot . emerge dev-perl/XML-Parser &>> ${LOG_FILE}
#echo "Installing GNOME"
#linux32 chroot . emerge gnome &>> ${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}
#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}
echo "Installing extlinux"
extlinux --heads 255 --sectors 63 --install boot
dd if=/usr/lib/extlinux/mbr.bin of=../${IMAGE_NAME} conv=notrunc
cp ../extlinux.conf boot/
cd ..
cleanup_mounts
#echo "Converting image from RAW to VDI"
#VBoxManage convertfromraw -format VDI ${IMAGE_NAME} ${IMAGE_OUT}
|