Ejemplo n.º 1
0
static int
g_sunlabel_modify(struct g_geom *gp, struct g_sunlabel_softc *ms, u_char *sec0)
{
	int i, error;
	u_int u, v, csize;
	struct sun_disklabel sl;
	MD5_CTX md5sum;

	error = sunlabel_dec(sec0, &sl);
	if (error)
		return (error);

	csize = sl.sl_ntracks * sl.sl_nsectors;

	for (i = 0; i < SUN_NPART; i++) {
		v = sl.sl_part[i].sdkp_cyloffset;
		u = sl.sl_part[i].sdkp_nsectors;
		error = g_slice_config(gp, i, G_SLICE_CONFIG_CHECK,
		    ((off_t)v * csize) << 9ULL,
		    ((off_t)u) << 9ULL,
		    ms->sectorsize,
		    "%s%c", gp->name, 'a' + i);
		if (error)
			return (error);
	}
	for (i = 0; i < SUN_NPART; i++) {
		v = sl.sl_part[i].sdkp_cyloffset;
		u = sl.sl_part[i].sdkp_nsectors;
		g_slice_config(gp, i, G_SLICE_CONFIG_SET,
		    ((off_t)v * csize) << 9ULL,
		    ((off_t)u) << 9ULL,
		    ms->sectorsize,
		    "%s%c", gp->name, 'a' + i);
	}
	ms->nalt = sl.sl_acylinders;
	ms->nheads = sl.sl_ntracks;
	ms->nsects = sl.sl_nsectors;

	/*
	 * Calculate MD5 from the first sector and use it for avoiding
	 * recursive labels creation.
	 */
	MD5Init(&md5sum);
	MD5Update(&md5sum, sec0, ms->sectorsize);
	MD5Final(ms->labelsum, &md5sum);

	return (0);
}
Ejemplo n.º 2
0
static void
read_label(struct sun_disklabel *sl, const char *disk)
{
	char path[MAXPATHLEN];
	uint32_t fwsectors;
	uint32_t fwheads;
	char buf[SUN_SIZE];
	int fd, error;

	snprintf(path, sizeof(path), "%s%s", _PATH_DEV, disk);
	if ((fd = open(path, O_RDONLY)) < 0)
		err(1, "open %s", path);
	if (read(fd, buf, sizeof(buf)) != sizeof(buf))
		err(1, "read");
	error = sunlabel_dec(buf, sl);
	if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) != 0)
		if (error)
			err(1, "%s: ioctl(DIOCGMEDIASIZE) failed", disk);
	if (ioctl(fd, DIOCGSECTORSIZE, &sectorsize) != 0) {
		if (error)
			err(1, "%s: DIOCGSECTORSIZE failed", disk);
		else
			sectorsize = 512;
	}
	if (error) {
		bzero(sl, sizeof(*sl));
		if (ioctl(fd, DIOCGFWSECTORS, &fwsectors) != 0)
			fwsectors = 63;
		if (ioctl(fd, DIOCGFWHEADS, &fwheads) != 0) {
			if (mediasize <= 63 * 1024 * sectorsize)
				fwheads = 1;
			else if (mediasize <= 63 * 16 * 1024 * sectorsize)
				fwheads = 16;
			else
				fwheads = 255;
		}
		sl->sl_rpm = 3600;
		sl->sl_pcylinders = mediasize / (fwsectors * fwheads *
		    sectorsize);
		sl->sl_sparespercyl = 0;
		sl->sl_interleave = 1;
		sl->sl_ncylinders = sl->sl_pcylinders - 2;
		sl->sl_acylinders = 2;
		sl->sl_nsectors = fwsectors;
		sl->sl_ntracks = fwheads;
		sl->sl_part[SUN_RAWPART].sdkp_cyloffset = 0;
		sl->sl_part[SUN_RAWPART].sdkp_nsectors = sl->sl_ncylinders *
		    sl->sl_ntracks * sl->sl_nsectors;
		if (mediasize > (off_t)4999L * 1024L * 1024L) {
			sprintf(sl->sl_text,
			    "FreeBSD%jdG cyl %u alt %u hd %u sec %u",
			    (intmax_t)(mediasize + 512 * 1024 * 1024) /
			        (1024 * 1024 * 1024),
			    sl->sl_ncylinders, sl->sl_acylinders,
			    sl->sl_ntracks, sl->sl_nsectors);
		} else {
			sprintf(sl->sl_text,
			    "FreeBSD%jdM cyl %u alt %u hd %u sec %u",
			    (intmax_t)(mediasize + 512 * 1024) / (1024 * 1024),
			    sl->sl_ncylinders, sl->sl_acylinders,
			    sl->sl_ntracks, sl->sl_nsectors);
		}
	}
	close(fd);
}