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