80e371ccfa7e9b4af52f79a30c68d5fd24a973dd
[catalyst.git] / targets / support / functions.sh
1 copy_to_chroot() {
2         local src_file=$1
3         local dest_dir=${clst_chroot_path}${2:-/tmp}
4         mkdir -p ${dest_dir}
5         echo "copying ${src_file##*/} to ${dest_dir}"
6         cp -pPR "${src_file}" "${dest_dir}"/
7 }
8
9 delete_from_chroot(){
10         if [ -e ${clst_chroot_path}${1} ]
11         then
12                 echo "removing ${clst_chroot_path}${1} from the chroot"
13                 rm -f ${clst_chroot_path}${1}
14         fi
15 }
16
17 exec_in_chroot(){
18 # Takes the full path to the source file as its argument
19 # copies the file to the /tmp directory of the chroot
20 # and executes it.
21         local file_name=$(basename ${1})
22         local subdir=${2}
23         local destdir=".${subdir}/tmp"
24
25         echo "Copying ${file_name} to ${destdir}"
26         copy_to_chroot ${1} ${destdir}
27         copy_to_chroot ${clst_sharedir}/targets/support/chroot-functions.sh \
28                 ${destdir}
29
30         chroot_path=${clst_chroot_path}${subdir}
31
32         echo "Ensure the file has the executable bit set"
33         chmod +x ${chroot_path}/${destdir}/${file_name}
34
35         echo "Running ${file_name} in chroot ${chroot_path}"
36         ${clst_CHROOT} ${chroot_path} ${destdir}/${file_name} || exit 1
37
38         delete_from_chroot ${destdir}/${file_name}
39         delete_from_chroot ${destdir}/chroot-functions.sh
40 }
41
42 #return codes
43 die() {
44         echo "$1"
45         exit 1
46 }
47
48 extract_cdtar() {
49         # Create a filesystem tree for the ISO at
50         # $clst_target_path. We extract the "cdtar" to this directory,
51         # which will normally contains a pre-built binary
52         # boot-loader/filesystem skeleton for the ISO.
53         cdtar=${clst_cdtar}
54         [ -z "${cdtar}" ] && die "Required key cdtar not defined, exiting"
55         tar -I lbzip2 -xpf ${cdtar} -C $1 || die "Couldn't extract cdtar ${cdtar}"
56 }
57
58 extract_kernels() {
59         # extract multiple kernels
60         # $1 = Destination
61         # ${clst_target_path}/kernel is often a good choice for ${1}
62
63         # Takes the relative desination dir for the kernel as an arguement
64         # i.e boot or isolinux
65         [ -z "$clst_boot_kernel" ] && \
66                 die "Required key boot/kernel not defined, exiting"
67         # install the kernels built in kmerge.sh
68         for x in ${clst_boot_kernel}
69         do
70                 first=${first:-""}
71                 kbinary="${clst_chroot_path}/tmp/kerncache/${x}-kernel-initrd-${clst_version_stamp}.tar.bz2"
72                 if [ -z "${first}" ]
73                 then
74                         # grab name of first kernel
75                         export first="${x}"
76                 fi
77
78                 [ ! -e "${kbinary}" ] && die "Can't find kernel tarball at ${kbinary}"
79                 mkdir -p ${1}/
80                 tar -I lbzip2 -xf ${kbinary} -C ${1}/
81
82                 # change config name from "config-*" to "gentoo", for example
83                 #mv ${1}/config-* ${1}/${x}-config
84                 rm ${1}/config-*
85
86                 # change kernel name from "kernel" to "gentoo", for example
87                 if [ -e ${1}/kernel-* ]
88                 then
89                         mv ${1}/kernel-* ${1}/${x}
90                 fi
91
92                 # change kernel name from "kernelz" to "gentoo", for example
93                 if [ -e ${1}/kernelz-* ]
94                 then
95                         mv ${1}/kernelz-* ${1}/${x}
96                 fi
97
98                 # change initrd name from "initrd" to "gentoo.igz", for example
99                 if [ -e ${1}/initrd-* ]
100                 then
101                         mv ${1}/initrd-* ${1}/${x}.igz
102                 fi
103
104                 if [ -e ${1}/initramfs-* ]
105                 then
106                         mv ${1}/initramfs-* ${1}/${x}.igz
107                 fi
108
109                 if [ -e ${1}/System.map-* ];
110                 then
111                         mv ${1}/System.map-* ${1}/System.map-${x}
112                 fi
113         done
114 }
115
116 extract_modules() {
117         # $1 = Destination
118         # $2 = kname
119         kmodules="${clst_chroot_path}/tmp/kerncache/${2}-modules-${clst_version_stamp}.tar.bz2"
120
121         if [ -f "${kmodules}" ]
122         then
123                 mkdir -p ${1}/
124                 tar -I lbzip2 -xf ${kmodules} --strip-components 1 -C ${1}/lib lib
125         else
126                 echo "Can't find kernel modules tarball at ${kmodules}.  Skipping...."
127         fi
128 }
129 extract_kernel() {
130         # $1 = Destination
131         # $2 = kname
132
133         kbinary="${clst_chroot_path}/tmp/kerncache/${2}-kernel-initrd-${clst_version_stamp}.tar.bz2"
134         [ ! -e "${kbinary}" ] && die "Can't find kernel tarball at ${kbinary}"
135         mkdir -p ${1}/
136         tar -I lbzip2 -xf ${kbinary} -C ${1}/
137         # change config name from "config-*" to "gentoo", for example
138         #mv ${1}/config-* ${1}/${2}-config
139         rm ${1}/config-*
140
141         # change kernel name from "kernel" to "gentoo", for example
142         mv ${1}/kernel-* ${1}/${2}
143
144         # change initrd name from "initrd" to "gentoo.igz", for example
145         if [ -e ${1}/initrd-* ]
146         then
147                 mv ${1}/initrd-* ${1}/${2}.igz
148         fi
149
150         # change initramfs name from "initramfs" to "gentoo.igz", for example
151         if [ -e ${1}/initramfs-* ]
152         then
153                 mv ${1}/initramfs-* ${1}/${2}.igz
154         fi
155 }
156
157 check_bootargs(){
158         # Add any additional options
159         if [ -n "${clst_livecd_bootargs}" ]
160         then
161                 for x in ${clst_livecd_bootargs}
162                 do
163                         cmdline_opts="${cmdline_opts} ${x}"
164                 done
165         fi
166 }
167
168 check_filesystem_type(){
169         case ${clst_fstype} in
170                 normal)
171                         cmdline_opts="${cmdline_opts} looptype=normal loop=/image.loop"
172                 ;;
173                 zisofs)
174                         cmdline_opts="${cmdline_opts} looptype=zisofs loop=/zisofs"
175                 ;;
176                 noloop)
177                 ;;
178                 squashfs)
179                         cmdline_opts="${cmdline_opts} looptype=squashfs loop=/image.squashfs"
180                 ;;
181                 jffs)
182                         cmdline_opts="${cmdline_opts} looptype=jffs loop=/image.jffs"
183                 ;;
184                 jffs2)
185                         cmdline_opts="${cmdline_opts} looptype=jffs2 loop=/image.jffs2"
186                 ;;
187                 cramfs)
188                         cmdline_opts="${cmdline_opts} looptype=cramfs loop=/image.cramfs"
189                 ;;
190         esac
191 }
192
193 run_crossdev() {
194         crossdev ${clst_CHOST}
195 }