summaryrefslogtreecommitdiffstats
path: root/abs/core/LinHES-config-svn/install_functions.sh
blob: 4f6f7ed46c7d2a4fa02dd4b11dc914866f86f7b5 (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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/bin/bash
. /etc/profile
disk=$2
mountpoint=new_boot

#-------------------------------------------
MYTHDBUSER=mythtv
MYTHTVPASSWD=mythtv
CMDLINE=$(cat /proc/cmdline)
hostname=`hostname`
MYSQLCMD_C="mysql -u$MYTHDBUSER  -p$MYTHTVPASSWD mythconverg -B --exec"
BASE=""


function update_db_settings () {

 echo "setting database value $1 to $2 for $hostname"
 $MYSQLCMD_C "delete from settings where value='${1}' and hostname=\"$hostname\";"
 $MYSQLCMD_C "REPLACE INTO  settings set value='${1}', data='${2}' , hostname=\"$hostname\";"
}

function random_theme () {
THEMES="basic-blue
basic-green
basic-red
basic-purple
basic-amber"

theme=($THEMES)                # Read into array variable.

num_themes=${#theme[*]}        # Count how many elements.

pick=${theme[$((RANDOM%num_themes))]}
echo "Selected $pick as the theme"
update_db_settings Theme "$pick"
}


function splash_setup (){
    echo $CMDLINE | grep -q splash=silent
    if [ $? -eq 0 ]
    then
        update_db_settings Hostbootsplash 1
    else
        update_db_settings Hostbootsplash 0
    fi
}


function setupremote {
    [ -e $BASE/etc/lircd.conf ] && mv -f $BASE/etc/lircd.conf $BASE/etc/lircd.conf.`date +%Y-%m-%d-%H-%M`
    if [ -d $TEMPLATES/remotes/$Remotetype ]
    then
            cd $TEMPLATES/remotes/$Remotetype
            for i in lircd*
            do
                    cat $i >> $BASE/etc/lircd.conf
            done
            cp -f lircrc* $BASE/etc/lircrc 2> /dev/null
            [ -e /etc/lircrc ] && chmod 755 /etc/lircrc
            update_db_settings HostRemoteType  "$Remotetype"
        #special case for special remote
        echo "Starting with support for $Remotetype"
        if [ "$Remotetype" == "dvico" ]
        then
                /usr/sbin/lircd  -d /dev/usb/hiddev0
        else
                /usr/sbin/lircd  -d /dev/lirc0
        fi
        [ -e /root/.mythtv ] ||  mkdir /root/.mythtv 2>/dev/null
        ln -s /etc/lircrc /root/.mythtv/lircrc 2>/dev/null
    else
        echo "Couldn't open directory $TEMPLATES/remotes/$Remotetype"
    fi
}

function scan_for_hpg_receiver() {
echo "Scanning for Hauppauge receiver"
for hpgid in `lspci -nm  -d4444: |cut -d" " -f6-  |tr -d '"'|tr " " :`
do
    line=`grep $hpgid $TEMPLATES/remotes/i2c.id`
    if [ $? -eq 0 ]
    then
        modprobe lirc_i2c
        FoundReceiverType=`echo "$line"|cut -d"|" -f2`
        Remotetype=`echo "$line"|cut -d"|" -f4`
        statusline=`echo "$line"|cut -d"|" -f2,4`
        echo "Found $statusline , $Remotetype"
        update_db_settings  HostReceiverType $FoundReceiverType
        setupremote
        break
    fi

done
}


function scan_for_usb_remote () {
found_remote=1
echo "Scanning for usb receiver/remote"
while read line
do
    USBID=`echo "$line"|cut -d"|"  -f1`
    lsusb -d "$USBID" > /dev/null 2>/dev/null
    if [ $? -eq 0 ]
    then
        Remotetype=`echo "$line"|cut -d"|" -f2`
        echo "Found $Remotetype"
        setupremote
        found_remote=0
        break
    fi
done <$BASE/$TEMPLATES/remotes/receiver_usb.id
if [ $found_remote -eq 1 ]
then
    scan_for_hpg_receiver
fi
echo "Scanning for TatIR..."
if lsusb | grep 04d8:0004 > /dev/null 2>/dev/null
then
	/usr/bin/PyroUsb.py > /dev/null 2>&1 &
fi
}

function rest_of_network () {
    #netmask
    echo $CMDLINE | grep -q netmask
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*netmask=}
        NETMASK=${TEMPVAR%% *}
        /sbin/ifconfig eth0 $IP netmask $NETMASK
        nm=`/usr/bin/nmconv.py -obits  $NETMASK`
        NETMASK="/$nm $NETMASK"
        echo $NETMASK
        update_db_settings HostNETMASK${MYTHDEFAULT} "$NETMASK"
    else
        echo "netmask not found"
    fi
    #gateway
    echo $CMDLINE | grep -q gateway
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*gateway=}
        GATEWAY=${TEMPVAR%% *}
        /sbin/route add default  gw $GATEWAY
        update_db_settings HostGW${MYTHDEFAULT} "$GATEWAY"

        else
        echo "gateway not found"
    fi

    #dns
    echo $CMDLINE | grep -q dns
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*dns=}
        DNS=${TEMPVAR%% *}
        echo "nameserver $DNS" >> /etc/resolv.conf
        update_db_settings HostDNS${MYTHDEFAULT} "$DNS"
        else
        echo "DNS not found"
    fi
}

