summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-config/mv_install.py
blob: 97fbae55aa94dc8a4bc887a66f879ca6057dc6eb (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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,  os,  commands,  glob,  time
import getopt,  re,  MySQLdb
import logging
try:
    import parted
except:
    print "module parted not found"

def usage():
    print "help text:"
    print "example usage:  --rootdisk=sda --rootfs=ext4 --rootsize=34240 --datafs=ext4 --datasize=3400 --datadisk=sda --swapsize=340 -c full_install"

def clean_upgrade():
    return False

def update_db(value, data):
    db = MySQLdb.connect(host="localhost",  user="mythtv",  passwd="mythtv",  db="mythconverg")
    try:
        cursor = db.cursor()
        cursor.execute("DELETE FROM settings where value=%s and hostname=%s;", (value, data_config.MVHOSTNAME))
        cursor.execute("REPLACE INTO  settings set value=%s, data=%s, hostname=%s;", (value, data, data_config.MVHOSTNAME))
        logging.debug("Updated database  with %s, %s", data, value)
    except  MySQLdb.Error,  e:
        logging.debug("Unable to update database with %s, %s", data, value)


def progress(pgnum):
    progressline = str(pgnum) + "% complete"
    logging.debug("Progress: %s",  pgnum)
    f = open('/tmp/.install_percent',  'w')
    f.write(progressline)
    f.close()

def update_status(status):
    logging.debug("Status: %s",  status)
    f = open('/tmp/.install_state',  'w')
    f.write(str(status))
    f.close()

def kill_dhcp_chroot():
    logging.debug("Killing off chroot dhcpcd")
    stddir = os.getcwd()
    piddir = ("%s/var/run/") %data_config.MOUNTPOINT
    try:
        os.chdir(piddir)
        for FILE in glob.glob("dhcpcd-*.pid"):
            f = open(FILE, 'r')
            pid = f.readline()
            f.close()
            cmd = "kill -9 %s" %pid
            runcmd(cmd)
            os.remove(FILE)
        os.chdir(stddir)
    except:
        pass

def statgrab(disk):
    cmd = "statgrab -M disk. |grep %s.write_bytes" % hostoptions["rootdisk"]
    out = commands.getoutput(cmd)
    try:
        prewritebytes = out.split("=")[1].strip()
    except:
        prewritebytes = "1024"
    outline = "STARTSIZE=%s" %prewritebytes
    f = open('/tmp/.startsize.io',  'w')
    f.write(str(outline))
    f.close()

def backup_sql_check():
    logging.debug("Searching for backup file")
    try:
        hostoptions["backupfile"]
    except:
        logging.debug("Backup file var is empty")
        return False
    if os.path.exists(data_config.MOUNTPOINT+hostoptions["backupfile"]):
        logging.debug("Backup file %s is present", data_config.MOUNTPOINT+hostoptions["backupfile"])
        return True
    else:
        logging.debug("Backup file %s is NOT present", data_config.MOUNTPOINT+hostoptions["backupfile"])
        return False

def mdadm_find(PREFIX):
    logging.debug("Searching for mdadm.conf")
    if os.path.exists(PREFIX+"/etc/KnoppMyth-version"):
        mdadmconf = "/etc/mdadm/mdadm.conf"
    else:
        mdadmconf = "/etc/mdadm.conf"
    logging.debug("Using %s for mdadm.conf", mdadmconf)
    return mdadmconf

def mdadm_assemble_all():
    # read mdadm.conf and start the arrays
    #ARRAY /dev/md5 uuid=19464854:03f71b1b:e0df2edd:246cc977
    logging.debug("Starting mdadm support")
    mdadmconf_file = mdadm_find("/tmp")
    cmd = "cdadm --assemble --scan -c /tmp/%s" %mdadmconf_file
    runcmd(cmd)
    mdadm_contents = ''
    try:
        f = open(mdadmconf_file,  'r')
        mdadm_contents = f.readlines()
        f.close()
    except:
        logging.debug("     Couldn't open mdadmconf file")
    for line in mdadm_contents:
        if line.startswith("ARRAY"):
            logging.debug("     Found MD array: %s", line)
            array = line.split()[1]
            logging.info("      assembling array: %s", array)
            cmd = "mdadm --assemble -c /tmp%s %s" %(mdadmconf_file, array)
            runcmd(cmd)
            time.sleep(2)
            cmd = "fsck -p %s" %array
            runcmd

def copy_updates():
    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("MVROOT was not defined,  using the default value")
        MVROOT = "/usr/MythVantage"
    cp_and_log2(MVROOT+"/bin/", data_config.MOUNTPOINT+MVROOT+"/bin", "*.sh")
    cp_and_log2(MVROOT+"/bin/", data_config.MOUNTPOINT+MVROOT+"/bin", "*.py")

def timezone_to_db(timefile):
    logging.info("importing timezone needs")
    try:
        f = open(timefile)
        timezonecontents = f.readline().strip()
        f.close()
    except:
        logging.debug("Couldn't open /tmp/etc/timezone, will not set the timezone")
        return
    tzsplit = timezonecontents.partition('/')
    print tzsplit
    if tzsplit[2]  == '' :
        update_db("HostTimeZoneRegion",  tzsplit[0])
    else:
        update_db("HostTimeZoneRegion",  tzsplit[0])
        tztemp="HostTimeZoneRegion_%s" % tzsplit[0]
        update_db(tztemp, tzsplit[2])



def cp_and_log(srcfile, destfile):
    #return
    if not os.path.exists(srcfile):
        logging.info("%s is not present,  skipping...", srcfile)
    else:
        cmd = ("rsync -arvp %s %s") %(srcfile, destfile)
        runcmd(cmd)

def cp_and_log2(srcfile, destfile, fileglob):
    #return
    logging.debug("cp_and_log2")
    logging.debug("%s,  %s,  %s", srcfile, destfile,  fileglob)

    if not os.path.exists(srcfile):
        logging.info("%s is not present,  skipping...", srcfile)
    else:
        if fileglob == '':
            cmd = ("rsync -arvp %s %s") %(srcfile, destfile)
            runcmd(cmd)
        else:
            fileglob = "*"
            cmd = ("rsync -arvp %s/%s %s") %(srcfile, fileglob, destfile)
            runcmd(cmd)

def runcmd(cmd):
    if data_config.NOOPDEBUG == "FALSE":
        pass
    else:
        cmd = "echo "+cmd
    logging.debug("    %s", cmd)
    cmdout = commands.getoutput(cmd)
    logging.debug("    %s", cmdout)
    return cmdout

def mysqldb(cmd, inchroot):
    if cmd == "start":
        mycmd = "  /etc/rc.d/mysqld start"
    elif cmd == "stop":
        mycmd = "  /etc/rc.d/mysqld stop"
    if inchroot == "chroot":
        mycmd = "  chroot %s" %mycmd
    runcmd(mycmd)


def mount_bind_chroot():
    logging.debug("Mounting dev/proc/sysfs for chroot")
    cmd = "  mount --bind /dev %s" %data_config.MOUNTPOINT+"/dev"
    runcmd(cmd)
    cmd = "  mount --bind /proc %s" %data_config.MOUNTPOINT+"/proc"
    runcmd(cmd)
    cmd = "  mount -t sysfs none  %s" %data_config.MOUNTPOINT+"/sys"
    runcmd(cmd)

def umount_bind_chroot():
    logging.debug("UnMounting dev/proc/sysfs for chroot")
    cmd = "  umount -l %s" %data_config.MOUNTPOINT+"/dev"
    runcmd(cmd)
    cmd = "  umount -l %s" %data_config.MOUNTPOINT+"/proc"
    runcmd(cmd)
    cmd = "  umount -l %s" %data_config.MOUNTPOINT+"/sys"
    runcmd(cmd)


def partitions_removeall(diskdevice, label):
    logging.info("Removing all partitions for %s  %s", label, diskdevice)
    device = parted.getDevice(diskdevice)
    partdisk = parted.Disk(device)
    partdisk.deleteAllPartitions()
    if data_config.NOOPDEBUG == "FALSE":
        partdisk.commit()

    for partition in partdisk.partitions:
        print "type: %s" %partition.type

def create_partitions(diskdevice, size, ptype, startsector):
    logging.debug("_____Create partitions______")
    if size == "NO":
        logging.info("Size is 0,  skipping")
        return "NO"
    partlist = []
    newstart = 0
    totalused = 0
    device = parted.getDevice(diskdevice)
    partdisk = parted.Disk(device)
    for partition in partdisk.partitions:
        if partition.type != parted.PARTITION_FREESPACE:
            partlist.append((partition,
                        partition.path,
                        partition.getFlag(parted.PARTITION_BOOT),
                        partition.geometry.start,
                        partition.geometry.end,
                        partition.geometry.length,
                        partition.type,
                        partition.fileSystem))
    for slice in partlist:
        (usedpartition,  usedpath,  usedbootable,  usedstart,  usedend,  usedlength,  usedtype,  usedfs) = slice
        #Start the new partition one after the end of last
        newstart = usedend+1

    if  startsector == 0:
        newstart = 0
    if size == "ALL":
        logging.debug("     Using the rest of the disk  %s", (device.length-newstart) )
        try:
            geom = parted.Geometry(device=device,  start=newstart,  length=(device.length-newstart))
        except:
            logging.info("An error occured,  probably invalid parition size")
            return
    else:
        # convert size in MB to a length on the device in sectors
        length = (int(size) * (1024 * 1024)) / device.sectorSize
        logging.debug("Size is %s", length)
        try:
            geom = parted.Geometry(device=device,  start=newstart,  length=length)
        except:
            logging.info("An error occured,  probably invalid parition size")
            return
    #collect device constraint
    constraint = device.getConstraint()
    # new partition
    if ptype == "NORMAL":
        newpart = parted.Partition(disk=partdisk, type=parted.PARTITION_NORMAL,  geometry=geom)
    elif ptype == "SWAP":
        newpart = parted.Partition(disk=partdisk, type=parted.PARTITION_NORMAL,  geometry=geom)

    #add the partition to the disk and commit changes
    partdisk.addPartition(partition=newpart,  constraint=constraint)
    if data_config.NOOPDEBUG == "FALSE":
        partdisk.commit()
    logging.info("created partition %s of %dMB and added it to %s" %
                (newpart.getDeviceNodeName(),  newpart.getSize(),  diskdevice))
    return newpart.getDeviceNodeName()


def partition_disk():
    global hostoptions
    logging.info("Partitioning")
    logging.debug("____start of partition_disk____")
    rootdisk = hostoptions["rootdisk"]
    datadisk = hostoptions["datadisk"]
    label = "root"
    partitions_removeall("/dev/"+rootdisk, label)
    label = "data"
    partitions_removeall("/dev/"+datadisk, label)
    hostoptions["rootpartition"] = create_partitions("/dev/"+rootdisk, hostoptions["rootsize"], "NORMAL", 0)
    hostoptions["swappartition"] = create_partitions("/dev/"+rootdisk, hostoptions["swapsize"], "SWAP", 1)
    if datadisk != rootdisk:
        hostoptions["datapartition"] = create_partitions("/dev/"+datadisk, hostoptions["datasize"], "NORMAL", 0)
    else:
        hostoptions["datapartition"] = create_partitions("/dev/"+datadisk, hostoptions["datasize"], "NORMAL", 1)
    time.sleep(5)

def fscmd(fstype):
    fscmds = {"reiserfs":"mkreiserfs -q -l ROOT",  "xfs": "mkfs -t xfs -f",  "ext3": "mkfs.ext3", "jfs":"mkfs.jfs -q", "ext4":"mkfs.ext4", "Do_not_format":"noformat", "no_format":"noformat"}
    try:
        rc = fscmds[fstype]
    except:
        logging.critical(" %s is not a valid fs type,  exiting now", fstype)
        sys.exit(3)
    return rc


def format_disk(install_type):
    logging.info("______Starting Disk Format______")
    rootfs = fscmd(hostoptions["rootfs"])
    datafs = fscmd(hostoptions["datafs"])

    rootdisk = hostoptions["rootdisk"]
    datadisk = hostoptions["datadisk"]
    rootpartition = hostoptions["rootpartition"]
    datapartition = hostoptions["datapartition"]
    if install_type != "upgrade":
        swapsize = hostoptions["swapsize"]
        swappartition = hostoptions["swappartition"]

    logging.debug("     Format command for rootfs  %s : %s ",  rootfs, rootpartition)
    if ( rootfs != "noformat"):
        logging.info("Starting format of %s", rootpartition)
        cmd = "  %s /dev/%s" %( rootfs,  rootpartition)
        #os.system(cmd)
        runcmd(cmd)
    else:
        logging.info("Will not format root partition: %s", rootpartition)

    logging.debug("     Format command for datafs  %s : %s ",  datafs, datapartition)
    if (datafs != "noformat"):
        logging.info("Starting format of %s", datapartition)
        cmd = "  %s /dev/%s" %( datafs,  datapartition)
        #os.system(cmd)
        runcmd(cmd)
    else:
        logging.info("Will not format data partition: %s",  datapartition)

    if install_type == "install":
        if (hostoptions["swapsize"] != "NO"):
            logging.info("Starting format for swap %s", swappartition)
            cmd = "  mkswap /dev/%s" % swappartition
            #os.system(cmd)
            runcmd(cmd)
        else:
            logging.debug("     Swap is set to NO,  will not run mkswap")

    logging.debug("_____End of format______")


def mount_it():
    logging.info("______Mounting disk______")
#   Create mount points
    try:
        mountpoint = data_config.MOUNTPOINT
        mp = mountpoint
        logging.info("Creating mountpoints %s", mp)
        os.makedirs(mp)
    except OSError:
        logging.debug("     Could not create %s", mp)

#   Mount root
    cmd = "mount /dev/%s %s"  %(hostoptions["rootpartition"], mountpoint)
    runcmd(cmd)
    #logging.debug(cmd)
    #cmdout=commands.getoutput(cmd)
    #logging.debug(cmdout)
#   Mount data
#make mountpoint after mounting /
    try:
        mountpoint = data_config.MOUNTPOINT
        datapoint = data_config.DATAMOUNT
        mp = mountpoint+datapoint
        logging.info("Creating mountpoints %s", mp)
        os.makedirs(mp)
    except OSError:
        logging.debug("     Could not create %s", mp)

    cmd = "mount /dev/%s %s"  %(hostoptions["datapartition"], mp)
    runcmd(cmd)
    #logging.debug(cmd)
    #cmdout=commands.getoutput(cmd)
    #logging.debug(cmdout)

def unmount_it():
    logging.info("______Unmounting disk______")
    cmd = "umount  %s"  %(data_config.MOUNTPOINT+data_config.DATAMOUNT)
    runcmd(cmd)
    time.sleep(2)

    cmd = "swapoff /dev/%s" %(hostoptions["swappartition"])
    runcmd(cmd)

    cmd = "sync"
    runcmd(cmd)

    cmd = "umount   %s"  %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "sync"
    runcmd(cmd)


def create_squashlist():
    logging.debug("Creating squashlist")
    squashfile = data_config.SQUASHFILE
    f = open(squashfile,  'w')
    for i in data_config.SQUASHLIST:
        f.write(i)
        f.write("\n")
        logging.debug(i)
    f.close()


def copy_it(install_type):
    logging.info("______Transferring to disk______")
    logging.debug(  install_type)
    if ( install_type == "install"):
        logging.info("Transferring system")
        cmd = "  unsquashfs -f -d %s /.livesys/medium/system.sqf" %(data_config.MOUNTPOINT)
        runcmd(cmd)
        #logging.debug(cmd)
        #cmdout=commands.getoutput(cmd)

    if ( install_type == "upgrade"):
        logging.info("Upgrading system")
        create_squashlist()
        cmd = "  unsquashfs -e %s -f -d %s /.livesys/medium/system.sqf" %(data_config.SQUASHFILE,  data_config.MOUNTPOINT)
        runcmd(cmd)
        #logging.debug(cmd)
        #cmdout=commands.getoutput(cmd)
#   Create the missing dir
    i = ("sys", "proc", "dev", "tmp", "mnt", "media", "media/cdrom", "media/dvd", "var/log/mythtv")
    mountpoint = data_config.MOUNTPOINT
    for item in i:
        try:
            mp = mountpoint+"/"+item
            logging.info("Creating mountpoints %s", mp)
            os.makedirs(mp)
        except OSError:
            logging.debug("     __Could not create %s", mp)
#   General fixup
    cmd = "chmod 777 %s/tmp" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "mknod %s/dev/null c 1 5" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "mknod %s/dev/console c 5 1" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "chmod +s  %s/usr/bin/Xorg" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "chmod +s  %s/usr/bin/crontab" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "chmod +s  %s/usr/bin/sudo" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    cmd = "chmod +s  %s/bin/mount" %(data_config.MOUNTPOINT)
    runcmd(cmd)
    logging.debug("__End of copy_it__")

def create_fstab(extralines):
    logging.debug("______Creating new fstab file_______")
    logging.info("Creating new fstab file")
    fstabfile = data_config.MOUNTPOINT+"/etc/fstab"
    fstab_list = []
    f = open(fstabfile,  'w')
    line = '''# <file system>        <dir>         <type>    <options>          <dump> <pass> \n'''
    fstab_list.append(line)
    line = '''none                   /dev/pts      devpts    defaults            0      0 \n'''
    fstab_list.append(line)
    line = '''none                   /dev/shm      tmpfs     defaults            0      0\n'''
    fstab_list.append(line)
    line = '''/dev/cdrom /media/cdrom   auto    ro,user,noauto,unhide   0      0\n'''
    fstab_list.append(line)
    line = '''/dev/dvd /media/dvd   auto    ro,user,noauto,unhide   0      0\n'''
    fstab_list.append(line)
    line = '''UUID=ROOTUID /  auto defaults,noatime 0 1\n'''
    fstab_list.append(line)
    line = '''UUID=DATAUID %s  auto defaults,noatime 0 1\n''' %(data_config.DATAMOUNT)
    fstab_list.append(line)
    line = '''UUID=SWAPUID swap swap defaults  0 0 \n'''
    fstab_list.append(line)
    for vline in extralines:
        fstab_list.append(vline)
    for outline in fstab_list:
        logging.debug(outline)
        f.write(outline)
    f.close()

def find_uuid(partition):
    logging.info("Finding the UUID for %s...", partition)
    cmd = "blkid -s UUID /dev/%s" %partition
    tmpuuid = runcmd(cmd)
    splituuid = tmpuuid.partition("=")
    uuid = splituuid[2].replace('"', "")
    logging.info("The uuid is %s", uuid)
    return uuid.strip()

def pick_out_vg():
    logging.info("Searching for Volume Groups in old fstab")
    vglines = []
    f = open("/tmp/etc/fstab",  'r')
    oldfscontents = f.readlines()
    for line in oldfscontents:
        if line.startswith("/dev/vg"):
            vglines.append(line)
            templine = line.split()
            mdir = templine[1]
            if not os.path.exists (mdir):
                logging.debug("Creating dir %s for VG mount", mdir)
                os.makedirs (mdir)
#               Might need to os.chown to mythtv:users
            else:
                logging.debug("Directory %s for VG mount already present", mdir)
    return vglines

def fstab_it(install_type):
    logging.info("______Checking fstab______")
    kmvg = []
    fstabfile = data_config.MOUNTPOINT+"/etc/fstab"
#   Check for knoppmyth install,  if found create new
    if install_type == "upgrade":
        if os.path.exists("/tmp/etc/KnoppMyth-version"):
            logging.debug("     KnoppMyth-Version found,  creating new fstab")
            kmvg = pick_out_vg()
            create_fstab(kmvg)
        elif os.path.exists("/tmp/etc/fstab"):
            logging.debug("     Upgrade and not Knoppmyth,  using old fstab")
            cp_and_log("/tmp/etc/fstab", fstabfile)
#   Catch all for creating new fstab
    if not os.path.exists(data_config.MOUNTPOINT+"/etc"):
        os.makedirs(data_config.MOUNTPOINT+"/etc")
    if not os.path.exists(fstabfile):
        create_fstab(kmvg)

    logging.info("____UUID check for %s",  "swap")
    swapuuid = find_uuid(hostoptions["swappartition"])

    logging.info("____UUID check for %s",  "data")
    datauuid = find_uuid(hostoptions["datapartition"])

    logging.info("____UUID check for %s",  "root")
    rootuuid = find_uuid(hostoptions["rootpartition"])

    fstabfile = data_config.MOUNTPOINT+"/etc/fstab"
    logging.info("Correcting UUID's in %s", fstabfile)
    f = open(fstabfile,  'r')
    oldfscontents = f.readlines()
    newfstab = []
    f.close()
    for line in oldfscontents:
        if line.startswith("UUID"):
            templine = line.split()
            if ( templine[1] == "/"):
                logging.debug("     Found Root fstab line:")
                logging.debug(      templine)
                templine[0] = "UUID=%s" %(rootuuid)
                newline = ''
                for i in templine:
                    newline+=i
                    newline+="    "
                newline+="\n"
                logging.debug("     New fstab line:")
                logging.debug(      newline)
                newfstab.append(newline)

            if ( templine[1] == data_config.DATAMOUNT):
                logging.debug("     Found DATA mount")
                logging.debug(      templine)
                templine[0] = "UUID=%s" %(datauuid)
                newline = ''
                for i in templine:
                    newline+=i
                    newline+="    "
                newline+="\n"
                logging.debug("     New fstab line:")
                logging.debug(      newline)
                newfstab.append(newline)

            if ( templine[1] == "swap"):
                if len(swapuuid) <= 5:
                    logging.debug("     swapp uuid is to small")
                else:
                    logging.debug("     Found swap partition")
                    logging.debug(      templine)
                    templine[0] = "UUID=%s" %(swapuuid)
                    newline = ''
                    for i in templine:
                        newline+=i
                        newline+="    "
                    newline+="\n"
                    logging.debug("     New fstab line:")
                    logging.debug(      newline)
                    newfstab.append(newline)
        else:
            logging.debug("     Line didn't match,  adding to newfstab:")
            logging.debug(      line)
            newfstab.append(line)
    logging.info("Writing out newfstab")
    logging.debug("______This is the new fstab_____")
    f = open(fstabfile,  'w')
    for line in newfstab:
        logging.debug(line)
        f.write(line)
        #f.write("\n")
    f.close()

def grub_it():
    logging.info("______Start of grub install______")
    cmd = "  grub-install --recheck --no-floppy --root-directory=%s \"(hd0)\" "  % data_config.MOUNTPOINT
    logging.info("Running grub-install")
    runcmd(cmd)
    rootuuid = find_uuid(hostoptions["rootpartition"])
    cmd = "  mkinitcpio -g %s/boot/kernel26.img" % data_config.MOUNTPOINT
    logging.info("Running mkinitcpio")
    runcmd(cmd)
    logging.info("Adding root uuid to grub menu")
    grubfile = data_config.MOUNTPOINT+"/boot/grub/menu.lst"
    try:
        f = open(grubfile,  'r')
        oldgrub = f.readlines()
        newgrub = []
        f.close()
        for line in oldgrub:
            if line.startswith("kernel"):
                templine = line.split()
                logging.debug("     Found kernel Root grubline:")
                logging.debug(      templine)
                templine[2] = "root=/dev/disk/by-uuid/%s" %(rootuuid)
                newline = ''
                for i in templine:
                    newline+=i
                    newline+="    "
                newline+="\n"
                logging.debug("     New grub menu.lst  line:")
                logging.debug(      newline)
                newgrub.append(newline)
            else:
                logging.debug("Line didn't match,  adding to newgrub:")
                logging.debug(      line)
                newgrub.append(     line)
        logging.info("Writing out new grub file")
        logging.debug("______This is the new grub_____")
        f = open(grubfile,  'w')
        for line in newgrub:
            logging.debug(line)
            f.write(line)
        f.close()
    except:
        logging.debug("Couldn't open grub file")

def fix_permissions():
    logging.info("Fixing permissions")
    SE = os.environ["TEMPLATES"]+"/settings/syssettings"
    cmd = "  chmod -R 755  %s" %(data_config.MOUNTPOINT+SE)
    runcmd(cmd)
    cmd = "  chmod 755  %s" %(data_config.MOUNTPOINT+"/etc/systemconfig")
    runcmd(cmd)
    cmd = "  chmod 755  %s" %(data_config.MOUNTPOINT+"/root")
    runcmd(cmd)
    cmd = "  chown root:mythtv  %s" %(data_config.MOUNTPOINT+"/etc/systemconfig")
    runcmd(cmd)
    cmd = "  chown -R mythtv:mythtv  %s" %(data_config.MOUNTPOINT+SE)
    runcmd(cmd)
    cmd = "  chown -R mythtv:mythtv  %s" %(data_config.MOUNTPOINT+"/var/log/mythtv")
    runcmd(cmd)

def apply_new_auth():
    logging.info("_____Applying Password updates_______")
    passfile = "/root/myth_user_call.out"
    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("MVROOT was not defined,  using the default value")
        MVROOT = "/usr/MythVantage"
    if data_config.NOOPDEBUG == "FALSE":
        cmdprefix = "chroot " + data_config.MOUNTPOINT + " " + MVROOT+"/bin/myth_user_call -i "
    else:
        cmdprefix = "echo chroot " + data_config.MOUNTPOINT + " " + MVROOT+"/bin/myth_user_call -i "
    try:
        f = open(passfile,  'r')
        passcmds = f.readlines()
        f.close()
        for cmd in passcmds:
            execmd = cmdprefix+cmd
#            Using os.system because runcmd fails
            logging.debug(execmd)
            os.system(execmd)
    except:
        logging.debug("Applying password updates failed,  couldn't open %s", passfile)
    logging.debug

def add_to_blacklist(module):
    rcfile = data_config.MOUNTPOINT + "/etc/rc.conf"
    logging.debug("    Attempting to add %s to blacklist", module)
    newline=''
    try:
        f = open(rcfile, 'r')
        conflines = f.readlines()
        f.close()
    except:
        logging.critical("    *Couldn't open %s for reading",rcfile)
        return

    try:
        f = open(rcfile, 'w')
    except:
        logging.critical("    *Couldn't open %s for reading",rcfile)
        return

    for line in conflines:
        newline = line
        if re.match("MOD_BLACKLIST",line):
            logging.debug(line)
            try:
                lastpar = line.rindex(')')
                logging.debug("    found ) at %s", lastpar)
                newline = line[:lastpar] +" !" + module + " " + line[lastpar:]
                logging.debug(newline)
            except:
                logging.debug("Couldn't find ending )")
                newline = line
        f.write(newline)
    f.close()

def special_hardware_check():
    logging.info("_____Applying special boot parameters_______")
    try:
        f = open('/proc/cmdline', 'r')
        bootoptions = f.readlines()
        f.close()
    except:
        logging.critical("    *Couldn't open /proc/cmdline")
        return
    bootoptions =  bootoptions[0].split()
    logging.debug("    Boot options: %s", bootoptions)
    for item in bootoptions:
        logging.debug(item)
        if re.match("disablemodules",item) != None :
            logging.debug("    Found disabledmodules")
            modulelist = item.split("=")[1]
            for module in modulelist.split(','):
                add_to_blacklist(module)


def swapsearch():
#currently unused!
    partlist = []
    stddir = os.getcwd()
    os.chdir("/sys/block")
    partitionlist = glob.glob("*")
    for item in partitionlist:
        try:
            newitem = item.strip().rpartition(" ")[2]
            if (not newitem == '') and  (not newitem.startswith("loop")):
                path = "/dev/"+newitem.strip()
                path = path.strip()
                device = parted.getDevice(path)
                (cylinders,  heads,  sectors) = device.biosGeometry
                sizeInBytes = device.length * device.sectorSize
                disk = parted.Disk(device)
                for partition in disk.partitions:
                    if partition.type == parted.PARTITION_PROTECTED or \
                    partition.type == parted.PARTITION_METADATA or \
                    partition.type == parted.PARTITION_FREESPACE:
                        continue

                    partlist.append((partition,
                                    partition.path,
                                    partition.getFlag(parted.PARTITION_BOOT),
                                    partition.geometry.start,
                                    partition.geometry.end,
                                    partition.geometry.length,
                                    partition.type,
                                    partition.fileSystem))
                for slice in partlist:
                    (partition,  path,  bootable,  start,  end,  length,  type,  fs) = slice
                    if partition.getFlag(parted.PARTITION_SWAP) or fs.type == "linux-swap":
                        print "found swap"
                        print path
        except:
            pass
    os.chdir(stddir)

def sane_settings(file):
    #Remove some settings from file_name
    removeline = ("HOSTrootfstype",  "HOSTrootfstype",  "HOSTdatafstype",  "HOSTOSsize",  "HostUseALLdata",  "HOSTDATAsize",  "HOSTuprootfstype",  "HostUseSWAP",  "HOSTSWAPsize")
    logging.debug("__Running sane settings")
    try:
        f = open(file,  'r')
        filecontents = f.readlines()
        f.close()
    except:
        logging.debug("     Couldn't find file %s to sane", file)
    try:
        f = open(file, 'w')
        for line in filecontents:
            for item in removeline:
                if line.startsize(line.strip()):
                    logging.debug("     Found a line to remove in %s,  %s", file, line)
                else:
                    f.write(line)
    except:
        logging.debug("     Couldn't open file %s for writing", file)
    logging.debug("     __End  sane settings")

def restore_default_settings():
    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("     MVROOT was not defined,  using the default value")
        MVROOT = "/usr/MythVantage"
    logging.info("Saving syssettings")
    cmd = "%s/bin/restore_default_settings.sh -c save -t syssettings -h %s -d localhost" %(MVROOT, data_config.MVHOSTNAME)
    runcmd(cmd)
    SE = os.environ["TEMPLATES"]+"/settings/syssettings/"
    cp_and_log(SE, data_config.MOUNTPOINT+SE)
    cp_and_log("/etc/mtab", data_config.MOUNTPOINT+"/etc/mtab")
    copy_updates()
    fix_permissions()

def full_install(hostoptions):
    logging.info("______Start of full install______")
    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("     MVROOT was not defined,  using the default value")
        MVROOT = "/usr/MythVantage"

    try:
        os.remove("/tmp/.this_is_upgrade")
    except OSError:
        logging.debug("     File /tmp/.this_is_upgrade not present,  couldn't delete it")
        pass
#   Partition disk
    statusmsg = "Partitioning %s"  %( hostoptions["rootdisk"])
    update_status(statusmsg)
    progress(1)
    if data_config.NOOPDEBUG == "FALSE":
        partition_disk()
    else:
        logging.debug("     Debug mode,  skipping partitioning step")

#   Format disk
    statusmsg = "Preparing  %s"  %( hostoptions["rootdisk"])
    update_status(statusmsg)
    progress(2)
    format_disk("install")


#   Mount partitions
    statusmsg = "Mounting  %s"  %( hostoptions["rootdisk"])
    update_status(statusmsg)
    progress(3)
    mount_it()

#   Find number of bytes written to disk before starting copy.  This is used
#   to have a somewhat decent progress indication.
    statgrab( hostoptions["rootdisk"])
    msg = "Creating %s" %(systemconfig["hostname"])
    update_status(msg)

#   Copy system to disk
    copy_it("install")
#   Remove old fstab so that a new one is created
    fstabfile = data_config.MOUNTPOINT+"/etc/fstab"
    try:
        os.remove(fstabfile)
    except OSError:
        logging.debug("     ERROR: deleting %s", fstabfile)
    fstab_it("full_install")
#   Configure system
    msg = "Configuring system"
    update_status(msg)
    progress(98)
    grub_it()
    special_hardware_check()
#   Configuring the system
    logging.info("______Configuring system________")
    cp_and_log("/etc/systemconfig", data_config.MOUNTPOINT+"/etc/systemconfig")
    cp_and_log("/root/xorg.conf.install", data_config.MOUNTPOINT+"/etc/X11/xorg.conf.install")
    restore_default_settings()
    #try:
        #MVROOT=os.environ["MV_ROOT"]
    #except:
        #logging.debug("     MVROOT was not defined,  using the default value")
        #MVROOT="/usr/MythVantage"
    #logging.info("Saving syssettings")
    #cmd="%s/bin/restore_default_settings.sh -c save -t syssettings -h %s -d localhost" %(MVROOT, data_config.MVHOSTNAME)
    #runcmd(cmd)
    #SE=os.environ["TEMPLATES"]+"/settings/syssettings"
    #cp_and_log(SE, data_config.MOUNTPOINT+SE)
    #cp_and_log("/etc/mtab", data_config.MOUNTPOINT+"/etc/mtab")
    #cp_and_log2(MVROOT+"/bin/", data_config.MOUNTPOINT+MVROOT+"/bin/", "*.sh")
    #cp_and_log2(MVROOT+"/bin/", data_config.MOUNTPOINT+MVROOT+"/bin/", "*.py")
    #fix_permissions()
    apply_new_auth()
    if ( systemconfig["SystemType"] == "Master_backend" or  systemconfig["SystemType"] == "Standalone" ):
#       This install will need a DB,  so install it
        logging.info("______Installing Database in CHROOT________")
        mysqldb("stop", '')
        mount_bind_chroot()
        cmd = "  chroot %s %s/bin/install_db_chroot.sh |tee /tmp/chrootdb.out" %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)
        logging.info("Running systemconfig in chroot")
        #cmd = "  chroot %s %s/bin/systemconfig.sh misc, hostype, network, advanced, user, this_is_install" %(data_config.MOUNTPOINT, MVROOT)
        cmd = "  chroot %s %s/bin/systemconfig.sh all , this_is_install" %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)
        mysqldb("stop", "chroot")
        kill_dhcp_chroot()
        logging.info("____End Database in CHROOT____")
        mysqldb("start", '')
        umount_bind_chroot()
    else:
        logging.info("______No database required,  continuing configuration________")
        mount_bind_chroot()
        cmd = "  chroot %s DISPLAY=127.0.0.1:0 %s/bin/MythVantage -t restore, default 1" %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)
