restore.c 17.7 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. 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
O
overweight 已提交
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.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide container list callback function definition
 ********************************************************************************/

#include <stdio.h>
#include <dlfcn.h>
#include <unistd.h>
#include <limits.h>
#include <pthread.h>

L
LiuHao 已提交
22
#include "isulad_config.h"
H
haozi007 已提交
23
#include "isula_libutils/log.h"
O
overweight 已提交
24
#include "restore.h"
L
lifeng68 已提交
25
#include "container_api.h"
O
overweight 已提交
26 27 28 29
#include "supervisor.h"
#include "containers_gc.h"
#include "container_unix.h"
#include "error.h"
L
lifeng68 已提交
30
#include "image_api.h"
31 32
#include "runtime_api.h"
#include "service_container_api.h"
L
lifeng68 已提交
33 34
#include "restartmanager.h"
#include "health_check.h"
O
overweight 已提交
35 36

/* restore supervisor */
L
LiFeng 已提交
37
static int restore_supervisor(const container_t *cont)
O
overweight 已提交
38 39 40 41 42 43
{
    int ret = 0;
    int nret = 0;
    int exit_fifo_fd = -1;
    char container_state[PATH_MAX] = { 0 };
    char *exit_fifo = NULL;
L
LiFeng 已提交
44 45 46 47
    char *id = cont->common_config->id;
    char *statepath = cont->state_path;
    char *runtime = cont->runtime;
    container_pid_t pid_info = { 0 };
O
overweight 已提交
48

O
openeuler-iSula 已提交
49 50
    nret = snprintf(container_state, sizeof(container_state), "%s/%s", statepath, id);
    if (nret < 0 || (size_t)nret >= sizeof(container_state)) {
O
overweight 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        ERROR("Failed to sprintf container state %s/%s", statepath, id);
        ret = -1;
        goto out;
    }

    exit_fifo = exit_fifo_name(container_state);
    if (exit_fifo == NULL) {
        ERROR("Failed to get exit fifo name %s/%s", statepath, id);
        ret = -1;
        goto out;
    }

    exit_fifo_fd = exit_fifo_open(exit_fifo);
    if (exit_fifo_fd < 0) {
        ERROR("Failed to open exit FIFO %s", exit_fifo);
        ret = -1;
        goto out;
    }

L
LiFeng 已提交
70 71 72 73
    pid_info.pid = cont->state->state->pid;
    pid_info.ppid = cont->state->state->p_pid;
    pid_info.start_time = cont->state->state->start_time;
    pid_info.pstart_time = cont->state->state->p_start_time;
O
overweight 已提交
74

L
LiFeng 已提交
75
    if (supervisor_add_exit_monitor(exit_fifo_fd, &pid_info, id, runtime)) {
O
overweight 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        ERROR("Failed to add exit monitor to supervisor");
        ret = -1;
        goto out;
    }

out:
    free(exit_fifo);

    return ret;
}

/* post stopped container to gc */
static int post_stopped_container_to_gc(const char *id, const char *runtime, const char *statepath, uint32_t pid)
{
    int ret = 0;
L
LiFeng 已提交
91
    container_pid_t pid_info = { 0 };
O
overweight 已提交
92

L
LiFeng 已提交
93
    (void)container_read_proc(pid, &pid_info);
O
overweight 已提交
94

L
LiFeng 已提交
95
    if (gc_add_container(id, runtime, &pid_info)) {
O
overweight 已提交
96 97 98 99 100 101 102 103 104 105
        ERROR("Failed to post container %s to garbage collector", id);
        ret = -1;
        goto out;
    }

out:
    return ret;
}

static void post_nonexist_image_containers(const container_t *cont, Container_Status status,
106
                                           const struct runtime_container_status_info *info)
