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