static int _wipe_known_signatures_with_blkid(struct device *dev, const char *name, uint32_t types_to_exclude, uint32_t types_no_prompt, int yes, force_t force, int *wiped) { blkid_probe probe = NULL; int found = 0, left = 0, wiped_tmp; int r_wipe; int r = 0; if (!wiped) wiped = &wiped_tmp; *wiped = 0; /* TODO: Should we check for valid dev - _dev_is_valid(dev)? */ if (!(probe = blkid_new_probe_from_filename(dev_name(dev)))) { log_error("Failed to create a new blkid probe for device %s.", dev_name(dev)); goto out; } blkid_probe_enable_partitions(probe, 1); blkid_probe_set_partitions_flags(probe, BLKID_PARTS_MAGIC); blkid_probe_enable_superblocks(probe, 1); blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE | BLKID_SUBLKS_VERSION | BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM); while (!blkid_do_probe(probe)) { if ((r_wipe = _blkid_wipe(probe, dev, name, types_to_exclude, types_no_prompt, yes, force)) == 1) { (*wiped)++; if (blkid_probe_step_back(probe)) { log_error("Failed to step back blkid probe to check just wiped signature."); goto out; } } /* do not count excluded types */ if (r_wipe != 2) found++; } if (!found) r = 1; left = found - *wiped; if (!left) r = 1; else log_warn("%d existing signature%s left on the device.", left, left > 1 ? "s" : ""); out: if (probe) blkid_free_probe(probe); return r; }
string_t zuluCryptGetFileSystemFromDevice( const char * device ) { string_t st = StringVoid ; const char * cf = NULL ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&cf,NULL ) ; st = String( cf ) ; blkid_free_probe( blkid ) ; } return st ; }
static char * _get_uuid_from_device( const char * device ) { char * r = NULL ; const char * e = NULL ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) ; r = StringCopy_2( e ) ; blkid_free_probe( blkid ) ; } return r ; }
static struct wipe_desc * do_wipe(struct wipe_desc *wp, const char *devname, int noact, int all, int quiet, int force) { int flags; blkid_probe pr; struct wipe_desc *w, *wp0 = clone_offset(wp); int zap = all ? 1 : wp->zap; flags = O_RDWR; if (!force) flags |= O_EXCL; pr = new_probe(devname, flags); if (!pr) return NULL; while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; /* Check if offset is in provided list */ w = wp0; while(w && w->offset != wp->offset) w = w->next; if (wp0 && !w) continue; /* Mark done if found in provided list */ if (w) w->on_disk = wp->on_disk; if (!wp->on_disk) continue; if (zap) do_wipe_real(pr, devname, wp, noact, quiet); } for (w = wp0; w != NULL; w = w->next) { if (!w->on_disk && !quiet) warnx(_("%s: offset 0x%jx not found"), devname, w->offset); } fsync(blkid_probe_get_fd(pr)); close(blkid_probe_get_fd(pr)); blkid_free_probe(pr); free_wipe(wp0); return wp; }
/* * This function prints a warning if the device is not wiped (e.g. wipefs(8). * Please don't call this function if there is already a PT. * * Returns: 0 if nothing found, < 0 on error, 1 if found a signature */ static int warn_wipe(struct fdisk_context *cxt) { #ifdef HAVE_LIBBLKID blkid_probe pr; #endif int rc = 0; assert(cxt); if (fdisk_dev_has_disklabel(cxt) || cxt->dev_fd < 0) return -EINVAL; #ifdef HAVE_LIBBLKID DBG(LABEL, dbgprint("wipe check: initialize libblkid prober")); pr = blkid_new_probe(); if (!pr) return -ENOMEM; rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0); if (rc) return rc; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); blkid_probe_enable_partitions(pr, 1); /* we care about the first found FS/raid, so don't call blkid_do_probe() * in loop or don't use blkid_do_fullprobe() ... */ rc = blkid_do_probe(pr); if (rc == 0) { const char *name = NULL; if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 || blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) { fdisk_warnx(cxt, _( "%s: device contains a valid '%s' signature, it's " "strongly recommended to wipe the device by command wipefs(8) " "if this setup is unexpected to avoid " "possible collisions."), cxt->dev_path, name); rc = 1; } } blkid_free_probe(pr); #endif return rc; }
static struct wipe_desc * read_offsets(struct wipe_desc *wp, const char *devname) { blkid_probe pr = new_probe(devname, 0); if (!pr) return NULL; while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; } blkid_free_probe(pr); return wp; }
static inline void probe_filesystem(struct format *target) { blkid_probe probe = 0; static char *filesystems[] = { "ext2", "ext3", "ext4", "reiserfs", "jfs", "xfs", "btrfs", "swap", 0 }; const char *filesystem = "unknown"; const char *result = 0; if((probe = blkid_new_probe_from_filename(target->devicepath)) == 0) goto bail; if(blkid_probe_enable_superblocks(probe,true) == -1) goto bail; if(blkid_probe_filter_superblocks_type(probe,BLKID_FLTR_ONLYIN,filesystems) == -1) goto bail; if(blkid_probe_set_superblocks_flags(probe,BLKID_SUBLKS_TYPE) == -1) goto bail; if(blkid_do_probe(probe) == -1) goto bail; if(blkid_probe_lookup_value(probe,"TYPE",&result,0) == -1) goto bail; filesystem = result; bail: if(probe != 0) blkid_free_probe(probe); target->filesystem = strdup(filesystem); }
int zuluCryptFileSystemIsFUSEbased( const char * device ) { const char * cf = NULL ; int st ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&cf,NULL ) ; #if 1 st = StringAtLeastOneMatch_1( cf,"ntfs","exfat",NULL ) ; #else st = StringAtLeastOneMatch_1( cf,"ntfs",NULL ) ; #endif blkid_free_probe( blkid ) ; return st ; }else{ return 0 ; } }
/* * Compose and print ID_FS_AMBIVALENT for udev */ static int print_udev_ambivalent(blkid_probe pr) { char *val = NULL; size_t valsz = 0; int count = 0, rc = -1; while (!blkid_do_probe(pr)) { const char *usage = NULL, *type = NULL, *version = NULL; char enc[256]; blkid_probe_lookup_value(pr, "USAGE", &usage, NULL); blkid_probe_lookup_value(pr, "TYPE", &type, NULL); blkid_probe_lookup_value(pr, "VERSION", &version, NULL); if (!usage || !type) continue; blkid_encode_string(usage, enc, sizeof(enc)); if (append_str(&val, &valsz, enc, ":")) goto done; blkid_encode_string(type, enc, sizeof(enc)); if (append_str(&val, &valsz, enc, version ? ":" : " ")) goto done; if (version) { blkid_encode_string(version, enc, sizeof(enc)); if (append_str(&val, &valsz, enc, " ")) goto done; } count++; } if (count > 1) { *(val + valsz - 1) = '\0'; /* rem tailing whitespace */ printf("ID_FS_AMBIVALEN=%s\n", val); rc = 0; } done: free(val); return rc; }
/* * This function prints a warning if the device is not wiped (e.g. wipefs(8). * Please don't call this function if there is already a PT. * * Returns: 0 if nothing found, < 0 on error, 1 if found a signature */ static int check_collisions(struct fdisk_context *cxt) { #ifdef HAVE_LIBBLKID int rc = 0; blkid_probe pr; assert(cxt); assert(cxt->dev_fd >= 0); DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober")); pr = blkid_new_probe(); if (!pr) return -ENOMEM; rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0); if (rc) return rc; blkid_probe_enable_superblocks(pr, 1); blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE); blkid_probe_enable_partitions(pr, 1); /* we care about the first found FS/raid, so don't call blkid_do_probe() * in loop or don't use blkid_do_fullprobe() ... */ rc = blkid_do_probe(pr); if (rc == 0) { const char *name = NULL; if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 || blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) { cxt->collision = strdup(name); if (!cxt->collision) rc = -ENOMEM; } } blkid_free_probe(pr); return rc; #else return 0; #endif }
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 }
void zuluCryptFileSystemProperties( string_t p,const char * mapper,const char * m_point ) { const char * e ; blkid_probe blkid ; struct statvfs vfs ; u_int64_t total ; u_int64_t used ; u_int64_t unused ; u_int64_t block_size ; char buff[ SIZE ] ; char * buffer = buff ; string_t q ; ssize_t index ; struct stat statstr ; blkid = blkid_new_probe_from_filename( mapper ) ; if( blkid == NULL ){ return ; } blkid_do_probe( blkid ) ; if( blkid_probe_lookup_value( blkid,"TYPE",&e,NULL ) == 0 ){ StringMultipleAppend( p,"\n file system:\t",e,NULL ) ; }else{ StringAppend( p,"\n file system:\tNil" ) ; } blkid_free_probe( blkid ) ; if( statvfs( m_point,&vfs ) != 0 ){ return ; } block_size = vfs.f_frsize ; total = block_size * vfs.f_blocks ; unused = block_size * vfs.f_bavail ; used = total - unused ; zuluCryptFormatSize( total,buffer,SIZE ) ; StringMultipleAppend( p,"\n total space:\t",buffer,NULL ) ; zuluCryptFormatSize( used,buffer,SIZE ) ; StringMultipleAppend( p,"\n used space:\t",buffer,NULL ) ; zuluCryptFormatSize( unused,buffer,SIZE ) ; StringMultipleAppend( p,"\n free space:\t",buffer,NULL ) ; if( used == total ){ StringAppend( p,"\n used%: \t100%\n" ) ; }else if( used == 0 ){ StringAppend( p,"\n used%: \t0%\n" ) ; }else{ snprintf( buff,SIZE,"%.2f%%",100 * ( ( float ) used / ( float ) total ) ) ; StringMultipleAppend( p,"\n used%: \t",buff,"\n",NULL ) ; } buffer = zuluCryptGetUUIDFromMapper( mapper ) ; StringAppend( p,buffer ) ; StringFree( buffer ) ; StringMultipleAppend( p,"\n mount point1:\t",m_point,NULL ) ; q = String( m_point ) ; index = StringLastIndexOfChar( q,'/' ) ; if( index == -1 ){ StringAppend( p,"\n mount point2:\tNil" ) ; }else{ StringRemoveLeft( q,index ) ; e = StringPrepend( q,"/run/media/public" ) ; if( stat( e,&statstr ) == 0 ){ StringMultipleAppend( p,"\n mount point2:\t",e,NULL ) ; }else{ StringAppend( p,"\n mount point2:\tNil" ) ; } } StringDelete( &q ) ; }
static void write_sb(char *dev, unsigned block_size, unsigned bucket_size, bool writeback, bool discard, bool wipe_bcache, unsigned cache_replacement_policy, uint64_t data_offset, uuid_t set_uuid, bool bdev) { int fd; char uuid_str[40], set_uuid_str[40], zeroes[SB_START] = {0}; struct cache_sb sb; blkid_probe pr; if ((fd = open(dev, O_RDWR|O_EXCL)) == -1) { fprintf(stderr, "Can't open dev %s: %s\n", dev, strerror(errno)); exit(EXIT_FAILURE); } if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) exit(EXIT_FAILURE); if (!memcmp(sb.magic, bcache_magic, 16) && !wipe_bcache) { fprintf(stderr, "Already a bcache device on %s, " "overwrite with --wipe-bcache\n", dev); exit(EXIT_FAILURE); } if (!(pr = blkid_new_probe())) exit(EXIT_FAILURE); if (blkid_probe_set_device(pr, fd, 0, 0)) exit(EXIT_FAILURE); /* enable ptable probing; superblock probing is enabled by default */ if (blkid_probe_enable_partitions(pr, true)) exit(EXIT_FAILURE); if (!blkid_do_probe(pr)) { /* XXX wipefs doesn't know how to remove partition tables */ fprintf(stderr, "Device %s already has a non-bcache superblock, " "remove it using wipefs and wipefs -a\n", dev); exit(EXIT_FAILURE); } memset(&sb, 0, sizeof(struct cache_sb)); sb.offset = SB_SECTOR; sb.version = bdev ? BCACHE_SB_VERSION_BDEV : BCACHE_SB_VERSION_CDEV; memcpy(sb.magic, bcache_magic, 16); uuid_generate(sb.uuid); memcpy(sb.set_uuid, set_uuid, sizeof(sb.set_uuid)); sb.bucket_size = bucket_size; sb.block_size = block_size; uuid_unparse(sb.uuid, uuid_str); uuid_unparse(sb.set_uuid, set_uuid_str); if (SB_IS_BDEV(&sb)) { SET_BDEV_CACHE_MODE( &sb, writeback ? CACHE_MODE_WRITEBACK : CACHE_MODE_WRITETHROUGH); if (data_offset != BDEV_DATA_START_DEFAULT) { sb.version = BCACHE_SB_VERSION_BDEV_WITH_OFFSET; sb.data_offset = data_offset; } printf("UUID: %s\n" "Set UUID: %s\n" "version: %u\n" "block_size: %u\n" "data_offset: %ju\n", uuid_str, set_uuid_str, (unsigned) sb.version, sb.block_size, data_offset); } else { sb.nbuckets = getblocks(fd) / sb.bucket_size; sb.nr_in_set = 1; sb.first_bucket = (23 / sb.bucket_size) + 1; if (sb.nbuckets < 1 << 7) { fprintf(stderr, "Not enough buckets: %ju, need %u\n", sb.nbuckets, 1 << 7); exit(EXIT_FAILURE); } SET_CACHE_DISCARD(&sb, discard); SET_CACHE_REPLACEMENT(&sb, cache_replacement_policy); printf("UUID: %s\n" "Set UUID: %s\n" "version: %u\n" "nbuckets: %ju\n" "block_size: %u\n" "bucket_size: %u\n" "nr_in_set: %u\n" "nr_this_dev: %u\n" "first_bucket: %u\n", uuid_str, set_uuid_str, (unsigned) sb.version, sb.nbuckets, sb.block_size, sb.bucket_size, sb.nr_in_set, sb.nr_this_dev, sb.first_bucket); } sb.csum = csum_set(&sb); /* Zero start of disk */ if (pwrite(fd, zeroes, SB_START, 0) != SB_START) { perror("write error\n"); exit(EXIT_FAILURE); } /* Write superblock */ if (pwrite(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) { perror("write error\n"); exit(EXIT_FAILURE); } fsync(fd); close(fd); }
static virStoragePoolProbeResult virStorageBackendFileSystemProbe(const char *device, const char *format) { virStoragePoolProbeResult ret = FILESYSTEM_PROBE_ERROR; blkid_probe probe = NULL; const char *fstype = NULL; char *names[2], *libblkid_format = NULL; VIR_DEBUG("Probing for existing filesystem of type %s on device %s", format, device); if (blkid_known_fstype(format) == 0) { virStorageReportError(VIR_ERR_STORAGE_PROBE_FAILED, _("Not capable of probing for " "filesystem of type %s"), format); goto error; } probe = blkid_new_probe_from_filename(device); if (probe == NULL) { virStorageReportError(VIR_ERR_STORAGE_PROBE_FAILED, _("Failed to create filesystem probe " "for device %s"), device); goto error; } if ((libblkid_format = strdup(format)) == NULL) { virReportOOMError(); goto error; } names[0] = libblkid_format; names[1] = NULL; blkid_probe_filter_superblocks_type(probe, BLKID_FLTR_ONLYIN, names); if (blkid_do_probe(probe) != 0) { VIR_INFO("No filesystem of type '%s' found on device '%s'", format, device); ret = FILESYSTEM_PROBE_NOT_FOUND; } else if (blkid_probe_lookup_value(probe, "TYPE", &fstype, NULL) == 0) { virStorageReportError(VIR_ERR_STORAGE_POOL_BUILT, _("Existing filesystem of type '%s' found on " "device '%s'"), fstype, device); ret = FILESYSTEM_PROBE_FOUND; } if (blkid_do_probe(probe) != 1) { virStorageReportError(VIR_ERR_STORAGE_PROBE_FAILED, _("Found additional probes to run, " "filesystem probing may be incorrect")); ret = FILESYSTEM_PROBE_ERROR; } error: VIR_FREE(libblkid_format); if (probe != NULL) { blkid_free_probe(probe); } return ret; }
int main(int argc, char **argv) { bool udev = false; int i, o; extern char *optarg; struct cache_sb sb; char uuid[40]; blkid_probe pr; while ((o = getopt(argc, argv, "o:")) != EOF) switch (o) { case 'o': if (strcmp("udev", optarg)) { printf("Invalid output format %s\n", optarg); exit(EXIT_FAILURE); } udev = true; break; } argv += optind; argc -= optind; for (i = 0; i < argc; i++) { int fd = open(argv[i], O_RDONLY); if (fd == -1) continue; if (!(pr = blkid_new_probe())) continue; if (blkid_probe_set_device(pr, fd, 0, 0)) continue; /* probe partitions too */ if (blkid_probe_enable_partitions(pr, true)) continue; /* bail if anything was found * probe-bcache isn't needed once blkid recognizes bcache */ if (!blkid_do_probe(pr)) { continue; } if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) continue; if (memcmp(sb.magic, bcache_magic, 16)) continue; uuid_unparse(sb.uuid, uuid); if (udev) printf("ID_FS_UUID=%s\n" "ID_FS_UUID_ENC=%s\n" "ID_FS_TYPE=bcache\n", uuid, uuid); else printf("%s: UUID=\"\" TYPE=\"bcache\"\n", uuid); } return 0; }
/* * It is possible for a btrfs volume to cover multiple volumes and this routine * keeps only the first one seen and removes the rest. */ static stringList_t _remove_btfs_multiple_devices( stringList_t stl ) { stringList_t stx = StringListVoid ; stringList_t stz = StringListVoid ; StringListIterator it ; StringListIterator end ; string_t st ; const char * e ; blkid_probe blkid ; StringListGetIterators( stl,&it,&end ) ; zuluCryptSecurityGainElevatedPrivileges() ; while( it != end ){ st = *it ; it++ ; blkid = blkid_new_probe_from_filename( StringContent( st ) ) ; if( blkid != NULL ){ e = NULL ; blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&e,NULL ) ; if( StringsAreEqual( e,"btrfs" ) ){ e = NULL ; blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) ; if( StringListHasNoEntry( stx,e ) ){ /* * we got a btrfs volume with UUID we do not know about, * This will be the only device with this btrfs UUID we support and * all device operations must happen through this device and this device only. */ stz = StringListAppendString( stz,st ) ; stx = StringListAppend( stx,e ) ; }else{ /* * we already know this UUID and this device is not supported.Any operation on this * device should fail. */ } }else{ /* * not a btrfs volume */ stz = StringListAppendString( stz,st ) ; } blkid_free_probe( blkid ) ; }else{ stz = StringListAppendString( stz,st ) ; } } zuluCryptSecurityDropElevatedPrivileges() ; StringListMultipleDelete( &stl,&stx,NULL ) ; return stz ; }
u_int64_t zuluCryptGetVolumeSize( const char * device ) { stringList_t stl = StringListVoid ; StringListIterator it ; StringListIterator end ; string_t xt ; const char * e ; u_int64_t r = 0 ; blkid_probe blkid = blkid_new_probe_from_filename( device ) ; if( blkid == NULL ){ return 0 ; } e = NULL ; blkid_do_probe( blkid ) ; blkid_probe_lookup_value( blkid,"TYPE",&e,NULL ) ; if( StringsAreNotEqual( e,"btrfs" ) ){ r = blkid_probe_get_size( blkid ) ; blkid_free_probe( blkid ) ; return r ; }else{ /* * we got a btrfs volume,this device could be one among a bunch of devices that makes the btfs volume. * iterate through all known devices and add their sizes to this device if they are a part of the same * btrfs volume. */ e = NULL ; if( blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) == 0 ){ xt = String( e ) ; }else{ xt = StringVoid ; } blkid_free_probe( blkid ) ; if( xt == StringVoid ){ return 0 ; }else{ /* * zuluCryptVolumeList() is defined in this source file */ stl = zuluCryptVolumeList() ; zuluCryptSecurityGainElevatedPrivileges() ; StringListGetIterators( stl,&it,&end ) ; while( it != end ){ blkid = blkid_new_probe_from_filename( StringContent( *it ) ) ; it++ ; if( blkid != NULL ){ blkid_do_probe( blkid ) ; e = NULL ; if( blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) == 0 ){ if( StringEqual( xt,e ) ){ r += blkid_probe_get_size( blkid ) ; } } blkid_free_probe( blkid ) ; } } StringDelete( &xt ) ; StringListDelete( &stl ) ; return r ; } } }
void zuluCryptPrintPartitionProperties( const char * device ) { #define SIZE 64 char buffer[ SIZE ] ; const char * e ; u_int64_t size ; blkid_probe blkid ; zuluCryptSecurityGainElevatedPrivileges() ; if( StringPrefixEqual( device,"/dev/loop" ) ){ /* * zuluCryptLoopDeviceAddress_1() is defined in ../lib/create_loop_device.c */ e = zuluCryptLoopDeviceAddress_1( device ) ; if( e != NULL ){ printf( "%s\t",e ) ; StringFree( e ) ; }else{ printf( "%s\t",device ) ; } }else{ printf( "%s\t",device ) ; } size = zuluCryptGetVolumeSize( device ) ; blkid = blkid_new_probe_from_filename( device ) ; if( blkid == NULL ){ printf( "Nil\tNil\tNil\tNil\n" ) ; }else{ /* * zuluCryptFormatSize() is defined in ../lib/status.c */ zuluCryptFormatSize( size,buffer,SIZE ) ; printf( "%s\t",buffer ) ; blkid_do_probe( blkid ) ; if( blkid_probe_lookup_value( blkid,"LABEL",&e,NULL ) == 0 ){ printf( "%s\t",e ) ; }else{ printf( "Nil\t" ) ; } if( blkid_probe_lookup_value( blkid,"TYPE",&e,NULL ) == 0 ){ printf( "%s\t",e ) ; }else{ printf( "Nil\t" ) ; } if( blkid_probe_lookup_value( blkid,"UUID",&e,NULL ) == 0 ){ printf( "%s\n",e ) ; }else{ printf( "Nil\n" ) ; } blkid_free_probe( blkid ) ; } zuluCryptSecurityDropElevatedPrivileges() ; }
static struct wipe_desc * do_wipe(struct wipe_desc *wp, const char *devname, int flags) { int mode = O_RDWR, reread = 0, need_force = 0; blkid_probe pr; struct wipe_desc *w, *wp0; int zap = (flags & WP_FL_ALL) ? 1 : wp->zap; char *backup = NULL; if (!(flags & WP_FL_FORCE)) mode |= O_EXCL; pr = new_probe(devname, mode); if (!pr) return NULL; if (zap && (flags & WP_FL_BACKUP)) { const char *home = getenv ("HOME"); char *tmp = xstrdup(devname); if (!home) errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); xasprintf (&backup, "%s/wipefs-%s-", home, basename(tmp)); free(tmp); } wp0 = clone_offset(wp); while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; /* Check if offset is in provided list */ w = wp0; while(w && w->offset != wp->offset) w = w->next; if (wp0 && !w) continue; /* Mark done if found in provided list */ if (w) w->on_disk = wp->on_disk; if (!wp->on_disk) continue; if (!(flags & WP_FL_FORCE) && wp->is_parttable && !blkid_probe_is_wholedisk(pr)) { warnx(_("%s: ignoring nested \"%s\" partition table " "on non-whole disk device"), devname, wp->type); need_force = 1; continue; } if (zap) { if (backup) do_backup(wp, backup); do_wipe_real(pr, devname, wp, flags); if (wp->is_parttable) reread = 1; } } for (w = wp0; w != NULL; w = w->next) { if (!w->on_disk && !(flags & WP_FL_QUIET)) warnx(_("%s: offset 0x%jx not found"), devname, (uintmax_t)w->offset); } if (need_force) warnx(_("Use the --force option to force erase.")); fsync(blkid_probe_get_fd(pr)); #ifdef BLKRRPART if (reread && (mode & O_EXCL)) rereadpt(blkid_probe_get_fd(pr), devname); #endif close(blkid_probe_get_fd(pr)); blkid_free_probe(pr); free_wipe(wp0); free(backup); return wp; }
/* * Verify that the data in dev is consistent with what is on the actual * block device (using the devname field only). Normally this will be * called when finding items in the cache, but for long running processes * is also desirable to revalidate an item before use. * * If we are unable to revalidate the data, we return the old data and * do not set the BLKID_BID_FL_VERIFIED flag on it. */ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev) { struct stat st; time_t diff, now; char *fltr[2]; int fd; if (!dev) return NULL; now = time(0); diff = now - dev->bid_time; if (stat(dev->bid_name, &st) < 0) { DBG(DEBUG_PROBE, printf("blkid_verify: error %m (%d) while " "trying to stat %s\n", errno, dev->bid_name)); open_err: if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) { /* We don't have read permission, just return cache data. */ DBG(DEBUG_PROBE, printf("returning unverified data for %s\n", dev->bid_name)); return dev; } blkid_free_dev(dev); return NULL; } if (now >= dev->bid_time && #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC (st.st_mtime < dev->bid_time || (st.st_mtime == dev->bid_time && st.st_mtim.tv_nsec / 1000 <= dev->bid_utime)) && #else st.st_mtime <= dev->bid_time && #endif (diff < BLKID_PROBE_MIN || (dev->bid_flags & BLKID_BID_FL_VERIFIED && diff < BLKID_PROBE_INTERVAL))) return dev; #ifndef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC DBG(DEBUG_PROBE, printf("need to revalidate %s (cache time %lu, stat time %lu,\n\t" "time since last check %lu)\n", dev->bid_name, (unsigned long)dev->bid_time, (unsigned long)st.st_mtime, (unsigned long)diff)); #else DBG(DEBUG_PROBE, printf("need to revalidate %s (cache time %lu.%lu, stat time %lu.%lu,\n\t" "time since last check %lu)\n", dev->bid_name, (unsigned long)dev->bid_time, (unsigned long)dev->bid_utime, (unsigned long)st.st_mtime, (unsigned long)st.st_mtim.tv_nsec / 1000, (unsigned long)diff)); #endif if (!cache->probe) { cache->probe = blkid_new_probe(); if (!cache->probe) { blkid_free_dev(dev); return NULL; } } fd = open(dev->bid_name, O_RDONLY|O_CLOEXEC); if (fd < 0) { DBG(DEBUG_PROBE, printf("blkid_verify: error %m (%d) while " "opening %s\n", errno, dev->bid_name)); goto open_err; } if (blkid_probe_set_device(cache->probe, fd, 0, 0)) { /* failed to read the device */ close(fd); blkid_free_dev(dev); return NULL; } blkid_probe_enable_superblocks(cache->probe, TRUE); blkid_probe_set_superblocks_flags(cache->probe, BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE); blkid_probe_enable_partitions(cache->probe, TRUE); blkid_probe_set_partitions_flags(cache->probe, BLKID_PARTS_ENTRY_DETAILS); /* * If we already know the type, then try that first. */ if (dev->bid_type) { blkid_tag_iterate iter; const char *type, *value; fltr[0] = dev->bid_type; fltr[1] = NULL; blkid_probe_filter_superblocks_type(cache->probe, BLKID_FLTR_ONLYIN, fltr); if (!blkid_do_probe(cache->probe)) goto found_type; blkid_probe_invert_superblocks_filter(cache->probe); /* * Zap the device filesystem information and try again */ DBG(DEBUG_PROBE, printf("previous fs type %s not valid, " "trying full probe\n", dev->bid_type)); iter = blkid_tag_iterate_begin(dev); while (blkid_tag_next(iter, &type, &value) == 0) blkid_set_tag(dev, type, 0, 0); blkid_tag_iterate_end(iter); } /* * Probe for all types. */ if (blkid_do_safeprobe(cache->probe)) { /* found nothing or error */ blkid_free_dev(dev); dev = NULL; } found_type: if (dev) { #ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC struct timeval tv; if (!gettimeofday(&tv, NULL)) { dev->bid_time = tv.tv_sec; dev->bid_utime = tv.tv_usec; } else #endif dev->bid_time = time(0); dev->bid_devno = st.st_rdev; dev->bid_flags |= BLKID_BID_FL_VERIFIED; cache->bic_flags |= BLKID_BIC_FL_CHANGED; blkid_probe_to_tags(cache->probe, dev); DBG(DEBUG_PROBE, printf("%s: devno 0x%04llx, type %s\n", dev->bid_name, (long long)st.st_rdev, dev->bid_type)); } blkid_reset_probe(cache->probe); blkid_probe_reset_superblocks_filter(cache->probe); close(fd); return dev; }
static struct wipe_desc * do_wipe(struct wipe_desc *wp, const char *devname, int flags) { int mode = O_RDWR, reread = 0; blkid_probe pr; struct wipe_desc *w, *wp0; int zap = (flags & WP_FL_ALL) ? 1 : wp->zap; char *backup = NULL; if (!(flags & WP_FL_FORCE)) mode |= O_EXCL; pr = new_probe(devname, mode); if (!pr) return NULL; if (zap && (flags & WP_FL_BACKUP)) { const char *home = getenv ("HOME"); if (!home) errx(EXIT_FAILURE, _("failed to create a signature backup, $HOME undefined")); xasprintf (&backup, "%s/wipefs-%s-", home, basename(devname)); } wp0 = clone_offset(wp); while (blkid_do_probe(pr) == 0) { wp = get_desc_for_probe(wp, pr); if (!wp) break; /* Check if offset is in provided list */ w = wp0; while(w && w->offset != wp->offset) w = w->next; if (wp0 && !w) continue; /* Mark done if found in provided list */ if (w) w->on_disk = wp->on_disk; if (!wp->on_disk) continue; if (zap) { if (backup) do_backup(wp, backup); do_wipe_real(pr, devname, wp, flags); if (wp->is_parttable) reread = 1; } } for (w = wp0; w != NULL; w = w->next) { if (!w->on_disk && !(flags & WP_FL_QUIET)) warnx(_("%s: offset 0x%jx not found"), devname, w->offset); } fsync(blkid_probe_get_fd(pr)); if (reread) rereadpt(blkid_probe_get_fd(pr), devname); close(blkid_probe_get_fd(pr)); blkid_free_probe(pr); free_wipe(wp0); free(backup); return wp; }