static int check_partition_table(const char *device) { blkid_probe pr; const char *value; int ret; pr = blkid_new_probe_from_filename(device); if (!pr) return -1; ret = blkid_probe_enable_partitions(pr, 1); if (ret < 0) goto errout; ret = blkid_probe_enable_superblocks(pr, 0); if (ret < 0) goto errout; ret = blkid_do_fullprobe(pr); if (ret < 0) goto errout; ret = blkid_probe_lookup_value(pr, "PTTYPE", &value, NULL); if (ret == 0) fprintf(stderr, _("Found a %s partition table in %s\n"), value, device); else ret = 1; errout: blkid_free_probe(pr); return ret; }
static int probe_superblocks(blkid_probe pr) { struct stat st; int rc; if (fstat(blkid_probe_get_fd(pr), &st)) return -1; blkid_probe_enable_partitions(pr, 1); if (!S_ISCHR(st.st_mode) && blkid_probe_get_size(pr) <= 1024 * 1440 && blkid_probe_is_wholedisk(pr)) { /* * check if the small disk is partitioned, if yes then * don't probe for filesystems. */ blkid_probe_enable_superblocks(pr, 0); rc = blkid_do_fullprobe(pr); if (rc < 0) return rc; /* -1 = error, 1 = nothing, 0 = success */ if (blkid_probe_lookup_value(pr, "PTTYPE", NULL, NULL) == 0) return 0; /* partition table detected */ } blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); blkid_probe_enable_superblocks(pr, 1); return blkid_do_safeprobe(pr); }
int main(int argc, char *argv[]) { int rc; char *devname; blkid_probe pr; blkid_topology tp; if (argc < 2) { fprintf(stderr, "usage: %s <device> " "-- checks based on libblkid for mkfs-like programs.\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); /* * check Filesystems / Partitions overwrite */ /* enable partitions probing (superblocks are enabled by default) */ blkid_probe_enable_partitions(pr, TRUE); rc = blkid_do_fullprobe(pr); if (rc == -1) errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname); else if (rc == 0) { const char *type; if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) errx(EXIT_FAILURE, "%s: appears to contain an existing " "%s superblock", devname, type); if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) errx(EXIT_FAILURE, "%s: appears to contain an partition " "table (%s)", devname, type); } /* * get topology details */ tp = blkid_probe_get_topology(pr); if (!tp) errx(EXIT_FAILURE, "%s: failed to read topology", devname); /* ... your mkfs.<type> code or so ... off = blkid_topology_get_alignment_offset(tp); */ blkid_free_probe(pr); return EXIT_SUCCESS; }
gboolean type_by_device(const gchar* device, gchar** type) { gboolean result = false; const char *devtype = NULL; blkid_probe probe = NULL; *type = NULL; probe = blkid_new_probe_from_filename(device); if(!probe) { LOG(MOD "Probe from filename failed!\n"); return false; } if (blkid_probe_enable_partitions(probe, true) != 0) { LOG(MOD "Enable partitions failed!\n"); goto fail; } if (blkid_do_fullprobe(probe) != 0) { LOG(MOD "Fullprobe failed!\n"); goto fail; } if (blkid_probe_lookup_value(probe, "TYPE", &devtype, NULL) != 0) { LOG(MOD "Lookup value failed!\n"); goto fail; } *type = g_strdup(devtype); result = true; fail: blkid_free_probe(probe); return result; }
static int lowprobe_topology(blkid_probe pr) { /* enable topology probing only */ blkid_probe_enable_topology(pr, 1); blkid_probe_enable_superblocks(pr, 0); blkid_probe_enable_partitions(pr, 0); return blkid_do_fullprobe(pr); }
static lgfs2_rgrps_t rgrps_init(struct gfs2_sbd *sdp) { int ret; int error; uint64_t al_base = 0; uint64_t al_off = 0; struct stat st; blkid_probe pr = blkid_new_probe(); if (pr == NULL || blkid_probe_set_device(pr, sdp->device_fd, 0, 0) != 0 || blkid_probe_enable_superblocks(pr, TRUE) != 0 || blkid_probe_enable_partitions(pr, TRUE) != 0) { fprintf(stderr, _("Failed to create probe\n")); return NULL; } error = fstat(sdp->device_fd, &st); if (error < 0) { fprintf(stderr, _("fstat failed\n")); return NULL; } if (!S_ISREG(st.st_mode) && blkid_probe_enable_topology(pr, TRUE) != 0) { fprintf(stderr, _("Failed to create probe\n")); return NULL; } ret = blkid_do_fullprobe(pr); if (ret == 0 && !S_ISREG(st.st_mode)) { blkid_topology tp = blkid_probe_get_topology(pr); if (tp != NULL) { unsigned long min_io_sz = blkid_topology_get_minimum_io_size(tp); unsigned long opt_io_sz = blkid_topology_get_optimal_io_size(tp); unsigned long phy_sector_sz = blkid_topology_get_physical_sector_size(tp); if ((min_io_sz > phy_sector_sz) && (opt_io_sz > phy_sector_sz)) { al_base = opt_io_sz / sdp->bsize; al_off = min_io_sz / sdp->bsize; } } } blkid_free_probe(pr); return lgfs2_rgrps_init(sdp, al_base, al_off); }
static struct wipe_desc * read_offsets(struct wipe_desc *wp, const char *fname, int zap) { blkid_probe pr; int rc; if (!fname) return NULL; pr = blkid_new_probe_from_filename(fname); if (!pr) errx(EXIT_FAILURE, _("error: %s: probing initialization failed"), fname); blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */ blkid_probe_enable_partitions(pr, 1); rc = blkid_do_fullprobe(pr); blkid_probe_enable_partitions(pr, 0); if (rc == 0) { const char *type = NULL; blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL); warnx(_("WARNING: %s: appears to contain '%s' " "partition table"), fname, type); } blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID); while (blkid_do_probe(pr) == 0) { wp = get_offset_from_probe(wp, pr, zap); if (!wp) break; } blkid_free_probe(pr); return wp; }
static void wipe_device(int fd, const char *devname, int force) { char *type = NULL; int zap = 1; #ifdef HAVE_LIBBLKID blkid_probe pr = NULL; #endif if (!force) { if (lseek(fd, 0, SEEK_SET) != 0) errx(EXIT_FAILURE, _("unable to rewind swap-device")); #ifdef HAVE_LIBBLKID pr = new_prober(fd); blkid_probe_enable_partitions(pr, 1); blkid_probe_enable_superblocks(pr, 0); if (blkid_do_fullprobe(pr) == 0 && blkid_probe_lookup_value(pr, "PTTYPE", (const char **) &type, NULL) == 0 && type) { type = xstrdup(type); zap = 0; } #else /* don't zap if compiled without libblkid */ zap = 0; #endif } if (zap) { /* * Wipe boodbits */ char buf[1024]; const char *data = NULL; if (lseek(fd, 0, SEEK_SET) != 0) errx(EXIT_FAILURE, _("unable to rewind swap-device")); memset(buf, 0, sizeof(buf)); if (write_all(fd, buf, sizeof(buf))) errx(EXIT_FAILURE, _("unable to erase bootbits sectors")); #ifdef HAVE_LIBBLKID /* * Wipe rest of the device */ if (!pr) pr = new_prober(fd); blkid_probe_enable_superblocks(pr, 1); blkid_probe_enable_partitions(pr, 0); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_TYPE); while (blkid_do_probe(pr) == 0) { if (blkid_probe_lookup_value(pr, "TYPE", &data, NULL) == 0 && data) warnx(_("%s: warning: wiping old %s signature."), devname, data); blkid_do_wipe(pr, 0); } #endif } else { warnx(_("%s: warning: don't erase bootbits sectors"), devname); if (type) fprintf(stderr, _(" (%s partition table detected). "), type); else fprintf(stderr, _(" (compiled without libblkid). ")); fprintf(stderr, _("Use -f to force.\n")); } #ifdef HAVE_LIBBLKID blkid_free_probe(pr); #endif }
static void zap_bootbits(int fd, const char *devname, int force, int is_blkdev) { char *type = NULL; int whole = 0; int zap = 1; if (!force) { if (lseek(fd, 0, SEEK_SET) != 0) die(_("unable to rewind swap-device")); if (is_blkdev && is_whole_disk_fd(fd, devname)) { /* don't zap bootbits on whole disk -- we know nothing * about bootloaders on the device */ whole = 1; zap = 0; } else { #ifdef HAVE_LIBBLKID_INTERNAL blkid_probe pr = blkid_new_probe(); if (!pr) die(_("unable to alloc new libblkid probe")); if (blkid_probe_set_device(pr, fd, 0, 0)) die(_("unable to assign device to libblkid probe")); blkid_probe_enable_partitions(pr, 1); blkid_probe_enable_superblocks(pr, 0); if (blkid_do_fullprobe(pr) == 0) blkid_probe_lookup_value(pr, "PTTYPE", (const char **) &type, NULL); if (type) { type = strdup(type); zap = 0; } blkid_free_probe(pr); #else /* don't zap if compiled without libblkid */ zap = 0; #endif } } if (zap) { char buf[1024]; if (lseek(fd, 0, SEEK_SET) != 0) die(_("unable to rewind swap-device")); memset(buf, 0, sizeof(buf)); if (write_all(fd, buf, sizeof(buf))) die(_("unable to erase bootbits sectors")); return; } fprintf(stderr, _("%s: %s: warning: don't erase bootbits sectors\n"), program_name, devname); if (type) fprintf(stderr, _(" (%s partition table detected). "), type); else if (whole) fprintf(stderr, _(" on whole disk. ")); else fprintf(stderr, _(" (compiled without libblkid). ")); fprintf(stderr, "Use -f to force.\n"); }
int main(int argc, char *argv[]) { int rc; char *devname; blkid_probe pr; blkid_topology tp; if (argc < 2) { fprintf(stderr, "usage: %s <device> " "-- prints topology details about the device\n", program_invocation_short_name); return EXIT_FAILURE; } devname = argv[1]; pr = blkid_new_probe_from_filename(devname); if (!pr) err(EXIT_FAILURE, "%s: failed to create a new libblkid probe", devname); /* * Binary interface */ tp = blkid_probe_get_topology(pr); if (tp) { printf("----- binary interface:\n"); printf("\talignment offset : %lu\n", blkid_topology_get_alignment_offset(tp)); printf("\tminimum io size : %lu\n", blkid_topology_get_minimum_io_size(tp)); printf("\toptimal io size : %lu\n", blkid_topology_get_optimal_io_size(tp)); printf("\tlogical sector size : %lu\n", blkid_topology_get_logical_sector_size(tp)); printf("\tphysical sector size : %lu\n", blkid_topology_get_physical_sector_size(tp)); } /* * NAME=value interface */ /* enable topology probing */ blkid_probe_enable_topology(pr, TRUE); /* disable superblocks probing (enabled by default) */ blkid_probe_enable_superblocks(pr, FALSE); rc = blkid_do_fullprobe(pr); if (rc == -1) errx(EXIT_FAILURE, "%s: blkid_do_fullprobe() failed", devname); else if (rc == 1) warnx("%s: missing topology information", devname); else { int i, nvals = blkid_probe_numof_values(pr); printf("----- NAME=value interface (values: %d):\n", nvals); for (i = 0; i < nvals; i++) { const char *name, *data; blkid_probe_get_value(pr, i, &name, &data, NULL); printf("\t%s = %s\n", name, data); } } blkid_free_probe(pr); return EXIT_SUCCESS; }
static int lowprobe_device(blkid_probe pr, const char *devname, char *show[], int output, blkid_loff_t offset, blkid_loff_t size) { const char *data; const char *name; int nvals = 0, n, num = 1; size_t len; int fd; int rc = 0; struct stat st; fd = open(devname, O_RDONLY); if (fd < 0) { fprintf(stderr, "error: %s: %s\n", devname, strerror(errno)); return 2; } if (blkid_probe_set_device(pr, fd, offset, size)) goto done; if (fstat(fd, &st)) goto done; /* * partitions probing */ blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */ blkid_probe_enable_partitions(pr, 1); rc = blkid_do_fullprobe(pr); blkid_probe_enable_partitions(pr, 0); if (rc < 0) goto done; /* -1 = error, 1 = nothing, 0 = succes */ /* * Don't probe for FS/RAIDs on small devices */ if (rc || S_ISCHR(st.st_mode) || blkid_probe_get_size(pr) > 1024 * 1440) { /* * filesystems/RAIDs probing */ blkid_probe_enable_superblocks(pr, 1); rc = blkid_do_safeprobe(pr); if (rc < 0) goto done; } nvals = blkid_probe_numof_values(pr); if (output & OUTPUT_DEVICE_ONLY) { printf("%s\n", devname); goto done; } for (n = 0; n < nvals; n++) { if (blkid_probe_get_value(pr, n, &name, &data, &len)) continue; if (show[0] && !has_item(show, name)) continue; len = strnlen((char *) data, len); print_value(output, num++, devname, (char *) data, name, len); } if (nvals >= 1 && !(output & (OUTPUT_VALUE_ONLY | OUTPUT_UDEV_LIST))) printf("\n"); done: if (rc == -2) { if (output & OUTPUT_UDEV_LIST) print_udev_ambivalent(pr); else fprintf(stderr, "%s: ambivalent result (probably more " "filesystems on the device, use wipefs(8) " "to see more details)\n", devname); } close(fd); return !nvals ? 2 : 0; }