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