root/ror-farm/ror-farm-image

Revision 37, 7.1 kB (checked in by zerodeux, 4 months ago)

new ror-farm-image emulator image masterizer

  • Property svn:executable set to *
Line 
1#!/bin/sh
2
3# ror-farm-image (c) 2008 Bearstech - http://bearstech.com
4#
5# ?Depends: util-vserver vserver-debiantools lvm2 passwd pwgen
6# ?Recommends: mysql-client
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21THIS_NAME=ror-farm-image
22THIS_VERSION=1
23
24# Distro-specific paths
25#
26#VSERVER_CONF=/etc/vservers
27VSERVER_HOME=/var/lib/vservers
28RORFARM_LIB="$PWD"/`dirname $0`
29
30# Default options
31#
32template=template
33
34
35help() {
36  cat << EOF
37Usage: $THIS_NAME [options] image-file
38
39Creates a new RoR-ready vserver instance.
40
41Options:
42  --template     vserver filesystem template (default: '$template')
43  --help, -h     this help
44  --version, -v  this script version
45EOF
46}
47
48version() {
49  echo "$THIS_NAME $THIS_VERSION"
50}
51
52error() {
53  echo "$THIS_NAME: error: $*"
54  exit 1
55}
56
57
58# Parse args
59#
60parse_opt=run
61while [ $parse_opt != "done"  ] ; do
62  case "$1" in
63
64    --template)    shift; template="$1"; shift;;
65
66    --help|-h)     shift; help; exit 0;;
67    --version|-v)  shift; version; exit 0;;
68
69    --)            shift; parse_opt=done;;
70    -*)            echo "$THIS_NAME: unknown option '$1'"; exit 1;;
71    *)             parse_opt=done;;
72  esac
73done
74image="$1"; shift
75
76
77# Sanity checks
78#
79if [ -z "$image" ]; then
80  error "missing image file name"
81fi
82if [ -e "$image" ]; then
83  error "image file '$image' exists"
84fi
85
86if [ -z "$template" ]; then
87  error "missing --template"
88fi
89if [ x`echo "$template" | sed -ne '/\//p'` != x ]; then
90  error "unsane template name ('$template')"
91fi
92if [ ! -d "$VSERVER_HOME/$template" ]; then
93  error "filesystem template '$template' not found (in $VSERVER_HOME)"
94fi
95
96
97cleanup() {
98  if [ ! -z "$image_mnt" ]; then
99    umount "$image_mnt"
100  fi
101  if [ ! -z "$image_loop" ]; then
102    losetup -d $image_loop
103  fi
104}
105
106# Safety net (and be nice an don't left loop-mounts around)
107#
108bug() {
109  echo "** $0: error, exiting" >&2
110  cleanup
111  exit 1
112}
113set -e
114trap bug ERR
115
116
117# Unpack our disk image template as the target image file
118# (and redirect stdout to <image>.log, hence we only see stderr on console)
119#
120image_path=`dirname $image`
121image_name=`basename $image`
122cd "$image_path"
123exec >"$image_name".log 2>&1
124tar xzf $RORFARM_LIB/disk-1gb.img.tar
125mv disk-1gb.img "$image_name"
126
127# Create (sparse) disk image with a single, filling, partition
128#
129#image_size=$((1024*1024*1024)) # in bytes
130#dd of="$image_name" bs=$image_size count=0 seek=1
131#echo -e "63,,83,*" | sfdisk -C32 -H255 -S63 -uS -f --no-reread "$image_name"
132#dd of="$image_name" if=/dev/zero bs=512 count=1
133#
134# Format the system partition, then mount it
135#
136#image_loop=/dev/loop7
137#losetup -o $((63*512)) $image_loop "$image_name"
138#mke2fs -j -L bearstech-ror -m1 $image_loop
139
140image_mnt=/mnt/"$image_name".mnt
141mkdir -p "$image_mnt"
142mount -o loop,offset=$((63*512)) "$image_name" "$image_mnt"
143
144
145# Copy the template
146#
147cp -a $VSERVER_HOME/"$template"/* "$image_mnt"/
148cd "$image_mnt"
149
150
151# Set empty passwords for 'root' and 'ror' accounts
152#
153sed -i \
154    -e 's/^root:[^:]\+/root:/' \
155    -e 's/^ror:[^:]\+/ror:/' \
156    etc/shadow
157
158# Setup proper fstab (depends on the reference virtual image layout)
159#
160cat <<EOF           > etc/fstab
161# /etc/fstab: static file system information.
162#
163# <file system> <mount point>   <type>  <options>       <dump>  <pass>
164proc            /proc           proc    defaults        0       0
165/dev/hda1       /               ext3    defaults,noatime,errors=remount-ro 0       1
166/dev/hda2       none            swap    sw              0       0
167/dev/hdc        /media/cdrom0   udf,iso9660 user,noauto     0       0
168EOF
169# Create swap as sparse file: bad practice, but faster and lighter image results
170#dd of=tmp/swap bs=$((32*1024*1024)) count=0 seek=1
171
172
173# Setup a working network
174#
175cat  <<EOF           > etc/network/interfaces
176# The loopback network interface
177auto lo
178iface lo inet loopback
179
180# The primary network interface
181auto eth0
182iface eth0 inet dhcp
183EOF
184cat  <<EOF           > etc/hosts
185127.0.0.1       localhost.localdomain localhost
186EOF
187echo ror-vm          > etc/hostname
188echo ror-vm.my.lan   > etc/mailname
189chroot "$image_mnt" update-rc.d -f networking remove
190chroot "$image_mnt" update-rc.d networking start 40 S . start 35 0 6 .
191
192# Be nice, say hello and give basic information about this environment
193#
194cat <<EOF            >>etc/issue
195
196--
197
198Welcome to 'ror-vm', a ready-to-play RoR virtual machine.
199
200  * The 'root' and 'ror' accounts have an empty password
201
202  * Reach your application via http://localhost:8080/
203   (use: qemu -nographic -redir tcp:8080::8080 <image>)
204
205This image has been mastered from the same template used on the
206hosting facility provided by the Bearstech RoR farm. See
207http://my.ror.bearstech.com/ for more information.
208
209Have fun ! The Bearstech RoR team.
210
211--
212
213
214EOF
215
216
217# Make the image bootable (kernel + GRUB)
218# Add packages for services which are mutualized (mysqld)
219#
220PACKAGES="linux-image-2.6-amd64 grub mysql-server-5.0 mysql-client-5.0"
221echo do_initrd = Yes  > etc/kernel-img.conf
222export DEBIAN_FRONTEND=noninteractive
223chroot "$image_mnt" apt-get update
224chroot "$image_mnt" apt-get --yes --force-yes install $PACKAGES
225chroot "$image_mnt" apt-get clean
226
227# Right now we prefer to run in headless mode (eg. using 'qemu -nographic'),
228# we setup a serial console for GRUB, Linux kernel, then getty.
229sed -i \
230    -e 's/^\(## default num.*\)/serial --unit=0 --speed=9600\nterminal serial\n\n\1/' \
231    -e 's/^\(# kopt=.*\)$/\1 console=ttyS0,9600n8/' \
232    boot/grub/menu.lst
233cat <<EOF >>etc/inittab
234T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
235EOF
236chroot "$image_mnt" update-grub -y
237
238
239# Setup MySQL accounts and databases
240#
241db_name="myapp"
242db_pass=`pwgen 8 1`
243cat <<EOF | chroot "$image_mnt" mysql
244CREATE DATABASE \`${db_name}_dev\` CHARSET utf8;
245CREATE DATABASE \`${db_name}_prod\` CHARSET utf8;
246GRANT ALL PRIVILEGES ON \`${db_name}\_dev\`.* TO '$db_name'@'%';
247GRANT ALL PRIVILEGES ON \`${db_name}\_prod\`.* TO '$db_name'@'%';
248SET PASSWORD FOR '$db_name'@'%'=PASSWORD('$db_pass');
249FLUSH PRIVILEGES;
250EOF
251cat <<EOF > .my.cnf
252[client]
253user = $db_name
254pass = $db_pass
255EOF
256# Important: stop mysqld in order we can undo our loop-mount
257chroot "$image_mnt" /etc/init.d/mysql stop
258
259
260# Cleanup /var (we actually only keep /var skeleton and /var/lib contents)
261#
262find var/backups var/cache var/lock var/log var/mail var/run var/spool var/tmp -type f -exec rm {} \;
263# Some script insist that this file exists, let's make it happy:
264touch var/log/dmesg
265
266# Cleanup user account
267#
268cd home/ror
269rm -rf .aptitude
270rm -f .bash_history .lesshst .viminfo
271rm -f .ssh/known_hosts .ssh/id_*
272rm -f gem/source_cache
273rm -f gem/cache/*
274rm -rf rubygems-*
275rm -f http/tmp/*
276rm -f http/log/*
277
278
279# All done: umount image, compress, profit !
280#
281cd "$image_path"
282cleanup
283tar cSzf "$image_name".tar.gz "$image_name"
Note: See TracBrowser for help on using the browser.