Пример #1
0
static int get_partition_start(int fd, int partno, uint64_t *start)
{
	struct stat st;
	struct sysfs_cxt disk = UL_SYSFSCXT_EMPTY,
			 part = UL_SYSFSCXT_EMPTY;
	dev_t devno = 0;
	int rc = -1;

	/*
	 * wholedisk
	 */
	if (fstat(fd, &st) || !S_ISBLK(st.st_mode))
		goto done;
	devno = st.st_rdev;
	if (sysfs_init(&disk, devno, NULL))
		goto done;
	/*
	 * partition
	 */
	devno = sysfs_partno_to_devno(&disk, partno);
	if (!devno)
		goto done;
	if (sysfs_init(&part, devno, &disk))
		goto done;
	if (sysfs_read_u64(&part, "start", start))
		goto done;

	rc = 0;
done:
	sysfs_deinit(&part);
	sysfs_deinit(&disk);
	return rc;
}
Пример #2
0
int main(int argc, char *argv[])
{
    struct sysfs_cxt cxt = UL_SYSFSCXT_EMPTY;
    char *devname;
    dev_t devno;
    char path[PATH_MAX];
    int i, is_part;
    uint64_t u64;
    ssize_t len;

    if (argc != 2)
        errx(EXIT_FAILURE, "usage: %s <devname>", argv[0]);

    devname = argv[1];
    devno = sysfs_devname_to_devno(devname, NULL);

    if (!devno)
        err(EXIT_FAILURE, "failed to read devno");

    is_part = sysfs_devno_has_attribute(devno, "partition");

    printf("NAME: %s\n", devname);
    printf("DEVNO: %u (%d:%d)\n", (unsigned int) devno, major(devno), minor(devno));
    printf("DEVNOPATH: %s\n", sysfs_devno_path(devno, path, sizeof(path)));
    printf("DEVPATH: %s\n", sysfs_devno_to_devpath(devno, path, sizeof(path)));
    printf("PARTITION: %s\n", is_part ? "YES" : "NOT");

    if (sysfs_init(&cxt, devno, NULL))
        return EXIT_FAILURE;

    len = sysfs_readlink(&cxt, NULL, path, sizeof(path) - 1);
    if (len > 0) {
        path[len] = '\0';
        printf("DEVNOLINK: %s\n", path);
    }

    if (!is_part) {
        printf("First 5 partitions:\n");
        for (i = 1; i <= 5; i++) {
            dev_t dev = sysfs_partno_to_devno(&cxt, i);
            if (dev)
                printf("\t#%d %d:%d\n", i, major(dev), minor(dev));
        }
    }

    printf("SLAVES: %d\n", sysfs_count_dirents(&cxt, "slaves"));

    if (sysfs_read_u64(&cxt, "size", &u64))
        printf("read SIZE failed\n");
    else
        printf("SIZE: %jd\n", u64);

    if (sysfs_read_int(&cxt, "queue/hw_sector_size", &i))
        printf("read SECTOR failed\n");
    else
        printf("SECTOR: %d\n", i);

    printf("DEVNAME: %s\n", sysfs_get_devname(&cxt, path, sizeof(path)));

    sysfs_deinit(&cxt);
    return EXIT_SUCCESS;
}