软硬件开发技术笔记
保持专注,拒绝内耗
/dev/sda is write-protected, mounting read-only解释
2021-12-05 19:20
[root@dev27 ~]# mount /dev/vdg /mnt
mount: /dev/vdg is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/vdg,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

寻找mount源码来源util-linux-xxx/sys-utils/mount.cmk_exit_code函数

static int mk_exit_code(struct libmnt_context *cxt, int rc)
{
    int syserr;
    struct stat st;
    unsigned long uflags = 0, mflags = 0;

    int restricted = mnt_context_is_restricted(cxt);
    const char *tgt = mnt_context_get_target(cxt);
    const char *src = mnt_context_get_source(cxt);

try_readonly:
    if (mnt_context_helper_executed(cxt)) {

...
    case EACCES:
    case EROFS:
        if (mflags & MS_RDONLY)
            warnx(_("cannot mount %s read-only"), src);

        else if (readwrite)
            warnx(_("%s is write-protected but explicit `-w' flag given"), src);

        else if (mflags & MS_REMOUNT)
            warnx(_("cannot remount %s read-write, is write-protected"), src);

        else {
            warnx(_("%s is write-protected, mounting read-only"), src);

            mnt_context_reset_status(cxt);
            mnt_context_set_mflags(cxt, mflags | MS_RDONLY);
            rc = mnt_context_do_mount(cxt);
            if (!rc)
                rc = mnt_context_finalize_mount(cxt);

            goto try_readonly;
        }
        break;

    case ENOMEDIUM:
        warnx(_("no medium found on %s"), src);
        break;
...

结论:
如代码所示,当底层存储返回EACCESEROFS错误返回码,会给出相应的提示,然后通过goto try_readonly;重新以readonly的方式尝试挂载。
所以mount只是在第一次挂载是感知到错误后,尝试以readonly的方式挂载。

分类
mac
1篇
4篇
c
1篇
4篇
2篇
1篇
搜索