chiark / gitweb /
import: print nice warning if we need btrfs but /var/lib/machines is not btrfs
[elogind.git] / src / import / import-raw.c
index d2cb053b5d17bd478283aa1674ec167316d08ee1..8d99f1085cb1111519dcd0db29b4fa8760c9543e 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/fs.h>
 #include <curl/curl.h>
 
+#include "sd-daemon.h"
 #include "utf8.h"
 #include "strv.h"
 #include "copy.h"
 #include "util.h"
 #include "macro.h"
 #include "mkdir.h"
+#include "import-util.h"
 #include "curl-util.h"
 #include "qcow2-util.h"
 #include "import-job.h"
-#include "import-util.h"
+#include "import-common.h"
 #include "import-raw.h"
 
-typedef struct RawImportFile RawImportFile;
+typedef enum RawProgress {
+        RAW_DOWNLOADING,
+        RAW_VERIFYING,
+        RAW_UNPACKING,
+        RAW_FINALIZING,
+        RAW_COPYING,
+} RawProgress;
 
 struct RawImport {
         sd_event *event;
@@ -84,7 +92,13 @@ RawImport* raw_import_unref(RawImport *i) {
         return NULL;
 }
 
-int raw_import_new(RawImport **ret, sd_event *event, const char *image_root, RawImportFinished on_finished, void *userdata) {
+int raw_import_new(
+                RawImport **ret,
+                sd_event *event,
+                const char *image_root,
+                RawImportFinished on_finished,
+                void *userdata) {
+
         _cleanup_(raw_import_unrefp) RawImport *i = NULL;
         int r;
 
@@ -122,6 +136,57 @@ int raw_import_new(RawImport **ret, sd_event *event, const char *image_root, Raw
         return 0;
 }
 
+static void raw_import_report_progress(RawImport *i, RawProgress p) {
+        unsigned percent;
+
+        assert(i);
+
+        switch (p) {
+
+        case RAW_DOWNLOADING: {
+                unsigned remain = 80;
+
+                percent = 0;
+
+                if (i->checksum_job) {
+                        percent += i->checksum_job->progress_percent * 5 / 100;
+                        remain -= 5;
+                }
+
+                if (i->signature_job) {
+                        percent += i->signature_job->progress_percent * 5 / 100;
+                        remain -= 5;
+                }
+
+                if (i->raw_job)
+                        percent += i->raw_job->progress_percent * remain / 100;
+                break;
+        }
+
+        case RAW_VERIFYING:
+                percent = 80;
+                break;
+
+        case RAW_UNPACKING:
+                percent = 85;
+                break;
+
+        case RAW_FINALIZING:
+                percent = 90;
+                break;
+
+        case RAW_COPYING:
+                percent = 95;
+                break;
+
+        default:
+                assert_not_reached("Unknown progress state");
+        }
+
+        sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
+        log_debug("Combined progress %u%%", percent);
+}
+
 static int raw_import_maybe_convert_qcow2(RawImport *i) {
         _cleanup_close_ int converted_fd = -1;
         _cleanup_free_ char *t = NULL;
@@ -205,7 +270,7 @@ static int raw_import_make_local_copy(RawImport *i) {
                         return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
         }
 
-        p = strappenda(i->image_root, "/", i->local, ".raw");
+        p = strjoina(i->image_root, "/", i->local, ".raw");
 
         if (i->force_local) {
                 (void) btrfs_subvol_remove(p);
@@ -249,6 +314,20 @@ static int raw_import_make_local_copy(RawImport *i) {
         return 0;
 }
 
+static bool raw_import_is_done(RawImport *i) {
+        assert(i);
+        assert(i->raw_job);
+
+        if (i->raw_job->state != IMPORT_JOB_DONE)
+                return false;
+        if (i->checksum_job && i->checksum_job->state != IMPORT_JOB_DONE)
+                return false;
+        if (i->signature_job && i->signature_job->state != IMPORT_JOB_DONE)
+                return false;
+
+        return true;
+}
+
 static void raw_import_job_on_finished(ImportJob *j) {
         RawImport *i;
         int r;
@@ -276,25 +355,27 @@ static void raw_import_job_on_finished(ImportJob *j) {
          *
          * We only do something when we got all three files */
 
-        if (i->raw_job->state != IMPORT_JOB_DONE)
-                return;
-        if (i->checksum_job && i->checksum_job->state != IMPORT_JOB_DONE)
-                return;
-        if (i->signature_job && i->signature_job->state != IMPORT_JOB_DONE)
+        if (!raw_import_is_done(i))
                 return;
 
         if (!i->raw_job->etag_exists) {
                 /* This is a new download, verify it, and move it into place */
                 assert(i->raw_job->disk_fd >= 0);
 
+                raw_import_report_progress(i, RAW_VERIFYING);
+
                 r = import_verify(i->raw_job, i->checksum_job, i->signature_job);
                 if (r < 0)
                         goto finish;
 
+                raw_import_report_progress(i, RAW_UNPACKING);
+
                 r = raw_import_maybe_convert_qcow2(i);
                 if (r < 0)
                         goto finish;
 
+                raw_import_report_progress(i, RAW_FINALIZING);
+
                 r = import_make_read_only_fd(i->raw_job->disk_fd);
                 if (r < 0)
                         goto finish;
@@ -309,6 +390,8 @@ static void raw_import_job_on_finished(ImportJob *j) {
                 i->temp_path = NULL;
         }
 
+        raw_import_report_progress(i, RAW_COPYING);
+
         r = raw_import_make_local_copy(i);
         if (r < 0)
                 goto finish;
@@ -330,6 +413,9 @@ static int raw_import_job_on_open_disk(ImportJob *j) {
         assert(j->userdata);
 
         i = j->userdata;
+        assert(i->raw_job == j);
+        assert(!i->final_path);
+        assert(!i->temp_path);
 
         r = import_make_path(j->url, j->etag, i->image_root, ".raw-", ".raw", &i->final_path);
         if (r < 0)
@@ -352,6 +438,17 @@ static int raw_import_job_on_open_disk(ImportJob *j) {
         return 0;
 }
 
+static void raw_import_job_on_progress(ImportJob *j) {
+        RawImport *i;
+
+        assert(j);
+        assert(j->userdata);
+
+        i = j->userdata;
+
+        raw_import_report_progress(i, RAW_DOWNLOADING);
+}
+
 int raw_import_pull(RawImport *i, const char *url, const char *local, bool force_local, ImportVerify verify) {
         int r;
 
@@ -359,15 +456,15 @@ int raw_import_pull(RawImport *i, const char *url, const char *local, bool force
         assert(verify < _IMPORT_VERIFY_MAX);
         assert(verify >= 0);
 
-        if (i->raw_job)
-                return -EBUSY;
-
         if (!http_url_is_valid(url))
                 return -EINVAL;
 
         if (local && !machine_name_is_valid(local))
                 return -EINVAL;
 
+        if (i->raw_job)
+                return -EBUSY;
+
         r = free_and_strdup(&i->local, local);
         if (r < 0)
                 return r;
@@ -381,6 +478,7 @@ int raw_import_pull(RawImport *i, const char *url, const char *local, bool force
 
         i->raw_job->on_finished = raw_import_job_on_finished;
         i->raw_job->on_open_disk = raw_import_job_on_open_disk;
+        i->raw_job->on_progress = raw_import_job_on_progress;
         i->raw_job->calc_checksum = verify != IMPORT_VERIFY_NO;
 
         r = import_find_old_etags(url, i->image_root, DT_REG, ".raw-", ".raw", &i->raw_job->old_etags);
@@ -396,12 +494,16 @@ int raw_import_pull(RawImport *i, const char *url, const char *local, bool force
                 return r;
 
         if (i->checksum_job) {
+                i->checksum_job->on_progress = raw_import_job_on_progress;
+
                 r = import_job_begin(i->checksum_job);
                 if (r < 0)
                         return r;
         }
 
         if (i->signature_job) {
+                i->signature_job->on_progress = raw_import_job_on_progress;
+
                 r = import_job_begin(i->signature_job);
                 if (r < 0)
                         return r;