- umount image lazily.
[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.3 2007/08/31 03:17:34 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_getFedoraRelease
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 pseudo ttys
93     mkdir -p $vroot/dev/pts
94
95     # (Might have to remove the following for vserver-reference.)
96
97     # for tmpfs mount
98     mkdir -p $vroot/dev/shm
99
100     # For TUN/TAP
101     mkdir -p $vroot/dev/net
102     mknod -m 600 $vroot/dev/net/tun c 10 200
103
104     # For mkinitrd (in case a kernel is being installed)
105     # As well for loop back mounting within a vserver. 
106     for i in $(seq 0 7) ; do
107         mknod -m 640 $vroot/dev/loop$i b 7 $i
108     done
109 }
110
111 function pl_mkfedora() {
112     root=$1
113     shift
114     options=$@
115
116     pl_makedevs $root
117
118     [ -n "$pl_FEDORA_URL" ] && options="$options -l $pl_FEDORA_URL"
119     [ -n "$pl_FEDORA_ARCH" ] && options="$options -a $pl_FEDORA_ARCH"
120     [ -n "$pl_FEDORA_RELEASE" ] && options="$options -r $pl_FEDORA_RELEASE"
121     # echo "mkfedora -v $options $root"
122     eval mkfedora -v $options $root
123 }
124
125 function pl_setup_chroot() {
126     root=$1
127     shift
128     options=$@
129
130     pl_mkfedora $root $options
131
132     # Disable all services in reference image
133     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
134
135     # FC2 minilogd starts up during shutdown and makes unmounting
136     # impossible. Just get rid of it.
137     rm -f $root/sbin/minilogd
138     ln -nsf /bin/true $root/sbin/minilogd
139
140     # This tells the Boot Manager that it is okay to update
141     # /etc/resolv.conf and /etc/hosts whenever the network configuration
142     # changes. Users are free to delete this file.
143     touch $vroot/etc/AUTO_UPDATE_NET_FILES
144 }
145
146 # Move specified directories out of a src tree into a dst tree, and
147 # then for each moved directory create a symlink in src to dst.
148 function pl_move_dirs() {
149     root=$1
150     data=$2
151     store=$3
152     shift 3
153
154     mkdir -p $root/data
155     for datadir in "$@" ; do
156         mkdir -p ${data}${datadir}
157         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
158             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
159         fi
160         rm -rf ${root}/${datadir}
161         mkdir -p $(dirname ${root}/${datadir})
162         ln -nsf /${store}/${datadir} ${root}/${datadir}
163     done
164 }
165
166 # Construct an image file from given some directory
167 # XXX in the future maybe use livecdtools?
168 function pl_make_image() {
169     root=$1
170     image=$2
171     extraspace=$3
172
173     # Leave about 100 MB free space and allow for about 20% inode overhead
174     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
175     bs=4096
176     blocks=$(($bytes / $bs))
177     dd bs=$bs count=$blocks if=/dev/zero of=$image
178     mkfs.ext3 -b $bs -j -F $image
179
180     # Temporarily mount it
181     tmp=$(mktemp -d tmp.XXXXXX)
182     mount -o loop $image $tmp
183     trap "umount $tmp; rmdir $tmp" ERR INT
184
185     # Move files to it
186     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
187
188     # Unmount it
189     umount -l $tmp
190     rmdir $tmp
191     trap - ERR INT
192 }
193
194 # Fix permissions on tmp directories
195 function pl_fixtmp_permissions() {
196     root=$1
197     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
198 }
199
200 function pl_fixdirs() {
201     root=$1
202     datadirs=$2
203     for datadir in datadirs ; do
204         if [ -h ${root}/${datadir} ] ; then
205             rm -f ${root}/${datadir}
206             mkdir -p ${root}/${datadir}
207         fi
208     done
209 }
210