[Libguestfs] [PATCH] customize: add the chmod command

Pino Toscano ptoscano at redhat.com
Fri Apr 4 14:17:00 UTC 2014


Add the chmod customize command to change the permissions of files.
---
 builder/cmdline.ml               |  2 +-
 customize/customize_run.ml       |  8 ++++++++
 customize/test-virt-customize.sh |  1 +
 generator/customize.ml           | 12 ++++++++++++
 4 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/builder/cmdline.ml b/builder/cmdline.ml
index 3a620fe..45c87fc 100644
--- a/builder/cmdline.ml
+++ b/builder/cmdline.ml
@@ -315,7 +315,7 @@ read the man page virt-builder(1).
           | `Delete _ | `Edit _ | `FirstbootCommand _ | `FirstbootPackages _
           | `FirstbootScript _ | `Hostname _ | `Link _ | `Mkdir _
           | `Password _ | `RootPassword _ | `Scrub _ | `Timezone _ | `Upload _
-          | `Write _ -> false
+          | `Write _ | `Chmod _ -> false
         ) ops.ops in
         if requires_execute_on_guest then (
           eprintf (f_"%s: sorry, cannot run commands on a guest with a different architecture\n")
diff --git a/customize/customize_run.ml b/customize/customize_run.ml
index 8a1df32..4d83e90 100644
--- a/customize/customize_run.ml
+++ b/customize/customize_run.ml
@@ -168,6 +168,14 @@ exec >>%s 2>&1
   (* Perform the remaining customizations in command-line order. *)
   List.iter (
     function
+    | `Chmod (mode, path) ->
+      msg (f_"Changing permissions of %s to %s") path mode;
+      (* If the mode string is octal, add the OCaml prefix for octal values
+       * so it is properly converted as octal integer.
+       *)
+      let mode = if string_prefix mode "0" then "0o" ^ mode else mode in
+      g#chmod (int_of_string mode) path
+
     | `Command cmd ->
       msg (f_"Running: %s") cmd;
       do_run ~display:cmd cmd
diff --git a/customize/test-virt-customize.sh b/customize/test-virt-customize.sh
index be61796..623e6a2 100755
--- a/customize/test-virt-customize.sh
+++ b/customize/test-virt-customize.sh
@@ -29,6 +29,7 @@ for f in ../tests/guests/{debian,fedora,ubuntu}.img; do
 	$VG ./virt-customize -n -a $f \
             --no-network \
             --write /etc/motd:HELLO \
+            --chmod 0600:/etc/motd \
             --delete /etc/motd
     fi
 done
diff --git a/generator/customize.ml b/generator/customize.ml
index 0b0580a..2c991ea 100644
--- a/generator/customize.ml
+++ b/generator/customize.ml
@@ -44,6 +44,18 @@ and op_type =
 | UserPasswordSelector of string        (* user:selector *)
 
 let ops = [
+  { op_name = "chmod";
+    op_type = StringPair "PERMISSIONS:FILE";
+    op_discrim = "`Chmod";
+    op_shortdesc = "Change the permissions of a file";
+    op_pod_longdesc = "\
+Change the permissions of C<FILE> to C<PERMISSIONS>.
+
+I<Note>: C<PERMISSIONS> by default would be decimal, unless you prefix
+it with C<0> to get octal, ie. use C<0700> not C<700>.
+
+The mode actually set is affected by the umask.";
+  };
   { op_name = "delete";
     op_type = String "PATH";
     op_discrim = "`Delete";
-- 
1.9.0




More information about the Libguestfs mailing list