Beispiel #1
0
/**
 * fdisk_get_columns:
 * @cxt: fdisk context
 * @all: 1 or 0
 * @cols: returns allocated array with FDISK_COL_* IDs
 * @ncols: returns number of items in cols
 *
 * This function returns the default or all columns for the current label.  The
 * library uses the columns for list operations (see fdisk_list_disklabel() and
 * fdisk_list_partitions()). Note that the set of the default columns depends
 * on fdisk_context_enable_details() function. If the details are eanable then
 * this function usually returns more columns.
 *
 * Returns 0 on success, otherwise, a corresponding error.
 */
int fdisk_get_columns(struct fdisk_context *cxt, int all, int **cols, size_t *ncols)
{
	size_t i, n;
	int *c;

	assert(cxt);

	if (!cxt->label)
		return -EINVAL;
	if (!cxt->label->columns || !cxt->label->ncolumns)
		return -ENOSYS;
	c = calloc(cxt->label->ncolumns, sizeof(int));
	if (!c)
		return -ENOMEM;
	for (n = 0, i = 0; i < cxt->label->ncolumns; i++) {
		int id = cxt->label->columns[i].id;

		if (!all &&
		    ((fdisk_context_display_details(cxt) &&
				(cxt->label->columns[i].flags & FDISK_COLFL_EYECANDY))
		     || (!fdisk_context_display_details(cxt) &&
				(cxt->label->columns[i].flags & FDISK_COLFL_DETAIL))
		     || (id == FDISK_COL_SECTORS &&
			         fdisk_context_use_cylinders(cxt))
		     || (id == FDISK_COL_CYLINDERS &&
			         !fdisk_context_use_cylinders(cxt))))
			continue;

		c[n++] = id;
	}
	if (cols)
		*cols = c;
	if (ncols)
		*ncols = n;
	return 0;
}
Beispiel #2
0
static int sgi_list_table(struct fdisk_context *cxt)
{
	struct sgi_disklabel *sgilabel = self_disklabel(cxt);
	struct sgi_device_parameter *sgiparam = &sgilabel->devparam;
	int rc = 0;

	if (fdisk_context_display_details(cxt))
		fdisk_info(cxt, _(
			"Label geometry: %d heads, %llu sectors\n"
			"                %llu cylinders, %d physical cylinders\n"
			"                %d extra sects/cyl, interleave %d:1\n"),
			cxt->geom.heads, cxt->geom.sectors,
			cxt->geom.cylinders, be16_to_cpu(sgiparam->pcylcount),
			(int) sgiparam->sparecyl, be16_to_cpu(sgiparam->ilfact));

	fdisk_info(cxt, _("Bootfile: %s"), sgilabel->boot_file);
	return rc;
}
Beispiel #3
0
static int bsd_list_disklabel(struct fdisk_context *cxt)
{
    struct bsd_disklabel *d = self_disklabel(cxt);

    assert(cxt);
    assert(cxt->label);
    assert(fdisk_is_disklabel(cxt, BSD));

    if (fdisk_context_display_details(cxt)) {
        fdisk_info(cxt, "# %s:", cxt->dev_path);

        if ((unsigned) d->d_type < BSD_DKMAXTYPES)
            fdisk_info(cxt, _("type: %s"), bsd_dktypenames[d->d_type]);
        else
            fdisk_info(cxt, _("type: %d"), d->d_type);

        fdisk_info(cxt, _("disk: %.*s"), (int) sizeof(d->d_typename), d->d_typename);
        fdisk_info(cxt, _("label: %.*s"), (int) sizeof(d->d_packname), d->d_packname);

        fdisk_info(cxt, _("flags: %s"),
                   d->d_flags & BSD_D_REMOVABLE ? _(" removable") :
                   d->d_flags & BSD_D_ECC ? _(" ecc") :
                   d->d_flags & BSD_D_BADSECT ? _(" badsect") : "");

        /* On various machines the fields of *lp are short/int/long */
        /* In order to avoid problems, we cast them all to long. */
        fdisk_info(cxt, _("bytes/sector: %ld"), (long) d->d_secsize);
        fdisk_info(cxt, _("sectors/track: %ld"), (long) d->d_nsectors);
        fdisk_info(cxt, _("tracks/cylinder: %ld"), (long) d->d_ntracks);
        fdisk_info(cxt, _("sectors/cylinder: %ld"), (long) d->d_secpercyl);
        fdisk_info(cxt, _("cylinders: %ld"), (long) d->d_ncylinders);
        fdisk_info(cxt, _("rpm: %d"), d->d_rpm);
        fdisk_info(cxt, _("interleave: %d"), d->d_interleave);
        fdisk_info(cxt, _("trackskew: %d"), d->d_trackskew);
        fdisk_info(cxt, _("cylinderskew: %d"), d->d_cylskew);
        fdisk_info(cxt, _("headswitch: %ld (milliseconds)"), (long) d->d_headswitch);
        fdisk_info(cxt, _("track-to-track seek: %ld (milliseconds)"), (long) d->d_trkseek);
    }

    fdisk_info(cxt, _("partitions: %d"), d->d_npartitions);

    return 0;
}
Beispiel #4
0
/* Returns 0 on success, < 0 on error. */
static int bsd_create_disklabel(struct fdisk_context *cxt)
{
    int rc, yes = 0;
    struct bsd_disklabel *d = self_disklabel(cxt);

    fdisk_info(cxt, _("The device %s does not contain BSD disklabel."), cxt->dev_path);
    rc = fdisk_ask_yesno(cxt,
                         _("Do you want to create a BSD disklabel?"),
                         &yes);
    if (rc)
        return rc;
    if (!yes)
        return 1;
    if (cxt->parent) {
        rc = bsd_assign_dos_partition(cxt);
        if (rc == 1)
            /* not found DOS partition usable for BSD label */
            rc = -EINVAL;
    }
    if (rc)
        return rc;

    rc = bsd_initlabel(cxt);
    if (!rc) {
        int org = fdisk_context_display_details(cxt);

        cxt->label->nparts_cur = d->d_npartitions;
        cxt->label->nparts_max = BSD_MAXPARTITIONS;

        fdisk_context_enable_details(cxt, 1);
        bsd_list_disklabel(cxt);
        fdisk_context_enable_details(cxt, org);
    }

    return rc;
}
Beispiel #5
0
static int sgi_list_table(struct fdisk_context *cxt)
{
	struct tt *tb = NULL;
	struct sgi_disklabel *sgilabel = self_disklabel(cxt);
	struct sgi_device_parameter *sgiparam = &sgilabel->devparam;
	size_t i, used;
	char *p;
	int rc;

	if (fdisk_context_display_details(cxt))
		fdisk_colon(cxt, _(
			"Label geometry: %d heads, %llu sectors\n"
			"                %llu cylinders, %d physical cylinders\n"
			"                %d extra sects/cyl, interleave %d:1\n"),
			cxt->geom.heads, cxt->geom.sectors,
			cxt->geom.cylinders, be16_to_cpu(sgiparam->pcylcount),
			(int) sgiparam->sparecyl, be16_to_cpu(sgiparam->ilfact));

	/*
	 * Partitions
	 */
	tb = tt_new_table(TT_FL_FREEDATA);
	if (!tb)
		return -ENOMEM;

	tt_define_column(tb, _("Pt#"),      3, TT_FL_RIGHT);
	tt_define_column(tb, _("Device"), 0.2, 0);
	tt_define_column(tb, _("Info"),     2, 0);
	tt_define_column(tb, _("Start"),    9, TT_FL_RIGHT);
	tt_define_column(tb, _("End"),      9, TT_FL_RIGHT);
	tt_define_column(tb, _("Sectors"),  9, TT_FL_RIGHT);
	tt_define_column(tb, _("Id"),       2, TT_FL_RIGHT);
	tt_define_column(tb, _("System"), 0.2, TT_FL_TRUNC);

	for (i = 0, used = 1; i < cxt->label->nparts_max; i++) {
		uint32_t start, len;
		struct fdisk_parttype *t;
		struct tt_line *ln;

		if (sgi_get_num_sectors(cxt, i) == 0)
			continue;

		ln = tt_add_line(tb, NULL);
		if (!ln)
			continue;
		start = sgi_get_start_sector(cxt, i);
		len = sgi_get_num_sectors(cxt, i);
		t = fdisk_get_partition_type(cxt, i);

		if (asprintf(&p, "%zu:", i + 1) > 0)
			tt_line_set_data(ln, 0, p);	/* # */
		p = fdisk_partname(cxt->dev_path, used++);
		if (p)
			tt_line_set_data(ln, 1, p);	/* Device */

		p = sgi_get_swappartition(cxt) == (int) i ? "swap" :
		    sgi_get_bootpartition(cxt) == (int) i ? "boot" : NULL;
		if (p)
			tt_line_set_data(ln, 2, strdup(p));	/* Info */

		if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start)) > 0)
			tt_line_set_data(ln, 3, p);	/* Start */
		if (asprintf(&p, "%ju",	(uintmax_t) fdisk_scround(cxt, start + len) - 1) > 0)
			tt_line_set_data(ln, 4, p);	/* End */
		if (asprintf(&p, "%ju",	(uintmax_t) len) > 0)
			tt_line_set_data(ln, 5, p);	/* Sectors*/
		if (asprintf(&p, "%2x", t->type) > 0)
			tt_line_set_data(ln, 6, p);	/* type ID */
		if (t->name)
			tt_line_set_data(ln, 7, strdup(t->name)); /* type Name */
		fdisk_free_parttype(t);
	}

	rc = fdisk_print_table(cxt, tb);
	tt_free_table(tb);
	if (rc)
		return rc;

	/*
	 * Volumes
	 */
	tb = tt_new_table(TT_FL_FREEDATA);
	if (!tb)
		return -ENOMEM;

	tt_define_column(tb, _("#"),       3, TT_FL_RIGHT);
	tt_define_column(tb, _("Name"),  0.2, 0);
	tt_define_column(tb, _("Sector"),  2, TT_FL_RIGHT);
	tt_define_column(tb, _("Size"),    9, TT_FL_RIGHT);

	for (i = 0, used = 0; i < SGI_MAXVOLUMES; i++) {
		struct tt_line *ln;
		uint32_t start = be32_to_cpu(sgilabel->volume[i].block_num),
			 len = be32_to_cpu(sgilabel->volume[i].num_bytes);
		if (!len)
			continue;
		ln = tt_add_line(tb, NULL);
		if (!ln)
			continue;
		if (asprintf(&p, "%zu:", i) > 0)
			tt_line_set_data(ln, 0, p);		/* # */
		if (*sgilabel->volume[i].name)
			tt_line_set_data(ln, 1,
				strndup((char *) sgilabel->volume[i].name,
				        sizeof(sgilabel->volume[i].name)));	/* Name */
		if (asprintf(&p, "%ju", (uintmax_t) start) > 0)
			tt_line_set_data(ln, 2, p);	/* Sector */
		if (asprintf(&p, "%ju",	(uintmax_t) len) > 0)
			tt_line_set_data(ln, 3, p);	/* Size */
		used++;
	}

	if (used)
		rc = fdisk_print_table(cxt, tb);
	tt_free_table(tb);

	fdisk_colon(cxt, _("Bootfile: %s"), sgilabel->boot_file);

	return rc;
}
Beispiel #6
0
static int sun_list_disklabel(struct fdisk_context *cxt)
{
	struct sun_disklabel *sunlabel;
	struct tt *tb = NULL;
	size_t i;
	int rc;

	assert(cxt);
	assert(cxt->label);
	assert(fdisk_is_disklabel(cxt, SUN));

	sunlabel = self_disklabel(cxt);

	if (fdisk_context_display_details(cxt)) {
		fdisk_colon(cxt,
		_("Label geometry: %d rpm, %d alternate and %d physical cylinders,\n"
		  "                %d extra sects/cyl, interleave %d:1"),
		       be16_to_cpu(sunlabel->rpm),
		       be16_to_cpu(sunlabel->acyl),
		       be16_to_cpu(sunlabel->pcyl),
		       be16_to_cpu(sunlabel->apc),
		       be16_to_cpu(sunlabel->intrlv));
		fdisk_colon(cxt, _("Label ID: %s"), sunlabel->label_id);
		fdisk_colon(cxt, _("Volume ID: %s"),
		       *sunlabel->vtoc.volume_id ? sunlabel->vtoc.volume_id : _("<none>"));
	}

	tb = tt_new_table(TT_FL_FREEDATA);
	if (!tb)
		return -ENOMEM;

	tt_define_column(tb, _("Device"), 0.2, 0);
	tt_define_column(tb, _("Flag"),     2, TT_FL_RIGHT);
	tt_define_column(tb, _("Start"),    9, TT_FL_RIGHT);
	tt_define_column(tb, _("End"),      9, TT_FL_RIGHT);
	/* TRANSLATORS: keep one blank space behind 'Blocks' */
	tt_define_column(tb, _("Blocks "),  9, TT_FL_RIGHT);
	tt_define_column(tb, _("Id"),       2, TT_FL_RIGHT);
	tt_define_column(tb, _("System"), 0.2, TT_FL_TRUNC);

	for (i = 0 ; i < cxt->label->nparts_max; i++) {
		struct sun_partition *part = &sunlabel->partitions[i];
		uint16_t flags = be16_to_cpu(sunlabel->vtoc.infos[i].flags);
		uint32_t start, len;
		struct fdisk_parttype *t;
		struct tt_line *ln;
		char *p;

		if (!part->num_sectors)
			continue;
		ln = tt_add_line(tb, NULL);
		if (!ln)
			continue;

		start = be32_to_cpu(part->start_cylinder)
				* cxt->geom.heads
				* cxt->geom.sectors;

		len = be32_to_cpu(part->num_sectors);
		t = fdisk_get_partition_type(cxt, i);

		p = fdisk_partname(cxt->dev_path, i + 1);
		if (p)
			tt_line_set_data(ln, 0, p);	/* devname */
		if ((flags & SUN_FLAG_UNMNT || flags & SUN_FLAG_RONLY)
		    && asprintf(&p, "%c%c",
				flags & SUN_FLAG_UNMNT ? 'u' : ' ',
				flags & SUN_FLAG_RONLY ? 'r' : ' ') > 0)
			tt_line_set_data(ln, 1, p);	/* flags */
		if (asprintf(&p, "%ju", (uintmax_t) fdisk_scround(cxt, start)) > 0)
			tt_line_set_data(ln, 2, p);	/* start */
		if (asprintf(&p, "%ju",	(uintmax_t) fdisk_scround(cxt, start + len - 1)) > 0)
			tt_line_set_data(ln, 3, p);	/* end */
		if (asprintf(&p, "%lu%c",
				(unsigned long) len / 2,
				len & 1 ? '+' : ' ') > 0)
			tt_line_set_data(ln, 4, p);	/* blocks + flag */
		if (asprintf(&p, "%2x", t->type) > 0)
			tt_line_set_data(ln, 5, p);	/* type ID */
		if (t->name)
			tt_line_set_data(ln, 6, strdup(t->name)); /* type Name */

		fdisk_free_parttype(t);
	}

	rc = fdisk_print_table(cxt, tb);
	tt_free_table(tb);

	return rc;
}