chiark / gitweb /
machined: when cloning a raw disk image, also set the NOCOW flag
authorLennart Poettering <lennart@poettering.net>
Thu, 8 Jan 2015 18:15:49 +0000 (19:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Jan 2015 22:13:45 +0000 (23:13 +0100)
src/firstboot/firstboot.c
src/nspawn/nspawn.c
src/shared/copy.c
src/shared/copy.h
src/shared/machine-image.c
src/systemctl/systemctl.c
src/test/test-copy.c

index d087ef35e913d4ed0ee62bc7f796e1c0729958fc..ce79574b906b12ea5c9ca73064c79a4145dde834 100644 (file)
@@ -256,7 +256,7 @@ static int process_locale(void) {
         if (arg_copy_locale && arg_root) {
 
                 mkdir_parents(etc_localeconf, 0755);
-                r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644);
+                r = copy_file("/etc/locale.conf", etc_localeconf, 0, 0644, 0);
                 if (r != -ENOENT) {
                         if (r < 0)
                                 return log_error_errno(r, "Failed to copy %s: %m", etc_localeconf);
index e5a24dda70c0460ea6fb7931de869f87c3cd9e27..59f6f38a49305bdb269e0fbe4be27e022ef4244f 100644 (file)
@@ -1092,7 +1092,7 @@ static int setup_resolv_conf(const char *dest) {
                 return 0;
         }
 
-        r = copy_file("/etc/resolv.conf", where, O_TRUNC|O_NOFOLLOW, 0644);
+        r = copy_file("/etc/resolv.conf", where, O_TRUNC|O_NOFOLLOW, 0644, 0);
         if (r < 0) {
                 log_warning_errno(r, "Failed to copy /etc/resolv.conf to %s: %m", where);
 
index 3df636704c3ba9a333bb440d00cc2948eec48e39..b681f6f109126b1856cfc0d6a23a4af2eac9981a 100644 (file)
@@ -359,7 +359,7 @@ int copy_file_fd(const char *from, int fdt, bool try_reflink) {
         return r;
 }
 
-int copy_file(const char *from, const char *to, int flags, mode_t mode) {
+int copy_file(const char *from, const char *to, int flags, mode_t mode, int chattr_flags) {
         int fdt, r;
 
         assert(from);
@@ -371,6 +371,9 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode) {
                         return -errno;
         }
 
+        if (chattr_flags != 0)
+                (void) chattr_fd(fdt, true, chattr_flags);
+
         r = copy_file_fd(from, fdt, true);
         if (r < 0) {
                 close(fdt);
@@ -386,7 +389,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode) {
         return 0;
 }
 
-int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace) {
+int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, int chattr_flags) {
         _cleanup_free_ char *t;
         int r;
 
@@ -397,7 +400,7 @@ int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace
         if (r < 0)
                 return r;
 
-        r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode);
+        r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags);
         if (r < 0)
                 return r;
 
index 58159a02cc60459ea7922dfbbae172527d7dd9dd..e4e307912096c7274de00ecb0053eadd3e21880a 100644 (file)
@@ -25,8 +25,8 @@
 #include <sys/types.h>
 
 int copy_file_fd(const char *from, int to, bool try_reflink);
-int copy_file(const char *from, const char *to, int flags, mode_t mode);
-int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace);
+int copy_file(const char *from, const char *to, int flags, mode_t mode, int chattr_flags);
+int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace, int chattr_flags);
 int copy_tree(const char *from, const char *to, bool merge);
 int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge);
 int copy_directory_fd(int dirfd, const char *to, bool merge);
index 36b64e1fab8fec9cf0f6db88157f78c1509cd52a..25689ca93cba91042ab0f9eec37520d7b7b54912 100644 (file)
@@ -20,6 +20,7 @@
 ***/
 
 #include <sys/statfs.h>
+#include <linux/fs.h>
 #include <fcntl.h>
 
 #include "strv.h"
@@ -440,7 +441,7 @@ int image_clone(Image *i, const char *new_name, bool read_only) {
         case IMAGE_GPT:
                 new_path = strappenda("/var/lib/container/", new_name, ".gpt");
 
-                r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false);
+                r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
                 break;
 
         default:
@@ -477,6 +478,12 @@ int image_read_only(Image *i, bool b) {
 
                 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
                         return -errno;
+
+                /* If the images is now read-only, it's a good time to
+                 * defrag it, given that no write patterns will
+                 * fragment it again. */
+                if (b)
+                        (void) btrfs_defrag(i->path);
                 break;
         }
 
index 74528dd5b6cbbefae35d610b0af78c745ab76085..312142827433575203577d477d4855d12df24d44 100644 (file)
@@ -5669,7 +5669,7 @@ static int create_edit_temp_file(const char *new_path, const char *original_path
                 return r;
         }
 
-        r = copy_file(original_path, t, 0, 0644);
+        r = copy_file(original_path, t, 0, 0644, 0);
         if (r == -ENOENT) {
                 r = touch(t);
                 if (r < 0) {
index d70a0be2a22b798052073756645a18c9d02b5195..3e1607e51d53fba50cd619f65496cbb9c676ceea 100644 (file)
@@ -44,7 +44,7 @@ static void test_copy_file(void) {
 
         assert_se(write_string_file(fn, "foo bar bar bar foo") == 0);
 
-        assert_se(copy_file(fn, fn_copy, 0, 0644) == 0);
+        assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0);
 
         assert_se(read_full_file(fn_copy, &buf, &sz) == 0);
         assert_se(streq(buf, "foo bar bar bar foo\n"));