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