|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | + |
| 3 | +#include <stdlib.h> |
| 4 | +#include <sys/stat.h> |
| 5 | + |
| 6 | +#include "alloc-util.h" |
| 7 | +#include "errno-util.h" |
| 8 | +#include "dropin.h" |
| 9 | +#include "escape.h" |
| 10 | +#include "fd-util.h" |
| 11 | +#include "fileio.h" |
| 12 | +#include "generator.h" |
| 13 | +#include "hashmap.h" |
| 14 | +#include "log.h" |
| 15 | +#include "path-util.h" |
| 16 | +#include "special.h" |
| 17 | +#include "string-util.h" |
| 18 | +#include "unit-name.h" |
| 19 | + |
| 20 | +typedef struct clone_device { |
| 21 | + char *uuid; |
| 22 | + char *datadev; |
| 23 | + char *name; |
| 24 | + char *options; |
| 25 | + bool clone; |
| 26 | +} clone_device; |
| 27 | + |
| 28 | +static const char *arg_dest = NULL; |
| 29 | +static const char *arg_runtime_directory = NULL; |
| 30 | +static Hashmap *arg_disks = NULL; |
| 31 | + |
| 32 | +static clone_device* clone_device_free(clone_device *d) { |
| 33 | + if (!d) |
| 34 | + return NULL; |
| 35 | + |
| 36 | + free(d->uuid); |
| 37 | + free(d->datadev); |
| 38 | + free(d->name); |
| 39 | + free(d->options); |
| 40 | + return mfree(d); |
| 41 | +} |
| 42 | + |
| 43 | +static int generate_clone_units(const char *clone_name, const char *source_dev, const char *dest_dev, |
| 44 | + const char *metadata_dev, const char *options) { |
| 45 | + |
| 46 | + /* unit files for each device */ |
| 47 | + _cleanup_fclose_ FILE *f = NULL; |
| 48 | + _cleanup_free_ char *source_unit = NULL, *dest_unit = NULL, *metadata_unit = NULL; |
| 49 | + _cleanup_free_ char *escaped_source = NULL, *escaped_dest = NULL, *escaped_metadata = NULL; |
| 50 | + _cleanup_free_ char *e = NULL, *unit = NULL; |
| 51 | + _cleanup_free_ char *clone_dev_path = NULL; |
| 52 | + const char *dmname; |
| 53 | + int r; |
| 54 | + |
| 55 | + assert(clone_name); |
| 56 | + assert(source_dev); |
| 57 | + assert(dest_dev); |
| 58 | + assert(metadata_dev); |
| 59 | + |
| 60 | + /* create clone_dev_path that holds path for new cloned device */ |
| 61 | + clone_dev_path = path_join("/dev/mapper", clone_name); |
| 62 | + if (!clone_dev_path) |
| 63 | + return log_oom(); |
| 64 | + |
| 65 | + /* escape clone name */ |
| 66 | + e = unit_name_escape(clone_name); |
| 67 | + if (!e) |
| 68 | + return log_oom(); |
| 69 | + |
| 70 | + /* Generate unit name for the clone service */ |
| 71 | + r = unit_name_build("systemd-clone", e, ".service", &unit); |
| 72 | + if (r < 0) |
| 73 | + return log_error_errno(r, "Failed to generate unit name: %m"); |
| 74 | + |
| 75 | + log_info("unit name=%s", unit); |
| 76 | + |
| 77 | + /* Generate unit names for dependencies */ |
| 78 | + r = unit_name_from_path(source_dev, ".device", &source_unit); |
| 79 | + if (r < 0) |
| 80 | + return log_error_errno(r, "Failed to generate source device unit name: %m"); |
| 81 | + |
| 82 | + r = unit_name_from_path(dest_dev, ".device", &dest_unit); |
| 83 | + if (r < 0) |
| 84 | + return log_error_errno(r, "Failed to generate dest device unit name: %m"); |
| 85 | + |
| 86 | + r = unit_name_from_path(metadata_dev, ".device", &metadata_unit); |
| 87 | + if (r < 0) |
| 88 | + return log_error_errno(r, "Failed to generate metadata device unit name: %m"); |
| 89 | + |
| 90 | + /* Escape device paths for ExecStart command */ |
| 91 | + escaped_source = cescape(source_dev); |
| 92 | + if (!escaped_source) |
| 93 | + return log_oom(); |
| 94 | + |
| 95 | + escaped_dest = cescape(dest_dev); |
| 96 | + if (!escaped_dest) |
| 97 | + return log_oom(); |
| 98 | + |
| 99 | + escaped_metadata = cescape(metadata_dev); |
| 100 | + if (!escaped_metadata) |
| 101 | + return log_oom(); |
| 102 | + |
| 103 | + r = generator_open_unit_file(arg_dest, /* source = */ NULL, unit, &f); |
| 104 | + if (r < 0) |
| 105 | + return r; |
| 106 | + |
| 107 | + /* Check if we're using loop devices and add setup if needed */ |
| 108 | + bool setup_loop = startswith(source_dev, "/dev/loop") && startswith(dest_dev, "/dev/loop") && startswith(metadata_dev, "/dev/loop"); |
| 109 | + |
| 110 | + fprintf(f, |
| 111 | + "[Unit]\n" |
| 112 | + "Description=Create dm-clone device %s\n" |
| 113 | + "Documentation=man:dmsetup(8) man:fstab(5) man:systemd-fstab-generator(8)\n" |
| 114 | + "DefaultDependencies=no\n", |
| 115 | + clone_dev_path); |
| 116 | + |
| 117 | + if (!setup_loop) { |
| 118 | + fprintf(f, |
| 119 | + "BindsTo=%s %s %s\n" |
| 120 | + "Requires=%s %s %s\n" |
| 121 | + "After=%s %s %s\n", |
| 122 | + source_unit, dest_unit, metadata_unit, |
| 123 | + source_unit, dest_unit, metadata_unit, |
| 124 | + source_unit, dest_unit, metadata_unit); |
| 125 | + } |
| 126 | + |
| 127 | + fprintf(f, |
| 128 | + "Before=blockdev@dev-mapper-%s.target\n" |
| 129 | + "Wants=blockdev@dev-mapper-%s.target\n" |
| 130 | + "Conflicts=shutdown.target\n" |
| 131 | + "\n" |
| 132 | + "[Service]\n" |
| 133 | + "Type=oneshot\n" |
| 134 | + "RemainAfterExit=yes\n", |
| 135 | + e, e); |
| 136 | + |
| 137 | + if (setup_loop) |
| 138 | + fprintf(f, "ExecStartPre=/usr/share/script.sh\n"); |
| 139 | + |
| 140 | + fprintf(f, |
| 141 | + "ExecStart=" SYSTEMD_CLONE_PATH " add '%s' '%s' '%s' '%s' '%s'\n" |
| 142 | + "ExecStop=" SYSTEMD_CLONE_PATH " remove %s\n" |
| 143 | + "TimeoutSec=0\n", |
| 144 | + clone_name, escaped_source, escaped_dest, escaped_metadata, "", |
| 145 | + clone_name); |
| 146 | + |
| 147 | + r = fflush_and_check(f); |
| 148 | + if (r < 0) |
| 149 | + return log_error_errno(r, "Failed to write unit %s: %m", unit); |
| 150 | + |
| 151 | + // symlink unit file to enable it |
| 152 | + dmname = strjoina("dev-mapper-", e, ".device"); |
| 153 | + r = generator_add_symlink(arg_dest, dmname, "requires", unit); |
| 154 | + if (r < 0) |
| 155 | + return r; |
| 156 | + |
| 157 | + /* Extend device timeout to allow clone service to complete */ |
| 158 | + r = write_drop_in(arg_dest, dmname, 40, "device-timeout", |
| 159 | + "# Automatically generated by systemd-clone-generator\n\n" |
| 160 | + "[Unit]\n" |
| 161 | + "JobTimeoutSec=infinity\n"); |
| 162 | + if (r < 0) |
| 163 | + log_warning_errno(r, "Failed to write device timeout drop-in: %m"); |
| 164 | + |
| 165 | + /* Add to clone.target so it starts at boot */ |
| 166 | + r = generator_add_symlink(arg_dest, SPECIAL_CLONE_TARGET, "requires", unit); |
| 167 | + if (r < 0) |
| 168 | + return r; |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +static int add_clone_devices(void) { |
| 174 | + _cleanup_fclose_ FILE *f = NULL; |
| 175 | + unsigned clone_line = 0; |
| 176 | + int r, ret = 0; |
| 177 | + const char *fname; |
| 178 | + |
| 179 | + fname = getenv("SYSTEMD_CLONETAB") ?: "/etc/clonetab"; |
| 180 | + |
| 181 | + log_info("Parsing %s", fname); |
| 182 | + r = fopen_unlocked(fname, "re", &f); |
| 183 | + if (r < 0) { |
| 184 | + if (errno != ENOENT) |
| 185 | + log_error_errno(errno, "Failed to open %s: %m", fname); |
| 186 | + return 0; |
| 187 | + } |
| 188 | + |
| 189 | + for (;;) { |
| 190 | + _cleanup_free_ char *line = NULL, *src = NULL, *name = NULL, *dst = NULL, *meta = NULL, *options = NULL; |
| 191 | + int k; |
| 192 | + |
| 193 | + r = read_stripped_line(f, LONG_LINE_MAX, &line); |
| 194 | + if (r < 0) |
| 195 | + return log_error_errno(r, "Failed to read %s: %m", fname); |
| 196 | + if (r == 0) |
| 197 | + break; |
| 198 | + |
| 199 | + clone_line++; |
| 200 | + |
| 201 | + if (IN_SET(line[0], 0, '#')) |
| 202 | + continue; |
| 203 | + |
| 204 | + k = sscanf(line, "%ms %ms %ms %ms %ms", &name, &src, &dst, &meta, &options); |
| 205 | + if (k < 4 || k > 5) { |
| 206 | + log_error("Failed to parse %s:%u, ignoring.", fname, clone_line); |
| 207 | + continue; |
| 208 | + } |
| 209 | + |
| 210 | + RET_GATHER(ret, generate_clone_units(name, src, dst, meta, options)); |
| 211 | + } |
| 212 | + |
| 213 | + return ret; |
| 214 | +} |
| 215 | + |
| 216 | +DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(clone_device_hash_ops, char, string_hash_func, string_compare_func, |
| 217 | + clone_device, clone_device_free); |
| 218 | + |
| 219 | + |
| 220 | +static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 221 | + int r; |
| 222 | + |
| 223 | + // dest usually is /run/systemd/generator |
| 224 | + assert_se(arg_dest = dest); |
| 225 | + |
| 226 | + arg_runtime_directory = getenv("RUNTIME_DIRECTORY") ?: "/run/systemd/dev-clone"; |
| 227 | + |
| 228 | + arg_disks = hashmap_new(&clone_device_hash_ops); |
| 229 | + if (!arg_disks) |
| 230 | + return log_oom(); |
| 231 | + |
| 232 | + r = add_clone_devices(); |
| 233 | + return r; |
| 234 | +} |
| 235 | + |
| 236 | +DEFINE_MAIN_GENERATOR_FUNCTION(run); |
0 commit comments