driver_devmapper.c 15.5 KB
Newer Older
W
WangFengTu 已提交
1 2
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
3 4 5 6
* iSulad licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*     http://license.coscl.org.cn/MulanPSL2
W
WangFengTu 已提交
7 8 9
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
10
* See the Mulan PSL v2 for more details.
G
gaohuatao 已提交
11
* Author: gaohuatao
W
WangFengTu 已提交
12 13 14 15 16 17 18
* Create: 2020-01-19
* Description: provide devicemapper graphdriver function definition
******************************************************************************/
#include "driver_devmapper.h"
#include <string.h>
#include <stdlib.h>
#include <limits.h>
G
gaohuatao 已提交
19 20 21 22 23
#include <libdevmapper.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
L
LiFeng 已提交
24
#include <sys/sysmacros.h>
G
gaohuatao 已提交
25
#include <sys/mount.h>
W
WangFengTu 已提交
26

L
lifeng68 已提交
27
#include "isula_libutils/log.h"
L
lifeng68 已提交
28
#include "err_msg.h"
W
WangFengTu 已提交
29
#include "utils.h"
G
gaohuatao 已提交
30 31
#include "wrapper_devmapper.h"
#include "devices_constants.h"
32
#include "deviceset.h"
L
lifeng68 已提交
33
#include "isula_libutils/json_common.h"
34
#include "util_archive.h"
W
WangFengTu 已提交
35

L
lifeng68 已提交
36
int devmapper_init(struct graphdriver *driver, const char *driver_home, const char **options, size_t len)
G
gaohuatao 已提交
37
{
L
lifeng68 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    int ret = 0;
    char *root_dir = NULL;

    if (driver == NULL || driver_home == NULL) {
        return -1;
    }

    driver->home = util_strdup_s(driver_home);

    root_dir = util_path_dir(driver_home);
    if (root_dir == NULL) {
        ERROR("Unable to get driver root home directory %s.", driver_home);
        ret = -1;
        goto out;
    }

    driver->backing_fs = util_get_fs_name(root_dir);
    if (driver->backing_fs == NULL) {
        ERROR("Failed to get backing fs");
        ret = -1;
        goto out;
    }

    if (util_mkdir_p(driver_home, DEFAULT_DEVICE_SET_MODE) != 0) {
        ERROR("Unable to create driver home directory %s.", driver_home);
        ret = -1;
        goto out;
    }

    if (device_set_init(driver, driver_home, options, len) != 0) {
        ERROR("Unable to init device mapper.");
        ret = -1;
        goto out;
    }

out:
    free(root_dir);
    return ret;
G
gaohuatao 已提交
76 77
}

L
lifeng68 已提交
78 79
static int do_create(const char *id, const char *parent, const struct graphdriver *driver,
                     const struct driver_create_opts *create_opts)
G
gaohuatao 已提交
80
{
L
lifeng68 已提交
81
    return add_device(id, parent, driver->devset, create_opts->storage_opt);
G
gaohuatao 已提交
82 83
}

84
// devmapper_create_rw creates a layer that is writable for use as a container file system
85
int devmapper_create_rw(const char *id, const char *parent, const struct graphdriver *driver,
G
gaohuatao 已提交
86
                        struct driver_create_opts *create_opts)
G
gaohuatao 已提交
87
{
L
LiFeng 已提交
88
    if (id == NULL || driver == NULL || create_opts == NULL) {
G
gaohuatao 已提交
89 90 91
        return -1;
    }

L
lifeng68 已提交
92
    return do_create(id, parent, driver, create_opts);
G
gaohuatao 已提交
93 94
}

95
// Create adds a device with a given id and the parent.
96
int devmapper_create_ro(const char *id, const char *parent, const struct graphdriver *driver,
L
LiFeng 已提交
97
                        const struct driver_create_opts *create_opts)
