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