driver_devmapper.c 15.6 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
* Create: 2020-01-19
* Description: provide devicemapper graphdriver function definition
******************************************************************************/
#include "driver_devmapper.h"
16

W
WangFengTu 已提交
17 18 19
#include <string.h>
#include <stdlib.h>
#include <limits.h>
G
gaohuatao 已提交
20
#include <sys/mount.h>
21
#include <stdio.h>
W
WangFengTu 已提交
22

L
lifeng68 已提交
23
#include "isula_libutils/log.h"
W
WangFengTu 已提交
24
#include "utils.h"
G
gaohuatao 已提交
25
#include "devices_constants.h"
26
#include "deviceset.h"
L
lifeng68 已提交
27
#include "isula_libutils/json_common.h"
28
#include "util_archive.h"
29 30 31 32 33 34 35 36 37
#include "constants.h"
#include "driver.h"
#include "image_api.h"
#include "utils_array.h"
#include "utils_file.h"
#include "utils_fs.h"
#include "utils_string.h"

struct io_read_wrapper;
W
WangFengTu 已提交
38

L
lifeng68 已提交
39
int devmapper_init(struct graphdriver *driver, const char *driver_home, const char **options, size_t len)
G
gaohuatao 已提交
40
{
L
lifeng68 已提交
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 76 77 78
    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 已提交
79 80
}

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

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

L
lifeng68 已提交
95
    return do_create(id, parent, driver, create_opts);
G
gaohuatao 已提交
96 97
}

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

L
lifeng68 已提交
106
    return do_create(id, parent, driver, create_opts);
G
gaohuatao 已提交
107 108
}

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

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

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

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

    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;
    }

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

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

157
// devmapper_mount_layer mounts a device with given id into the root filesystem
G
gaohuatao 已提交
158
char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
L
LiFeng 已提交
159
                            const struct driver_mount_opts *mount_opts)
G
gaohuatao 已提交
160
{
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    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;
    }

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

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

    rootfs = util_path_join(mnt_point_dir, "rootfs");
    if (rootfs == NULL) {
198
        ret = -1;
199 200 201 202 203 204 205
        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 已提交
206
        if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
207 208 209 210 211 212 213 214 215
            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
216
        if (util_atomic_write_file(id_file, id, strlen(id), SECURE_CONFIG_FILE_MODE) != 0) {
L
lifeng68 已提交
217
            if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
218 219 220 221 222 223 224 225 226 227 228 229 230 231
                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 已提交
232 233 234 235
}

int devmapper_umount_layer(const char *id, const struct graphdriver *driver)
{
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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;
    }

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

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

270 271 272 273 274 275 276 277 278 279 280 281 282 283
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 已提交
284 285
bool devmapper_layer_exists(const char *id, const struct graphdriver *driver)
{
L
lifeng68 已提交
286
    return has_device(id, driver->devset);
G
gaohuatao 已提交
287 288 289
}

int devmapper_apply_diff(const char *id, const struct graphdriver *driver, const struct io_read_wrapper *content,
L
LiFeng 已提交
290
                         int64_t *layer_size)
G
gaohuatao 已提交
291
{
292 293 294 295 296 297 298 299 300 301 302 303
    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) {
304
        ret = -1;
305
        ERROR("devmapper: out of memory");
306
        goto out;
307 308 309 310 311 312 313 314 315 316
    }

    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;
317 318
    if (archive_unpack(content, layer_fs, &options) != 0) {
        ret = -1;
319
        ERROR("devmapper: failed to unpack to :%s", layer_fs);
320
        goto out;
321 322
    }

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

out:
    free_driver_mount_opts(mount_opts);
    free(layer_fs);
    return ret;
G
gaohuatao 已提交
333 334 335 336
}

int devmapper_get_layer_metadata(const char *id, const struct graphdriver *driver, json_map_string_string *map_info)
{
337 338 339 340
    int ret = 0;
    char *mnt_dir = NULL;
    char *id_dir = NULL;
    char *rootfs_dir = NULL;
G
gaohuatao 已提交
341
    struct device_metadata dev_metadata = { 0 };
342 343 344 345 346 347 348 349 350
    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;
    }

351 352
    if (export_device_metadata(&dev_metadata, id, driver->devset) != 0) {
        ret = -1;
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 413 414 415
        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 已提交
416
    free(dev_metadata.device_name);
417 418 419 420 421 422
    free(mnt_dir);
    free(id_dir);
    free(rootfs_dir);
    free(device_id_str);
    free(device_size_str);
    return ret;
G
gaohuatao 已提交
423 424
}

G
gaohuatao 已提交
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 496 497 498
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 已提交
499 500
int devmapper_get_driver_status(const struct graphdriver *driver, struct graphdriver_status *status)
{
501 502
    int ret = 0;
    struct status *st = NULL;
G
gaohuatao 已提交
503
    char *status_str = NULL;
504 505 506 507 508

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

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

    status->driver_name = util_strdup_s(driver->name);
G
gaohuatao 已提交
517 518 519 520 521 522 523
    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;
    }
524

G
gaohuatao 已提交
525
out:
526
    free_devmapper_status(st);
G
gaohuatao 已提交
527
    free(status_str);
528
    return ret;
529
}
G
gaohuatao 已提交
530 531 532 533

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

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

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

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;
}