fstab_mount.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
X
xionglei6 已提交
25
#include "beget_ext.h"
26 27 28 29
#include "fs_manager/fs_manager.h"
#include "init_utils.h"
#include "securec.h"

S
sun_fan 已提交
30 31 32 33 34 35
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

36
#define FS_MANAGER_BUFFER_SIZE 512
S
sun_fan 已提交
37
#define BLOCK_SIZE_BUFFER (64)
38
#define RESIZE_BUFFER_SIZE 1024
S
sun_fan 已提交
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
bool IsSupportedFilesystem(const char *fsType)
{
    static const char *supportedFilesystem[] = {"ext4", "f2fs", NULL};

    bool supported = false;
    int index = 0;
    if (fsType != NULL) {
        while (supportedFilesystem[index] != NULL) {
            if (strcmp(supportedFilesystem[index++], fsType) == 0) {
                supported = true;
                break;
            }
        }
    }
    return supported;
}

static int ExecCommand(int argc, char **argv)
{
    if (argc == 0 || argv == NULL || argv[0] == NULL) {
        return -1;
    }
    pid_t pid = fork();
    if (pid < 0) {
X
xionglei6 已提交
64
        BEGET_LOGE("Fork new process to format failed: %d", errno);
65 66 67 68 69 70 71 72 73
        return -1;
    }
    if (pid == 0) {
        execv(argv[0], argv);
        exit(-1);
    }
    int status;
    waitpid(pid, &status, 0);
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
X
xionglei6 已提交
74
        BEGET_LOGE("Command %s failed with status %d", argv[0], WEXITSTATUS(status));
75 76 77 78 79 80 81 82 83 84 85
    }
    return WEXITSTATUS(status);
}

int DoFormat(const char *devPath, const char *fsType)
{
    if (devPath == NULL || fsType == NULL) {
        return -1;
    }

    if (!IsSupportedFilesystem(fsType)) {
X
xionglei6 已提交
86
        BEGET_LOGE("Do not support filesystem \" %s \"", fsType);
87 88 89 90 91 92 93
        return -1;
    }
    int ret = 0;
    char blockSizeBuffer[BLOCK_SIZE_BUFFER] = {0};
    if (strcmp(fsType, "ext4") == 0) {
        const unsigned int blockSize = 4096;
        if (snprintf_s(blockSizeBuffer, BLOCK_SIZE_BUFFER, BLOCK_SIZE_BUFFER - 1, "%u", blockSize) == -1) {
X
xionglei6 已提交
94
            BEGET_LOGE("Failed to build block size buffer");
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
            return -1;
        }
        char *formatCmds[] = {
            "/bin/mke2fs", "-F", "-t", (char *)fsType, "-b", blockSizeBuffer, (char *)devPath, NULL
        };
        int argc = ARRAY_LENGTH(formatCmds);
        char **argv = (char **)formatCmds;
        ret = ExecCommand(argc, argv);
    } else if (strcmp(fsType, "f2fs") == 0) {
        char *formatCmds[] = {
            "/bin/make_f2fs", (char *)devPath, NULL
        };
        int argc = ARRAY_LENGTH(formatCmds);
        char **argv = (char **)formatCmds;
        ret = ExecCommand(argc, argv);
    }
    return ret;
}

MountStatus GetMountStatusForMountPoint(const char *mp)
{
    if (mp == NULL) {
        return MOUNT_ERROR;
    }
    char buffer[FS_MANAGER_BUFFER_SIZE] = {0};
    const int expectedItems = 6;
    int count = 0;
    char **mountItems = NULL;
    MountStatus status = MOUNT_ERROR;
    bool found = false;

    FILE *fp = fopen("/proc/mounts", "r");
    if (fp == NULL) {
        return status;
    }
    while (fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
X
xionglei6 已提交
131
        size_t n = strlen(buffer);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        if (buffer[n - 1] == '\n') {
            buffer[n - 1] = '\0';
        }
        mountItems = SplitStringExt(buffer, " ", &count, expectedItems);
        if (mountItems != NULL && count == expectedItems) {
            // Second item in /proc/mounts is mount point
            if (strcmp(mountItems[1], mp) == 0) {
                FreeStringVector(mountItems, count);
                found = true;
                break;
            }
            FreeStringVector(mountItems, count);
        }
    }
    if (found == true) {
        status = MOUNT_MOUNTED;
    } else if (feof(fp) > 0) {
        status = MOUNT_UMOUNTED;
    }
S
sun_fan 已提交
151
    (void)fclose(fp);
152 153 154 155
    fp = NULL;
    return status;
}

