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