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