#       Need to check for to touch  /tmp/.dbsysfailed
        cmd = "  chroot %s %s/bin/restore_default_settings.sh -c restore  -t syssettings " %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)
        if ( 'x' == '1' ):
            logging.debug("touching /tmp/.dbsysfailed")
        else:
            #cmd = "  chroot %s %s/bin/systemconfig.sh misc, hostype, network, advanced, user, this_is_install" %(data_config.MOUNTPOINT, MVROOT)
            cmd = "  chroot %s %s/bin/systemconfig.sh all , this_is_install" %(data_config.MOUNTPOINT, MVROOT)
            runcmd(cmd)
            cmd = "  chroot %s %s/bin/restore_default_settings.sh -c ACCESSCONTROL " %(data_config.MOUNTPOINT, MVROOT)
            runcmd(cmd)
        umount_bind_chroot()
    #cmd = "  chroot %s %s/bin/systemconfig.sh advanced, user" %(data_config.MOUNTPOINT, MVROOT)
    #runcmd(cmd)
    cmd = "  touch %s%s/.configure" %(data_config.MOUNTPOINT, data_config.MYTHHOME)
    runcmd(cmd)
    cmd = "  chmod 777 %s%s/.configure" %(data_config.MOUNTPOINT, data_config.MYTHHOME)
    runcmd(cmd)
    msg = "Done"
    update_status(msg)
    cp_and_log("/tmp/mythvantage_install.log", data_config.MOUNTPOINT+"/var/log/mythvantage_install.log")
    cp_and_log("/tmp/mv_debug.log", data_config.MOUNTPOINT+"/var/log/mv_debug.log")
    unmount_it()
    logging.debug("_____End of full install_____")

