pass CVS tag through to internal myplc build
[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.1 2006/07/17 21:31:31 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:///usr/share/mirrors/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 TAG=planetlab-$(date +%Y-%m-%d)
39
40 # We may be running inside a myplc-devel environment, which can
41 # override these defaults. Specifically, whether to build myplc inside
42 # myplc-devel (PLC_DEVEL_BOOTSTRAP).
43 if [ -f /etc/planetlab/plc_config ] ; then
44     . /etc/planetlab/plc_config
45 fi
46
47 usage()
48 {
49     echo "Usage: $0 [OPTION]..."
50     echo "      -l url          Fedora mirror location (default: $PLC_DEVEL_FEDORA_URL)"
51     echo "      -r release      Fedora release number (default: $PLC_DEVEL_FEDORA_RELEASE)"
52     echo "      -a arch         Fedora architecture (default: $PLC_DEVEL_FEDORA_ARCH)"
53     echo "      -t tag          Source tag (default: $TAG)"
54     echo "      -h              This message"
55     exit 1
56 }
57
58 # Get options
59 while getopts "l:r:a:t:h" opt ; do
60     case $opt in
61         l)
62             PLC_DEVEL_FEDORA_URL=$OPTARG
63             ;;
64         r)
65             PLC_DEVEL_FEDORA_RELEASE=$OPTARG
66             ;;
67         a)
68             PLC_DEVEL_FEDORA_ARCH=$OPTARG
69             ;;
70         t)
71             TAG=$OPTARG
72             ;;
73         h|*)
74             usage
75             ;;
76     esac
77 done
78
79 # Do not tolerate errors
80 set -e
81
82 # Be verbose
83 set -x
84
85 # Make a basic chroot at the specified location given the specified
86 # configuration.
87 make_chroot() {
88     root=$1
89     config=$2
90
91     # Get group list
92     groups=
93     while read group ; do
94         groups="$groups -g \"$group\""
95     done < <(./plc-config --groups $config)
96
97     # Get package list
98     packages=
99     while read package ; do
100         packages="$packages -p \"$package\""
101     done < <(./plc-config --packages $config)
102
103     # Install base system
104     eval mkfedora -v -l $PLC_DEVEL_FEDORA_URL -r $PLC_DEVEL_FEDORA_RELEASE -a $PLC_DEVEL_FEDORA_ARCH $packages $groups $root
105
106     # Disable all services in reference image
107     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
108
109     # FC2 minilogd starts up during shutdown and makes unmounting
110     # impossible. Just get rid of it.
111     rm -f $root/sbin/minilogd
112     ln -nsf /bin/true $root/sbin/minilogd
113 }
114
115 # Move specified directories out of the chroot and into a "data"
116 # directory that will be bind mounted on /data inside the chroot.
117 move_datadirs() {
118     root=$1
119     data=$2
120     shift 2
121
122     mkdir -p $root/data
123     for datadir in "$@" ; do
124         mkdir -p ${data}$datadir
125         if [ -d $root/$datadir -a ! -h $root/$datadir ] ; then
126             (cd $root && find ./$datadir | cpio -p -d -u ../$data/)
127         fi
128         rm -rf $root/$datadir
129         mkdir -p $(dirname $root/$datadir)
130         ln -nsf /data$datadir $root/$datadir
131     done
132 }
133
134 # Make loopback filesystem from specified location
135 make_image() {
136     root=$1
137     image=$2
138
139     # Leave about 100 MB free space and allow for about 20% inode overhead
140     bytes=$((($(du -sb $root | cut -f1) + 100000000) * 120 / 100))
141     bs=4096
142     blocks=$(($bytes / $bs))
143     dd bs=$bs count=$blocks if=/dev/zero of=$image
144     mkfs.ext3 -b $bs -j -F $image
145
146     # Temporarily mount it
147     tmp=$(mktemp -d tmp.XXXXXX)
148     mount -o loop $image $tmp
149     trap "umount $tmp; rmdir $tmp" ERR INT
150
151     # Move files to it
152     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
153
154     # Unmount it
155     umount $tmp
156     rmdir $tmp
157     trap - ERR INT
158 }