int print_device_pt(struct fdisk_context *cxt, char *device, int warnme, int verify) { if (fdisk_assign_device(cxt, device, 1) != 0) { /* read-only */ if (warnme || errno == EACCES) warn(_("cannot open %s"), device); return -1; } list_disk_geometry(cxt); if (fdisk_has_label(cxt)) { list_disklabel(cxt); if (verify) fdisk_verify_disklabel(cxt); } fdisk_deassign_device(cxt, 1); return 0; }
void list_disk_geometry(struct fdisk_context *cxt) { char *id = NULL; struct fdisk_label *lb = fdisk_get_label(cxt, NULL); uint64_t bytes = fdisk_get_nsectors(cxt) * fdisk_get_sector_size(cxt); char *strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, bytes); color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(cxt, _("Disk %s: %s, %ju bytes, %ju sectors"), fdisk_get_devname(cxt), strsz, bytes, (uintmax_t) fdisk_get_nsectors(cxt)); color_disable(); free(strsz); if (lb && (fdisk_label_require_geometry(lb) || fdisk_use_cylinders(cxt))) fdisk_info(cxt, _("Geometry: %d heads, %llu sectors/track, %llu cylinders"), fdisk_get_geom_heads(cxt), fdisk_get_geom_sectors(cxt), fdisk_get_geom_cylinders(cxt)); fdisk_info(cxt, _("Units: %s of %d * %ld = %ld bytes"), fdisk_get_unit(cxt, FDISK_PLURAL), fdisk_get_units_per_sector(cxt), fdisk_get_sector_size(cxt), fdisk_get_units_per_sector(cxt) * fdisk_get_sector_size(cxt)); fdisk_info(cxt, _("Sector size (logical/physical): %lu bytes / %lu bytes"), fdisk_get_sector_size(cxt), fdisk_get_physector_size(cxt)); fdisk_info(cxt, _("I/O size (minimum/optimal): %lu bytes / %lu bytes"), fdisk_get_minimal_iosize(cxt), fdisk_get_optimal_iosize(cxt)); if (fdisk_get_alignment_offset(cxt)) fdisk_info(cxt, _("Alignment offset: %lu bytes"), fdisk_get_alignment_offset(cxt)); if (fdisk_has_label(cxt)) fdisk_info(cxt, _("Disklabel type: %s"), fdisk_label_get_name(lb)); if (fdisk_get_disklabel_id(cxt, &id) == 0 && id) fdisk_info(cxt, _("Disk identifier: %s"), id); }
static int verify_device(struct sfdisk *sf, const char *devname) { int rc = 1; fdisk_enable_listonly(sf->cxt, 1); if (fdisk_assign_device(sf->cxt, devname, 1)) { warn(_("cannot open %s"), devname); return 1; } color_scheme_enable("header", UL_COLOR_BOLD); fdisk_info(sf->cxt, "%s:", devname); color_disable(); if (!fdisk_has_label(sf->cxt)) fdisk_info(sf->cxt, _("unrecognized partition table type")); else rc = fdisk_verify_disklabel(sf->cxt); fdisk_deassign_device(sf->cxt, 1); return rc; }
/** * fdisk_assign_device: * @cxt: context * @fname: path to the device to be handled * @readonly: how to open the device * * Open the device, discovery topology, geometry, detect disklabel and switch * the current label driver to reflect the probing result. * * Note that this function resets all generic setting in context. If the @cxt * is nested context then the device is assigned to the parental context and * necessary properties are copied to the @cxt. The change is propagated in * child->parent direction only. It's impossible to use a different device for * primary and nested contexts. * * Returns: 0 on success, < 0 on error. */ int fdisk_assign_device(struct fdisk_context *cxt, const char *fname, int readonly) { int fd; DBG(CXT, ul_debugobj(cxt, "assigning device %s", fname)); assert(cxt); /* redirect request to parent */ if (cxt->parent) { int rc, org = fdisk_is_listonly(cxt->parent); /* assign_device() is sensitive to "listonly" mode, so let's * follow the current context setting for the parent to avoid * unwanted extra warnings. */ fdisk_enable_listonly(cxt->parent, fdisk_is_listonly(cxt)); rc = fdisk_assign_device(cxt->parent, fname, readonly); fdisk_enable_listonly(cxt->parent, org); if (!rc) rc = init_nested_from_parent(cxt, 0); if (!rc) fdisk_probe_labels(cxt); return rc; } reset_context(cxt); fd = open(fname, (readonly ? O_RDONLY : O_RDWR ) | O_CLOEXEC); if (fd < 0) return -errno; cxt->readonly = readonly; cxt->dev_fd = fd; cxt->dev_path = strdup(fname); if (!cxt->dev_path) goto fail; fdisk_discover_topology(cxt); fdisk_discover_geometry(cxt); if (fdisk_read_firstsector(cxt) < 0) goto fail; /* detect labels and apply labes specific stuff (e.g geomery) * to the context */ fdisk_probe_labels(cxt); /* let's apply user geometry *after* label prober * to make it possible to override in-label setting */ fdisk_apply_user_device_properties(cxt); /* warn about obsolete stuff on the device if we aren't in * list-only mode and there is not PT yet */ if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt)) warn_wipe(cxt); DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]", fname, readonly ? "READ-ONLY" : "READ-WRITE")); return 0; fail: DBG(CXT, ul_debugobj(cxt, "failed to assign device")); return -errno; }