O
overweight 已提交
107 108 109 110
{
    int nret;
    const char *id = cont->common_config->id;

111
    if (info->status == RUNTIME_CONTAINER_STATUS_STOPPED) {
112
        if (status != CONTAINER_STATUS_STOPPED && status != CONTAINER_STATUS_CREATED) {
O
overweight 已提交
113 114 115 116
            nret = post_stopped_container_to_gc(id, cont->runtime, cont->state_path, 0);
            if (nret != 0) {
                ERROR("Failed to post container %s to garbage"
                      "collector, that may lost some resources"
117 118
                      "used with container!",
                      id);
O
overweight 已提交
119 120 121
            }
            state_set_stopped(cont->state, 255);
        }
122
    } else if (info->status == RUNTIME_CONTAINER_STATUS_RUNNING) {
O
overweight 已提交
123 124 125 126
        nret = post_stopped_container_to_gc(id, cont->runtime, cont->state_path, info->pid);
        if (nret != 0) {
            ERROR("Failed to post container %s to garbage"
                  "collector, that may lost some resources"
127 128
                  "used with container!",
                  id);
O
overweight 已提交
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
        }
        state_set_stopped(cont->state, 255);
    } else {
        ERROR("Container %s get invalid status %d", id, info->status);
    }

    return;
}

static int check_container_image_exist(const container_t *cont)
{
    int ret = 0;
    char *tmp = NULL;
    const char *id = cont->common_config->id;
    const char *image_name = cont->common_config->image;
    const char *image_type = cont->common_config->image_type;

    if (image_type == NULL || image_name == NULL) {
        ERROR("Failed to get image type for container %s", id);
        ret = -1;
        goto out;
    }

    /* only check exist for oci image */
    if (strcmp(image_type, IMAGE_TYPE_OCI) == 0) {
D
dogsheng 已提交
154 155
        ret = im_resolv_image_name(image_type, image_name, &tmp);
        if (ret != 0) {
O
overweight 已提交
156 157 158
            ERROR("Failed to resolve image %s", image_name);
            goto out;
        }
L
LiFeng 已提交
159

160
        if (!im_oci_image_exist(tmp)) {
O
overweight 已提交
161 162 163 164 165 166 167 168 169 170 171
            WARN("Image %s not exist", tmp);
            ret = -1;
            goto out;
        }
    }

out:
    free(tmp);
    return ret;
}

L
LiFeng 已提交
172
static bool is_same_process(const container_t *cont, const container_pid_t *pid_info)
O
overweight 已提交
173
{
174
    if (pid_info->pid == cont->state->state->pid && pid_info->ppid == cont->state->state->p_pid &&
L
LiFeng 已提交
175 176 177
        pid_info->start_time == cont->state->state->start_time &&
        pid_info->pstart_time == cont->state->state->p_start_time) {
        return true;
O
overweight 已提交
178
    }
L
LiFeng 已提交
179
    return false;
O
overweight 已提交
180 181
}

D
dogsheng 已提交
182 183 184
static void try_to_set_paused_container_pid(Container_Status status, const container_t *cont,
                                            const container_pid_t *pid_info)
{
L
LiFeng 已提交
185
    if (status != CONTAINER_STATUS_RUNNING || !is_same_process(cont, pid_info)) {
D
dogsheng 已提交
186 187 188 189
        state_set_running(cont->state, pid_info, false);
    }
}

190
static void try_to_set_container_running(Container_Status status, container_t *cont, const container_pid_t *pid_info)
O
overweight 已提交
191
{
L
LiFeng 已提交
192 193
    if (status != CONTAINER_STATUS_RUNNING || !is_same_process(cont, pid_info)) {
        state_set_running(cont->state, pid_info, true);
O
overweight 已提交
194 195 196 197 198 199
    }
}

static int restore_stopped_container(Container_Status status, const container_t *cont, bool *need_save)
{
    const char *id = cont->common_config->id;
L
LiFeng 已提交
200
    pid_t pid = 0;
O
overweight 已提交
201

202
    if (status != CONTAINER_STATUS_STOPPED && status != CONTAINER_STATUS_CREATED) {
L
LiFeng 已提交
203 204 205 206
        if (util_process_alive(cont->state->state->pid, cont->state->state->start_time)) {
            pid = cont->state->state->pid;
        }
        int nret = post_stopped_container_to_gc(id, cont->runtime, cont->state_path, pid);
O
overweight 已提交
207 208 209
        if (nret != 0) {
            ERROR("Failed to post container %s to garbage"
                  "collector, that may lost some resources"
210 211
                  "used with container!",
                  id);
O
overweight 已提交
212 213 214 215 216 217 218 219 220
        }
        state_set_stopped(cont->state, 255);
        *need_save = true;
    }

    return 0;
}

static int restore_running_container(Container_Status status, container_t *cont,
221
                                     const struct runtime_container_status_info *info)
