tytso / e2fsprogs

Ext2/3/4 file system utilities
http://ext4.wiki.kernel.org
374 stars 221 forks source link

e2mmpstatus man page has some errors #106

Open hifilove opened 2 years ago

hifilove commented 2 years ago

e2mmpstatus man page show e2mmpstatus can use UUID and LABEL, But it is wrong when try to use UUID and LABEL.

man e2mmpstatus DESCRIPTION e2mmpstatus is used to check Multiple-Mount Protection (MMP) status of an ext4 filesystem with the mmp feature enabled. The specified filesystem can be a device name (e.g./dev/hdc1, /dev/sdb2), or an ext4 filesystem label or UUID, for example UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd or LABEL=root. By default, the e2mmpstatus program checks whether it is safe to mount the filesystem without taking the risk of mounting it more than once. blkid /dev/sdc: UUID="9e2f89c1-6baa-4a99-a385-3f4f3abccf21" BLOCK_SIZE="4096" TYPE="ext4" e2mmpstatus 9e2f89c1-6baa-4a99-a385-3f4f3abccf21 e2mmpstatus: No such file or directory while trying to open 9e2f89c1-6baa-4a99-a385-3f4f3abccf21 Couldn't find valid filesystem superblock.

Because the device_name parameter is not converted before the ext2fs_open function call. tune2fs supports this because tune2fs will use the parse_tune2fs_options->blkid_get_devname function to parse UUID and LABEL.

ljluestc commented 1 year ago

Here's a conceptual example of how you can modify the code to handle UUID and LABEL:

#include <stdio.h>
#include <stdlib.h>
#include <blkid/blkid.h>
#include <ext2fs/ext2_fs.h>

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <UUID or LABEL>\n", argv[0]);
        return 1;
    }

    const char *devname = argv[1];

    /* Check if the input is a UUID or LABEL */
    blkid_cache cache = NULL;
    blkid_dev dev = NULL;

    cache = blkid_new_cache();

    if (blkid_get_cache(cache) < 0) {
        fprintf(stderr, "Failed to initialize blkid cache.\n");
        return 1;
    }

    dev = blkid_get_dev(cache, devname);

    if (dev != NULL) {
        devname = blkid_dev_devname(dev);
    }

    /* Now 'devname' contains the device name based on UUID or LABEL */

    // Rest of your e2mmpstatus code here, using 'devname' as the device name.

    blkid_put_cache(cache);
    return 0;
}

In this conceptual code snippet:

  1. We use the blkid library to check if the input is a UUID or LABEL and convert it into a device name.
  2. blkid_get_dev checks if the input is a UUID or LABEL and, if found, retrieves the corresponding device name.
  3. The rest of your e2mmpstatus code can then use the converted devname as the device name for further processing.