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