Ejemplo n.º 1
0
static void blkid_fill_info(struct device_info *info, blkid_probe pr)
{
    blkid_partlist plist;

    if (device_info_verbose >= 3)
	printf("blkid_fill_info()\n");

    if (blkid_probe_is_wholedisk(pr))
	info->partition = 0;
    else
	info->partition = 1;

    plist = blkid_probe_get_partitions(pr);
    if (plist && blkid_partlist_numof_partitions(plist))
	info->has_children = 1;
}
Ejemplo n.º 2
0
static int find_gpt_root(struct udev_device *dev, blkid_probe pr, bool test) {

#if defined(GPT_ROOT_NATIVE) && defined(ENABLE_EFI)

    _cleanup_free_ char *root_id = NULL;
    bool found_esp = false;
    blkid_partlist pl;
    int i, nvals, r;

    assert(pr);

    /* Iterate through the partitions on this disk, and see if the
     * EFI ESP we booted from is on it. If so, find the first root
     * disk, and add a property indicating its partition UUID. */

    errno = 0;
    pl = blkid_probe_get_partitions(pr);
    if (!pl)
        return errno ? -errno : -ENOMEM;

    nvals = blkid_partlist_numof_partitions(pl);
    for (i = 0; i < nvals; i++) {
        blkid_partition pp;
        const char *stype, *sid;
        sd_id128_t type;

        pp = blkid_partlist_get_partition(pl, i);
        if (!pp)
            continue;

        sid = blkid_partition_get_uuid(pp);
        if (!sid)
            continue;

        stype = blkid_partition_get_type_string(pp);
        if (!stype)
            continue;

        if (sd_id128_from_string(stype, &type) < 0)
            continue;

        if (sd_id128_equal(type, GPT_ESP)) {
            sd_id128_t id, esp;

            /* We found an ESP, let's see if it matches
             * the ESP we booted from. */

            if (sd_id128_from_string(sid, &id) < 0)
                continue;

            r = efi_loader_get_device_part_uuid(&esp);
            if (r < 0)
                return r;

            if (sd_id128_equal(id, esp))
                found_esp = true;

        } else if (sd_id128_equal(type, GPT_ROOT_NATIVE)) {

            /* We found a suitable root partition, let's
             * remember the first one. */

            if (!root_id) {
                root_id = strdup(sid);
                if (!root_id)
                    return -ENOMEM;
            }
        }
    }

    /* We found the ESP on this disk, and also found a root
     * partition, nice! Let's export its UUID */
    if (found_esp && root_id)
        udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", root_id);
#endif

    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    int i, nparts;
    char *devname;
    blkid_probe pr;
    blkid_partlist ls;
    blkid_parttable root_tab;

    if (argc < 2) {
        fprintf(stderr, "usage: %s <device|file>  "
                "-- prints partitions\n",
                program_invocation_short_name);
        return EXIT_FAILURE;
    }

    devname = argv[1];
    pr = blkid_new_probe_from_filename(devname);
    if (!pr)
        err(EXIT_FAILURE, "%s: faild to create a new libblkid probe",
            devname);
    /* Binary interface */
    ls = blkid_probe_get_partitions(pr);
    if (!ls)
        errx(EXIT_FAILURE, "%s: failed to read partitions\n", devname);

    /*
     * Print info about the primary (root) partition table
     */
    root_tab = blkid_partlist_get_table(ls);
    if (!root_tab)
        errx(EXIT_FAILURE, "%s: does not contains any "
             "known partition table\n", devname);

    printf("size: %jd, sector size: %u, PT: %s, offset: %jd\n---\n",
           blkid_probe_get_size(pr),
           blkid_probe_get_sectorsize(pr),
           blkid_parttable_get_type(root_tab),
           blkid_parttable_get_offset(root_tab));

    /*
     * List partitions
     */
    nparts = blkid_partlist_numof_partitions(ls);
    if (!nparts)
        goto done;

    for (i = 0; i < nparts; i++) {
        const char *p;
        blkid_partition par = blkid_partlist_get_partition(ls, i);
        blkid_parttable tab = blkid_partition_get_table(par);

        printf("#%d: %10llu %10llu  0x%x",
               blkid_partition_get_partno(par),
               (unsigned long long) blkid_partition_get_start(par),
               (unsigned long long) blkid_partition_get_size(par),
               blkid_partition_get_type(par));

        if (root_tab != tab)
            /* subpartition (BSD, Minix, ...) */
            printf(" (%s)", blkid_parttable_get_type(tab));

        p = blkid_partition_get_name(par);
        if (p)
            printf(" name='%s'", p);
        p = blkid_partition_get_uuid(par);
        if (p)
            printf(" uuid='%s'", p);
        p = blkid_partition_get_type_string(par);
        if (p)
            printf(" type='%s'", p);

        putc('\n', stdout);
    }

done:
    blkid_free_probe(pr);
    return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
int mountdevice(const char *devnode){
	/*Using device's MBR information to retrieve partitions and its types
	 * to mount it correctly*/
	blkid_probe pr; /*BLKID probe struct*/
	blkid_partlist pl; /*BLKID partitions list*/
	int nparts, i;
	char *pname = (char*)malloc(32 * sizeof(char));
	char *mountpoint = (char*)malloc(128 * sizeof(char));

	pr = blkid_new_probe_from_filename(devnode); /*Starts blkid probe*/

	if (!pr){
		fprintf(stderr, "Error on opening device: %s - %s\n", devnode, strerror(errno));
		return -1;
	}

	if (!pname || !mountpoint){
		fprintf(stderr, "Not able to allocate memory, will not mount anything\n");
		return -1;
	}

	pl = blkid_probe_get_partitions(pr); /*Getting partition list*/
	nparts = blkid_partlist_numof_partitions(pl); /*Getting the number of partitions*/

	for (i = 0; i < nparts; i++){
		char *pnametmp; /*temp variable to be tokenized by udev_get_partition*/
		char *ptype, *cmd;
		int t_part, partno, mstatus;
		struct stat st = {0};
		blkid_partition part = blkid_partlist_get_partition(pl, i);

		t_part = blkid_partition_get_type(part);
		partno = blkid_partition_get_partno(part);

		fprintf(stdout, "Partition: %d, type: 0x%x\n", partno, t_part);
		
		sprintf(pname, "%s%d", devnode, partno);

		pnametmp = (char*)malloc(strlen(pname) * sizeof(char));
		
		if (!pnametmp){
			fprintf(stderr, "Could not allocate memory, will not mount anything\n");
			return -1;
		}

		strcpy(pnametmp, pname);
		
		sprintf(mountpoint, "/media/USBSTICK%s", udev_get_partition(pnametmp));

		/*Freeing temporary variable*/
		free(pnametmp);

		fprintf(stdout, "Preparing to mount: %s\n", pname);

		if (stat(mountpoint, &st) == -1){
			fprintf(stdout, "Creating dir: %s\n", mountpoint);
			/*create a new dir*/
			if(mkdir(mountpoint, 0777) == -1){
				fprintf(stderr, "Unable to create: %s. Error: %s\n", mountpoint, strerror(errno));
				return -1;
			}else{
				fprintf(stdout, "%s create successfully.\n", mountpoint);
			}
		}

		fprintf(stdout, "Mounting %s on %s\n", pname, mountpoint);

		ptype = getpartitiontype(t_part);

		mstatus = mount(pname, mountpoint, ptype, MS_SYNCHRONOUS, "");

		if (mstatus == -1){
			cmd = (char*)malloc(SLENGHT * sizeof(char));
			fprintf(stderr, "Error mounting [%s]: %s\n", pname, strerror(errno));
			sprintf(cmd, "DISPLAY=:0 zenity --notification --text\"Não foi possivel montar\
						  %s\" --timeout=2", pname);
			
			if(fork() == 0)
				system(cmd);
			else
				wait(NULL);

			free(cmd);
			
			return -1;
		}else{
Ejemplo n.º 5
0
int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectedImage **ret) {

#ifdef HAVE_BLKID
        sd_id128_t root_uuid = SD_ID128_NULL, verity_uuid = SD_ID128_NULL;
        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
        bool is_gpt, is_mbr, generic_rw, multiple_generic = false;
        _cleanup_udev_device_unref_ struct udev_device *d = NULL;
        _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
        _cleanup_blkid_free_probe_ blkid_probe b = NULL;
        _cleanup_udev_unref_ struct udev *udev = NULL;
        _cleanup_free_ char *generic_node = NULL;
        const char *pttype = NULL, *usage = NULL;
        struct udev_list_entry *first, *item;
        blkid_partlist pl;
        int r, generic_nr;
        struct stat st;
        unsigned i;

        assert(fd >= 0);
        assert(ret);
        assert(root_hash || root_hash_size == 0);

        /* Probes a disk image, and returns information about what it found in *ret.
         *
         * Returns -ENOPKG if no suitable partition table or file system could be found.
         * Returns -EADDRNOTAVAIL if a root hash was specified but no matching root/verity partitions found. */

        if (root_hash) {
                /* If a root hash is supplied, then we use the root partition that has a UUID that match the first
                 * 128bit of the root hash. And we use the verity partition that has a UUID that match the final
                 * 128bit. */

                if (root_hash_size < sizeof(sd_id128_t))
                        return -EINVAL;

                memcpy(&root_uuid, root_hash, sizeof(sd_id128_t));
                memcpy(&verity_uuid, (const uint8_t*) root_hash + root_hash_size - sizeof(sd_id128_t), sizeof(sd_id128_t));

                if (sd_id128_is_null(root_uuid))
                        return -EINVAL;
                if (sd_id128_is_null(verity_uuid))
                        return -EINVAL;
        }

        if (fstat(fd, &st) < 0)
                return -errno;

        if (!S_ISBLK(st.st_mode))
                return -ENOTBLK;

        b = blkid_new_probe();
        if (!b)
                return -ENOMEM;

        errno = 0;
        r = blkid_probe_set_device(b, fd, 0, 0);
        if (r != 0) {
                if (errno == 0)
                        return -ENOMEM;

                return -errno;
        }

        blkid_probe_enable_superblocks(b, 1);
        blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_USAGE);
        blkid_probe_enable_partitions(b, 1);
        blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);

        errno = 0;
        r = blkid_do_safeprobe(b);
        if (r == -2 || r == 1) {
                log_debug("Failed to identify any partition table.");
                return -ENOPKG;
        }
        if (r != 0) {
                if (errno == 0)
                        return -EIO;

                return -errno;
        }

        m = new0(DissectedImage, 1);
        if (!m)
                return -ENOMEM;

        (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
        if (STRPTR_IN_SET(usage, "filesystem", "crypto")) {
                _cleanup_free_ char *t = NULL, *n = NULL;
                const char *fstype = NULL;

                /* OK, we have found a file system, that's our root partition then. */
                (void) blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);

                if (fstype) {
                        t = strdup(fstype);
                        if (!t)
                                return -ENOMEM;
                }

                if (asprintf(&n, "/dev/block/%u:%u", major(st.st_rdev), minor(st.st_rdev)) < 0)
                        return -ENOMEM;

                m->partitions[PARTITION_ROOT] = (DissectedPartition) {
                        .found = true,
                        .rw = true,
                        .partno = -1,
                        .architecture = _ARCHITECTURE_INVALID,
                        .fstype = t,
                        .node = n,
                };

                t = n = NULL;

                m->encrypted = streq(fstype, "crypto_LUKS");

                *ret = m;
                m = NULL;

                return 0;
        }

        (void) blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
        if (!pttype)
                return -ENOPKG;

        is_gpt = streq_ptr(pttype, "gpt");
        is_mbr = streq_ptr(pttype, "dos");

        if (!is_gpt && !is_mbr)
                return -ENOPKG;

        errno = 0;
        pl = blkid_probe_get_partitions(b);
        if (!pl) {
                if (errno == 0)
                        return -ENOMEM;

                return -errno;
        }

        udev = udev_new();
        if (!udev)
                return -errno;

        d = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
        if (!d)
                return -ENOMEM;

        for (i = 0;; i++) {
                int n, z;

                if (i >= 10) {
                        log_debug("Kernel partitions never appeared.");
                        return -ENXIO;
                }

                e = udev_enumerate_new(udev);
                if (!e)
                        return -errno;

                r = udev_enumerate_add_match_parent(e, d);
                if (r < 0)
                        return r;

                r = udev_enumerate_scan_devices(e);
                if (r < 0)
                        return r;

                /* Count the partitions enumerated by the kernel */
                n = 0;
                first = udev_enumerate_get_list_entry(e);
                udev_list_entry_foreach(item, first)
                        n++;

                /* Count the partitions enumerated by blkid */
                z = blkid_partlist_numof_partitions(pl);
                if (n == z + 1)
                        break;
                if (n > z + 1) {
                        log_debug("blkid and kernel partition list do not match.");
                        return -EIO;
                }
                if (n < z + 1) {
                        unsigned j;

                        /* The kernel has probed fewer partitions than blkid? Maybe the kernel prober is still running
                         * or it got EBUSY because udev already opened the device. Let's reprobe the device, which is a
                         * synchronous call that waits until probing is complete. */

                        for (j = 0; j < 20; j++) {

                                r = ioctl(fd, BLKRRPART, 0);
                                if (r < 0)
                                        r = -errno;
                                if (r >= 0 || r != -EBUSY)
                                        break;

                                /* If something else has the device open, such as an udev rule, the ioctl will return
                                 * EBUSY. Since there's no way to wait until it isn't busy anymore, let's just wait a
                                 * bit, and try again.
                                 *
                                 * This is really something they should fix in the kernel! */

                                usleep(50 * USEC_PER_MSEC);
                        }

                        if (r < 0)
                                return r;
                }

                e = udev_enumerate_unref(e);
        }

        first = udev_enumerate_get_list_entry(e);
        udev_list_entry_foreach(item, first) {
                _cleanup_udev_device_unref_ struct udev_device *q;
                unsigned long long flags;
                blkid_partition pp;
                const char *node;
                dev_t qn;
                int nr;

                q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
                if (!q)
                        return -errno;

                qn = udev_device_get_devnum(q);
                if (major(qn) == 0)
                        continue;

                if (st.st_rdev == qn)
                        continue;

                node = udev_device_get_devnode(q);
                if (!node)
                        continue;

                pp = blkid_partlist_devno_to_partition(pl, qn);
                if (!pp)
                        continue;

                flags = blkid_partition_get_flags(pp);

                nr = blkid_partition_get_partno(pp);
                if (nr < 0)
                        continue;

                if (is_gpt) {
                        int designator = _PARTITION_DESIGNATOR_INVALID, architecture = _ARCHITECTURE_INVALID;
                        const char *stype, *sid, *fstype = NULL;
                        sd_id128_t type_id, id;
                        bool rw = true;

                        if (flags & GPT_FLAG_NO_AUTO)
                                continue;

                        sid = blkid_partition_get_uuid(pp);
                        if (!sid)
                                continue;
                        if (sd_id128_from_string(sid, &id) < 0)
                                continue;

                        stype = blkid_partition_get_type_string(pp);
                        if (!stype)
                                continue;
                        if (sd_id128_from_string(stype, &type_id) < 0)
                                continue;

                        if (sd_id128_equal(type_id, GPT_HOME)) {
                                designator = PARTITION_HOME;
                                rw = !(flags & GPT_FLAG_READ_ONLY);
                        } else if (sd_id128_equal(type_id, GPT_SRV)) {
                                designator = PARTITION_SRV;
                                rw = !(flags & GPT_FLAG_READ_ONLY);
                        } else if (sd_id128_equal(type_id, GPT_ESP)) {
                                designator = PARTITION_ESP;
                                fstype = "vfat";
                        }
#ifdef GPT_ROOT_NATIVE
                        else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE)) {

                                /* If a root ID is specified, ignore everything but the root id */
                                if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
                                        continue;

                                designator = PARTITION_ROOT;
                                architecture = native_architecture();
                                rw = !(flags & GPT_FLAG_READ_ONLY);
                        } else if (sd_id128_equal(type_id, GPT_ROOT_NATIVE_VERITY)) {

                                m->can_verity = true;

                                /* Ignore verity unless a root hash is specified */
                                if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
                                        continue;

                                designator = PARTITION_ROOT_VERITY;
                                fstype = "DM_verity_hash";
                                architecture = native_architecture();
                                rw = false;
                        }
#endif
#ifdef GPT_ROOT_SECONDARY
                        else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY)) {

                                /* If a root ID is specified, ignore everything but the root id */
                                if (!sd_id128_is_null(root_uuid) && !sd_id128_equal(root_uuid, id))
                                        continue;

                                designator = PARTITION_ROOT_SECONDARY;
                                architecture = SECONDARY_ARCHITECTURE;
                                rw = !(flags & GPT_FLAG_READ_ONLY);
                        } else if (sd_id128_equal(type_id, GPT_ROOT_SECONDARY_VERITY)) {

                                m->can_verity = true;

                                /* Ignore verity unless root has is specified */
                                if (sd_id128_is_null(verity_uuid) || !sd_id128_equal(verity_uuid, id))
                                        continue;

                                designator = PARTITION_ROOT_SECONDARY_VERITY;
                                fstype = "DM_verity_hash";
                                architecture = SECONDARY_ARCHITECTURE;
                                rw = false;
                        }
#endif
                        else if (sd_id128_equal(type_id, GPT_SWAP)) {
                                designator = PARTITION_SWAP;
                                fstype = "swap";
                        } else if (sd_id128_equal(type_id, GPT_LINUX_GENERIC)) {

                                if (generic_node)
                                        multiple_generic = true;
                                else {
                                        generic_nr = nr;
                                        generic_rw = !(flags & GPT_FLAG_READ_ONLY);
                                        generic_node = strdup(node);
                                        if (!generic_node)
                                                return -ENOMEM;
                                }
                        }

                        if (designator != _PARTITION_DESIGNATOR_INVALID) {
                                _cleanup_free_ char *t = NULL, *n = NULL;

                                /* First one wins */
                                if (m->partitions[designator].found)
                                        continue;

                                if (fstype) {
                                        t = strdup(fstype);
                                        if (!t)
                                                return -ENOMEM;
                                }

                                n = strdup(node);
                                if (!n)
                                        return -ENOMEM;

                                m->partitions[designator] = (DissectedPartition) {
                                        .found = true,
                                        .partno = nr,
                                        .rw = rw,
                                        .architecture = architecture,
                                        .node = n,
                                        .fstype = t,
                                };

                                n = t = NULL;
                        }

                } else if (is_mbr) {