- fix version number, bump release number, added changelog
[myplc.git] / build.functions
1 # -*-Shell-script-*-
2 #
3 # Common functions for MyPLC build scripts (build_devel.sh and
4 # build.sh)
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
8 #
9 # $Id: build.functions,v 1.4 2006/08/16 01:27:16 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14 # In both a normal CVS environment and a PlanetLab RPM
15 # build environment, all of our dependencies are checked out into
16 # directories at the same level as us.
17 if [ -d ../build ] ; then
18     PATH=$PATH:../build
19     srcdir=..
20 else
21     echo "Error: Could not find $(cd .. && pwd -P)/build/"
22     exit 1
23 fi
24
25 export PATH
26
27 # Release and architecture to install
28 PLC_DEVEL_FEDORA_RELEASE=4
29 PLC_DEVEL_FEDORA_ARCH=i386
30
31 # Fedora Core mirror from which to install filesystems
32 PLC_DEVEL_FEDORA_URL=file:///data/fedora
33
34 # Build myplc inside myplc-devel
35 PLC_DEVEL_BOOTSTRAP=true
36
37 # Source tag to use for initial import of sources into local CVS
38 IMPORT_TAG=planetlab-$(date +%Y-%m-%d)
39
40 # Source tag to use for building from local CVS
41 BUILD_TAG=HEAD
42
43 # We may be running inside a myplc-devel environment, which can
44 # override these defaults. Specifically, whether to build myplc inside
45 # myplc-devel (PLC_DEVEL_BOOTSTRAP).
46 if [ -f /etc/planetlab/plc_config ] ; then
47     . /etc/planetlab/plc_config
48 fi
49
50 usage()
51 {
52     echo "Usage: $0 [OPTION]..."
53     echo "      -l url          Fedora mirror location (default: $PLC_DEVEL_FEDORA_URL)"
54     echo "      -r release      Fedora release number (default: $PLC_DEVEL_FEDORA_RELEASE)"
55     echo "      -a arch         Fedora architecture (default: $PLC_DEVEL_FEDORA_ARCH)"
56     echo "      -t tag          Source tag (default: $TAG)"
57     echo "      -h              This message"
58     exit 1
59 }
60
61 # Get options
62 while getopts "l:r:a:t:h" opt ; do
63     case $opt in
64         l)
65             PLC_DEVEL_FEDORA_URL=$OPTARG
66             ;;
67         r)
68             PLC_DEVEL_FEDORA_RELEASE=$OPTARG
69             ;;
70         a)
71             PLC_DEVEL_FEDORA_ARCH=$OPTARG
72             ;;
73         t)
74             IMPORT_TAG=$OPTARG
75             BUILD_TAG=$OPTARG
76             ;;
77         h|*)
78             usage
79             ;;
80     esac
81 done
82
83 # Do not tolerate errors
84 set -e
85
86 # Be verbose
87 set -x
88
89 # Make a basic chroot at the specified location given the specified
90 # configuration.
91 make_chroot() {
92     root=$1
93     config=$2
94
95     # Get group list
96     groups=
97     while read group ; do
98         groups="$groups -g \"$group\""
99     done < <(./plc-config --groups $config)
100
101     # Get package list
102     packages=
103     while read package ; do
104         packages="$packages -p \"$package\""
105     done < <(./plc-config --packages $config)
106
107     # Install base system
108     eval mkfedora -v -l $PLC_DEVEL_FEDORA_URL -r $PLC_DEVEL_FEDORA_RELEASE -a $PLC_DEVEL_FEDORA_ARCH $packages $groups $root
109
110     # Disable all services in reference image
111     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
112
113     # FC2 minilogd starts up during shutdown and makes unmounting
114     # impossible. Just get rid of it.
115     rm -f $root/sbin/minilogd
116     ln -nsf /bin/true $root/sbin/minilogd
117 }
118
119 # Move specified directories out of the chroot and into a "data"
120 # directory that will be bind mounted on /data inside the chroot.
121 move_datadirs() {
122     root=$1
123     data=$2
124     shift 2
125
126     mkdir -p $root/data
127     for datadir in "$@" ; do
128         mkdir -p ${data}$datadir
129         if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
130             (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
131         fi
132         rm -rf $root/$datadir
133         mkdir -p $(dirname $root/$datadir)
134         ln -nsf /data$datadir $root/$datadir
135     done
136 }
137
138 # Make loopback filesystem from specified location
139 make_image() {
140     root=$1
141     image=$2
142
143     # Leave about 100 MB free space and allow for about 20% inode overhead
144     bytes=$((($(du -sb $root | cut -f1) + 100000000) * 120 / 100))
145     bs=4096
146     blocks=$(($bytes / $bs))
147     dd bs=$bs count=$blocks if=/dev/zero of=$image
148     mkfs.ext3 -b $bs -j -F $image
149
150     # Temporarily mount it
151     tmp=$(mktemp -d tmp.XXXXXX)
152     mount -o loop $image $tmp
153     trap "umount $tmp; rmdir $tmp" ERR INT
154
155     # Move files to it
156     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
157
158     # Unmount it
159     umount $tmp
160     rmdir $tmp
161     trap - ERR INT
162 }