O
overweight 已提交
222 223
{
    int ret = 0;
L
LiFeng 已提交
224
    int nret = 0;
O
overweight 已提交
225
    const char *id = cont->common_config->id;
L
LiFeng 已提交
226
    container_pid_t pid_info = { 0 };
O
overweight 已提交
227

L
LiFeng 已提交
228 229 230 231 232 233
    nret = container_read_proc(info->pid, &pid_info);
    if (nret == 0) {
        try_to_set_container_running(status, cont, &pid_info);
    } else {
        ERROR("Failed to restore container:%s due to unable to read container pid information", id);
        nret = post_stopped_container_to_gc(id, cont->runtime, cont->state_path, 0);
O
overweight 已提交
234 235 236
        if (nret != 0) {
            ERROR("Failed to post container %s to garbage"
                  "collector, that may lost some resources"
237 238
                  "used with container!",
                  id);
O
overweight 已提交
239 240 241 242
        }
        ret = -1;
        goto out;
    }
L
LiFeng 已提交
243

O
overweight 已提交
244 245 246 247 248 249
    container_reset_manually_stopped(cont);

out:
    return ret;
}

D
dogsheng 已提交
250
static int restore_paused_container(Container_Status status, container_t *cont,
251
                                    const struct runtime_container_status_info *info)
D
dogsheng 已提交
252 253
{
    int ret = 0;
L
LiFeng 已提交
254
    int nret = 0;
D
dogsheng 已提交
255
    const char *id = cont->common_config->id;
L
LiFeng 已提交
256
    container_pid_t pid_info = { 0 };
D
dogsheng 已提交
257 258 259

    state_set_paused(cont->state);

L
LiFeng 已提交
260 261 262 263 264 265
    nret = container_read_proc(info->pid, &pid_info);
    if (nret == 0) {
        try_to_set_paused_container_pid(status, cont, &pid_info);
    } else {
        ERROR("Failed to restore container:%s due to unable to read container pid information", id);
        nret = post_stopped_container_to_gc(id, cont->runtime, cont->state_path, 0);
D
dogsheng 已提交
266 267 268
        if (nret != 0) {
            ERROR("Failed to post container %s to garbage"
                  "collector, that may lost some resources"
269 270
                  "used with container!",
                  id);
D
dogsheng 已提交
271 272 273 274
        }
        ret = -1;
        goto out;
    }
L
LiFeng 已提交
275

D
dogsheng 已提交
276 277 278 279 280 281
    container_reset_manually_stopped(cont);

out:
    return ret;
}

O
overweight 已提交
282
/* restore state */
L
LiFeng 已提交
283
static int restore_state(container_t *cont)
O
overweight 已提交
284 285
{
    int ret = 0;
L
LiFeng 已提交
286
    int nret = 0;
O
overweight 已提交
287 288
    bool need_save = false;
    const char *id = cont->common_config->id;
L
LiFeng 已提交
289 290
    const char *runtime = cont->runtime;
    rt_status_params_t params = { 0 };
291
    struct runtime_container_status_info real_status = { 0 };
292
    Container_Status status = state_get_status(cont->state);
O
overweight 已提交
293 294 295 296 297

    (void)container_exit_on_next(cont); /* cancel restart policy */

    if (check_container_image_exist(cont) != 0) {
        ERROR("Failed to restore container:%s due to image not exist", id);
L
LiFeng 已提交
298
        post_nonexist_image_containers(cont, status, &real_status);
O
overweight 已提交
299 300 301 302
        ret = -1;
        goto out;
    }

303
    params.rootpath = cont->root_path;
304
    params.state = cont->state_path;
305 306
    nret = runtime_status(id, runtime, &params, &real_status);
    if (nret != 0) {
307 308
        ERROR("Failed to restore container %s, make real status to STOPPED. Due to can not load container with status %d",
              id, status);
309
        real_status.status = RUNTIME_CONTAINER_STATUS_STOPPED;
310 311
    }

312
    if (real_status.status == RUNTIME_CONTAINER_STATUS_STOPPED) {
O
overweight 已提交
313 314 315 316
        ret = restore_stopped_container(status, cont, &need_save);
        if (ret != 0) {
            goto out;
        }
317
    } else if (real_status.status == RUNTIME_CONTAINER_STATUS_RUNNING) {
L
LiFeng 已提交
318
        ret = restore_running_container(status, cont, &real_status);
O
overweight 已提交
319 320 321
        if (ret != 0) {
            goto out;
        }
322
    } else if (real_status.status == RUNTIME_CONTAINER_STATUS_PAUSED) {
L
LiFeng 已提交
323
        ret = restore_paused_container(status, cont, &real_status);
D
dogsheng 已提交
324 325 326
        if (ret != 0) {
            goto out;
        }
O
overweight 已提交
327
    } else {
L
LiFeng 已提交
328 329 330
        ERROR("Container %s get invalid status %d", id, real_status.status);
        ret = -1;
        goto out;
O
overweight 已提交
331 332
    }

333
out:
O
overweight 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    if (is_removal_in_progress(cont->state)) {
        state_reset_removal_in_progress(cont->state);
        need_save = true;
    }
    if (need_save && container_to_disk(cont) != 0) {
        ERROR("Failed to re-save container \"%s\" to disk", id);
        ret = -1;
    }
    return ret;
}