def find_upgrade():
    logging.info("_____Start of find_upgrade_____")
    global hostoptions
    #try to mount first partition of disk
    #copy old /etc/systemconfig to live,  import settings into db
    #umount drive.
    #check for clean upgrade
    if ( clean_upgrade() ):
        logging.info("Clean upgrade requested,  not using old data")
        return
    mount_it()
    oldbackupfile = data_config.MOUNTPOINT+"/root/backup/"+data_config.BACKUPFILE
    newbackupfile = "/tmp/"+data_config.BACKUPFILE
    if os.path.exists(oldbackupfile):
        logging.debug("Setting backup file to %s", newbackupfile)
        hostoptions["backupfile"] = newbackupfile
        cp_and_log(oldbackupfile, newbackupfile)

    srcfile = data_config.MOUNTPOINT+"/etc/systemconfig"
    logging.info("Searching for systemconfig file %s", srcfile)
    if os.path.exists(srcfile):
        logging.info("Found systemconfig file %fs", srcfile)
        TEMPLATES = os.environ["TEMPLATES"]+"/settings/syssettings"
        cp_and_log2(data_config.MOUNTPOINT+TEMPLATES, data_config.TEMP_TEMPLATES, '')
        sane_settings("/tmp/templates/settings/syssettings/settings.txt")
        cp_and_log2(data_config.TEMP_TEMPLATES, TEMPLATES, '')
        cp_and_log(srcfile, "/etc/systemconfig")
        cp_and_log(data_config.MOUNTPOINT+"/etc/", "/tmp/etc/")
        cp_and_log(data_config.MOUNTPOINT+"/var/lib/oss/", "/tmp/oss")
        cp_and_log("/tmp/etc/mdadm.conf", "/etc/mdadm.conf")
    else:
        logging.info("Could not find systemconfig file %s", srcfile)
        cp_and_log(data_config.MOUNTPOINT+"/etc/", "/tmp/etc/")
        cp_and_log("/tmp/etc/mdadm.conf", "/etc")
        timezone_to_db("/tmp/etc/timezone")
    unmount_it()
    logging.debug("End of find_upgrade")