G
gaohuatao 已提交
98
{
L
LiFeng 已提交
99
    if (id == NULL || driver == NULL || create_opts == NULL) {
100 101 102
        return -1;
    }

L
lifeng68 已提交
103
    return do_create(id, parent, driver, create_opts);
G
gaohuatao 已提交
104 105
}

106
// Remove removes a device with a given id, unmounts the filesystem.
G
gaohuatao 已提交
107 108
int devmapper_rm_layer(const char *id, const struct graphdriver *driver)
{
109 110 111 112 113 114 115 116
    char *mnt_parent_dir = NULL;
    char *mnt_point_dir = NULL;
    int ret = 0;

    if (id == NULL || driver == NULL) {
        return -1;
    }

L
lifeng68 已提交
117
    if (!has_device(id, driver->devset)) {
118 119
        DEBUG("Device with id:%s is not exist", id);
        goto out;
120 121
    }

122 123
    if (delete_device(id, false, driver->devset) != 0) {
        ret = -1;
124
        ERROR("failed to remove device %s", id);
125
        goto out;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    }

    mnt_parent_dir = util_path_join(driver->home, "mnt");
    if (mnt_parent_dir == NULL) {
        ret = -1;
        ERROR("Failed to join devmapper mnt dir %s", id);
        goto out;
    }

    mnt_point_dir = util_path_join(mnt_parent_dir, id);
    if (mnt_point_dir == NULL) {
        ret = -1;
        ERROR("Failed to join devampper mount point dir %s", id);
        goto out;
    }

142 143 144 145 146
    if (util_path_remove(mnt_point_dir) != 0) {
        ret = -1;
        ERROR("Remove path:%s failed", mnt_point_dir);
        goto out;
    }
147 148 149 150 151

out:
    free(mnt_parent_dir);
    free(mnt_point_dir);
    return ret;
G
gaohuatao 已提交
152 153
}

154
// devmapper_mount_layer mounts a device with given id into the root filesystem
G
gaohuatao 已提交
155
char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
L
LiFeng 已提交
156
                            const struct driver_mount_opts *mount_opts)
G
gaohuatao 已提交
157
{
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    char *mnt_point_dir = NULL;
    char *mnt_parent_dir = NULL;
    char *rootfs = NULL;
    char *id_file = NULL;
    int ret = 0;

    if (id == NULL || driver == NULL || mount_opts == NULL) {
        return NULL;
    }

    mnt_parent_dir = util_path_join(driver->home, "mnt");
    if (mnt_parent_dir == NULL) {
        ERROR("Failed to join devmapper mnt dir%s", id);
        goto out;
    }

    mnt_point_dir = util_path_join(mnt_parent_dir, id);
    if (mnt_point_dir == NULL) {
        ERROR("Failed to join devampper mount point dir:%s", id);
        goto out;
    }

180 181
    if (util_mkdir_p(mnt_point_dir, DEFAULT_SECURE_DIRECTORY_MODE) != 0) {
        ret = -1;
G
gaohuatao 已提交
182 183 184 185
        ERROR("Failed to mkdir path:%s", mnt_point_dir);
        goto out;
    }

186
    DEBUG("devmapper: start to mount container device");
187 188
    if (mount_device(id, mnt_point_dir, mount_opts, driver->devset) != 0) {
        ret = -1;
G
gaohuatao 已提交
189
        ERROR("Mount device:%s to path:%s failed", id, mnt_parent_dir);
190 191 192 193 194
        goto out;
    }

    rootfs = util_path_join(mnt_point_dir, "rootfs");
    if (rootfs == NULL) {
195
        ret = -1;
196 197 198 199 200 201 202
        ERROR("Failed to join devmapper rootfs %s", mnt_point_dir);
        goto out;
    }

    if (util_mkdir_p(rootfs, 0755) != 0 || !util_dir_exists(rootfs)) {
        ERROR("Unable to create devmapper rootfs directory %s.", rootfs);
        ret = -1;
L
lifeng68 已提交
203
        if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
204 205 206 207 208 209 210 211 212
            DEBUG("devmapper: unmount %s failed", mnt_point_dir);
        }
        goto out;
    }

    id_file = util_path_join(mnt_point_dir, "id");
    if (!util_file_exists(id_file)) {
        // Create an "id" file with the container/image id in it to help reconstruct this in case
        // of later problems
213
        if (util_atomic_write_file(id_file, id, strlen(id), SECURE_CONFIG_FILE_MODE) != 0) {
L
lifeng68 已提交
214
            if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228
                DEBUG("devmapper: unmount %s failed", mnt_point_dir);
            }
        }
    }

