]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
growfs: add support for resizing encrypted partitions
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 21 Nov 2017 17:56:52 +0000 (18:56 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 30 Nov 2017 19:43:25 +0000 (20:43 +0100)
meson.build
src/partition/growfs.c
src/shared/dissect-image.c
src/shared/dissect-image.h

index 94917ca6676db03dc2f0dc315c55c96cb2194ea0..031776c41f179609330b5f703d3c04405eb84b0b 100644 (file)
@@ -1940,6 +1940,7 @@ executable('systemd-growfs',
            'src/partition/growfs.c',
            include_directories : includes,
            link_with : [libshared],
+           dependencies : [libcryptsetup],
            install_rpath : rootlibexecdir,
            install : true,
            install_dir : rootlibexecdir)
index bebbda1bf38970dbd36c08211038810ef825894e..e5dd1d54d25df7ca30495c062d6dbcd95b67c865 100644 (file)
@@ -27,7 +27,9 @@
 #include <sys/types.h>
 #include <sys/vfs.h>
 
+#include "crypt-util.h"
 #include "device-nodes.h"
+#include "dissect-image.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "format-util.h"
@@ -71,6 +73,81 @@ static int resize_btrfs(const char *path, int mountfd, int devfd, uint64_t numbl
         return 0;
 }
 
+static int resize_crypt_luks_device(dev_t devno, const char *fstype, dev_t main_devno) {
+        char devpath[DEV_NUM_PATH_MAX], main_devpath[DEV_NUM_PATH_MAX];
+        _cleanup_close_ int main_devfd = -1;
+        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
+        uint64_t size;
+        int r;
+
+        xsprintf_dev_num_path(main_devpath, "block", main_devno);
+        main_devfd = open(main_devpath, O_RDONLY|O_CLOEXEC);
+        if (main_devfd < 0)
+                return log_error_errno(errno, "Failed to open \"%s\": %m", main_devpath);
+
+        if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
+                return log_error_errno(errno, "Failed to query size of \"%s\" (before resize): %m",
+                                       main_devpath);
+
+        log_debug("%s is %"PRIu64" bytes", main_devpath, size);
+
+        xsprintf_dev_num_path(devpath, "block", devno);
+        r = crypt_init(&cd, devpath);
+        if (r < 0)
+                return log_error_errno(r, "crypt_init(\"%s\") failed: %m", devpath);
+
+        crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
+
+        r = crypt_load(cd, CRYPT_LUKS, NULL);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to load LUKS metadata for %s: %m", devpath);
+
+        r = crypt_resize(cd, main_devpath, 0);
+        if (r < 0)
+                return log_error_errno(r, "crypt_resize() of %s failed: %m", devpath);
+
+        if (ioctl(main_devfd, BLKGETSIZE64, &size) != 0)
+                log_warning_errno(errno, "Failed to query size of \"%s\" (after resize): %m",
+                                  devpath);
+        else
+                log_debug("%s is now %"PRIu64" bytes", main_devpath, size);
+
+        return 1;
+}
+
+static int maybe_resize_slave_device(const char *mountpath, dev_t main_devno) {
+        dev_t devno;
+        char devpath[DEV_NUM_PATH_MAX];
+        _cleanup_free_ char *fstype = NULL;
+        int r;
+
+        crypt_set_log_callback(NULL, cryptsetup_log_glue, NULL);
+        crypt_set_debug_level(1);
+
+        r = get_block_device_harder(mountpath, &devno);
+        if (r < 0)
+                return log_error_errno(r, "Failed to determine underlying block device of \"%s\": %m",
+                                       mountpath);
+
+        log_debug("Underlying device %d:%d, main dev %d:%d, %s",
+                  major(devno), minor(devno),
+                  major(main_devno), minor(main_devno),
+                  devno == main_devno ? "same" : "different");
+        if (devno == main_devno)
+                return 0;
+
+        xsprintf_dev_num_path(devpath, "block", devno);
+        r = probe_filesystem(devpath, &fstype);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to probe \"%s\": %m", devpath);
+
+        if (streq_ptr(fstype, "crypto_LUKS"))
+                return resize_crypt_luks_device(devno, fstype, main_devno);
+
+        log_debug("Don't know how to resize %s of type %s, ignoring", devpath, strnull(fstype));
+        return 0;
+}
+
 int main(int argc, char *argv[]) {
         dev_t devno;
         _cleanup_close_ int mountfd = -1, devfd = -1;
@@ -105,6 +182,10 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
+        r = maybe_resize_slave_device(argv[1], devno);
+        if (r < 0)
+                return EXIT_FAILURE;
+
         mountfd = open(argv[1], O_RDONLY|O_CLOEXEC);
         if (mountfd < 0) {
                 log_error_errno(errno, "Failed to open \"%s\": %m", argv[1]);
index 6f294ed0bf2e898fccc6b5b6fc369c4d41d6a461..a7679a14230e6592dd745a49cf978f4f32dc15e2 100644 (file)
@@ -51,7 +51,7 @@
 #include "udev-util.h"
 #include "xattr-util.h"
 
-_unused_ static int probe_filesystem(const char *node, char **ret_fstype) {
+int probe_filesystem(const char *node, char **ret_fstype) {
 #if HAVE_BLKID
         _cleanup_blkid_free_probe_ blkid_probe b = NULL;
         const char *fstype;
index 30a12cb54080c0e6d4015002079f7bac4fcbd98c..7c7ce46015b21c75e9d38aed7745dfcb78e55884 100644 (file)
@@ -86,6 +86,7 @@ struct DissectedImage {
         char **os_release;
 };
 
+int probe_filesystem(const char *node, char **ret_fstype);
 int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DissectedImage **ret);
 
 DissectedImage* dissected_image_unref(DissectedImage *m);