Diff of /trunk/wharfie/wharfie.py [r26] .. [r27]  Maximize  Restore

Switch to side-by-side view

--- a/trunk/wharfie/wharfie.py
+++ b/trunk/wharfie/wharfie.py
@@ -85,8 +85,10 @@
 import os
 import re
 import sys
+import glob
 import binascii
 import argparse
+from shutil import copyfile
 from lib import makefile as make
 from lib import actions
 from lib import files
@@ -190,10 +192,28 @@
     parser.add_argument('--incremental', action='store_true', help="An experimental feature, which uses incremental backup mechanisms of tar." )
     parser.add_argument('--gen-only', action='store_true', help="Generate makefile only, but don't build it" )
     parser.add_argument('--dry-run', action='store_true', help="Generate makefile with disabled run actions and don't build it" )
+    parser.add_argument('--qemu', action='store_true', help="Enable qemu build (default if qemu support is available)" )
+    parser.add_argument('--no-qemu', action='store_true', help="Even if qemu support for wharfie is available, run without" )
+    parser.add_argument('--no-proc', action='store_true', help="suppress mounting of the proc filesystem." )
     parser.add_argument('--verbose', action='store_true', help="Print verbose make output" )
     parser.add_argument('wharfile', default='Wharfile', nargs='?', help="Filename of a 'Wharfile'. By default ./Wharfile is used." )
     args = parser.parse_args()
 
+    # check if qemu should be used
+    if os.path.isfile(os.path.abspath(os.path.dirname(sys.argv[0])) + "/qemu/qwharfie.qcow"):
+        args.qemu = True
+    else:
+        if args.qemu:
+            print("error: qemu build forced, but no qemu support available.")
+            exit(1)
+            
+    if args.no_qemu:
+        args.qemu = False
+
+    # If qemu is used, we generate only the make file. The build is then done in a second stage.
+    if args.qemu:
+        args.gen_only=True
+    
     # generate makefile
     if os.path.isfile(args.wharfile):
         read_wharfile(args.wharfile);
@@ -202,9 +222,9 @@
         exit(1)
 
     if os.path.isfile(os.path.abspath(os.path.dirname(sys.argv[0])) + "/wharfie.mk"):
-        make.write_makefile('Makefile', args.dry_run, os.path.dirname(sys.argv[0]), args.incremental);
+        make.write_makefile('Makefile', args.dry_run, os.path.dirname(sys.argv[0]), args.incremental, not args.no_proc);
     else:
-        make.write_makefile('Makefile', args.dry_run, os.path.abspath(os.path.dirname(sys.argv[0])) + "/../share/wharfie", args.incremental);
+        make.write_makefile('Makefile', args.dry_run, os.path.abspath(os.path.dirname(sys.argv[0])) + "/../share/wharfie", args.incremental, not args.no_proc);
 
     # call make
     flags=""
@@ -218,6 +238,34 @@
     elif not args.gen_only and not args.dry_run:
         os.system("make %s" % flags);
 
+    # call qemu if enabled
+    if args.qemu and not args.clean:
+        qemu = "qemu-system-x86_64"
+        disk = os.path.dirname(sys.argv[0]) + "/qemu/qwharfie.qcow"
+        cache = "qwharfie.cache.qcow"
+        input = "qwharfie.input.raw"
+        output = "qwharfie.output.raw"
+        kernel = glob.glob(os.path.dirname(sys.argv[0]) + "/qemu/boot/vmlinuz*")[0]
+        initrd = glob.glob(os.path.dirname(sys.argv[0]) + "/qemu/boot/initrd*")[0]
+        
+        uuid = open(os.path.dirname(sys.argv[0]) + "/qemu/qwharfie.qcow.uuid", "r").readline().rstrip()
+
+        # copy cache and output
+        if not os.path.isfile(cache):
+            copyfile(os.path.dirname(sys.argv[0]) + "/qemu/" + cache, cache)
+        copyfile(os.path.dirname(sys.argv[0]) + "/qemu/" + output, output)
+        copyfile(os.path.dirname(sys.argv[0]) + "/wharfie.mk", "wharfie.mk")
+
+        input_command='tar --exclude="%s" --exclude="%s" --exclude="%s" -cf %s .' % (input, output, cache, input)
+        qemu_command='%s -machine accel=kvm -m 512 -drive file=%s,index=0,media=disk,snapshot=on -drive file=%s,index=1,media=disk,snapshot=off -drive file=%s,index=2,media=disk,snapshot=off -drive file=%s,index=3,media=disk,snapshot=off -net nic,model=virtio -net user -kernel %s -initrd %s -append "root=UUID=%s ro single console=ttyS0 fsck.mode=skip systemd.unit=multi-user.target" -nographic' % (qemu, disk, cache, input, output, kernel, initrd, uuid)
+        output_command='tar -xf %s' % (output)
+        print("cmd: %s\n" % input_command)
+        os.system(input_command);
+        print("cmd: %s\n" % qemu_command)
+        os.system(qemu_command);
+        print("cmd: %s\n" % output_command)
+        os.system(output_command);
+        
         
 if __name__ == "__main__":
     main()