156 157 158 159
static int DoResizeF2fs(const char* device, const unsigned long long size)
{
    char *file = "/system/bin/resize.f2fs";
    if (access(file, F_OK) != 0) {
X
xionglei6 已提交
160
        BEGET_LOGE("resize.f2fs is not exists.");
161 162 163 164 165 166 167 168 169 170 171 172
        return -1;
    }

    int ret = 0;
    if (size <= 0) {
        char *cmd[] = {
            file, "-s", (char *)device, NULL
        };
        int argc = ARRAY_LENGTH(cmd);
        char **argv = (char **)cmd;
        ret = ExecCommand(argc, argv);
    } else {
X
xionglei6 已提交
173 174
        unsigned long long realSize = size *
            ((unsigned long long)RESIZE_BUFFER_SIZE * RESIZE_BUFFER_SIZE / FS_MANAGER_BUFFER_SIZE);
175
        char sizeStr[RESIZE_BUFFER_SIZE] = {0};
176
        sprintf_s(sizeStr, RESIZE_BUFFER_SIZE, "%llu", realSize);
177 178 179 180 181 182 183
        char *cmd[] = {
            file, "-s", "-t", sizeStr, (char *)device, NULL
        };
        int argc = ARRAY_LENGTH(cmd);
        char **argv = (char **)cmd;
        ret = ExecCommand(argc, argv);
    }
X
xionglei6 已提交
184
    BEGET_LOGI("resize.f2fs is ending.");
185 186 187 188 189 190 191
    return ret;
}

static int DoFsckF2fs(const char* device)
{
    char *file = "/system/bin/fsck.f2fs";
    if (access(file, F_OK) != 0) {
X
xionglei6 已提交
192
        BEGET_LOGE("fsck.f2fs is not exists.");
193 194 195 196 197 198 199 200
        return -1;
    }

    char *cmd[] = {
        file, "-a", (char *)device, NULL
    };
    int argc = ARRAY_LENGTH(cmd);
    char **argv = (char **)cmd;
X
xionglei6 已提交
201
    BEGET_LOGI("fsck.f2fs is ending.");
X
xlfeng 已提交
202
    return ExecCommand(argc, argv);
203 204 205 206 207 208
}

static int DoResizeExt(const char* device, const unsigned long long size)
{
    char *file = "/system/bin/resize2fs";
    if (access(file, F_OK) != 0) {
X
xionglei6 已提交
209
        BEGET_LOGE("resize2fs is not exists.");
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        return -1;
    }

    int ret = 0;
    if (size <= 0) {
        char *cmd[] = {
            file, "-f", (char *)device, NULL
        };
        int argc = ARRAY_LENGTH(cmd);
        char **argv = (char **)cmd;
        ret = ExecCommand(argc, argv);
    } else {
        char sizeStr[RESIZE_BUFFER_SIZE] = {0};
        sprintf_s(sizeStr, RESIZE_BUFFER_SIZE, "%lluM", size);
        char *cmd[] = {
            file, "-f", (char *)device, sizeStr, NULL
        };
        int argc = ARRAY_LENGTH(cmd);
        char **argv = (char **)cmd;
        ret = ExecCommand(argc, argv);
    }
X
xionglei6 已提交
231
    BEGET_LOGI("resize2fs is ending.");
232 233 234 235 236 237 238
    return ret;
}

static int DoFsckExt(const char* device)
{
    char *file = "/system/bin/e2fsck";
    if (access(file, F_OK) != 0) {
X
xionglei6 已提交
239
        BEGET_LOGE("e2fsck is not exists.");
240 241 242 243 244 245 246 247
        return -1;
    }

    char *cmd[] = {
        file, "-y", (char *)device, NULL
    };
    int argc = ARRAY_LENGTH(cmd);
    char **argv = (char **)cmd;
X
xionglei6 已提交
248
    BEGET_LOGI("e2fsck is ending.");
249 250 251
    return ExecCommand(argc, argv);
}