def upgrade_mount_search():
    #Search for data/myth partition based on contents of fstab
    logging.debug("______Start of upgrade_mount_search_____")
    cmd = "umount %s%s"  %(data_config.MOUNTPOINT, data_config.DATAMOUNT)
    runcmd(cmd)
    fstab_list = []
    fstab_data_mount = ''
    try:
        f = open("/tmp/etc/fstab",  'r')
        oldfscontents = f.readlines()
        f.close()
        ##    ['UUID=',  '/myth',  'auto',  'defaults, noatime',  '0',  '1']
        #for line in oldfscontents:
            #mountdir=line.split()
            #if mountdir[1] == (data_config.DATAMOUNT):
                #fstablist.append(mountdir)
    except:
        logging.debug("    Couldn't open /tmp/etc/fstab")
    for line in oldfscontents:
        if line.startswith("#"):
            continue
        mountdir=line.split()
        try:
            if mountdir[1] == (data_config.DATAMOUNT):
                fstablist.append(mountdir)
        except:
            continue
    #search fstab for data/myth mountpoint
    for i in fstab_list:
        if i[1] == data_config.DATAMOUNT:
            fstab_data_mount = i[0]
            break

    #start software raid support if needed
    for i in fstab_list:
        if i[0].startswith("/dev/md"):
            logging.debug("    starting software raid support")
            mdadm_assemble_all


    if re.search("UUID", fstab_data_mount):
        fstab_data_uuid = fstab_data_mount.split("=")[1]
        cmd = "blkid -t UUID=%s" %fstab_data_uuid
        retcode = call(cmd,  shell=True)
        if retcode != 0:
            logging.debug("    Couldn't find uuid %s,  starting md support", fstab_data_uuid)
            mdadm_assemble_all
        #hoping everything is up and running and data/myth will be available for mount
        cmd = "mount -U %s %s" %(fstab_data_uuid, data_config.MOUNTPOINT)
        runcmd(cmd)
        cmd = "blkid -t UUID=%s|cut -d: -f1" %fstab_data_uuid
        logging.debug(cmd)
        datadisk = commands.getoutput(cmd)
        cmd = "basename %s" %datadisk
        datadisk = commands.getoutput(cmd)
    else:
        cmd = "mount  %s %s" %(fstab_data_mount, data_config.MOUNTPOINT)
        runcmd(cmd)
        cmd = "basename %s" %fstab_data_mount
        datadisk = commands.getoutput(cmd)
    time.sleep(3)
