Magellan Linux

Contents of /trunk/mlivecdbuild/mlivecdbuild2.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 360 - (show annotations) (download) (as text)
Mon Mar 27 21:23:02 2006 UTC (18 years, 1 month ago) by niro
File MIME type: application/x-sh
File size: 8499 byte(s)
several updates:
- better custom service and package support
- custom_prepare script support
- added usage and some build commands

1 #!/bin/bash
2
3 # some default settings - gets overidden with the profile config
4 CDPROFILE="$2"
5 [[ -z ${CDPROFILE} ]] && CDPROFILE="default"
6 LIVECDROOT="/mnt/MLIVECDBUILD/${CDPROFILE}"
7 CDCHROOTDIR="${LIVECDROOT}/chroot"
8 CDISONAME="livecd-${CDPROFILE}.iso"
9 CDISOROOT="${LIVECDROOT}/isoroot"
10 CDKERNELNAME="magelive"
11 CDHOSTNAME="livecd-${CDPROFILE}"
12 MAGERC="/etc/mage.rc"
13 MAGE_PROFILE="$(basename $(readlink /etc/mage-profile))"
14 #TOOLCHAIN=toolchain
15 BASESYSTEM=livecdsystem26
16 CDID="mlcd-$(date +%F-%k%M)"
17
18 # global profile pathes
19 PROFILES_DIR="/usr/lib/mlivecdbuild/profiles"
20 GLOBAL_PROFILE="${PROFILES_DIR}/global"
21
22 die() { echo "ERROR: $@"; exit 1; }
23
24 # get_profile $filename
25 get_profile()
26 {
27 local file="$1"
28
29 if [[ -z ${file} ]] || [[ ! -e ${PROFILES_DIR}/${CDPROFILE}/${file} ]]
30 then
31 echo "${GLOBAL_PROFILE}/${file}"
32 return 0
33 fi
34
35 echo "${PROFILES_DIR}/${CDPROFILE}/${file}"
36 }
37
38 bootstrap_system()
39 {
40 mage-bootstrap \
41 --root "${CDCHROOTDIR}" \
42 --magerc "${MAGERC}" \
43 --profile "${MAGE_PROFILE}" \
44 --toolchain "${TOOLCHAIN}" \
45 --basesystem "${BASESYSTEM}" \
46 || die "bootstrapping toolchain"
47 }
48
49 custom_services()
50 {
51 local cmd="$1"
52 local services="$2"
53
54 case ${cmd} in
55 add|del|default) continue;;
56 *) die "custom_services: unkown command ${cmd}";;
57 esac
58
59 # add given services from profile
60 if [[ -n ${services} ]]
61 then
62 local i
63 for i in ${services}
64 do
65 MROOT="${CDCHROOTDIR}" rc-config ${cmd} ${i} || die "rc ${cmd} ${i}"
66 done
67 fi
68 }
69
70 add_initrc()
71 {
72 local var="$1"
73
74 # sanity checks
75 [ -z "${CDCHROOTDIR}" ] && die "\$CDCHROOTDIR not given."
76 echo "${var}" >> ${CDCHROOTDIR}/.installrc || die "add_initrc() adding \$var"
77 }
78
79 custom_packages()
80 {
81 local cmd="$1"
82 local packages="$2"
83
84 case ${cmd} in
85 install|uninstall) continue;;
86 *) die "custom_packages: unkown command ${cmd}";;
87 esac
88
89 if [[ -n ${packages} ]]
90 then
91
92 :> ${CDCHROOTDIR}/.installrc
93 local i
94 for i in ${packages}
95 do
96 add_initrc "mage install ${i}"
97 done
98 add_initrc "mage clean"
99
100 # now run the .installrc script
101 mount -t proc proc ${CDCHROOTDIR}/proc || die "mount proc"
102 mount -t sysfs sysfs ${CDCHROOTDIR}/sys || die "mount sys"
103 mount -o bind /dev ${CDCHROOTDIR}/dev || die "mount dev"
104 chroot ${CDCHROOTDIR} /bin/bash -i /.installrc || die "chr00ting"
105 umount ${CDCHROOTDIR}/dev || die "umount dev"
106 umount ${CDCHROOTDIR}/proc || die "umount proc"
107 umount ${CDCHROOTDIR}/sys || die "umount sys"
108 [ -f ${CDCHROOTDIR}/.installrc ] && rm ${CDCHROOTDIR}/.installrc
109 fi
110 }
111
112 read_config()
113 {
114 local file="$1"
115 local pkg
116
117 ( cat ${file}; echo ) | while read pkg
118 do
119 case "${pkg}" in
120 \#*|"") continue ;;
121 esac
122 echo "${pkg}"
123 done
124 }
125
126 prepare_iso()
127 {
128 echo Preparing LiveCD ISO Image ...
129
130 # fixes some issues with xfree/xorg xkb
131 if [ -L "${CDCHROOTDIR}/etc/X11/xkb" -a -d "/usr/X11R6/lib/X11/xkb" ]
132 then
133 rm ${CDCHROOTDIR}/etc/X11/xkb || die
134 mv ${CDCHROOTDIR}/usr/X11R6/lib/X11/xkb ${CDCHROOTDIR}/etc/X11 || die
135 ln -s ../../../../etc/X11/xkb ${CDCHROOTDIR}/usr/X11R6/lib/X11/xkb || die
136 fi
137
138 install -m 0644 $(get_profile inittab) ${CDCHROOTDIR}/etc/inittab || die
139 install -m 0644 $(get_profile fstab) ${CDCHROOTDIR}/etc/fstab || die
140 install -m 0644 $(get_profile motd) ${CDCHROOTDIR}/etc/motd || die
141 install -m 0644 $(get_profile issue) ${CDCHROOTDIR}/etc/issue || die
142 echo "${CDHOSTNAME}" > ${CDCHROOTDIR}/etc/hostname || die
143
144 echo Setting up services ...
145
146 # not needed with initscripts 0.3.6-r2
147 # # change mountfs with mountfslivecd
148 # MROOT="${CDCHROOTDIR}" rc-config del mountfs || die "rc del mountfs"
149 # MROOT="${CDCHROOTDIR}" rc-config add mountfslivecd || die "rc add mountfslivecd"
150
151 # add hardware detection
152 MROOT="${CDCHROOTDIR}" rc-config add hwdetect || die "rc add hwdetect"
153
154 # del checkfs
155 MROOT="${CDCHROOTDIR}" rc-config del checkfs || die "rc del checkfs"
156
157 # add custom packages
158 [[ -n ${ADD_PACKAGES} ]] && custom_packages install "${ADD_PACKAGES}"
159 [ -f $(get_profile add_packages) ] && custom_packages install "$(read_config $(get_profile add_packages))"
160
161 # del custom packages
162 [[ -n ${DEL_PACKAGES} ]] && custom_packages uninstall "${DEL_PACKAGES}"
163 [ -f $(get_profile del_packages) ] && custom_packages uninstall "$(read_config $(get_profile del_packages))"
164
165 # add given services from profile
166 [[ -n ${ADD_SERVICES} ]] && custom_services add "${ADD_SERVICES}"
167 [ -f $(get_profile add_services) ] && custom_services add "$(read_config $(get_profile add_services))"
168
169 # del given services from profile
170 [[ -n ${DEL_SERVICES} ]] && custom_services del "${DEL_SERVICES}"
171 [ -f $(get_profile del_services) ] && custom_services del "$(read_config $(get_profile del_services))"
172
173 # setup default runlevel
174 [[ -n ${DEFAULT_RUNLEVEL} ]] && custom_services default "${DEFAULT_RUNLEVEL}"
175
176 if [ -f $(get_profile prepare_custom) ]
177 then
178 echo Running custom user script ...
179 source $(get_profile prepare_custom) || die "running custom user script"
180 fi
181
182 echo Cleaning unwanted files ...
183 :> ${CDCHROOTDIR}/etc/mtab || die "whiping /etc/mtab"
184 [ -f ${CDCHROOTDIR}/root/.bash_history ] && rm ${CDCHROOTDIR}/root/.bash_history
185 }
186
187 generate_rootfs()
188 {
189 echo Generating squashfs compressed rootfs loopfile ...
190 mksquashfs ${CDCHROOTDIR} ${LIVECDROOT}/livecdrootfs.sqsh #>/dev/null 2>&1
191
192 echo Moving rootfs loopfile to isoroot ...
193 install -d ${CDISOROOT}
194 mv ${LIVECDROOT}/livecdrootfs.sqsh ${CDISOROOT}
195 }
196
197 install_bootloader()
198 {
199 echo Installing Bootloader ...
200
201 # iso linux binary
202 install -d ${CDISOROOT}/isolinux
203 install /usr/lib/mkinitrd/isolinux.bin ${CDISOROOT}/isolinux || die
204
205 # kernel
206 local kimg="$(basename $(readlink ${CDCHROOTDIR}/boot/vmlinuz))"
207 install ${CDCHROOTDIR}/boot/${kimg} ${CDISOROOT}/isolinux/${CDKERNELNAME} || die
208
209 install -m 0644 $(get_profile isolinux.cfg) ${CDISOROOT}/isolinux || die
210 install -m 0644 $(get_profile boot.lss) ${CDISOROOT}/isolinux || die
211 install -m 0644 $(get_profile boot.msg) ${CDISOROOT}/isolinux || die
212 install -m 0644 $(get_profile help.msg) ${CDISOROOT}/isolinux || die
213
214 return 0
215 }
216
217 generate_initrd()
218 {
219 local CHROOTSH="$(mktemp -p ${CDCHROOTDIR})"
220
221 echo Generating initrd image ...
222 echo '#!/bin/bash' > ${CHROOTSH} || die
223 echo 'kv="$(readlink /boot/vmlinuz)"' >> ${CHROOTSH} || die
224 echo 'kv="${kv/kernel-}/"' >> ${CHROOTSH} || die
225 echo 'mkinitrd-livecd -f --initramfs /initrd.gz ${kv}' >> ${CHROOTSH} || die
226 chmod +x ${CHROOTSH} || die
227 chroot ${CDCHROOTDIR} /$(basename ${CHROOTSH}) || die
228 [[ -f ${CHROOTSH} ]] && rm ${CHROOTSH} || die
229
230 # move initrd to isoroot
231 install -d ${CDISOROOT}/isolinux || die
232 mv ${CDCHROOTDIR}/initrd.gz ${CDISOROOT}/isolinux || die
233 }
234
235 generate_iso()
236 {
237 echo Generating ISO Image ...
238 install -d ${CDISOROOT}
239 pushd ${CDISOROOT} &&
240 mkisofs -rock \
241 -full-iso9660-filenames \
242 -allow-leading-dots \
243 -disable-deep-relocation \
244 -output ${LIVECDROOT}/${CDISONAME} \
245 -eltorito-boot isolinux/isolinux.bin \
246 -eltorito-catalog isolinux/boot.cat \
247 -no-emul-boot \
248 -boot-load-size 4 \
249 -boot-info-table \
250 -volid "${CDID}" \
251 ${CDISOROOT} #&> /dev/null
252 popd
253 }
254
255 enter_livecd()
256 {
257 mount -t sysfs sysfs ${CDCHROOTDIR}/proc
258 mount -t proc proc ${CDCHROOTDIR}/proc
259 mount -o bind /dev ${CDCHROOTDIR}/dev
260
261 chroot ${CDCHROOTDIR} \
262 /usr/bin/env -i \
263 HOME=/root \
264 TERM=$TERM PS1='\u:\w\$ ' \
265 PATH=/bin:/usr/bin:/sbin:/usr/sbin \
266 /bin/bash -i
267 }
268
269 usage()
270 {
271 echo "Usage: $(basename $0) [command] [profile] ..."
272 echo
273 echo "Commands:"
274 echo " bootstrap - bootstraps the rootfs for the livecd"
275 echo " prepare - prepare the rootfs to run from a livecd"
276 echo " rootfs - generates the squashfs rootfs image"
277 echo " initrd - generates a livecd suitable initrd"
278 echo " bootloader - installs the bootloader"
279 echo " isogen - generate the final livecd iso image"
280 echo " all - runs all tasks to get a livecd from zero"
281 echo
282 echo " for example '$(basename $0) all default'"
283 echo " creates a livecd using the default profile."
284 echo
285 }
286
287 # source profile config - overrides all other vars
288 [ -f ${PROFILES_DIR}/${CDPROFILE}/config ] && . ${PROFILES_DIR}/${CDPROFILE}/config
289
290 #bootstrap_system
291 #prepare_iso
292 #generate_rootfs
293 #generate_initrd
294 #install_bootloader
295 #generate_iso
296
297 case $1 in
298 bootstrap) bootstrap_system;;
299 prepare) prepare_iso;;
300 rootfs) generate_rootfs;;
301 initrd) generate_initrd;;
302 bootloader) install_bootloader;;
303 isogen) generate_iso;;
304 all)
305 bootstrap_system
306 prepare_iso
307 generate_rootfs
308 generate_initrd
309 install_bootloader
310 generate_iso
311 ;;
312 *|'') usage;;
313 esac