/* remove invalid container */
static int remove_invalid_container(const container_t *cont, const char *runtime, const char *root, const char *state,
                                    const char *id)
{
    int ret = 0;
    char container_root[PATH_MAX] = { 0x00 };
    char container_state[PATH_MAX] = { 0x00 };

O
openeuler-iSula 已提交
353 354
    ret = snprintf(container_state, sizeof(container_state), "%s/%s", state, id);
    if (ret < 0 || (size_t)ret >= sizeof(container_state)) {
O
overweight 已提交
355 356 357 358 359 360 361 362 363 364 365
        ERROR("Failed to sprintf container state %s/%s", state, id);
        ret = -1;
        goto out;
    }
    ret = util_recursive_rmdir(container_state, 0);
    if (ret != 0) {
        ERROR("Failed to delete container's state directory %s", container_state);
        ret = -1;
        goto out;
    }

H
haozi007 已提交
366 367 368 369 370 371 372
    ret = cleanup_mounts_by_id(id, root);
    if (ret != 0) {
        ERROR("Failed to clean container's mounts");
        ret = -1;
        goto out;
    }

O
openeuler-iSula 已提交
373 374
    ret = snprintf(container_root, sizeof(container_root), "%s/%s", root, id);
    if (ret < 0 || (size_t)ret >= sizeof(container_root)) {
O
overweight 已提交
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
        ERROR("Failed to sprintf invalid root directory %s/%s", root, id);
        ret = -1;
        goto out;
    }

    if (cont != NULL && im_remove_container_rootfs(cont->common_config->image_type, id)) {
        ERROR("Failed to remove rootfs for container %s", id);
        ret = -1;
        goto out;
    }

    ret = util_recursive_rmdir(container_root, 0);
    if (ret != 0) {
        ERROR("Failed to delete container's state directory %s", container_state);
        ret = -1;
        goto out;
    }
out:
    return ret;
}

static void restored_restart_container(container_t *cont)
{
    char *id = NULL;
    char *started_at = NULL;
    uint64_t timeout = 0;

    id = cont->common_config->id;

    started_at = state_get_started_at(cont->state);
    if (restart_manager_should_restart(id, state_get_exitcode(cont->state),
406
                                       cont->common_config->has_been_manually_stopped, time_seconds_since(started_at),
O
overweight 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
                                       &timeout)) {
        cont->common_config->restart_count++;
        INFO("Restart container %s after 5 second", id);
        (void)container_restart_in_thread(id, 5ULL * Time_Second, (int)state_get_exitcode(cont->state));
    }
    free(started_at);
}

/* handle restored container */
static void handle_restored_container()
{
    int ret = 0;
    size_t i = 0;
    size_t container_num = 0;
    char *id = NULL;
    container_t **conts = NULL;
    container_t *cont = NULL;

    ret = containers_store_list(&conts, &container_num);
    if (ret != 0) {
        ERROR("query all containers info failed");
        return;
    }

    for (i = 0; i < container_num; i++) {
        cont = conts[i];
        container_lock(cont);

        (void)reset_restart_manager(cont, false);

        id = cont->common_config->id;

        if (is_running(cont->state)) {
L
LiFeng 已提交
440
            if (restore_supervisor(cont)) {
O
overweight 已提交
441 442 443 444 445 446 447
                ERROR("Failed to restore %s supervisor", id);
            }
            init_health_monitor(id);
        } else {
            if (cont->hostconfig != NULL && cont->hostconfig->auto_remove_bak) {
                (void)set_container_to_removal(cont);
                container_unlock(cont);
L
lifeng68 已提交
448
                (void)delete_container(cont, true);
O
overweight 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
                container_lock(cont);
            } else {
                restored_restart_container(cont);
            }
        }

        container_unlock(cont);
        container_unref(cont);
    }

    free(conts);
    return;
}