function init_network {
    echo $CMDLINE |grep -q netdev
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*netdev=}
        MYTHDEFAULT=${TEMPVAR%% *}
    else
        MYTHDEFAULT="eth0"
    fi



    echo $CMDLINE | grep -q ip
    if [ $? -eq 0 ]
    then
        update_db_settings HostDefaulteth0 0
        update_db_settings HostDefault${MYTHDEFAULT} 1

        update_db_settings HostActiveonbooteth0 0
        update_db_settings HostActiveonboot${MYTHDEFAULT} 1
        update_db_settings HostNetDevice ${MYTHDEFAULT}

        TEMPVAR=${CMDLINE#*ip=}
        IP=${TEMPVAR%% *}
        if [ "$IP" == "dhcp" ]
        then
            /sbin/dhcpcd $MYTHDEFAULT
            update_db_settings HostUseDHCP${MYTHDEFAULT} 0
        else
            /sbin/ifconfig ${MYTHDEFAULT} $IP
            update_db_settings HostUseDHCP${MYTHDEFAULT} 1
            update_db_settings HostIP${MYTHDEFAULT} "$IP"
            rest_of_network
        fi
    fi
}

function init_remote {
    echo $CMDLINE | grep -q remoteport
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*remoteport=}
        REMOTEPORT=${TEMPVAR%% *}
    if [ "$ReceiverType" == "Serial" ]
    then
        if [ -e /dev/$REMOTEPORT ]
        then
            /usr/bin/setserial /dev/$REMOTEPORT uart none
            /sbin/modprobe lirc_serial
            update_db_settings HostReceiverType Serial
            update_db_settings HostSerialPortlirc "$REMOTEPORT"
        fi
    fi

    fi

    echo $CMDLINE | grep -q remote
    if [ $? -eq 0 ]
    then
        TEMPVAR=${CMDLINE#*remote=}
        Remotetype=${TEMPVAR%% *}
        setupremote
    else
        scan_for_usb_remote
    fi
}

#-----------------------

#set -x

partition_it () {
ROOT=$1
DATA=$2
SWAP=$3

if [ "$SWAP" != "NO" ]
    then
        SWAPLINE=",$SWAP,S"
        MKSWAPCMD="mkswap /dev/${disk}2"
    else
        SWAPLINE=",0,0"
       MKSWAPCMD=""
    fi

if [ "$DATA" == "ALL" ]
then
    DATALINE=",,,"
    EXTRALINE=""
else
    DATALINE=",$DATA,,"
    EXTRALINE=",0,,"
fi
sfdisk /dev/$disk -uM << EOF
,$1,,*
$SWAPLINE
$DATALINE
$EXTRALINE
;
EOF

$MKSWAPCMD
#sfdisk -l /dev/$disk
}


function fscmd () {

case $1 in
       reiserfs) FSCMDC="mkreiserfs -q -l ROOT"
                ;;

        xfs)    FSCMDC="mkfs -t $1 -f"
                ;;
        ext3)   FSCMDC="mkfs.ext3"
                ;;
        jfs)    FSCMDC="mkfs.jfs -q "
                ;;
        ext4)   FSCMDC="mkfs.ext4"
                ;;
        *)  FSCMDC="echo $1"
        ;;
esac
}

format_it ()  {
    fscmd $1
    $FSCMDC /dev/${disk}1|  tr -s [:cntrl:]  \\n
    fscmd $2
    $FSCMDC /dev/${disk}3|  tr -s [:cntrl:]  \\n
}

mount_it () {
        if [ ! -d \/$mountpoint ]
        then
        mkdir \/$mountpoint
        fi
        mount /dev/${disk}1  \/$mountpoint
        mkdir \/$mountpoint/myth
        mount /dev/${disk}3 \/$mountpoint/myth
}

unmount_it () {
        umount /dev/${disk}1
        umount /dev/${disk}3
        umount \/$mountpoint/myth
        umount  \/$mountpoint
}