#    hostoptions["backupfile"]=data_config.BACKUPPATH+data_config.BACKUPFILE
    if not backup_sql_check():
        newbackupfile = "/tmp/"+data_config.BACKUPFILE
        if os.path.exists(newbackupfile):
            logging.debug("Setting backup file to %s", newbackupfile)
            cp_and_log(newbackupfile, data_config.MOUNTPOINT+newbackupfile)
            hostoptions["backupfile"] = newbackupfile
        else:
            logging.info("Couldn't find any database to restore,  upgrade will continue with a new database")



def upgrade(hostoptions):

    try:
        MVROOT = os.environ["MV_ROOT"]
    except:
        logging.debug("MVROOT was not defined,  using the default value")
        MVROOT = "/usr/MythVantage"
    logging.info("______Start of upgrade______")
    cmd = "touch /tmp/.this_is_upgrade"
    runcmd(cmd)
    if hostoptions["rootfs"] == "Do_not_format":
        logging.info("Will not format root filesystem")
    else:
        statusmsg = "Preparing  %s"  %( hostoptions["rootdisk"])
        update_status(statusmsg)
        progress(2)
        format_disk("upgrade")
    #sys.exit(2)
    statusmsg = "Mounting  %s"  %( hostoptions["rootdisk"])
    update_status(statusmsg)
    progress(3)
    mount_it()
    hostoptions["backupfile"] = data_config.BACKUPPATH+data_config.BACKUPFILE
    statgrab( hostoptions["rootdisk"])
    msg = "Upgrading %s" %(systemconfig["hostname"])
    update_status(msg)
    time.sleep(3)
    copy_it("upgrade")
    cmd = "rm -rf %s/etc.old" %data_config.MOUNTPOINT
    runcmd(cmd)

    cp_and_log("/tmp/etc/", data_config.MOUNTPOINT+"/etc.old/")
    cp_and_log("/tmp/oss", data_config.MOUNTPOINT+"/var/lib/oss.old")
    srcfile = "%s/etc.old/ssh/" %(data_config.MOUNTPOINT)
    destfile = "%s/etc/ssh/" %(data_config.MOUNTPOINT)
    cp_and_log2(srcfile, destfile, '*.pub')
    cp_and_log2(srcfile, destfile, '*.key')
    cp_and_log2(srcfile, destfile, '*key')
    mdfile = mdadm_find("/tmp")
    cp_and_log("/tmp"+mdfile, data_config.MOUNTPOINT+"/etc")
    srcfile = "%s/etc.old/asound.state" %(data_config.MOUNTPOINT)
    destfile = "%s/etc/asound.state" %(data_config.MOUNTPOINT)
    cp_and_log(srcfile, destfile)
    if not os.path.exists("/tmp/etc/KnoppMyth-version"):
        logging.debug("    standard upgrade,  restoring auth files")
        cp_and_log(data_config.MOUNTPOINT+"/etc.old/passwd", data_config.MOUNTPOINT+"/etc/passwd")
        cp_and_log(data_config.MOUNTPOINT+"/etc.old/shadow", data_config.MOUNTPOINT+"/etc/shadow")
        cp_and_log(data_config.MOUNTPOINT+"/etc.old/group", data_config.MOUNTPOINT+"/etc/group")
    msg = "Configuring system"

    update_status(msg)
    progress(98)
    logging.info("______Configuring system________")
    cp_and_log("/etc/systemconfig", data_config.MOUNTPOINT+"/etc/systemconfig")
    if not backup_sql_check():
        upgrade_mount_search()
    fstab_it("upgrade")
    time.sleep(1)
    grub_it()
    time.sleep(1)
    #needed to get around a bug with pacman
    special_hardware_check()
    cp_and_log("/root/xorg.conf.install", data_config.MOUNTPOINT+"/etc/X11/xorg.conf.install")
    cp_and_log("/etc/mtab", data_config.MOUNTPOINT+"/etc/mtab")
    if clean_upgrade() or os.path.exists("/tmp/etc/KnoppMyth-version"):
        logging.debug("clean upgrade or knoppmyth upgrade detected,  running restore settings")
        restore_default_settings()
        if clean_upgrade():
            file = "%s/%s/.kmupgrade" %(data_config.MOUNTPOINT, data_config.MYTHHOME)
            cmd = "touch %s && chmod 777 %s" %(file, file)
            runcmd(cmd)
    copy_updates()
    fix_permissions()
    mount_bind_chroot()
    logging.info("Running systemconfig in chroot")
    #cmd = "  chroot %s %s/bin/systemconfig.sh misc, hostype, network, advanced, user, this_is_install" %(data_config.MOUNTPOINT, MVROOT)
    cmd = "  chroot %s %s/bin/systemconfig.sh all , this_is_install" %(data_config.MOUNTPOINT, MVROOT)
    runcmd(cmd)
    mysqldb("stop", "")
    if ( systemconfig["SystemType"] == "Master_backend" or  systemconfig["SystemType"] == "Standalone" ):
        if clean_upgrade() or not backup_sql_check():
            logging.info("Installing new database")
            cmd = "  chroot %s %s/bin/install_db_chroot.sh |tee /tmp/chrootdb.out" %(data_config.MOUNTPOINT, MVROOT)
            runcmd(cmd)
        else:
            logging.info("Restoring database")
            cmd = "  chroot %s %s/bin/restore_km_db_chroot.sh %s|tee /tmp/chrootdb.out" %(data_config.MOUNTPOINT, MVROOT, hostoptions["backupfile"])
            runcmd(cmd)

    if os.path.exists("/tmp/etc/KnoppMyth-version"):
        cmd = "  chroot %s %s/bin/restore_default_settings.sh -c restore  -t hostsettings " %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)
        logging.debug("Correcting permissions because of km->linhes upgrade")
        cmd = "  chown -R mythtv:mythtv  %s" %(data_config.MOUNTPOINT+data_config.DATAMOUNT)
        runcmd(cmd)
        cmd = "  chown -R root:root  %s" %(data_config.MOUNTPOINT+data_config.DATAMOUNT+"/backup")
        runcmd(cmd)
    else:
        cmd = "  chroot %s %s/bin/restore_default_settings.sh -c restore  -t syssettings " %(data_config.MOUNTPOINT, MVROOT)
        runcmd(cmd)

    logging.info("Running systemconfig in chroot 2nd time")
    #cmd = "  chroot %s %s/bin/systemconfig.sh misc, hostype, network, advanced, user, this_is_install" %(data_config.MOUNTPOINT, MVROOT)
    cmd = "  chroot %s %s/bin/systemconfig.sh all , this_is_install" %(data_config.MOUNTPOINT, MVROOT)
    runcmd(cmd)
    #logging.info("Running systemconfig in chroot")
    #cmd = "  chroot %s %s/bin/systemconfig.sh advanced" %(data_config.MOUNTPOINT, MVROOT)
    #runcmd(cmd)
    mysqldb("stop", 'chroot')
    apply_new_auth()
    kill_dhcp_chroot()
    umount_bind_chroot()
    cp_and_log("/tmp/mythvantage_install.log", data_config.MOUNTPOINT+"/var/log/mythvantage_install.log")
    cp_and_log("/tmp/mv_debug.log", data_config.MOUNTPOINT+"/var/log/mv_debug.log")
    unmount_it()
    msg = "Done"
    update_status(msg)
    mysqldb("start", '')




