summaryrefslogtreecommitdiffstats
path: root/build_tools/clarch/larch/buildlive
blob: 4fe1a901f29b6584a9999ec7db3a6866e14c1f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# buildlive - functions to build larch live CD
#
# Author: Michael Towers <gradgrind[at]online[dot]de>
#
# This file is part of the larch project.
#
#    larch is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    larch is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with larch; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#----------------------------------------------------------------------------
# 2008.06.07

# Location for the CD image
CDDATA="${LARCHBUILD}/cd"

############ ENTRY POINT - mklive ###############

#+++++++mklive: main function for building larch system from
#               Arch installation at ${INSTLDIR}, which can
#               be '' to use currently running Arch installation.

mklive ()
{
    echo "//"
    echo "// **********************************************************"
    echo "//"
    if [ -n "${REUSE}" ] && [ -e ${CDDATA}/system.sqf ]; then
        echo "// ***** Recreating live CD with old squashed system"
        REGEN="yes"
    else
        echo "// *****    Creating live CD from system at '${INSTLDIR}'"
        REGEN="no"
    fi

    echo "//"
    echo "// This will delete EVERYTHING under  ::: ${CDDATA} :::"
    echo "//"
    if [ -z "${DONTASK}" ]; then
        echo "// I really mean it ... Are you sure you want to do this?"
        echo "// **********************************************************"
        # Await yes or no
        read -p "[y/N]: " ans
        if [ -z "$( echo ${ans} | grep '^ *[yY]' )" ]; then return 0; fi
        echo
    fi

    # Get kernel version
    if ! find_kernel; then
        return 1
    fi

    # If not building live CD from currently running system, chroot is
    # needed sometimes. 'chrootx' simplifies this a bit.
    if [ -n  "${INSTLDIR}" ]; then
        chrootx="chroot ${INSTLDIR} "
    else
        chrootx=""
    fi

    # Test for necessary packages/modules
    fail=0
    aufs=''
    if ! ${chrootx}grep /squashfs.ko \
            /lib/modules/${KVERSION}/modules.dep &>/dev/null; then
        echo "ERROR: No squashfs module found"
        fail=1
    fi
    if ${chrootx}grep /aufs.ko \
            /lib/modules/${KVERSION}/modules.dep &>/dev/null; then
        aufs='_aufs'
        echo "Using aufs"
    elif ${chrootx}grep /unionfs.ko \
            /lib/modules/${KVERSION}/modules.dep &>/dev/null; then
        echo "Using unionfs"
    else
        echo "ERROR: No aufs or unionfs module found"
        fail=1
    fi

    mustpacs="linhes-live lzop tar squashfs-tools"
    if [ -z "${GRUB}" ]; then
        mustpacs="${mustpacs} syslinux"
    fi
    for p in ${mustpacs}; do
        pac=$( ls ${INSTLDIR}/var/lib/pacman/local | egrep "^${p}-[^-]+-[^-]+$" )
        if [ -z "${pac}" ]; then
            echo "ERROR: Package ${p} is not installed on target"
            fail=1
        fi
    done

    if [ -z "${USB}" ] && ! which mkisofs &>/dev/null; then
        echo "ERROR: mkisofs not found -"
        echo "      cdrkit (or cdrtools) must be installed on host"
        fail=1
    fi

    if [ ${fail} -ne 0 ]; then
        return 1
    fi

    # Temporary directory for building stuff
    rm -rf ${LARCHBUILD}/tmp
    mkdir -p ${LARCHBUILD}/tmp

    # If using old sqf files move them to a temporary, safe location
    if [ ${REGEN} = "yes" ]; then
        echo "// moving old system image to ${LARCHBUILD}/tmp"
        mv ${CDDATA}/system.sqf ${LARCHBUILD}/tmp
    fi


    ############## START: copying data to boot medium image directory (CDDATA)

    # Clear out the directory.
    echo "// copying cd-root to ${CDDATA}"
    mkdir -p ${CDDATA}
    rm -Rf ${CDDATA}/*

    # cd-root: first general stuff, then from profile
    cp -R ${LARCHDATA}/cd-root/* ${CDDATA}
    if [ -n "${PROFILE}" ]; then
        cp -Rf ${PROFILE}/cd-root/* ${CDDATA}
    fi

    # kernel
    echo "// copying kernel from ${INSTLDIR}/boot to ${CDDATA}/boot/vmlinuz"
    echo "//  ... using ${VMLINUZ}"
    cp -f ${INSTLDIR}/boot/${VMLINUZ} ${CDDATA}/boot/vmlinuz

    ### Generate initcpio (using chroot if necessary) then copy it to CDDATA

    # If there is a mkinitcpio.conf in profile, use it
    if  [ -n "${PROFILE}" ] && [ -f ${PROFILE}/mkinitcpio.conf ]; then
        cp -f ${PROFILE}/mkinitcpio.conf ${INSTLDIR}/lib/initcpio
    fi

    # Fix up mkinitcpio.conf for unionfs/aufs
    sed -i "s|___aufs___|${aufs}|g" ${INSTLDIR}/lib/initcpio/mkinitcpio.conf

    echo "// calling gen_larch_init to generate the initramfs"
    ${chrootx}/lib/initcpio/gen_larch_init ${KVERSION}
    if [ -n  "${INSTLDIR}" ]; then
        mv -f ${INSTLDIR}/larch.img ${CDDATA}/boot
    else
        mv -f larch.img ${CDDATA}/boot
    fi

    #!!!!! This can't be allowed if ${INSTLDIR} = ''
    # mkinitcpio seems to use the '/tmp' directory, so clear it out
    if [ -n  "${INSTLDIR}" ]; then
        rm -rf ${INSTLDIR}/tmp/*
    fi

    ### END of initcpio generation

    ### if no saved system.sqf, squash the Arch installation at ${INSTLDIR}
    if [ ! -e "${LARCHBUILD}/tmp/system.sqf" ]; then
        ignorefiles=/.larch/ignorefiles
        :>${INSTLDIR}${ignorefiles}

        echo "// ignoring superfluous initrd/initramfs images from /boot"
        # Only the fallbacks should make it to system.sqf, as the others
        # won't work on other systems anyway, and should be regenerated.
        for i in $( ls ${INSTLDIR}/boot | grep "kernel.*\.img" ); do
            if [ -z "$( echo ${i} | grep "fallback" )" ]; then
                echo "  ... ${i}"
                echo "boot/${i}"  >>${INSTLDIR}${ignorefiles}
            fi
        done

       # root directories which are not included in the squashed system.sqf
        ignoredirs="dev mnt media proc sys tmp var .larch .livesys"

        echo "// creating compressed image of linux system: system.sqf"
        mksquash "/" "/.larch/tmp/system.sqf" -ef ${ignorefiles} \
                -e ${ignoredirs}
        if [ $? -ne 0 ]; then
            return 1
        fi

        # Add /var, but mask out some undesirable stuff: use mount --bind
        #    first make fresh pacman cache, log and tmp directories
        mkdir -p ${LARCHBUILD}/tmp/varbld/paccache
        mkdir -p ${LARCHBUILD}/tmp/varbld/varlog/old
        :>${LARCHBUILD}/tmp/varbld/varlog/wtmp
        :>${LARCHBUILD}/tmp/varbld/varlog/utmp
        :>${LARCHBUILD}/tmp/varbld/varlog/btmp
        chmod 600 ${LARCHBUILD}/tmp/varbld/varlog/btmp
        :>${LARCHBUILD}/tmp/varbld/varlog/lastlog
        mkdir -m 1777 ${LARCHBUILD}/tmp/varbld/vartmp
        #    and a mount point for var, then mount it
        mkdir -p ${LARCHBUILD}/tmp/varbld/var0/var
        mount --bind ${INSTLDIR}/var ${LARCHBUILD}/tmp/varbld/var0/var
        #    mount the cover-ups
        mount --bind ${LARCHBUILD}/tmp/varbld/paccache \
                ${LARCHBUILD}/tmp/varbld/var0/var/cache/pacman
        mount --bind ${LARCHBUILD}/tmp/varbld/varlog \
                ${LARCHBUILD}/tmp/varbld/var0/var/log
        mount --bind ${LARCHBUILD}/tmp/varbld/vartmp \
                ${LARCHBUILD}/tmp/varbld/var0/var/tmp
        #    do the squashing
        mksquash "/.larch/tmp/varbld/var0" "/.larch/tmp/system.sqf"
        res=$?

        #    unmount all the binds
        umount ${LARCHBUILD}/tmp/varbld/var0/var/tmp
        umount ${LARCHBUILD}/tmp/varbld/var0/var/log
        umount ${LARCHBUILD}/tmp/varbld/var0/var/cache/pacman
        umount ${LARCHBUILD}/tmp/varbld/var0/var
        rm -r ${LARCHBUILD}/tmp/varbld

        if [ ${res} -ne 0 ]; then
            echo "Failed while extending /var"
            return 1
        fi

    fi

    # move system.sqf to boot-medium image directory
    echo "// moving squashed system image from ${LARCHBUILD}/tmp"
    mv ${LARCHBUILD}/tmp/system.sqf ${CDDATA}
    if [ $? -ne 0 ]; then
        echo "ERROR: failed to move system.sqf to build area"
        return 1
    fi

    ##### Prepare initial overlay
    echo "// building overlay"

    # Build overlay in a temporary directory
    rm -rf ${LARCHBUILD}/tmp/overlay

    # Copy over the overlay(s) from the selected profile
    if [ -n "${PROFILE}" ]; then
        # Note that ownership/mode of all files in overlay(.xpk) must be
        # correct! This information will be preserved.
        if [ -d ${PROFILE}/overlay ]; then
            cp -a ${PROFILE}/overlay ${LARCHBUILD}/tmp
        elif [ -f ${PROFILE}/overlay.xpk ]; then
            # deal with packed overlay
            ${PROFILE}/overlay.xpk ${LARCHBUILD}/tmp
        else
            mkdir ${LARCHBUILD}/tmp/overlay
        fi

        if [ -d ${PROFILE}/rootoverlay ]; then
            cp -rf ${PROFILE}/rootoverlay/* ${LARCHBUILD}/tmp/overlay
        fi

    fi

    # Ensure the overlay has at least the /etc folder
    mkdir -p ${LARCHBUILD}/tmp/overlay/etc

    # Save original /etc/inittab, /etc/rc.sysinit and /etc/rc.shutdown
    for fsave in inittab rc.sysinit rc.shutdown; do
        if [ -f ${INSTLDIR}/etc/${fsave}.livesave ]; then
            # A running larch system will already have this file, and it
            # shouldn't be overwritten
            echo "// /etc/${fsave}.livesave exists already, not overwriting"
        elif [ -f ${LARCHBUILD}/tmp/overlay/etc/${fsave}.livesave ]; then
            # A profile can supply this file in the overlay, and it
            # shouldn't be overwritten
            echo "// /etc/${fsave}.livesave found in overlay, not overwriting"
        else
            cp ${INSTLDIR}/etc/${fsave} \
                    ${LARCHBUILD}/tmp/overlay/etc/${fsave}.livesave
        fi
    done

    # Place the live rc.sysinit and rc.shutdown scripts in the overlay
    cp ${INSTLDIR}/etc/rc.sysinit-live ${LARCHBUILD}/tmp/overlay/etc/rc.sysinit
    cp ${INSTLDIR}/etc/rc.shutdown-live ${LARCHBUILD}/tmp/overlay/etc/rc.shutdown

    # Add IgnorePkg to pacman.conf
    if ! [ -f ${LARCHBUILD}/tmp/overlay/etc/pacman.conf ]; then
        cp ${INSTLDIR}/etc/pacman.conf ${LARCHBUILD}/tmp/overlay/etc/pacman.conf
    fi
    if ! grep "+++LARCH-IGNORE+++" ${LARCHBUILD}/tmp/overlay/etc/pacman.conf \
            &>/dev/null; then
        igpak="IgnorePkg = kernel26 aufs initscripts"
        for pak in $( cat ${PROFILE}/noupdate 2>/dev/null ); do
            igpak="${igpak} ${pak}"
        done
        sed "/\[options\]/ a\#+++LARCH-IGNORE+++\n${igpak}\n#---LARCH-IGNORE---" \
                -i ${LARCHBUILD}/tmp/overlay/etc/pacman.conf
    fi

    # Generate customized /etc/rc.conf
    if [ -n "${PROFILE}" ] && [ -f ${PROFILE}/rcconfx ]; then
        # If there is one in the given overlay, start with that
        if [ ! -f ${LARCHBUILD}/tmp/overlay/etc/rc.conf ]; then
            # else copy the default one
            cp ${INSTLDIR}/etc/rc.conf ${LARCHBUILD}/tmp/overlay/etc
        fi
        cat ${PROFILE}/rcconfx | grep -v "^#" | grep "=" | { while read line; do
                var="$( echo ${line} | cut -d'=' -f1 )"
                sed -i "s|^${var}=.*|${line}|" \
                        ${LARCHBUILD}/tmp/overlay/etc/rc.conf
            done }
    fi

    # Add hostname to /etc/hosts localhost, if making new rc.conf
    if [ -f ${LARCHBUILD}/tmp/overlay/etc/rc.conf ]; then
        hosts=${LARCHBUILD}/tmp/overlay/etc/hosts
        if ! [ -f ${LARCHBUILD}/tmp/overlay/etc/hosts ]; then
            cp ${INSTLDIR}/etc/hosts ${LARCHBUILD}/tmp/overlay/etc
        fi
        (   . ${LARCHBUILD}/tmp/overlay/etc/rc.conf;
            lh="127.0.0.1       localhost.localdomain   localhost       ";
            sed -i "s|^127\.0\.0\.1.*|${lh}${HOSTNAME}|" ${hosts}
        )
    fi

    # Handle /mnt
    mkdir -p ${LARCHBUILD}/tmp/overlay/mnt
    for d in $( ls ${INSTLDIR}/mnt ); do
        if [ -d ${INSTLDIR}/mnt/${d} ]; then
            mkdir ${LARCHBUILD}/tmp/overlay/mnt/${d}
        fi
    done

    echo "// creating compressed image of larch mods: mods.sqf"

    # Make 'mods' archive from all but /etc
    mksquash "/.larch/tmp/overlay" "/.larch/cd/mods.sqf" -e etc
    if [ $? -ne 0 ]; then
        return 1
    fi

    echo "// creating compressed image of remaining larch mods: overlay.ovl"

    # Compress the overlay (with root dir 'overlay' retained)
    ${chrootx} bash -c "tar -cf - -C /.larch/tmp overlay/etc |
            lzop > /.larch/cd/overlay.ovl"

    rm -rf ${LARCHBUILD}/tmp/overlay

    ##### End of overlay creation
    ############## END: copying data to boot medium image directory (CDDATA)

    # The boot medium image is now ready
    buildiso
}



########## START: functions for building iso from image directory ##########

#+++++++buildiso: set up boot medium image folder and then
#                 create iso or call usb medium handler

buildiso ()
{
    mkdir ${CDDATA}/tmp
    if [ -f ${CDDATA}/boot/vmlinuz ]; then
        bd=${CDDATA}/boot
    else
        bd=${CDDATA}/isolinux
    fi
    cp ${bd}/{vmlinuz,larch.img} ${CDDATA}/tmp
    rm -rf ${CDDATA}/{isolinux,boot}
    if [ -n "${GRUB}" ]; then
        mv ${CDDATA}/tmp ${CDDATA}/boot

        mkdir -p ${CDDATA}/boot/grub
        cp ${INSTLDIR}/usr/lib/grub/i386-pc/* ${CDDATA}/boot/grub
        cp -r ${LARCHDATA}/cd-root/boot ${CDDATA}
        if [ -n "${PROFILE}" ] && [ -d ${PROFILE}/cd-root/boot ]; then
            cp -rf ${PROFILE}/cd-root/boot ${CDDATA} 2>/dev/null
        fi

        if [ -n "${USB}" ]; then
            usbboot_grub ${INSTLDIR}
        else
            echo "// creating GRUB-booting LiveCD ISO image..."
            mkiso "-b boot/grub/stage2_eltorito"
        fi
    else
        mv ${CDDATA}/tmp ${CDDATA}/isolinux

        cp -r ${LARCHDATA}/cd-root/isolinux ${CDDATA}
        if [ -n "${PROFILE}" ] && [ -d ${PROFILE}/cd-root/isolinux ]; then
            cp -rf ${PROFILE}/cd-root/isolinux ${CDDATA} 2>/dev/null
        fi

        isolinuxbin=${INSTLDIR}/usr/lib/syslinux/isolinux.bin
        if [ -f ${isolinuxbin} ]; then
            cp ${isolinuxbin} ${CDDATA}/isolinux
        else
            echo "ERROR: ${isolinuxbin} not found -"
            echo "      syslinux must be installed on live system"
            return 1
        fi

        #if [ -n "${USB}" ]; then
        #    usbboot ${INSTLDIR}
       # else
            echo "// creating isolinux-booting LiveCD ISO image..."
            mkiso "-b isolinux/isolinux.bin -c isolinux/isolinux.boot"
            usbboot ${INSTLDIR} ${startdir}
            
       # fi
    fi
}

#+++++++mkiso: helper function for calling mkisofs

mkiso ()
{
    mkisofs -r -l $1 \
        -no-emul-boot -boot-load-size 4 -boot-info-table \
        -input-charset=UTF-8 \
        -publisher "Cecil H. Watson, license: GPL" \
        -A "LinHES_6" \
        -p "http://www.knoppmyth.net" \
        -o "${LARCHBUILD}/mylivecd.iso" "${CDDATA}"

    if [ $? -eq 0 ]; then
        echo "// Your ISO has been created as ${LARCHBUILD}/mylivecd.iso"
    else
        echo "ERROR: iso build failed" 1>&2
        return 1
    fi
}

########## END: functions for building iso ##########



#+++++++mksquash: helper function for using mksquashfs via chroot

mksquash ()
{
    if [ -n "${INSTLDIR}" ]; then
	mount --bind /proc ${INSTLDIR}/proc
        eval chroot ${INSTLDIR} /sbin/mksquashfs $*
	umount ${INSTLDIR}/proc
    else
        /sbin/mksquashfs $*
    fi
    if [ $? -ne 0 ]; then
        echo "ERROR: squash failed --- mksquashfs $*"
        return 1
    fi
    chmod oga-x "${INSTLDIR}/$2" # remove execute attrib
}


#+++++++find_kernel: helper function to get kernel information

find_kernel ()
{
    # Discover kernel
    if  [ -n "${PROFILE}" ] && [ -f ${PROFILE}/kernel ]; then
        . ${PROFILE}/kernel
    else
        VMLINUZ=( $( ls ${INSTLDIR}/boot | egrep ".*vmlinuz.*" ) )
        if [ ${#VMLINUZ[@]} -gt 1 ]; then
            echo "Error - more than 1 kernel found:"
            for k in ${VMLINUZ[@]}; do
                echo "        $k"
            done
            return 1
        elif [ ${#VMLINUZ[@]} -ne 1 ]; then
            echo "Error - no kernel found"
            return 1
        fi

        # Discover kernel version
        KVERSION=""
        KVERSIONS=$( ls ${INSTLDIR}/lib/modules )
        for kv in ${KVERSIONS}; do
            # Check for 'build' symlink
            if [ -h ${INSTLDIR}/lib/modules/${kv}/build ]; then
                if [ -n "${KVERSION}" ]; then
                    echo "Error - more than one set of kernel modules in ${INSTLDIR}/lib/modules"
                    return 1
                fi
                KVERSION=${kv}
            else
                # Dubious set of modules found
                echo "WARNING:"
                echo "  You seem to have installed a package containing modules"
                echo "which aren't compatible with your kernel."
                echo "Please check that this won't cause problems."
                echo "Maybe you need the corresponding package"
                echo "for your kernel?"
                km=$( find ${INSTLDIR}/lib/modules/${kv} -name "*.ko" | \
                        sed "s|^${INSTLDIR}||" )
                pkgs=""
                for m in ${km}; do
                    # Use pacman to find the owning package
                    p=$( chroot ${INSTLDIR} pacman -Qo ${m} )
                    # Extract the package name, and surround it with '|'s for easier matching
                    pname="|$( echo "${p}" | cut -d " " -f 5 )|"
                    # Only report each package once
                    if [ -z "$( echo ${pkgs} | grep "${pname}" )" ]; then
                        pkgs="${pkgs} ${pname}"
                        echo ${p}
                        echo "   Package: $( echo "${pname}" | \
                                sed "s/|//g" )"
                        echo
                    fi
                done
                # Await yes or no
                read -p "Continue? [y/N]: " ans
                if [ -z "$( echo ${ans} | grep '^ *[yY]' )" ]; then return 0; fi
            fi
        done
    fi
    if [ -z "${KVERSION}" ]; then
        echo "Error - couldn't find kernel modules"
        return 1
    fi
    echo "// Kernel version: ${KVERSION}"
    # Must regenerate kernel dependency files
    echo "//  -> regenerating kernel dependencies"
    if [ -n "${INSTLDIR}" ]; then
        depmod -b "${INSTLDIR}" "${KVERSION}"
    else
        depmod "${KVERSION}"
    fi
    echo
}