device_setup.c 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
 * 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
 * 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.
 * See the Mulan PSL v2 for more details.
 * Author: gaohuatao
 * Create: 2020-06-12
 * Description: provide overlay2 function definition
 ******************************************************************************/
L
LiFeng 已提交
15 16
#include "device_setup.h"
#include "utils_file.h"
L
lifeng68 已提交
17
#include "isula_libutils/log.h"
G
gaohuatao 已提交
18 19
#include "utils_string.h"
#include "utils.h"
L
LiFeng 已提交
20 21 22 23 24 25 26 27

#define LVM_DISK_SCAN "lvmdiskscan"
#define PVDISPLAY "pvdisplay"

int validate_lvm_config(image_devmapper_direct_lvm_config *cfg)
{
    int ret = -1;

G
gaohuatao 已提交
28 29 30 31 32
    if (cfg == NULL) {
        ERROR("direct lvm config is empty");
        return -1;
    }

G
gaohuatao 已提交
33
    if (!util_valid_str(cfg->device)) {
L
LiFeng 已提交
34 35 36 37
        ERROR("must provide device path in `dm.directlvm_device` in order to configure direct-lvm");
        return ret;
    }

L
lifeng68 已提交
38 39
    if ((cfg->thinp_percent > 0 && cfg->thinp_meta_percent == 0) ||
        (cfg->thinp_meta_percent > 0 && cfg->thinp_percent)) {
L
LiFeng 已提交
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        ERROR("must set both `dm.thinp_percent` and `dm.thinp_metapercent` if either is specified");
        return ret;
    }

    if (cfg->thinp_percent + cfg->thinp_meta_percent > 100) {
        ERROR("combined `dm.thinp_percent` and `dm.thinp_metapercent` must not be greater than 100");
        return ret;
    }

    return 0;
}

int check_dev_available(const char *dev)
{
    // char *err = NULL;
    // char *lvm_scan_fullpath = NULL;
    // lvm_scan_fullpath = look_path("lvmdiskscan", &err);

    // TODO: exec and get output and match dev

    return 0;
}

int check_dev_invg(const char *dev)
{
    // char *err = NULL;
    // char *pvdisplay_fullpath = NULL;
    // pvdisplay_fullpath = look_path("pvdisplay", &err);

    return 0;
}

int check_dev_hasfs(const char *dev)
{
    // char *err = NULL;
    // char *blkid_fullpath = NULL;
    // pvdisplay_fullpath = look_path("blkid", &err);

    return 0;
}

int verify_block_device(const char *dev, bool force)
{
    int ret = 0;

    ret = check_dev_available(dev);
    if (ret != 0) {
        //ERROR();
        return ret;
    }

    ret = check_dev_invg(dev);
    if (ret != 0) {
        //ERROR();
        return ret;
    }

    if (force) {
        return ret;
    }

    ret = check_dev_hasfs(dev);
    if (ret != 0) {
        //ERROR();
        return ret;
    }
    return ret;
}

image_devmapper_direct_lvm_config *read_lvm_config(const char *root)
{
    parser_error err = NULL;
    image_devmapper_direct_lvm_config *cfg = NULL;
    char *path = NULL;

    path = util_path_join(root, "setup-config.json");
    if (!util_file_exists(path)) {
        goto out;
    }

    cfg = image_devmapper_direct_lvm_config_parse_file(path, NULL, &err);
    if (cfg == NULL) {
        ERROR("load setup-config.json failed %s", err != NULL ? err : "");
        goto out;
    }

out:
    free(path);
    free(err);
    return cfg;
}

int write_lvm_config(const char *root, image_devmapper_direct_lvm_config *cfg)
{
    char *path = NULL;
    int ret = 0;

    path = util_path_join(root, "setup-config.json");
    if (!util_file_exists(path)) {
        goto out;
    }
    // TODO:write to file

out:
    free(path);
    return ret;
}

int setup_direct_lvm(image_devmapper_direct_lvm_config *cfg)
{
    // char *lvm_profile_dir = "/etc/lvm/profile";
    // char *binaries[] = {"pvcreate", "vgcreate", "lvcreate", "lvconvert", "lvchange", "thin_check"};
    //lookpath
    // TODO: 执行上述命令
    return 0;
}
G
gaohuatao 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

// ProbeFsType returns the filesystem name for the given device id.
// device: /dev/mapper/%s-hash
char *probe_fs_type(const char *device)
{
    return NULL;
}

void append_mount_options(char **dest, const char *suffix)
{
    char *res_string = NULL;
    size_t length;

    if (dest == NULL) {
        // ERROR();
        return;
    }

    if (*dest == NULL) {
        *dest = util_strdup_s(suffix);
    }

    if (suffix == NULL) {
        return;
    }

    if (strlen(suffix) > ((SIZE_MAX - strlen(*dest) - strlen(",")) - 1)) {
        ERROR("String is too long to be appended");
        return;
    }

    length = strlen(*dest) + strlen(",") + strlen(suffix) + 1;
    res_string = util_common_calloc_s(length);
    if (res_string == NULL) {
        ERROR("Out of memory");
        return;
    }
    (void)strcat(res_string, *dest);
    (void)strcat(res_string, ",");
    (void)strcat(res_string, suffix);

    free(*dest);
    *dest = res_string;
}