Magellan Linux

Annotation of /trunk/mlivecdbuild/mlivecdbuild2.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1422 - (hide annotations) (download) (as text)
Sun Jul 3 21:34:53 2011 UTC (13 years, 4 months ago) by niro
File MIME type: application/x-sh
File size: 13301 byte(s)
-support standalone syslinux, mkinitrd gets superseeded by dracut
1 niro 346 #!/bin/bash
2    
3     # some default settings - gets overidden with the profile config
4 niro 360 CDPROFILE="$2"
5 niro 346 [[ -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 niro 1422 # get full path to isolinux.bin, may vary on multilib systems
23     if [[ -f /usr/share/syslinux/isolinux.bin ]]
24 niro 1402 then
25 niro 1422 ISOLINUX_BIN=/usr/share/syslinux/isolinux.bin
26     elif [[ -f /usr/lib64/mkinitrd/isolinux.bin ]]
27     then
28 niro 1402 ISOLINUX_BIN=/usr/lib64/mkinitrd/isolinux.bin
29     else
30     ISOLINUX_BIN=/usr/lib/mkinitrd/isolinux.bin
31     fi
32    
33 niro 346 die() { echo "ERROR: $@"; exit 1; }
34    
35     # get_profile $filename
36     get_profile()
37     {
38     local file="$1"
39    
40     if [[ -z ${file} ]] || [[ ! -e ${PROFILES_DIR}/${CDPROFILE}/${file} ]]
41     then
42     echo "${GLOBAL_PROFILE}/${file}"
43     return 0
44     fi
45    
46     echo "${PROFILES_DIR}/${CDPROFILE}/${file}"
47     }
48    
49     bootstrap_system()
50     {
51     mage-bootstrap \
52     --root "${CDCHROOTDIR}" \
53     --magerc "${MAGERC}" \
54     --profile "${MAGE_PROFILE}" \
55     --toolchain "${TOOLCHAIN}" \
56     --basesystem "${BASESYSTEM}" \
57     || die "bootstrapping toolchain"
58     }
59    
60 niro 360 custom_services()
61     {
62     local cmd="$1"
63     local services="$2"
64 niro 1403 local systemdcmd
65 niro 360
66     case ${cmd} in
67 niro 471 add|del|default)
68     # add given services from profile
69     if [[ -n ${services} ]]
70     then
71     local i
72     for i in ${services}
73     do
74 niro 1403 # systemd
75     if [[ -x ${CDCHROOTDIR}/sbin/systemctl ]]
76     then
77     case ${cmd} in
78     add) chroot ${CDCHROOTDIR} systemctl enable ${i} ;;
79     del) chroot ${CDCHROOTDIR} systemctl disable ${i} ;;
80     esac
81     fi
82     # busybox and sysvinit
83     if [[ -x ${CDCHROOTDIR}/sbin/rc-config ]]
84     then
85     MROOT="${CDCHROOTDIR}" rc-config ${cmd} ${i} || die "rc ${cmd} ${i}"
86     fi
87 niro 471 done
88     fi
89     ;;
90 niro 360 *) die "custom_services: unkown command ${cmd}";;
91     esac
92     }
93    
94     add_initrc()
95     {
96     local var="$1"
97    
98     # sanity checks
99 niro 473 [[ -z ${CDCHROOTDIR} ]] && die "\$CDCHROOTDIR not given."
100 niro 360 echo "${var}" >> ${CDCHROOTDIR}/.installrc || die "add_initrc() adding \$var"
101     }
102    
103     custom_packages()
104     {
105     local cmd="$1"
106     local packages="$2"
107    
108     case ${cmd} in
109 niro 471 install|uninstall)
110     if [[ -n ${packages} ]]
111     then
112     :> ${CDCHROOTDIR}/.installrc
113 niro 472
114     # respect proxies
115     [[ -n ${http_proxy} ]] && add_initrc "export http_proxy=${http_proxy}"
116     [[ -n ${ftp_proxy} ]] && add_initrc "export ftp_proxy=${ftp_proxy}"
117     [[ -n ${no_proxy} ]] && add_initrc "export no_proxy=${no_proxy}"
118    
119 niro 580 # do not auto-start any services!
120     add_initrc "export MAGE_BOOTSTRAP=true"
121 niro 863 add_initrc 'die() { if [ $? -eq 1 ]; then echo "ERROR: $@"; exit 1; fi; }'
122 niro 580
123 niro 471 local i
124     for i in ${packages}
125     do
126 niro 863 add_initrc "mage ${cmd} ${i} || die ${i}"
127 niro 471 done
128     add_initrc "mage clean"
129    
130     # now run the .installrc script
131     mount -t proc proc ${CDCHROOTDIR}/proc || die "mount proc"
132     mount -t sysfs sysfs ${CDCHROOTDIR}/sys || die "mount sys"
133     mount -o bind /dev ${CDCHROOTDIR}/dev || die "mount dev"
134     chroot ${CDCHROOTDIR} /bin/bash -i /.installrc || die "chr00ting"
135     umount ${CDCHROOTDIR}/dev || die "umount dev"
136     umount ${CDCHROOTDIR}/proc || die "umount proc"
137     umount ${CDCHROOTDIR}/sys || die "umount sys"
138     [ -f ${CDCHROOTDIR}/.installrc ] && rm ${CDCHROOTDIR}/.installrc
139     fi
140     ;;
141 niro 360 *) die "custom_packages: unkown command ${cmd}";;
142     esac
143     }
144    
145     read_config()
146     {
147     local file="$1"
148     local pkg
149    
150     ( cat ${file}; echo ) | while read pkg
151     do
152     case "${pkg}" in
153     \#*|"") continue ;;
154     esac
155     echo "${pkg}"
156     done
157     }
158    
159 niro 346 prepare_iso()
160     {
161     echo Preparing LiveCD ISO Image ...
162    
163     # fixes some issues with xfree/xorg xkb
164 niro 473 if [[ -L ${CDCHROOTDIR}/etc/X11/xkb ]] && [[ -d /usr/X11R6/lib/X11/xkb ]]
165 niro 346 then
166     rm ${CDCHROOTDIR}/etc/X11/xkb || die
167     mv ${CDCHROOTDIR}/usr/X11R6/lib/X11/xkb ${CDCHROOTDIR}/etc/X11 || die
168     ln -s ../../../../etc/X11/xkb ${CDCHROOTDIR}/usr/X11R6/lib/X11/xkb || die
169     fi
170    
171 niro 1403 # only on sysvinit or busybox systems
172     if [[ -x ${CDCHROOTDIR}/sbin/rc-config ]]
173     then
174     install -m 0644 $(get_profile inittab) ${CDCHROOTDIR}/etc/inittab || die
175     fi
176 niro 346 install -m 0644 $(get_profile fstab) ${CDCHROOTDIR}/etc/fstab || die
177     install -m 0644 $(get_profile motd) ${CDCHROOTDIR}/etc/motd || die
178     install -m 0644 $(get_profile issue) ${CDCHROOTDIR}/etc/issue || die
179 niro 712 install -m 0644 $(get_profile net.eth0) ${CDCHROOTDIR}/etc/conf.d/net.eth0 || die
180 niro 346 echo "${CDHOSTNAME}" > ${CDCHROOTDIR}/etc/hostname || die
181    
182     echo Setting up services ...
183    
184 niro 978 # # add hardware detection
185 niro 1403 # if [[ -x ${CDCHROOTDIR}/sbin/rc-config ]]
186     # then
187     # MROOT="${CDCHROOTDIR}" rc-config add hwdetect || die "rc add hwdetect"
188     # fi
189 niro 346
190     # del checkfs
191 niro 1403 if [[ -x ${CDCHROOTDIR}/sbin/rc-config ]]
192     then
193     MROOT="${CDCHROOTDIR}" rc-config del checkfs || die "rc del checkfs"
194     fi
195 niro 346
196 niro 360 # add custom packages
197     [[ -n ${ADD_PACKAGES} ]] && custom_packages install "${ADD_PACKAGES}"
198     [ -f $(get_profile add_packages) ] && custom_packages install "$(read_config $(get_profile add_packages))"
199    
200     # del custom packages
201     [[ -n ${DEL_PACKAGES} ]] && custom_packages uninstall "${DEL_PACKAGES}"
202     [ -f $(get_profile del_packages) ] && custom_packages uninstall "$(read_config $(get_profile del_packages))"
203    
204     # add given services from profile
205     [[ -n ${ADD_SERVICES} ]] && custom_services add "${ADD_SERVICES}"
206     [ -f $(get_profile add_services) ] && custom_services add "$(read_config $(get_profile add_services))"
207    
208     # del given services from profile
209     [[ -n ${DEL_SERVICES} ]] && custom_services del "${DEL_SERVICES}"
210     [ -f $(get_profile del_services) ] && custom_services del "$(read_config $(get_profile del_services))"
211    
212     # setup default runlevel
213     [[ -n ${DEFAULT_RUNLEVEL} ]] && custom_services default "${DEFAULT_RUNLEVEL}"
214    
215     if [ -f $(get_profile prepare_custom) ]
216     then
217     echo Running custom user script ...
218     source $(get_profile prepare_custom) || die "running custom user script"
219     fi
220    
221 niro 346 echo Cleaning unwanted files ...
222 niro 1403 # do this only if not a symlink!
223     if [[ ! -L ${CDCHROOTDIR}/etc/mtab ]]
224     then
225     :> ${CDCHROOTDIR}/etc/mtab || die "whiping /etc/mtab"
226     fi
227 niro 360 [ -f ${CDCHROOTDIR}/root/.bash_history ] && rm ${CDCHROOTDIR}/root/.bash_history
228 niro 346 }
229    
230     generate_rootfs()
231     {
232 niro 1403 local loopdev
233     local size
234    
235     if [ -f ${LIVECDROOT}/loop/LiveOS/ext3fs.img ]
236     then
237     rm ${LIVECDROOT}/loop/LiveOS/ext3fs.img || die
238     fi
239     if [ -f ${CDISOROOT}/LiveOS/squashfs.img ]
240     then
241     rm ${CDISOROOT}/LiveOS/squashfs.img
242     fi
243     if [ -f ${CDISOROOT}/livecdrootfs.sqsh ]
244     then
245     rm ${CDISOROOT}/livecdrootfs.sqsh
246     fi
247    
248 niro 346 echo Generating squashfs compressed rootfs loopfile ...
249 niro 1403 if [[ -x ${CDCHROOTDIR}/sbin/dracut ]]
250     then
251     install -d ${LIVECDROOT}/loop/{LiveOS,mnt} || die
252 niro 346
253 niro 1403 # get the actual size of the chroot
254     size=$(du -s chroot | sed 's:^\(.*\)[[:space:]].*:\1:')
255    
256     # generate a ext3fs file for devicemapper
257     dd if=/dev/zero of=${LIVECDROOT}/loop/LiveOS/ext3fs.img bs=1024 count=$(( ${size} + 10 )) || die
258     # create a filesystem
259     mkfs.ext3 -L "_${CDID}_EXT3" -m 1 -b 1024 -F ${LIVECDROOT}/loop/LiveOS/ext3fs.img || die
260     # set mount_counts and check_intervals to 0
261     # set dir_index top, to speed up thing with hashed b-trees
262     # allow acls too
263     tune2fs -c0 -i0 -Odir_index -ouser_xattr,acl ${LIVECDROOT}/loop/LiveOS/ext3fs.img || die
264    
265     # losetup the device
266     loopdev=$(losetup -f)
267     [[ -z ${loopdev} ]] && die "No unused loopdev found. Maybe you want 'modprobe loop'?"
268    
269     # mount the image
270     losetup ${loopdev} ${LIVECDROOT}/loop/LiveOS/ext3fs.img || die
271     mount ${loopdev} ${LIVECDROOT}/loop/mnt || die
272    
273     # copy everything to the image file and preserve permissions
274     ( cd ${CDCHROOTDIR} && tar cpf - . ) | ( cd ${LIVECDROOT}/loop/mnt && tar xvpf - )
275    
276     # now umount everything and create the squashfs image
277     umount ${LIVECDROOT}/loop/mnt || die
278     losetup -d ${loopdev} || die
279     # remove mount to not ending up in the squashfs image
280     if [[ -d ${LIVECDROOT}/loop/mnt ]]
281     then
282     rm -r ${LIVECDROOT}/loop/mnt || die
283     fi
284     mksquashfs ${LIVECDROOT}/loop ${LIVECDROOT}/livecdrootfs.sqsh || die
285    
286     # final cleanup
287     if [[ -d ${LIVECDROOT}/loop ]]
288     then
289     rm -r ${LIVECDROOT}/loop || die
290     fi
291     else
292     mksquashfs ${CDCHROOTDIR} ${LIVECDROOT}/livecdrootfs.sqsh || die
293     fi
294    
295 niro 346 echo Moving rootfs loopfile to isoroot ...
296     install -d ${CDISOROOT}
297 niro 1403 if [[ -x ${CDCHROOTDIR}/sbin/dracut ]]
298     then
299     install -d ${CDISOROOT}/LiveOS
300     mv ${LIVECDROOT}/livecdrootfs.sqsh ${CDISOROOT}/LiveOS/squashfs.img
301     else
302     mv ${LIVECDROOT}/livecdrootfs.sqsh ${CDISOROOT}
303     fi
304 niro 346 }
305    
306     install_bootloader()
307     {
308     echo Installing Bootloader ...
309    
310     # iso linux binary
311     install -d ${CDISOROOT}/isolinux
312 niro 1402 install ${ISOLINUX_BIN} ${CDISOROOT}/isolinux || die
313 niro 346
314     # kernel
315     local kimg="$(basename $(readlink ${CDCHROOTDIR}/boot/vmlinuz))"
316     install ${CDCHROOTDIR}/boot/${kimg} ${CDISOROOT}/isolinux/${CDKERNELNAME} || die
317    
318     install -m 0644 $(get_profile isolinux.cfg) ${CDISOROOT}/isolinux || die
319     install -m 0644 $(get_profile boot.lss) ${CDISOROOT}/isolinux || die
320     install -m 0644 $(get_profile boot.msg) ${CDISOROOT}/isolinux || die
321     install -m 0644 $(get_profile help.msg) ${CDISOROOT}/isolinux || die
322    
323     return 0
324     }
325    
326     generate_initrd()
327     {
328     local CHROOTSH="$(mktemp -p ${CDCHROOTDIR})"
329    
330 niro 1403 is_loc_mounted "${CDCHROOTDIR}/sys" || mount -t sysfs sysfs ${CDCHROOTDIR}/sys
331     is_loc_mounted "${CDCHROOTDIR}/proc" || mount -t proc proc ${CDCHROOTDIR}/proc
332     is_loc_mounted "${CDCHROOTDIR}/dev" || mount -o bind /dev ${CDCHROOTDIR}/dev
333    
334 niro 346 echo Generating initrd image ...
335     echo '#!/bin/bash' > ${CHROOTSH} || die
336 niro 1403 echo 'export LC_ALL=C' >> ${CHROOTSH} || die
337 niro 346 echo 'kv="$(readlink /boot/vmlinuz)"' >> ${CHROOTSH} || die
338     echo 'kv="${kv/kernel-}/"' >> ${CHROOTSH} || die
339 niro 1403 # prefer dracut
340     echo 'if [[ -x /sbin/dracut ]]' >> ${CHROOTSH} || die
341     echo 'then' >> ${CHROOTSH} || die
342     echo ' dracut -v -f /initrd.gz ${kv}' >> ${CHROOTSH} || die
343     echo 'else' >> ${CHROOTSH} || die
344     echo ' mkinitrd-livecd -f --initramfs /initrd.gz ${kv}' >> ${CHROOTSH} || die
345     echo 'fi' >> ${CHROOTSH} || die
346 niro 346 chmod +x ${CHROOTSH} || die
347     chroot ${CDCHROOTDIR} /$(basename ${CHROOTSH}) || die
348     [[ -f ${CHROOTSH} ]] && rm ${CHROOTSH} || die
349    
350 niro 1403 is_loc_mounted "${CDCHROOTDIR}/dev" && umount ${CDCHROOTDIR}/dev
351     is_loc_mounted "${CDCHROOTDIR}/proc" && umount ${CDCHROOTDIR}/proc
352     is_loc_mounted "${CDCHROOTDIR}/sys" && umount ${CDCHROOTDIR}/sys
353    
354 niro 346 # move initrd to isoroot
355     install -d ${CDISOROOT}/isolinux || die
356     mv ${CDCHROOTDIR}/initrd.gz ${CDISOROOT}/isolinux || die
357     }
358    
359     generate_iso()
360     {
361     echo Generating ISO Image ...
362     install -d ${CDISOROOT}
363     pushd ${CDISOROOT} &&
364     mkisofs -rock \
365     -full-iso9660-filenames \
366     -allow-leading-dots \
367     -disable-deep-relocation \
368     -output ${LIVECDROOT}/${CDISONAME} \
369     -eltorito-boot isolinux/isolinux.bin \
370     -eltorito-catalog isolinux/boot.cat \
371     -no-emul-boot \
372     -boot-load-size 4 \
373     -boot-info-table \
374     -volid "${CDID}" \
375     ${CDISOROOT} #&> /dev/null
376     popd
377     }
378    
379     enter_livecd()
380     {
381 niro 1403 is_loc_mounted "${CDCHROOTDIR}/sys" || mount -t sysfs sysfs ${CDCHROOTDIR}/sys
382     is_loc_mounted "${CDCHROOTDIR}/proc" || mount -t proc proc ${CDCHROOTDIR}/proc
383     is_loc_mounted "${CDCHROOTDIR}/dev" || mount -o bind /dev ${CDCHROOTDIR}/dev
384 niro 346
385     chroot ${CDCHROOTDIR} \
386     /usr/bin/env -i \
387     HOME=/root \
388     TERM=$TERM PS1='\u:\w\$ ' \
389     PATH=/bin:/usr/bin:/sbin:/usr/sbin \
390 niro 472 http_proxy=${http_proxy} \
391     ftp_proxy=${ftp_proxy} \
392     no_proxy=${no_proxy} \
393 niro 346 /bin/bash -i
394 niro 609
395 niro 1403 is_loc_mounted "${CDCHROOTDIR}/dev" && umount ${CDCHROOTDIR}/dev
396     is_loc_mounted "${CDCHROOTDIR}/proc" && umount ${CDCHROOTDIR}/proc
397     is_loc_mounted "${CDCHROOTDIR}/sys" && umount ${CDCHROOTDIR}/sys
398 niro 346 }
399    
400 niro 1403 is_loc_mounted()
401     {
402     local filesys
403     local i
404     filesys=$1
405    
406     i="$(cat /proc/mounts | grep " ${filesys} " | cut -d ' ' -f2)"
407     [[ ${i} != ${filesys} ]] && return 1
408    
409     return 0
410     }
411    
412     trap_exit()
413     {
414     is_loc_mounted "${CDCHROOTDIR}/dev" && umount ${CDCHROOTDIR}/dev
415     is_loc_mounted "${CDCHROOTDIR}/proc" && umount ${CDCHROOTDIR}/proc
416     is_loc_mounted "${CDCHROOTDIR}/sys" && umount ${CDCHROOTDIR}/sys
417     }
418    
419 niro 360 usage()
420     {
421     echo "Usage: $(basename $0) [command] [profile] ..."
422     echo
423     echo "Commands:"
424     echo " bootstrap - bootstraps the rootfs for the livecd"
425     echo " prepare - prepare the rootfs to run from a livecd"
426     echo " rootfs - generates the squashfs rootfs image"
427     echo " initrd - generates a livecd suitable initrd"
428     echo " bootloader - installs the bootloader"
429     echo " isogen - generate the final livecd iso image"
430     echo " all - runs all tasks to get a livecd from zero"
431 niro 472 echo " enter - enters the rootfs of a livecd"
432 niro 360 echo
433     echo " for example '$(basename $0) all default'"
434     echo " creates a livecd using the default profile."
435     echo
436     }
437    
438 niro 1403 # set some proper traps
439     trap "trap_exit" SIGINT SIGQUIT
440    
441 niro 346 # source profile config - overrides all other vars
442     [ -f ${PROFILES_DIR}/${CDPROFILE}/config ] && . ${PROFILES_DIR}/${CDPROFILE}/config
443    
444 niro 360 case $1 in
445     bootstrap) bootstrap_system;;
446     prepare) prepare_iso;;
447     rootfs) generate_rootfs;;
448     initrd) generate_initrd;;
449     bootloader) install_bootloader;;
450     isogen) generate_iso;;
451     all)
452     bootstrap_system
453     prepare_iso
454     generate_rootfs
455     generate_initrd
456     install_bootloader
457 niro 713 generate_iso
458 niro 360 ;;
459 niro 472 enter) enter_livecd ;;
460 niro 360 *|'') usage;;
461     esac