2006-12-06 17:25:34 -06:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
|
2014-09-02 11:57:27 -05:00
|
|
|
* Copyright (C) 2014 Karel Zak <kzak@redhat.com>
|
2006-12-06 17:25:34 -06:00
|
|
|
*
|
|
|
|
* This program is free software. You can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation: either Version 1
|
|
|
|
* or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
|
|
|
|
* patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
|
|
|
|
* leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
|
|
|
|
* 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
|
|
|
|
* This program had (head,sector,cylinder) as basic unit, and was
|
|
|
|
* (therefore) broken in several ways for the use on larger disks -
|
|
|
|
* for example, my last patch (from 2.0d to 2.0e) was required
|
|
|
|
* to allow a partition to cross cylinder 8064, and to write an
|
|
|
|
* extended partition past the 4GB mark.
|
|
|
|
*
|
2014-09-02 11:57:27 -05:00
|
|
|
* Karel Zak wrote new sfdisk based on libfdisk from util-linux
|
|
|
|
* in 2014.
|
2006-12-06 17:25:34 -06:00
|
|
|
*/
|
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
#include <unistd.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <stdio.h>
|
2014-09-02 11:57:27 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <ctype.h>
|
2014-09-02 11:57:27 -05:00
|
|
|
#include <errno.h>
|
2006-12-06 17:25:34 -06:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <sys/stat.h>
|
2014-09-04 07:05:16 -05:00
|
|
|
#include <assert.h>
|
2009-10-15 18:15:19 -05:00
|
|
|
|
|
|
|
#include "c.h"
|
2011-12-20 07:42:00 -06:00
|
|
|
#include "xalloc.h"
|
2014-09-02 11:57:27 -05:00
|
|
|
#include "nls.h"
|
|
|
|
#include "debug.h"
|
2012-04-23 05:12:32 -05:00
|
|
|
#include "strutils.h"
|
2014-09-02 11:57:27 -05:00
|
|
|
#include "closestream.h"
|
2014-09-04 07:05:16 -05:00
|
|
|
#include "colors.h"
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
#include "libfdisk.h"
|
2014-09-04 07:05:16 -05:00
|
|
|
#include "fdisk-list.h"
|
2006-12-06 17:25:34 -06:00
|
|
|
|
|
|
|
/*
|
2014-09-02 11:57:27 -05:00
|
|
|
* sfdisk debug stuff (see fdisk.h and include/debug.h)
|
2006-12-06 17:25:34 -06:00
|
|
|
*/
|
2014-09-02 11:57:27 -05:00
|
|
|
UL_DEBUG_DEFINE_MASK(sfdisk);
|
|
|
|
UL_DEBUG_DEFINE_MASKANEMS(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
#define SFDISKPROG_DEBUG_INIT (1 << 1)
|
|
|
|
#define SFDISKPROG_DEBUG_PARSE (1 << 2)
|
|
|
|
#define SFDISKPROG_DEBUG_MISC (1 << 3)
|
|
|
|
#define SFDISKPROG_DEBUG_ALL 0xFFFF
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
#define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
|
|
|
|
#define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
enum {
|
|
|
|
ACT_FDISK = 0, /* default */
|
|
|
|
|
|
|
|
ACT_ACTIVATE,
|
|
|
|
ACT_CHANGE_ID,
|
|
|
|
ACT_DUMP,
|
|
|
|
ACT_LIST,
|
|
|
|
ACT_LIST_SIZES,
|
|
|
|
ACT_LIST_TYPES,
|
|
|
|
ACT_VERIFY
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sfdisk {
|
|
|
|
int act; /* action */
|
|
|
|
size_t partno; /* partition number <1..N> */
|
|
|
|
|
|
|
|
struct fdisk_context *cxt; /* libfdisk context */
|
|
|
|
};
|
|
|
|
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
static void sfdiskprog_init_debug(void)
|
|
|
|
{
|
|
|
|
__UL_INIT_DEBUG(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
|
2006-12-06 17:25:34 -06:00
|
|
|
}
|
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
static int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
|
|
|
|
void *data __attribute__((__unused__)))
|
|
|
|
{
|
|
|
|
assert(cxt);
|
|
|
|
assert(ask);
|
|
|
|
|
|
|
|
switch(fdisk_ask_get_type(ask)) {
|
|
|
|
case FDISK_ASKTYPE_INFO:
|
|
|
|
fputs(fdisk_ask_print_get_mesg(ask), stdout);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
break;
|
|
|
|
case FDISK_ASKTYPE_WARNX:
|
|
|
|
color_scheme_fenable("warn", UL_COLOR_RED, stderr);
|
|
|
|
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
|
|
|
color_fdisable(stderr);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
break;
|
|
|
|
case FDISK_ASKTYPE_WARN:
|
|
|
|
color_scheme_fenable("warn", UL_COLOR_RED, stderr);
|
|
|
|
fputs(fdisk_ask_print_get_mesg(ask), stderr);
|
|
|
|
errno = fdisk_ask_print_get_errno(ask);
|
|
|
|
fprintf(stderr, ": %m\n");
|
|
|
|
color_fdisable(stderr);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sfdisk_init(struct sfdisk *sf)
|
|
|
|
{
|
|
|
|
fdisk_init_debug(0);
|
|
|
|
sfdiskprog_init_debug();
|
|
|
|
|
|
|
|
colors_init(UL_COLORMODE_UNDEF, "sfdisk");
|
|
|
|
|
|
|
|
sf->cxt = fdisk_new_context();
|
|
|
|
if (!sf->cxt)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
|
|
|
|
fdisk_set_ask(sf->cxt, ask_callback, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sfdisk_deinit(struct sfdisk *sf)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(sf);
|
|
|
|
assert(sf->cxt);
|
|
|
|
|
|
|
|
fdisk_unref_context(sf->cxt);
|
|
|
|
memset(sf, 0, sizeof(*sf));
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --list backend */
|
|
|
|
static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fdisk_enable_listonly(sf->cxt, 1);
|
|
|
|
|
2014-09-04 07:16:08 -05:00
|
|
|
if (argc) {
|
2014-09-04 08:50:36 -05:00
|
|
|
int ct = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (ct)
|
|
|
|
fputs("\n\n", stdout);
|
|
|
|
if (print_device_pt(sf->cxt, argv[i], 0) == 0)
|
|
|
|
ct++;
|
|
|
|
}
|
2014-09-04 07:05:16 -05:00
|
|
|
} else
|
|
|
|
print_all_devices_pt(sf->cxt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-04 08:50:36 -05:00
|
|
|
static int command_dump(struct sfdisk *sf, int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *devname = NULL;
|
|
|
|
struct fdisk_script *dp;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
devname = argv[0];
|
|
|
|
if (!devname)
|
|
|
|
errx(EXIT_FAILURE, _("no disk device specified"));
|
|
|
|
|
|
|
|
rc = fdisk_assign_device(sf->cxt, devname, 1);
|
|
|
|
if (rc)
|
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), devname);
|
|
|
|
|
|
|
|
dp = fdisk_new_script(sf->cxt);
|
|
|
|
if (!dp)
|
|
|
|
err(EXIT_FAILURE, _("failed to allocate dump struct"));
|
|
|
|
|
|
|
|
rc = fdisk_script_read_context(dp, NULL);
|
|
|
|
if (rc)
|
|
|
|
err(EXIT_FAILURE, _("failed to dump partition table"));
|
|
|
|
|
|
|
|
fdisk_script_write_file(dp, stdout);
|
|
|
|
|
|
|
|
fdisk_unref_script(dp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
/* default command */
|
|
|
|
static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int rc;
|
2014-09-04 08:50:36 -05:00
|
|
|
const char *devname = NULL;
|
2014-09-04 07:05:16 -05:00
|
|
|
|
2014-09-04 08:50:36 -05:00
|
|
|
if (argc)
|
|
|
|
devname = argv[0];
|
2014-09-04 07:05:16 -05:00
|
|
|
if (argc > 1)
|
2014-09-04 08:50:36 -05:00
|
|
|
sf->partno = strtou32_or_err(argv[1],
|
2014-09-04 07:05:16 -05:00
|
|
|
_("failed to parse partition number"));
|
2014-09-04 08:50:36 -05:00
|
|
|
if (!devname)
|
2014-09-04 07:05:16 -05:00
|
|
|
errx(EXIT_FAILURE, _("no disk device specified"));
|
|
|
|
|
2014-09-04 08:50:36 -05:00
|
|
|
rc = fdisk_assign_device(sf->cxt, devname, 0);
|
|
|
|
if (rc)
|
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), devname);
|
2014-09-04 07:05:16 -05:00
|
|
|
|
2014-09-04 08:50:36 -05:00
|
|
|
fdisk_deassign_device(sf->cxt, 1);
|
2014-09-04 07:05:16 -05:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
|
|
|
{
|
|
|
|
fputs(USAGE_HEADER, out);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
fprintf(out,
|
2014-09-04 08:54:59 -05:00
|
|
|
_(" %1$s [options] <disk>\n"
|
|
|
|
" %1$s [options] --list <disk> [...]\n"),
|
|
|
|
program_invocation_short_name);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
2014-09-04 08:54:59 -05:00
|
|
|
fputs(_(" -d, --dump dump partition table (suitable for later input)\n"), out);
|
|
|
|
fputs(_(" -l, --list list partitions of each device\n"), out);
|
2014-09-04 08:50:36 -05:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
fprintf(out, USAGE_MAN_TAIL("sfdisk(8)"));
|
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
2006-12-06 17:25:34 -06:00
|
|
|
}
|
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
|
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-09-04 07:05:16 -05:00
|
|
|
struct sfdisk _sf = { .partno = 0 }, *sf = &_sf;
|
2014-09-04 08:50:36 -05:00
|
|
|
int rc = -EINVAL, c;
|
2014-09-02 11:57:27 -05:00
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
2014-09-04 07:05:16 -05:00
|
|
|
{ "list", no_argument, NULL, 'l' },
|
2014-09-04 08:50:36 -05:00
|
|
|
{ "dump", no_argument, NULL, 'd' },
|
2014-09-02 11:57:27 -05:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ NULL, 0, 0, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
|
|
|
|
2014-09-04 08:50:36 -05:00
|
|
|
while((c = getopt_long(argc, argv, "dhlv", longopts, NULL)) != -1) {
|
2014-09-02 11:57:27 -05:00
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
break;
|
2014-09-04 07:05:16 -05:00
|
|
|
case 'l':
|
|
|
|
sf->act = ACT_LIST;
|
|
|
|
break;
|
2014-09-04 08:50:36 -05:00
|
|
|
case 'd':
|
|
|
|
sf->act = ACT_DUMP;
|
|
|
|
break;
|
2014-09-02 11:57:27 -05:00
|
|
|
case 'v':
|
|
|
|
printf(_("%s from %s\n"), program_invocation_short_name,
|
|
|
|
PACKAGE_STRING);
|
|
|
|
return EXIT_SUCCESS;
|
2011-06-29 05:44:30 -05:00
|
|
|
}
|
|
|
|
}
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
sfdisk_init(sf);
|
2006-12-06 17:25:43 -06:00
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
switch (sf->act) {
|
|
|
|
case ACT_LIST:
|
|
|
|
rc = command_list_partitions(sf, argc - optind, argv + optind);
|
|
|
|
break;
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
case ACT_FDISK:
|
|
|
|
rc = command_fdisk(sf, argc - optind, argv + optind);
|
|
|
|
break;
|
2014-09-04 08:50:36 -05:00
|
|
|
|
|
|
|
case ACT_DUMP:
|
|
|
|
rc = command_dump(sf, argc - optind, argv + optind);
|
|
|
|
break;
|
2014-09-04 07:05:16 -05:00
|
|
|
}
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-04 07:05:16 -05:00
|
|
|
sfdisk_deinit(sf);
|
2006-12-06 17:25:34 -06:00
|
|
|
|
2014-09-02 11:57:27 -05:00
|
|
|
DBG(MISC, ul_debug("bye! [rc=%d]", rc));
|
|
|
|
return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2014-03-10 07:36:22 -05:00
|
|
|
}
|
|
|
|
|