/* scan dir to add store */
static void scan_dir_to_add_store(const char *runtime, const char *rootpath, const char *statepath,
L
LiFeng 已提交
465
                                  const size_t subdir_num, const char **subdir)
O
overweight 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479
{
    size_t i = 0;
    container_t *cont = NULL;

    for (i = 0; i < subdir_num; i++) {
        cont = NULL;
        bool aret = false;
        bool index_flag = false;
        cont = container_load(runtime, rootpath, statepath, subdir[i]);
        if (cont == NULL) {
            ERROR("Failed to load subdir:%s", subdir[i]);
            goto error_load;
        }

L
LiFeng 已提交
480
        if (restore_state(cont)) {
O
overweight 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
            WARN("Failed to restore container %s state", subdir[i]);
            goto error_load;
        }

        index_flag = name_index_add(cont->common_config->name, cont->common_config->id);
        if (!index_flag) {
            ERROR("Failed add %s into name indexs", subdir[i]);
            goto error_load;
        }
        aret = containers_store_add(cont->common_config->id, cont);
        if (!aret) {
            ERROR("Failed add container %s to store", subdir[i]);
            goto error_load;
        }

        continue;
L
lifeng68 已提交
497
    error_load:
O
overweight 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
        if (remove_invalid_container(cont, runtime, rootpath, statepath, subdir[i])) {
            ERROR("Failed to delete subdir:%s", subdir[i]);
        }
        container_unref(cont);

        if (index_flag) {
            name_index_remove(subdir[i]);
        }
        continue;
    }
}

/* restore container by runtime */
static int restore_container_by_runtime(const char *runtime)
{
    int ret = 0;
    char *rootpath = NULL;
    char *statepath = NULL;
    size_t subdir_num = 0;
    char **subdir = NULL;

    rootpath = conf_get_routine_rootdir(runtime);
    if (rootpath == NULL) {
        ERROR("Root path is NULL");
        ret = -1;
        goto out;
    }

    statepath = conf_get_routine_statedir(runtime);
    if (statepath == NULL) {
        ERROR("State path is NULL");
        ret = -1;
        goto out;
    }

    ret = util_list_all_subdir(rootpath, &subdir);
    if (ret != 0) {
        ERROR("Failed to read %s'subdirectory", rootpath);
        ret = -1;
        goto out;
    }
Z
zhuchunyi 已提交
539
    subdir_num = util_array_len((const char **)subdir);
O
overweight 已提交
540 541 542 543
    if (subdir_num == 0) {
        goto out;
    }

L
LiFeng 已提交
544
    scan_dir_to_add_store(runtime, rootpath, statepath, subdir_num, (const char **)subdir);
O
overweight 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

out:
    free(rootpath);
    free(statepath);
    util_free_array(subdir);
    return ret;
}

/* containers restore */
void containers_restore(void)
{
    int ret = 0;
    size_t subdir_num = 0;
    size_t i = 0;
    char *engines_path = NULL;
    char **subdir = NULL;

    engines_path = conf_get_engine_rootpath();
    if (engines_path == NULL) {
        ERROR("Failed to get engines path");
        goto out;
    }

    ret = util_list_all_subdir(engines_path, &subdir);
    if (ret != 0) {
        ERROR("Failed to list engines");
        goto out;
    }
Z
zhuchunyi 已提交
573
    subdir_num = util_array_len((const char **)subdir);
O
overweight 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589

    for (i = 0; i < subdir_num; i++) {
        DEBUG("Restore the containers by runtime:%s", subdir[i]);
        ret = restore_container_by_runtime(subdir[i]);
        if (ret != 0) {
            ERROR("Failed to restore containers by runtime:%s", subdir[i]);
        }
    }

    handle_restored_container();

out:
    free(engines_path);
    util_free_array(subdir);
    return;
}