def main(argv):
    global hostoptions
    try:
        opts,  args = getopt.getopt(argv,  'c:h',  ["help",  "rootdisk=",  "rootfs=",  "rootsize=",  "datafs=",  "datasize=",  "datadisk=",  "swapsize=",  "datapartition=" ] )
    except getopt.GetoptError, why:
        print why
        usage()
        sys.exit(2)
    hostoptions = {"op": 'null'}
    for opt,  arg in opts:
        if opt in ("-h",  "--help"):
            usage()
            sys.exit(0)
        elif opt in ( "--rootdisk"):
            hostoptions["rootdisk"] = arg
            hostoptions["rootpartition"] = arg+str(1)
        elif opt in ("--rootfs"):
            hostoptions["rootfs"] = arg
        elif opt in ("--rootsize"):
            hostoptions["rootsize"] = arg
        elif opt in ("--datafs"):
            hostoptions["datafs"] = arg
        elif opt in ("--datasize"):
            hostoptions["datasize"] = arg
        elif opt in ("--datadisk"):
            hostoptions["datadisk"] = arg
        elif opt in ("--datapartition"):
            hostoptions["datapartition"] = arg
        elif opt in ("--swapsize"):
            hostoptions["swapsize"]  = arg

        elif opt in ("-c"):
            validop = ["full_install", "upgrade", "netboot", "find_upgrade"]
            if arg not in validop:
                logging.critical("-c %s is not a valid option", arg)
                sys.exit(2)
            else:
                hostoptions["op"]  = arg
    try:
        hostoptions["datadisk"]
    except:
        hostoptions["datadisk"] = hostoptions["rootdisk"]
        hostoptions["datapartition"] = hostoptions["datadisk"]+str(3)
    try:
        hostoptions["datapartition"]
    except:
        hostoptions["datapartition"] = hostoptions["datadisk"]+str(3)
    hostoptions["swappartition"]  =  hostoptions["rootdisk"] + str(2)


    if ( hostoptions["op"]  == "full_install" ) :
        full_install(hostoptions)
    elif (hostoptions["op"]  == "upgrade" ) :
        hostoptions["datafs"] = "no_format"
        find_upgrade()
        upgrade(hostoptions)
    elif (hostoptions["op"]  == "find_upgrade" ) :
        find_upgrade()
    return hostoptions