out:
    free(mnt_parent_dir);
    free(mnt_point_dir);
    free(id_file);
    if (ret != 0) {
        free(rootfs);
        rootfs = NULL;
    }
    return rootfs;
G
gaohuatao 已提交
229 230 231 232
}

int devmapper_umount_layer(const char *id, const struct graphdriver *driver)
{
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    int ret = 0;
    char *mp = NULL;
    char *mnt_dir = NULL;

    if (id == NULL || driver == NULL) {
        return -1;
    }

    mnt_dir = util_path_join(driver->home, "mnt");
    if (mnt_dir == NULL) {
        ERROR("Failed to join layer dir mnt");
        ret = -1;
        goto out;
    }

    mp = util_path_join(mnt_dir, id);
    if (mp == NULL) {
        ERROR("Failed to join layer dir:%s", id);
        ret = -1;
        goto out;
    }

255 256 257 258
    if (unmount_device(id, mp, driver->devset) != 0) {
        ret = -1;
        ERROR("devmapper: unmount %s failed", mp);
        goto out;
259 260 261 262 263 264
    }

out:
    free(mnt_dir);
    free(mp);
    return ret;
G
gaohuatao 已提交
265 266
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280
static void free_driver_mount_opts(struct driver_mount_opts *opts)
{
    if (opts == NULL) {
        return;
    }
    free(opts->mount_label);
    opts->mount_label = NULL;

    util_free_array_by_len(opts->options, opts->options_len);
    opts->options = NULL;

    free(opts);
}

G
gaohuatao 已提交
281 282
bool devmapper_layer_exists(const char *id, const struct graphdriver *driver)
{
L
lifeng68 已提交
283
    return has_device(id, driver->devset);
G
gaohuatao 已提交
284 285 286
}

int devmapper_apply_diff(const char *id, const struct graphdriver *driver, const struct io_read_wrapper *content,
L
LiFeng 已提交
287
                         int64_t *layer_size)
G
gaohuatao 已提交
288
{
289 290 291 292 293 294 295 296 297 298 299 300
    struct driver_mount_opts *mount_opts = NULL;
    char *layer_fs = NULL;
    int ret = 0;
    struct archive_options options = { 0 };

    if (id == NULL || driver == NULL || content == NULL) {
        ERROR("invalid argument");
        return -1;
    }

    mount_opts = util_common_calloc_s(sizeof(struct driver_mount_opts));
    if (mount_opts == NULL) {
301
        ret = -1;
302
        ERROR("devmapper: out of memory");
303
        goto out;
304 305 306 307 308 309 310 311 312 313
    }

    layer_fs = devmapper_mount_layer(id, driver, mount_opts);
    if (layer_fs == NULL) {
        ERROR("devmapper: failed to mount layer %s", id);
        ret = -1;
        goto out;
    }

    options.whiteout_format = OVERLAY_WHITEOUT_FORMATE;
314 315
    if (archive_unpack(content, layer_fs, &options) != 0) {
        ret = -1;
316
        ERROR("devmapper: failed to unpack to :%s", layer_fs);
317
        goto out;
318 319
    }

320
    if (devmapper_umount_layer(id, driver) != 0) {
321 322
        ERROR("devmapper: failed to umount layer %s", id);
        ret = -1;
323
        goto out;
324 325 326 327 328 329
    }

out:
    free_driver_mount_opts(mount_opts);
    free(layer_fs);
    return ret;
G
gaohuatao 已提交
330 331 332 333
}

int devmapper_get_layer_metadata(const char *id, const struct graphdriver *driver, json_map_string_string *map_info)
{
334 335 336 337
    int ret = 0;
    char *mnt_dir = NULL;
    char *id_dir = NULL;
    char *rootfs_dir = NULL;
G
gaohuatao 已提交
338
    struct device_metadata dev_metadata = { 0 };
339 340 341 342 343 344 345 346 347
    char *device_id_str = NULL;
    char *device_size_str = NULL;

    if (id == NULL || driver == NULL || map_info == NULL) {
        ERROR("invalid argument");
        ret = -1;
        goto out;
    }

348 349
    if (export_device_metadata(&dev_metadata, id, driver->devset) != 0) {
        ret = -1;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        ERROR("Failed to export device metadata of device %s", id);
        goto out;
    }

    device_id_str = util_int_to_string(dev_metadata.device_id);
    if (device_id_str == NULL) {
        ret = -1;
        ERROR("Failed to map long long int to string");
        goto out;
    }

    device_size_str = util_uint_to_string(dev_metadata.device_size);
    if (device_size_str == NULL) {
        ret = -1;
        ERROR("Failed to map long long unsigned int to string");
        goto out;
    }

    mnt_dir = util_path_join(driver->home, "mnt");
    if (mnt_dir == NULL) {
        ret = -1;
        ERROR("Failed to join mnt dir");
        goto out;
    }

    id_dir = util_path_join(mnt_dir, id);
    if (id_dir == NULL) {
        ERROR("Failed to join devmapper id dir:%s", id);
        ret = -1;
        goto out;
    }
    rootfs_dir = util_path_join(id_dir, "rootfs");
    if (rootfs_dir == NULL) {
        ret = -1;
        ERROR("Failed to join devmapper rootfs dir");
        goto out;
    }

    if (append_json_map_string_string(map_info, "DeviceId", device_id_str) != 0) {
        ERROR("Failed to append device id:%s", device_id_str);
        ret = -1;
        goto out;
    }

    if (append_json_map_string_string(map_info, "DeviceSize", device_size_str) != 0) {
        ERROR("Failed to append device size:%s", device_size_str);
        ret = -1;
        goto out;
    }

    if (append_json_map_string_string(map_info, "DeviceName", dev_metadata.device_name) != 0) {
        ERROR("Failed to append device name:%s", dev_metadata.device_name);
        ret = -1;
        goto out;
    }

    if (append_json_map_string_string(map_info, "MergedDir", rootfs_dir) != 0) {
        ERROR("Failed to append device merge dir:%s", rootfs_dir);
        ret = -1;
        goto out;
    }

out:
G
gaohuatao 已提交
413
    free(dev_metadata.device_name);
414 415 416 417 418 419
    free(mnt_dir);
    free(id_dir);
    free(rootfs_dir);
    free(device_id_str);
    free(device_size_str);
    return ret;
G
gaohuatao 已提交
420 421
}

G
gaohuatao 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
static void status_append(const char *name, const char *value, uint64_t data, char **status, data_type type)
{
#define MAX_INFO_LENGTH 100
    char tmp[PATH_MAX] = { 0 };
    char *str = NULL;
    size_t nret = 0;

    if (name == NULL) {
        return;
    }

    switch (type) {
        case STRING:
            nret = snprintf(tmp, MAX_INFO_LENGTH, "%s: %s\n", name, value);
            break;
        case UINT64_T:
            nret = snprintf(tmp, MAX_INFO_LENGTH, "%s: %lu\n", name, data);
            break;
        default:
            break;
    }

    if (nret < 0 || nret >= MAX_INFO_LENGTH) {
        ERROR("Failed to print status");
        return;
    }

    str = *status;
    *status = NULL;
    *status = util_string_append(tmp, str);
    free(str);
}

char *status_to_str(const struct status *st)
{
    char *str = NULL;

    status_append("Pool Name", st->pool_name, 0, &str, STRING);
    status_append("Pool Blocksize", NULL, st->sector_size, &str, UINT64_T);
    status_append("Base Device Size", NULL, st->base_device_size, &str, UINT64_T);
    status_append("Backing Filesystem", st->base_device_fs, 0, &str, STRING);
    status_append("Data file", st->data_file, 0, &str, STRING);
    status_append("Metadata file", st->metadata_file, 0, &str, STRING);
    status_append("Data Space Used", NULL, st->data.used, &str, UINT64_T);
    status_append("Data Space Total", NULL, st->data.total, &str, UINT64_T);
    status_append("Data Space Available", NULL, st->data.available, &str, UINT64_T);
    status_append("Metadata Space Used", NULL, st->metadata.used, &str, UINT64_T);
    status_append("Metadata Space Total", NULL, st->metadata.total, &str, UINT64_T);
    status_append("Metadata Space Available", NULL, st->metadata.available, &str, UINT64_T);
    status_append("Thin Pool Minimum Free Space", NULL, st->min_free_space, &str, UINT64_T);

    if (st->udev_sync_supported) {
        status_append("Udev Sync Supported", "true", 0, &str, STRING);
    } else {
        status_append("Udev Sync Supported", "false", 0, &str, STRING);
    }

    if (st->deferred_remove_enabled) {
        status_append("Deferred Removal Enabled", "true", 0, &str, STRING);
    } else {
        status_append("Deferred Removal Enabled", "false", 0, &str, STRING);
    }

    if (st->deferred_delete_enabled) {
        status_append("Deferred Deletion Enabled", "true", 0, &str, STRING);
    } else {
        status_append("Deferred Deletion Enabled", "false", 0, &str, STRING);
    }

    status_append("Deferred Deleted Device Count", NULL, st->deferred_deleted_device_count, &str, UINT64_T);

    return str;
}

G
gaohuatao 已提交
496 497
int devmapper_get_driver_status(const struct graphdriver *driver, struct graphdriver_status *status)
{
498 499
    int ret = 0;
    struct status *st = NULL;
G
gaohuatao 已提交
500
    char *status_str = NULL;
501 502 503 504 505

    if (driver == NULL || status == NULL) {
        return -1;
    }

506
    st = device_set_status(driver->devset);
507 508
    if (st == NULL) {
        ERROR("Failed to get device set status");
G
gaohuatao 已提交
509 510
        ret = -1;
        goto out;
511 512 513
    }

    status->driver_name = util_strdup_s(driver->name);
G
gaohuatao 已提交
514 515 516 517 518 519 520
    status->backing_fs = util_strdup_s(driver->backing_fs);
    status_str = status_to_str(st);
    status->status = util_strdup_s(status_str);
    if (status->status == NULL) {
        ret = -1;
        goto out;
    }
521

G
gaohuatao 已提交
522
out:
523
    free_devmapper_status(st);
G
gaohuatao 已提交
524
    free(status_str);
525
    return ret;
526
}
G
gaohuatao 已提交
527 528 529 530

int devmapper_clean_up(const struct graphdriver *driver)
{
    if (driver == NULL) {
G
gaohuatao 已提交
531 532 533 534
        ERROR("Invalid input param to cleanup devicemapper");
        return -1;
    }

535
    if (device_set_shutdown(driver->devset, driver->home) != 0) {
G
gaohuatao 已提交
536
        ERROR("devmapper: shutdown device set failed root is %s", driver->home);
G
gaohuatao 已提交
537 538
        return -1;
    }
G
gaohuatao 已提交
539

540
    // Is it necessary to execute recursiveUmount()?
G
gaohuatao 已提交
541 542
    return umount(driver->home);
}
G
gaohuatao 已提交
543 544 545 546 547 548 549 550 551 552

int devmapper_repair_lowers(const char *id, const char *parent, const struct graphdriver *driver)
{
    return 0;
}

int devmapper_get_layer_fs_info(const char *id, const struct graphdriver *driver, imagetool_fs_info *fs_info)
{
    return 0;
}