252 253 254 255 256 257 258
static int Mount(const char *source, const char *target, const char *fsType,
    unsigned long flags, const char *data)
{
    struct stat st = {};
    int rc = -1;

    if (source == NULL || target == NULL || fsType == NULL) {
X
xionglei6 已提交
259
        BEGET_LOGE("Invalid argment for mount.");
260 261 262
        return -1;
    }
    if (stat(target, &st) != 0 && errno != ENOENT) {
X
xionglei6 已提交
263
        BEGET_LOGE("Cannot get stat of \" %s \", err = %d", target, errno);
264 265 266 267 268 269 270
        return -1;
    }
    if ((st.st_mode & S_IFMT) == S_IFLNK) { // link, delete it.
        unlink(target);
    }
    if (mkdir(target, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
        if (errno != EEXIST) {
X
xionglei6 已提交
271
            BEGET_LOGE("Failed to create dir \" %s \", err = %d", target, errno);
272 273 274 275
            return -1;
        }
    }
    errno = 0;
S
sun_fan 已提交
276
    while ((rc = mount(source, target, fsType, flags, data)) != 0) {
277
        if (errno == EAGAIN) {
X
xionglei6 已提交
278
            BEGET_LOGE("Mount %s to %s failed. try again", source, target);
279 280
            continue;
        }
281 282 283 284
        if (errno == EBUSY) {
            rc = 0;
        }
        break;
285 286 287 288 289 290 291 292 293 294 295 296 297 298
    }
    return rc;
}

int MountOneItem(FstabItem *item)
{
    if (item == NULL) {
        return -1;
    }
    unsigned long mountFlags;
    char fsSpecificData[FS_MANAGER_BUFFER_SIZE] = {0};

    mountFlags = GetMountFlags(item->mountOptions, fsSpecificData, sizeof(fsSpecificData));
    if (!IsSupportedFilesystem(item->fsType)) {
X
xionglei6 已提交
299
        BEGET_LOGE("Unsupported file system \" %s \"", item->fsType);
X
xionglei6 已提交
300
        return 0;
301 302
    }
    if (FM_MANAGER_WAIT_ENABLED(item->fsManagerFlags)) {
H
huangshan 已提交
303
        WaitForFile(item->deviceName, WAIT_MAX_SECOND);
304
    }
305 306 307 308

    if (strcmp(item->fsType, "f2fs") == 0 && strcmp(item->mountPoint, "/data") == 0) {
        int ret = DoResizeF2fs(item->deviceName, 0);
        if (ret != 0) {
X
xionglei6 已提交
309
            BEGET_LOGE("Failed to resize.f2fs dir %s , ret = %d", item->deviceName, ret);
310 311 312 313
        }

        ret = DoFsckF2fs(item->deviceName);
        if (ret != 0) {
X
xionglei6 已提交
314
            BEGET_LOGE("Failed to fsck.f2fs dir %s , ret = %d", item->deviceName, ret);
315 316 317 318
        }
    } else if (strcmp(item->fsType, "ext4") == 0 && strcmp(item->mountPoint, "/data") == 0) {
        int ret = DoResizeExt(item->deviceName, 0);
        if (ret != 0) {
X
xionglei6 已提交
319
            BEGET_LOGE("Failed to resize2fs dir %s , ret = %d", item->deviceName, ret);
320 321 322
        }
        ret = DoFsckExt(item->deviceName);
        if (ret != 0) {
X
xionglei6 已提交
323
            BEGET_LOGE("Failed to e2fsck dir %s , ret = %d", item->deviceName, ret);
324 325 326
        }
    }

327 328
    int rc = Mount(item->deviceName, item->mountPoint, item->fsType, mountFlags, fsSpecificData);
    if (rc != 0) {
X
xionglei6 已提交
329
        BEGET_LOGE("Mount %s to %s failed %d", item->deviceName, item->mountPoint, errno);
330
    } else {
X
xionglei6 已提交
331
        BEGET_LOGI("Mount %s to %s successful", item->deviceName, item->mountPoint);
332 333 334 335 336 337
    }
    return rc;
}

int CheckRequiredAndMount(FstabItem *item, bool required)
{
S
sun_fan 已提交
338
    int rc = 0;
339
    if (item == NULL) {
S
sun_fan 已提交
340
        return -1;
341 342 343 344 345 346 347 348 349 350 351 352 353
    }
    if (required) { // Mount partition during first startup.
        if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
            rc = MountOneItem(item);
        }
    } else { // Mount partition during second startup.
        if (!FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
            rc = MountOneItem(item);
        }
    }
    return rc;
}

