[ostree] ostbuild: Create deploy-qemu subcommand



commit 9aa9397e422e4c29fc54d32d52f75364a7e9e313
Author: Colin Walters <walters verbum org>
Date:   Thu May 3 11:16:08 2012 -0400

    ostbuild: Create deploy-qemu subcommand

 Makefile-ostbuild.am                               |    2 +
 gnomeos/gnomeos-qemu-create.sh                     |   74 -----------------
 gnomeos/gnomeos-qemu-pull.sh                       |   70 ----------------
 src/ostbuild/pyostbuild/builtin_deploy_qemu.py     |    8 +-
 .../pyostbuild/builtin_privhelper_deploy_qemu.py   |   86 ++++++++++++++++----
 src/ostbuild/pyostbuild/main.py                    |    4 +
 src/ostbuild/pyostbuild/privileged_subproc.py      |    4 +-
 7 files changed, 82 insertions(+), 166 deletions(-)
---
diff --git a/Makefile-ostbuild.am b/Makefile-ostbuild.am
index 0c87e65..a6e9706 100644
--- a/Makefile-ostbuild.am
+++ b/Makefile-ostbuild.am
@@ -29,8 +29,10 @@ pyostbuild_PYTHON =					\
 	src/ostbuild/pyostbuild/builtin_compose.py	\
 	src/ostbuild/pyostbuild/builtin_chroot_compile_one.py	\
 	src/ostbuild/pyostbuild/builtin_compile_one.py	\
+	src/ostbuild/pyostbuild/builtin_deploy_qemu.py	\
 	src/ostbuild/pyostbuild/builtin_deploy_root.py	\
 	src/ostbuild/pyostbuild/builtin_pull_components.py	\
+	src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py	\
 	src/ostbuild/pyostbuild/builtin_git_mirror.py	\
 	src/ostbuild/pyostbuild/builtin_prefix.py	\
 	src/ostbuild/pyostbuild/builtin_resolve.py	\
diff --git a/src/ostbuild/pyostbuild/builtin_deploy_qemu.py b/src/ostbuild/pyostbuild/builtin_deploy_qemu.py
index 5afea83..549bbca 100755
--- a/src/ostbuild/pyostbuild/builtin_deploy_qemu.py
+++ b/src/ostbuild/pyostbuild/builtin_deploy_qemu.py
@@ -50,9 +50,9 @@ class OstbuildDeployQemu(builtins.Builtin):
             target_names.append(target['name'])
 
         helper = privileged_subproc.PrivilegedSubprocess()
-        sys_repo = os.path.join(self.ostree_dir, 'repo')
         shadow_path = os.path.join(self.workdir, 'shadow-repo')
-        helper.spawn_sync(['ostree', '--repo=' + sys_repo,
-                           'pull-local', shadow_path])
+        child_args = ['ostbuild', 'privhelper-deploy-qemu', shadow_path]
+        child_args.extend(target_names)
+        helper.spawn_sync(child_args)
         
-builtins.register(OstbuildDeployRoot)
+builtins.register(OstbuildDeployQemu)
diff --git a/src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py b/src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py
index 16b6bbb..2fc852c 100755
--- a/src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py
+++ b/src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py
@@ -24,8 +24,9 @@ from StringIO import StringIO
 
 from . import builtins
 from .ostbuildlog import log, fatal
+from .subprocess_helpers import run_sync
 from . import ostbuildrc
-from . import privileged_subproc
+from . import fileutil
 
 class OstbuildPrivhelperDeployQemu(builtins.Builtin):
     name = "privhelper-deploy-qemu"
@@ -34,23 +35,76 @@ class OstbuildPrivhelperDeployQemu(builtins.Builtin):
     def __init__(self):
         builtins.Builtin.__init__(self)
 
+    def _create_qemu_disk(self):
+        log("%s not found, creating" % (self.qemu_path, ))
+        success = False
+        tmppath = self.qemu_path + '.tmp'
+        if os.path.exists(tmppath):
+            os.unlink(tmppath)
+        subprocess.check_call(['qemu-img', 'create', tmppath, '6G'])
+        subprocess.check_call(['mkfs.ext4', '-q', '-F', tmppath])
+
+        subprocess.call(['umount', self.mountpoint], stderr=open('/dev/null', 'w'))
+        try:
+            subprocess.check_call(['mount', '-o', 'loop', tmppath, self.mountpoint])
+            
+            for topdir in ['mnt', 'sys', 'root', 'home', 'opt', 'tmp', 'run',
+                           'ostree']:
+                path = os.path.join(self.mountpoint, topdir)
+                fileutil.ensure_dir(path)
+            os.chmod(os.path.join(self.mountpoint, 'root'), 0700)
+            os.chmod(os.path.join(self.mountpoint, 'tmp'), 01777)
+
+            varpath = os.path.join(self.mountpoint, 'ostree', 'var')
+            fileutil.ensure_dir(varpath)
+            modulespath = os.path.join(self.mountpoint, 'ostree', 'modules')
+            fileutil.ensure_dir(modulespath)
+            
+            repo_path = os.path.join(self.mountpoint, 'ostree', 'repo')
+            fileutil.ensure_dir(repo_path)
+            subprocess.check_call(['ostree', '--repo=' + repo_path, 'init'])
+            success = True
+        finally:
+            subprocess.call(['umount', self.mountpoint])
+        if success:
+            os.rename(tmppath, self.qemu_path)
+
     def execute(self, argv):
         parser = argparse.ArgumentParser(description=self.short_description)
