size_t geom_get_size(char *gname) { size_t sz; int g; g = g_open(gname, 0); if (g == -1) return (0); sz = g_mediasize(g); if (sz == -1) return (0); g_close(g); return (sz); }
int main(int argc, char **argv) { struct stat sb; struct disk d; if (argc < 2) errx(1, "Usage: %s <GEOM provider name> | " "<disk image file name>", argv[0]); d.name = argv[1]; if (stat(d.name, &sb) == 0 && S_ISREG(sb.st_mode)) { d.fd = open(d.name, O_RDONLY); if (d.fd < 0) err(1, "open %s", d.name); d.mediasize = sb.st_size; d.sectorsize = 512; d.file = 1; } else { d.fd = g_open(d.name, 0); if (d.fd < 0) err(1, "g_open %s", d.name); d.mediasize = g_mediasize(d.fd); d.sectorsize = g_sectorsize(d.fd); d.file = 0; } d.offset = 0; printf("%s \"%s\" opened\n", d.file ? "Disk image": "GEOM provider", d.name); printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n", d.mediasize, d.mediasize / d.sectorsize, d.sectorsize); inspect_disk(&d); if (d.file) close(d.fd); else g_close(d.fd); return (0); }
off_t diskfile_device_size(const char *path) { int fd = g_open(path, 0); off_t size = g_mediasize(fd); g_close(fd); return size; }
/* * Fetch disklabel for disk. */ static int readlabel(int flag) { ssize_t nbytes; uint32_t lba; int f, i; int error; f = open(specname, O_RDONLY); if (f < 0) err(1, "%s", specname); if (is_file) get_file_parms(f); else { mediasize = g_mediasize(f); secsize = g_sectorsize(f); if (secsize < 0 || mediasize < 0) err(4, "cannot get disk geometry"); } if (mediasize > (off_t)0xffffffff * secsize) errx(1, "disks with more than 2^32-1 sectors are not supported"); (void)lseek(f, (off_t)0, SEEK_SET); nbytes = read(f, bootarea, BBSIZE); if (nbytes == -1) err(4, "%s read", specname); if (nbytes != BBSIZE) errx(4, "couldn't read %d bytes from %s", BBSIZE, specname); close (f); error = bsd_disklabel_le_dec( bootarea + (labeloffset + labelsoffset * secsize), &lab, MAXPARTITIONS); if (flag && error) errx(1, "%s: no valid label found", specname); if (is_file) return(0); /* * Compensate for absolute block addressing by finding the * smallest partition offset and if the offset of the 'c' * partition is equal to that, subtract it from all offsets. */ lba = ~0; for (i = 0; i < lab.d_npartitions; i++) { if (lab.d_partitions[i].p_size) lba = MIN(lba, lab.d_partitions[i].p_offset); } if (lba != 0 && lab.d_partitions[RAW_PART].p_offset == lba) { for (i = 0; i < lab.d_npartitions; i++) { if (lab.d_partitions[i].p_size) lab.d_partitions[i].p_offset -= lba; } /* * Save the offset so that we can write the label * back with absolute block addresses. */ lba_offset = lba; } return (error); }
/* * When operating on a "virgin" disk, try getting an initial label * from the associated device driver. This might work for all device * drivers that are able to fetch some initial device parameters * without even having access to a (BSD) disklabel, like SCSI disks, * most IDE drives, or vn devices. * * The device name must be given in its "canonical" form. */ static struct disklabel * getvirginlabel(void) { static struct disklabel loclab; struct partition *dp; int f; u_int u; if ((f = open(specname, O_RDONLY)) == -1) { warn("cannot open %s", specname); return (NULL); } if (is_file) get_file_parms(f); else { mediasize = g_mediasize(f); secsize = g_sectorsize(f); if (secsize < 0 || mediasize < 0) { close (f); return (NULL); } } memset(&loclab, 0, sizeof loclab); loclab.d_magic = DISKMAGIC; loclab.d_magic2 = DISKMAGIC; loclab.d_secsize = secsize; loclab.d_secperunit = mediasize / secsize; /* * Nobody in these enlightened days uses the CHS geometry for * anything, but nonetheless try to get it right. If we fail * to get any good ideas from the device, construct something * which is IBM-PC friendly. */ if (ioctl(f, DIOCGFWSECTORS, &u) == 0) loclab.d_nsectors = u; else loclab.d_nsectors = 63; if (ioctl(f, DIOCGFWHEADS, &u) == 0) loclab.d_ntracks = u; else if (loclab.d_secperunit <= 63*1*1024) loclab.d_ntracks = 1; else if (loclab.d_secperunit <= 63*16*1024) loclab.d_ntracks = 16; else loclab.d_ntracks = 255; loclab.d_secpercyl = loclab.d_ntracks * loclab.d_nsectors; loclab.d_ncylinders = loclab.d_secperunit / loclab.d_secpercyl; loclab.d_npartitions = DEFPARTITIONS; /* Various (unneeded) compat stuff */ loclab.d_rpm = 3600; loclab.d_bbsize = BBSIZE; loclab.d_interleave = 1; strncpy(loclab.d_typename, "amnesiac", sizeof(loclab.d_typename)); dp = &loclab.d_partitions[RAW_PART]; dp->p_size = loclab.d_secperunit; loclab.d_checksum = dkcksum(&loclab); close (f); return (&loclab); }