X
xionglei6 已提交
354
int MountAllWithFstab(const Fstab *fstab, bool required)
355
{
X
xionglei6 已提交
356
    if (fstab == NULL) {
357 358 359 360 361 362 363
        return -1;
    }

    FstabItem *item = NULL;
    int rc = -1;
    for (item = fstab->head; item != NULL; item = item->next) {
        rc = CheckRequiredAndMount(item, required);
S
sun_fan 已提交
364 365 366
        if (required && (rc < 0)) { // Init fail to mount in the first stage and exit directly.
            break;
        }
367
    }
X
xionglei6 已提交
368 369 370 371 372 373 374 375 376 377
    return rc;
}

int MountAllWithFstabFile(const char *fstabFile, bool required)
{
    if (fstabFile == NULL || *fstabFile == '\0') {
        return -1;
    }
    Fstab *fstab = NULL;
    if ((fstab = ReadFstabFromFile(fstabFile, false)) == NULL) {
X
xionglei6 已提交
378
        BEGET_LOGE("[fs_manager][error] Read fstab file \" %s \" failed\n", fstabFile);
X
xionglei6 已提交
379 380 381 382
        return -1;
    }

    int rc = MountAllWithFstab(fstab, required);
383 384 385 386 387 388 389 390 391 392 393 394
    ReleaseFstab(fstab);
    fstab = NULL;
    return rc;
}

int UmountAllWithFstabFile(const char *fstabFile)
{
    if (fstabFile == NULL || *fstabFile == '\0') {
        return -1;
    }
    Fstab *fstab = NULL;
    if ((fstab = ReadFstabFromFile(fstabFile, false)) == NULL) {
X
xionglei6 已提交
395
        BEGET_LOGE("Read fstab file \" %s \" failed.", fstabFile);
S
sun_fan 已提交
396
        return -1;
397 398 399 400 401
    }

    FstabItem *item = NULL;
    int rc = -1;
    for (item = fstab->head; item != NULL; item = item->next) {
X
xionglei6 已提交
402
        BEGET_LOGI("Umount %s.", item->mountPoint);
403 404
        MountStatus status = GetMountStatusForMountPoint(item->mountPoint);
        if (status == MOUNT_ERROR) {
X
xionglei6 已提交
405
            BEGET_LOGW("Cannot get mount status of mount point \" %s \"", item->mountPoint);
406 407
            continue; // Cannot get mount status, just ignore it and try next one.
        } else if (status == MOUNT_UMOUNTED) {
X
xionglei6 已提交
408
            BEGET_LOGI("Mount point \" %s \" already unmounted. device path: %s, fs type: %s.",
409 410 411 412 413
                item->mountPoint, item->deviceName, item->fsType);
            continue;
        } else {
            rc = umount(item->mountPoint);
            if (rc == -1) {
X
xionglei6 已提交
414
                BEGET_LOGE("Umount %s failed, device path: %s, fs type: %s, err = %d.",
415 416
                    item->mountPoint, item->deviceName, item->fsType, errno);
            } else {
X
xionglei6 已提交
417
                BEGET_LOGE("Umount %s successfully.", item->mountPoint);
418 419 420 421 422 423 424 425 426 427 428
            }
        }
    }
    ReleaseFstab(fstab);
    fstab = NULL;
    return rc;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
X
xionglei6 已提交
429
#endif