copy_it () {

        if [ "$1" == "ALL" ]
        then
            echo "Transferring system"
            unsquashfs -f -d /$mountpoint /.livesys/medium/system.sqf
        else
             echo "Upgrading system"
            unsquashfs -e $1 -f -d /$mountpoint /.livesys/medium/system.sqf
        fi

        for i in sys proc dev tmp
        do
        mkdir /$mountpoint/$i

        done
        chmod 777 /$mountpoint/tmp
        mknod /$mountpoint/dev/null c 1 3
        mknod /$mountpoint/dev/null c 1 5
        mknod /$mountpoint/dev/console c 5 1
        chmod +s /$mountpoint/usr/bin/Xorg
        chmod +s /$mountpoint/usr/bin/crontab
        chmod +s /$mountpoint/usr/bin/sudo
        chmod +s /$mountpoint/bin/mount

}


device_node () {
                cd /dev
                mknod hda b 3 0
                mknod hda1 b 3 1
                mknod hda2 b 3 2
                mknod hda3 b 3 3
                mknod hda4 b 3 4
                mknod hda5 b 3 5
                mknod hda6 b 3 6
                mknod hda7 b 3 7
                mknod hda8 b 3 8
                mknod sda b 8 0
                mknod sda1 b 8 1
                mknod sda2 b 8 2
                mknod sda3 b 8 3
                mknod sda4 b 8 4
                mknod sda5 b 8 5
                mknod sda6 b 8 6
                mknod sda7 b 8 7
                mknod sda8 b 8 8
                mknod sdb b 8 16
                mknod sdb1 b 8 17
                mknod sdb2 b 8 18
                mknod sdb3 b 8 19
                mknod sdb4 b 8 20
                mknod sdb5 b 8 21
                mknod sdb6 b 8 22
                mknod sdb7 b 8 23
                mknod sdb8 b 8 24
                cd -
                }




function create_fstab()
{
echo "creating fstab"
cat > /$mountpoint/etc/fstab << EOF
# <file system>        <dir>         <type>    <options>          <dump> <pass>
none                   /dev/pts      devpts    defaults            0      0
none                   /dev/shm      tmpfs     defaults            0      0
/dev/cdrom /media/cdrom   auto    ro,user,noauto,unhide   0      0
/dev/dvd /media/dvd   auto    ro,user,noauto,unhide   0      0
UUID=ROOTUID / auto defaults 0 1
UUID=DATAUID /myth auto defaults 0 1
UUID=SWAPUID swap swap defaults 0 0
EOF


}


function find_uuid() {
    uuid=`blkid -s UUID /dev/$1 |cut -d= -f2|cut -d\" -f2`
    echo $uuid for $1
    FOUNDUUID=$uuid

}


get_grub_map() {
    [ -e /tmp/dev.map ] && rm /tmp/dev.map
    /sbin/grub --no-floppy --device-map /tmp/dev.map <<EOF
quit
EOF
}

