summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorRick Harris <rconradharris@gmail.com>2012-02-27 21:01:49 +0000
committerRick Harris <rconradharris@gmail.com>2012-02-27 21:14:48 +0000
commit6315eb5086a96e4ba614ba01c9bbfaec8bd8504b (patch)
tree4361a2edfe4c8b1f8333bacef51c0c503c894adf /nova/utils.py
parentb88cd9439c2d655b4dcf5e6cb214a1e4aeffa9b7 (diff)
downloadnova-6315eb5086a96e4ba614ba01c9bbfaec8bd8504b.tar.gz
nova-6315eb5086a96e4ba614ba01c9bbfaec8bd8504b.tar.xz
nova-6315eb5086a96e4ba614ba01c9bbfaec8bd8504b.zip
Adds temporary chown to sparse_copy.
`sparse_copy` needs read and write access to the devices. Since we cannot shell out to a run-as-root command here, we temporarily take ownership of the device. Change-Id: I891c38dbcba7177286dca729684c88ac065bd085
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py
index 0f3e61897..6bb0dd0f2 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -1523,3 +1523,23 @@ def read_file_as_root(file_path):
return out
except exception.ProcessExecutionError:
raise exception.FileNotFound(file_path=file_path)
+
+
+@contextlib.contextmanager
+def temporary_chown(path, owner_uid=None):
+ """Temporarily chown a path.
+
+ :params owner_uid: UID of temporary owner (defaults to current user)
+ """
+ if owner_uid is None:
+ owner_uid = os.getuid()
+
+ orig_uid = os.stat(path).st_uid
+
+ if orig_uid != owner_uid:
+ execute('chown', owner_uid, path, run_as_root=True)
+ try:
+ yield
+ finally:
+ if orig_uid != owner_uid:
+ execute('chown', orig_uid, path, run_as_root=True)