fix pl_mkfedora: call mkfedora not ./mkfedora
[build.git] / build.common
1 # -*-Shell-script-*-
2 #
3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
5 #
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
8 #
9 # $Id: build.common,v 1.1.2.3 2007/08/30 17:43:08 mef Exp $
10 #
11
12 function pl_getDefaultFedoraRelease() {
13     # FC4 is currently the default release
14     return 4
15 }
16
17 function pl_getFedoraRelease() {
18     pl_getDefaultFedoraRelease
19     defaultFedoraRelease=$?
20     if [ -f "/etc/fedora-release" ] ; then
21         fedoraRelease=$(awk ' { if ($2=="Core") print $4; else print $3 } ' /etc/fedora-release)
22         [ $fedoraRelease -lt $defaultFedoraRelease ] && fedoraRelease=$defaultFedoraRelease
23     else
24         fedoraRelease=defaultFedoraRelease
25     fi
26     return $fedoraRelease
27 }
28
29 # select basearch of the host devel environment
30 pl_FEDORA_ARCH=$(uname -i)
31
32 # let mkfedora select one of its mirrors
33 pl_FEDORA_URL=""
34
35 pl_getDefaultFedoraRelease
36 pl_FEDORA_RELEASE=$?
37
38 # get patch to appropriate yumgroups.xml file
39 # XXX This path should be relative to PLDISTRO, as defined in
40 # build/Makefile
41 pl_YUMGROUPSXML="../build/groups/v4_yumgroups.xml"
42
43 function pl_process_fedora_options () {
44     # Get options
45     shiftcount=0
46     while getopts "l:r:a:h" opt ; do
47         case $opt in
48             l)
49                 pl_FEDORA_URL=$OPTARG
50                 let shiftcount=$shiftcount+2
51                 ;;
52             r)
53                 pl_FEDORA_RELEASE=$OPTARG
54                 let shiftcount=$shiftcount+2
55                 ;;
56             a)
57                 pl_FEDORA_ARCH=$OPTARG
58                 let shiftcount=$shiftcount+2
59                 ;;
60             h|*)
61                 echo "Usage: $0 [OPTION]..."
62                 echo "  -l url          Fedora mirror location (default: $pl_FEDORA_URL)"
63                 echo "  -r release      Fedora release number (default: $pl_FEDORA_RELEASE)"
64                 echo "  -a arch         Fedora architecture (default: $pl_FEDORA_ARCH)"
65                 echo "  -h              This message"
66                 exit 1
67                 ;;
68         esac
69     done
70     return $shiftcount
71 }
72
73 function pl_makedevs() {
74     vroot=$1
75     # Clean ${vroot}/dev, but only when ${vroot}!=""
76     [ -n $vroot ] && rm -rf $vroot/dev
77     
78     mkdir -p $vroot/dev
79     mknod -m 666 $vroot/dev/null c 1 3
80     mknod -m 666 $vroot/dev/zero c 1 5
81     mknod -m 666 $vroot/dev/full c 1 7
82     mknod -m 644 $vroot/dev/random c 1 8
83     mknod -m 644 $vroot/dev/urandom c 1 9
84     mknod -m 666 $vroot/dev/tty c 5 0
85     mknod -m 666 $vroot/dev/ptmx c 5 2
86     # For bash command substitution
87     ln -nsf ../proc/self/fd $vroot/dev/fd
88
89     # For df and linuxconf
90     touch $vroot/dev/hdv1
91
92     # For TUN/TAP
93     mkdir -p $vroot/dev/net
94     mknod -m 600 $vroot/dev/net/tun c 10 200
95
96     # For pseudo ttys
97     mkdir -p $vroot/dev/pts
98
99     # for tmpfs mount
100     mkdir -p $vroot/dev/shm
101
102     # For mkinitrd (in case a kernel is being installed)
103     for i in $(seq 0 7) ; do
104         mknod -m 640 $vroot/dev/loop$i b 7 $i
105     done
106 }
107
108 function pl_mkfedora() {
109     root=$1
110     shift
111     options=$@
112
113     pl_makedevs $root
114
115     [ -n "$pl_FEDORA_URL" ] && options="$options -l $pl_FEDORA_URL"
116     [ -n "$pl_FEDORA_ARCH" ] && options="$options -a $pl_FEDORA_ARCH"
117     [ -n "$pl_FEDORA_RELEASE" ] && options="$options -r $pl_FEDORA_RELEASE"
118     # echo "mkfedora -v $options $root"
119     eval mkfedora -v $options $root
120 }
121
122 function pl_setup_chroot() {
123     root=$1
124     shift
125     options=$@
126
127     pl_mkfedora $root $options
128
129     # Disable all services in reference image
130     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
131
132     # FC2 minilogd starts up during shutdown and makes unmounting
133     # impossible. Just get rid of it.
134     rm -f $root/sbin/minilogd
135     ln -nsf /bin/true $root/sbin/minilogd
136
137     # This tells the Boot Manager that it is okay to update
138     # /etc/resolv.conf and /etc/hosts whenever the network configuration
139     # changes. Users are free to delete this file.
140     touch $vroot/etc/AUTO_UPDATE_NET_FILES
141 }
142
143 # Move specified directories out of a src tree into a dst tree, and
144 # then for each moved directory create a symlink in src to dst.
145 function pl_move_dirs() {
146     root=$1
147     data=$2
148     store=$3
149     shift 3
150
151     mkdir -p $root/data
152     for datadir in "$@" ; do
153         mkdir -p ${data}${datadir}
154         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
155             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
156         fi
157         rm -rf ${root}/${datadir}
158         mkdir -p $(dirname ${root}/${datadir})
159         ln -nsf /${store}/${datadir} ${root}/${datadir}
160     done
161 }
162
163 # Construct an image file from given some directory
164 # XXX in the future maybe use livecdtools?
165 function pl_make_image() {
166     root=$1
167     image=$2
168     extraspace=$3
169
170     # Leave about 100 MB free space and allow for about 20% inode overhead
171     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
172     bs=4096
173     blocks=$(($bytes / $bs))
174     dd bs=$bs count=$blocks if=/dev/zero of=$image
175     mkfs.ext3 -b $bs -j -F $image
176
177     # Temporarily mount it
178     tmp=$(mktemp -d tmp.XXXXXX)
179     mount -o loop $image $tmp
180     trap "umount $tmp; rmdir $tmp" ERR INT
181
182     # Move files to it
183     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
184
185     # Unmount it
186     umount $tmp
187     rmdir $tmp
188     trap - ERR INT
189 }
190
191 # Fix permissions on tmp directories
192 function pl_fixtmp_permissions() {
193     root=$1
194     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
195 }
196
197 function pl_fixdirs() {
198     root=$1
199     datadirs=$2
200     for datadir in datadirs ; do
201         if [ -h ${root}/${datadir} ] ; then
202             rm -f ${root}/${datadir}
203             mkdir -p ${root}/${datadir}
204         fi
205     done
206 }
207