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