+        parser.add_argument('srcrepo')
+        parser.add_argument('targets', nargs='+')
 
         args = parser.parse_args(argv)
-        self.args = args
-        
-        self.parse_config()
-        self.parse_bin_snapshot(args.prefix, args.bin_snapshot)
-        
-        target_names = []
-        for target in self.bin_snapshot['targets']:
-            target_names.append(target['name'])
-
-        helper = privileged_subproc.PrivilegedSubprocess()
-        sys_repo = os.path.join(self.ostree_dir, 'repo')
-        shadow_path = os.path.join(self.workdir, 'shadow-repo')
-        helper.spawn_sync(['ostree', '--repo=' + sys_repo,
-                           'pull-local', shadow_path])
+
+        if os.geteuid() != 0:
+            fatal("This helper can only be run as root")
+
+        self.ostree_dir = self.find_ostree_dir()
+        self.qemu_path = os.path.join(self.ostree_dir, "ostree-qemu.img")
+
+        self.mountpoint = os.path.join(self.ostree_dir, 'ostree-qemu-mnt')
+        fileutil.ensure_dir(self.mountpoint)
+
+        if not os.path.exists(self.qemu_path):
+            self._create_qemu_disk()
+
+        subprocess.call(['umount', self.mountpoint], stderr=open('/dev/null', 'w'))
+        repo_path = os.path.join(self.mountpoint, 'ostree', 'repo')
+        try:
+            subprocess.check_call(['mount', '-o', 'loop', self.qemu_path, self.mountpoint])
+            child_args = ['ostree', '--repo=' + repo_path, 'pull-local', args.srcrepo]
+            child_args.extend(args.targets)
+            run_sync(child_args)
+
+            first_target = args.targets[0]
+            for target in args.targets:
+                run_sync(['ostree', '--repo=' + repo_path, 'checkout', '--atomic-retarget', target],
+                         cwd=os.path.join(self.mountpoint, 'ostree'))
+            current_link_path = os.path.join(self.mountpoint, 'ostree', 'current')
+            os.symlink(first_target, current_link_path + '.tmp')
+            os.rename(current_link_path + '.tmp', current_link_path)
+        finally:
+            subprocess.call(['umount', self.mountpoint])
+
         
-builtins.register(OstbuildDeployRoot)
+builtins.register(OstbuildPrivhelperDeployQemu)
diff --git a/src/ostbuild/pyostbuild/main.py b/src/ostbuild/pyostbuild/main.py
index 35d150a..7f91e38 100755
--- a/src/ostbuild/pyostbuild/main.py
+++ b/src/ostbuild/pyostbuild/main.py
@@ -30,8 +30,10 @@ from . import builtin_chroot_compile_one
 from . import builtin_compose
 from . import builtin_compile_one
 from . import builtin_deploy_root
+from . import builtin_deploy_qemu
 from . import builtin_git_mirror
 from . import builtin_pull_components
+from . import builtin_privhelper_deploy_qemu
 from . import builtin_prefix
 from . import builtin_resolve
 from . import builtin_modify_snapshot
@@ -42,6 +44,8 @@ from . import builtin_status
 def usage(ecode):
     print "Builtins:"
     for builtin in builtins.get_all():
+        if builtin.name.startswith('privhelper'):
+            continue
         print "    %s - %s" % (builtin.name, builtin.short_description)
     return ecode
 
diff --git a/src/ostbuild/pyostbuild/privileged_subproc.py b/src/ostbuild/pyostbuild/privileged_subproc.py
index 982984b..0116cef 100755
--- a/src/ostbuild/pyostbuild/privileged_subproc.py
+++ b/src/ostbuild/pyostbuild/privileged_subproc.py
@@ -19,6 +19,7 @@ import os,sys,subprocess
 
 from .ostbuildlog import log, fatal
 from . import ostbuildrc
+from .subprocess_helpers import run_sync
 
 class PrivilegedSubprocess(object):
 
@@ -35,5 +36,4 @@ class PrivilegedSubprocess(object):
 
     def _pkexec_spawn_sync(self, argv):
         pkexec_argv = ['pkexec'] + argv
-        log("Running: %s" % (subprocess.list2cmdline(pkexec_argv), ))
-        subprocess.check_call(pkexec_argv)
+        run_sync(pkexec_argv)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]