mapdev() {
    partition_flag=0
    device_found=0
    devs=$(cat /tmp/dev.map | grep -v fd | sed 's/ *\t/ /' | sed ':a;$!N;$!ba;s/\n/ /g')
    linuxdevice=$(echo $1 | cut -b1-8)
    if [ "$(echo $1 | egrep '[0-9]$')" ]; then
        # /dev/hdXY
        pnum=$(echo $1 | cut -b9-)
        pnum=$(($pnum-1))
        partition_flag=1
    fi
    for  dev in $devs
    do
        if [ "(" == $(echo $dev | cut -b1) ]; then
        grubdevice="$dev"
        else
        if [ "$dev" == "$linuxdevice" ]; then
            device_found=1
            break
        fi
       fi
    done
    if [ "$device_found" == "1" ]; then
        if [ "$partition_flag" == "0" ]; then
            echo "$grubdevice"
        else
            grubdevice_stringlen=${#grubdevice}
            grubdevice_stringlen=$(($grubdevice_stringlen - 1))
            grubdevice=$(echo $grubdevice | cut -b1-$grubdevice_stringlen)
            echo "$grubdevice,$pnum)"
        fi
    else
        echo "DEVICE NOT FOUND"
    fi
}


dogrub() {
    get_grub_map
    sync
    ROOTDEV=/dev/$1
    echo $ROOTDEV

    bootdev=$(mapdev $ROOTDEV)
    echo $bootdev
    cp -a /$mountpoint/usr/lib/grub/i386-pc/* /$mountpoint/boot/grub/
    sync
    # freeze xfs filesystems to enable grub installation on xfs filesystems
    if [ -x /usr/sbin/xfs_freeze ]; then
        #/usr/sbin/xfs_freeze -f /$mountpoint > /dev/null 2>&1
        echo "freezing xfs fs"
        /usr/sbin/xfs_freeze -f /$mountpoint
        if [ $? -eq 0 ]
        then
            sleep 40
            echo "unfreezing xfs"
            /usr/sbin/xfs_freeze -u /$mountpoint
        fi
    fi
    #$DESTDIR/sbin/grub-install --recheck --root-directory=$DESTDIR $bootdev >/tmp/grub.log 2>&1
    echo grub-install  --recheck --root-directory=/$mountpoint $bootdev
    grub-install --recheck  --root-directory=/$mountpoint $bootdev
    # unfreeze xfs filesystems


    if grep "Error [0-9]*: " /tmp/grub.log >/dev/null; then
        echo  "Error installing GRUB. "
        return 1
    fi
    echo "GRUB was successfully installed."
}

function removevg () {
    LIST=`pvscan |grep $1|tr -s " " |cut -d" " -f 5`
    for vg in $LIST
    do
        echo "Removing vg $vg"
        vgremove -f -v $vg
    done




}


case $1 in
        partition_it )
    #drive  ROOT DATA(ALL) SWAP(NO)
                for i in `mount | grep $disk | awk ' { print $1 } '`
                do
                umount $i
                done
                swap=`cat /etc/fstab |grep $disk|grep swap|awk ' { print $1 } '`
                swapoff $swap
                removevg $disk
                partition_it $3 $4 $5
                exit 0

        ;;
        format_it )
                device_node

                format_it $3 $4
                exit 0
        ;;
        mount_it )
                mount_it
        ;;
        copy_it )
                copy_it $3

        ;;


        fstab_fix_it)
                    echo $@ >> /tmp/fstab.input
                    if [  ! -f  /$mountpoint/etc/fstab ]
                    then
                        create_fstab
                    fi
                    #was 5
                    if [ "$3" == "UPGRADE" ]
                    then
                       if [ ! -f /tmp/etc/KnoppMyth-version ]
                       then
                           if [ -f /tmp/etc/fstab ]
                           then
                                cp -f /tmp/etc/fstab /$mountpoint/etc/fstab
                           else
                            create_fstab
                           fi
                       else
                           create_fstab
                       fi
                    fi

                     #search for UUID
                      FOUNDUUID=""
                      find_uuid ${2}1
                      ROOTUUID=$FOUNDUUID
                        echo "--"
                        echo $ROOTUUID
                        echo "--"
                      FOUNDUUID=""
                      find_uuid ${2}2
                      SWAPUUID=$FOUNDUUID

                      FOUNDUUID=""
                      if [ "$4" == "" ]
                      then
                        #upgrade
                        find_uuid ${2}3
                      elif [ "$5" == "FULL_INSTALL" ]
                      then
                        #install
                        find_uuid ${2}3
                      else
                        #upgrade with non statndard myth
                        find_uuid $4
                      fi

                      DATAUUID=$FOUNDUUID



                     sed -i -e "s/.*\/\ .*$/UUID=$ROOTUUID \/  auto defaults,noatime 0 1/g"  /$mountpoint/etc/fstab
                     sed -i -e "s/.*\/myth\ .*$/UUID=$DATAUUID \/myth  auto defaults,noatime 0 1/g" /$mountpoint/etc/fstab
                     sed -i -e "s/.*swap\ .*$/UUID=$SWAPUUID swap swap defaults  0/g" /$mountpoint/etc/fstab

#if Knoppmyth upgrade then loop through old fstab picking out vg
                    if [  -f /tmp/etc/KnoppMyth-version ]
                    then
                        if [ -f /tmp/etc/fstab ]
                        then
                            grep -q /dev/vg /tmp/etc/fstab
                            if [ $? -eq 0 ]
                            then
                                grep /dev/vg /tmp/etc/fstab >> /$mountpoint/etc/fstab
                                grep /dev/vg /tmp/etc/fstab | awk -v mount=$mountpoint ' { print "/" mount $2 }' |xargs mkdir
                                grep /dev/vg /tmp/etc/fstab | awk -v mount=$mountpoint ' { print "/" mount $2 }' |xargs chown -R mythtv:users
                            fi
                        fi
                    fi
        ;;

        grub_it )

                #grub-install  --recheck --no-floppy --root-directory=/$mountpoint "(hd0)"
                #fixing grub/menu.1st
                dogrub ${disk}
                FOUNDUUID=""
                find_uuid ${disk}1
                ROOTUUID=$FOUNDUUID
                sed -i -e "s/root=.\S*/root=\/dev\/disk\/by-uuid\/$ROOTUUID/g"   /$mountpoint/boot/grub/menu.lst
                mkinitcpio -g /$mountpoint/boot/kernel26.img
                ;;
        umount_it)
                umount \/$mountpoint/myth
                umount \/$mountpoint
                ;;
        update_db_it )
                update_db_settings "$2" "$3"
                ;;


esac