#____________________________________________setup the logging______________________________________________________
LOG_FILENAME = '/tmp/mv_status.log'
DEBUGLOG = '/tmp/mv_debug.log'
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename=DEBUGLOG,
                    filemode='a')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
#formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
formatter = logging.Formatter("%(message)s")
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

#infoformatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
infoformatter = logging.Formatter("%(message)s")
infofile = logging.FileHandler(LOG_FILENAME, 'w')
infofile.setFormatter(infoformatter)
infofile.setLevel(logging.INFO)
logging.getLogger('').addHandler(infofile)

#Read in systemconfig
global systemconfig
systemconfig = {}
file_name = "/etc/systemconfig"
try:
    config_file = open(file_name)
except:
    logging.debug("%s could not be opened", file_name)
    config_file = ''

for line in config_file:
    line = line.strip()
    if line and line[0] is not "#" and line[-1] is not "=":
        var, val = line.rsplit("=", 1)
        val = val.strip('"')
        systemconfig[var.strip()] = val.strip()


if __name__ == "__main__":
    config_file = "mv_config"
    data_config = __import__(config_file,  globals(),  locals(),  [])
    logging.debug("___________START OF DEBUG_________________________")
    cmdoptions = main(sys.argv[1:])
    logging.debug("______cmd line options______")
    for i in cmdoptions.items():
        logging.debug (i)
    logging.debug("______systemconfig______")
    for i in systemconfig.items():
        logging.debug(i)
    logging.debug("______hostoptions______")
    for i in hostoptions.items():
        logging.debug(i)
    pass