forked from Surachai-kent/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfdisk.c
More file actions
2477 lines (2083 loc) · 64.1 KB
/
sfdisk.c
File metadata and controls
2477 lines (2083 loc) · 64.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
* Copyright (C) 2014 Karel Zak <kzak@redhat.com>
*
* 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.
*
* Karel Zak wrote new sfdisk based on libfdisk from util-linux
* in 2014.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <libsmartcols.h>
#ifdef HAVE_LIBREADLINE
# define _FUNCTION_DEF
# include <readline/readline.h>
#endif
#include <libgen.h>
#include <sys/time.h>
#include "c.h"
#include "xalloc.h"
#include "nls.h"
#include "debug.h"
#include "strutils.h"
#include "closestream.h"
#include "colors.h"
#include "blkdev.h"
#include "all-io.h"
#include "rpmatch.h"
#include "optutils.h"
#include "ttyutils.h"
#include "libfdisk.h"
#include "fdisk-list.h"
/*
* sfdisk debug stuff (see fdisk.h and include/debug.h)
*/
static UL_DEBUG_DEFINE_MASK(sfdisk);
UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
#define SFDISKPROG_DEBUG_INIT (1 << 1)
#define SFDISKPROG_DEBUG_PARSE (1 << 2)
#define SFDISKPROG_DEBUG_MISC (1 << 3)
#define SFDISKPROG_DEBUG_ASK (1 << 4)
#define SFDISKPROG_DEBUG_ALL 0xFFFF
#define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
#define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
enum {
ACT_FDISK = 1,
ACT_ACTIVATE,
ACT_CHANGE_ID,
ACT_DUMP,
ACT_LIST,
ACT_LIST_FREE,
ACT_LIST_TYPES,
ACT_REORDER,
ACT_RELOCATE,
ACT_SHOW_SIZE,
ACT_SHOW_GEOM,
ACT_VERIFY,
ACT_PARTTYPE,
ACT_PARTUUID,
ACT_PARTLABEL,
ACT_PARTATTRS,
ACT_DISKID,
ACT_DELETE,
ACT_BACKUP_SECTORS,
};
struct sfdisk {
int act; /* ACT_* */
int partno; /* -N <partno>, default -1 */
int wipemode; /* remove foreign signatures from disk */
int pwipemode; /* remove foreign signatures from partitions */
const char *lockmode; /* as specified by --lock */
const char *label; /* --label <label> */
const char *label_nested; /* --label-nested <label> */
const char *backup_file; /* -O <path> */
const char *move_typescript; /* --movedata <typescript> */
char *prompt;
struct fdisk_context *cxt; /* libfdisk context */
struct fdisk_partition *orig_pa; /* -N <partno> before the change */
unsigned int verify : 1, /* call fdisk_verify_disklabel() */
quiet : 1, /* suppress extra messages */
interactive : 1, /* running on tty */
noreread : 1, /* don't check device is in use */
force : 1, /* do also stupid things */
backup : 1, /* backup sectors before write PT */
container : 1, /* PT contains container (MBR extended) partitions */
unused : 1, /* PT contains unused partition */
append : 1, /* don't create new PT, append partitions only */
json : 1, /* JSON dump */
movedata: 1, /* move data after resize */
movefsync: 1, /* use fsync() after each write() */
notell : 1, /* don't tell kernel aout new PT */
noact : 1; /* do not write to device */
};
#define SFDISK_PROMPT ">>> "
static void sfdiskprog_init_debug(void)
{
__UL_INIT_DEBUG_FROM_ENV(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
}
static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
{
char *p;
size_t sz;
#ifdef HAVE_LIBREADLINE
if (isatty(STDIN_FILENO)) {
p = readline(prompt);
if (!p)
return 1;
xstrncpy(buf, p, bufsz);
free(p);
} else
#endif
{
fputs(prompt, stdout);
fflush(stdout);
if (!fgets(buf, bufsz, stdin))
return 1;
}
for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
if (p > buf)
memmove(buf, p, p - buf); /* remove blank space */
sz = strlen(buf);
if (sz && *(buf + sz - 1) == '\n')
*(buf + sz - 1) = '\0';
DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
return 0;
}
static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
struct fdisk_ask *ask,
void *data)
{
struct sfdisk *sf = (struct sfdisk *) data;
int rc = 0;
assert(ask);
switch(fdisk_ask_get_type(ask)) {
case FDISK_ASKTYPE_INFO:
if (sf->quiet)
break;
fputs(fdisk_ask_print_get_mesg(ask), stdout);
fputc('\n', stdout);
break;
case FDISK_ASKTYPE_WARNX:
fflush(stdout);
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:
fflush(stdout);
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;
case FDISK_ASKTYPE_YESNO:
{
char buf[BUFSIZ] = { '\0' };
fputc('\n', stdout);
do {
int x;
fputs(fdisk_ask_get_query(ask), stdout);
rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf));
if (rc)
break;
x = rpmatch(buf);
if (x == RPMATCH_YES || x == RPMATCH_NO) {
fdisk_ask_yesno_set_result(ask, x);
break;
}
} while(1);
DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
break;
}
default:
break;
}
return rc;
}
static void sfdisk_init(struct sfdisk *sf)
{
fdisk_init_debug(0);
scols_init_debug(0);
sfdiskprog_init_debug();
sf->cxt = fdisk_new_context();
if (!sf->cxt)
err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
fdisk_set_ask(sf->cxt, ask_callback, (void *) sf);
if (sf->wipemode != WIPEMODE_ALWAYS)
fdisk_enable_bootbits_protection(sf->cxt, 1);
if (sf->label_nested) {
struct fdisk_context *x = fdisk_new_nested_context(sf->cxt,
sf->label_nested);
if (!x)
err(EXIT_FAILURE, _("failed to allocate nested libfdisk context"));
/* the original context is available by fdisk_get_parent() */
sf->cxt = x;
}
}
static int sfdisk_deinit(struct sfdisk *sf)
{
struct fdisk_context *parent;
assert(sf);
assert(sf->cxt);
parent = fdisk_get_parent(sf->cxt);
if (parent) {
fdisk_unref_context(sf->cxt);
sf->cxt = parent;
}
fdisk_unref_context(sf->cxt);
free(sf->prompt);
memset(sf, 0, sizeof(*sf));
return 0;
}
static struct fdisk_partition *get_partition(struct fdisk_context *cxt, size_t partno)
{
struct fdisk_table *tb = NULL;
struct fdisk_partition *pa;
if (fdisk_get_partitions(cxt, &tb) != 0)
return NULL;
pa = fdisk_table_get_partition_by_partno(tb, partno);
if (pa)
fdisk_ref_partition(pa);
fdisk_unref_table(tb);
return pa;
}
static void backup_sectors(struct sfdisk *sf,
const char *tpl,
const char *name,
const char *devname,
uint64_t offset, size_t size)
{
char *fname;
int fd, devfd;
devfd = fdisk_get_devfd(sf->cxt);
assert(devfd >= 0);
xasprintf(&fname, "%s0x%08"PRIx64".bak", tpl, offset);
fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0)
goto fail;
if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) {
fdisk_warn(sf->cxt, _("cannot seek %s"), devname);
goto fail;
} else {
unsigned char *buf = xmalloc(size);
if (read_all(devfd, (char *) buf, size) != (ssize_t) size) {
fdisk_warn(sf->cxt, _("cannot read %s"), devname);
free(buf);
goto fail;
}
if (write_all(fd, buf, size) != 0) {
fdisk_warn(sf->cxt, _("cannot write %s"), fname);
free(buf);
goto fail;
}
free(buf);
}
fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"),
name, (uintmax_t) offset, (uintmax_t) size, fname);
close(fd);
free(fname);
return;
fail:
errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname);
}
static char *mk_backup_filename_tpl(const char *filename, const char *devname, const char *suffix)
{
char *tpl = NULL;
char *name, *buf = xstrdup(devname);
name = basename(buf);
if (!filename || strcmp(filename, "@default") == 0) {
const char *home = getenv ("HOME");
if (!home)
errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined"));
xasprintf(&tpl, "%s/sfdisk-%s%s", home, name, suffix);
} else
xasprintf(&tpl, "%s-%s%s", filename, name, suffix);
free(buf);
return tpl;
}
static void backup_partition_table(struct sfdisk *sf, const char *devname)
{
const char *name;
char *tpl;
uint64_t offset = 0;
size_t size = 0;
int i = 0;
assert(sf);
if (!fdisk_has_label(sf->cxt))
return;
tpl = mk_backup_filename_tpl(sf->backup_file, devname, "-");
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, _("Backup files:"));
color_disable();
while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size)
backup_sectors(sf, tpl, name, devname, offset, size);
if (!sf->quiet)
fputc('\n', stdout);
free(tpl);
}
static int assign_device(struct sfdisk *sf, const char *devname, int rdonly)
{
struct fdisk_context *cxt = sf->cxt;
if (fdisk_assign_device(cxt, devname, rdonly) != 0)
err(EXIT_FAILURE, _("cannot open %s"), devname);
if (!fdisk_is_readonly(cxt)) {
if (blkdev_lock(fdisk_get_devfd(cxt), devname, sf->lockmode) != 0) {
fdisk_deassign_device(cxt, 1);
exit(EXIT_FAILURE);
}
if (sf->backup)
backup_partition_table(sf, devname);
}
return 0;
}
static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa)
{
struct fdisk_partition *pa = get_partition(sf->cxt, partno);
char *devname = NULL, *typescript = NULL, *buf = NULL;
FILE *f = NULL;
int ok = 0, fd, backward = 0;
fdisk_sector_t nsectors, from, to, step, i, prev;
size_t io, ss, step_bytes, cc, ioerr = 0;
uintmax_t src, dst, nbytes;
int progress = 0, rc = 0;
struct timeval prev_time;
uint64_t bytes_per_sec = 0;
assert(sf->movedata);
if (!pa)
warnx(_("failed to read new partition from device; ignoring --move-data"));
else if (!fdisk_partition_has_size(pa))
warnx(_("failed to get size of the new partition; ignoring --move-data"));
else if (!fdisk_partition_has_start(pa))
warnx(_("failed to get start of the new partition; ignoring --move-data"));
else if (!fdisk_partition_has_size(orig_pa))
warnx(_("failed to get size of the old partition; ignoring --move-data"));
else if (!fdisk_partition_has_start(orig_pa))
warnx(_("failed to get start of the old partition; ignoring --move-data"));
else if (fdisk_partition_get_start(pa) == fdisk_partition_get_start(orig_pa))
warnx(_("start of the partition has not been moved; ignoring --move-data"));
else if (fdisk_partition_get_size(orig_pa) > fdisk_partition_get_size(pa))
warnx(_("new partition is smaller than original; ignoring --move-data"));
else
ok = 1;
if (!ok)
return -EINVAL;
DBG(MISC, ul_debug("moving data"));
fd = fdisk_get_devfd(sf->cxt);
/* set move direction and overlay */
nsectors = fdisk_partition_get_size(orig_pa);
from = fdisk_partition_get_start(orig_pa);
to = fdisk_partition_get_start(pa);
if ((to >= from && from + nsectors >= to) ||
(from >= to && to + nsectors >= from)) {
/* source and target overlay, check if we need to copy
* backwardly from end of the source */
DBG(MISC, ul_debug("overlay between source and target"));
backward = from < to;
DBG(MISC, ul_debug(" copy order: %s", backward ? "backward" : "forward"));
}
/* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
io = fdisk_get_optimal_iosize(sf->cxt);
ss = fdisk_get_sector_size(sf->cxt);
if (!io)
io = ss;
if (io < 1024 * 1024)
step_bytes = ((1024 * 1024) + io/2) / io * io;
else
step_bytes = io;
step = step_bytes / ss;
nbytes = nsectors * ss;
DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes));
#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
if (!backward)
ignore_result( posix_fadvise(fd, from * ss,
nsectors * ss, POSIX_FADV_SEQUENTIAL) );
#endif
devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1);
if (sf->move_typescript)
typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
if (!sf->quiet) {
fdisk_info(sf->cxt, "%s", "");
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, sf->noact ? _("Data move: (--no-act)") : _("Data move:"));
color_disable();
if (typescript)
fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from, (uintmax_t) to);
printf(_(" sectors: %ju\n"), (uintmax_t) nsectors);
printf(_(" step size: %zu bytes\n"), step_bytes);
putchar('\n');
fflush(stdout);
if (isatty(fileno(stdout)))
progress = 1;
}
if (sf->interactive) {
int yes = 0;
fdisk_ask_yesno(sf->cxt, _("Do you want to move partition data?"), &yes);
if (!yes) {
fdisk_info(sf->cxt, _("Leaving."));
free(devname);
return 0;
}
}
if (typescript) {
f = fopen(typescript, "w");
if (!f) {
rc = -errno;
fdisk_warn(sf->cxt, _("cannot open %s"), typescript);
goto done;
}
/* don't translate */
fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
fprintf(f, "# Disk: %s\n", devname);
fprintf(f, "# Partition: %zu\n", partno + 1);
fprintf(f, "# Operation: move data\n");
fprintf(f, "# Sector size: %zu\n", ss);
fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
(uintmax_t)from, (uintmax_t)from * ss);
fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
(uintmax_t)to, (uintmax_t)to * ss);
fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
(uintmax_t)nsectors, (uintmax_t)nsectors * ss);
fprintf(f, "# Step size (sectors/bytes): %" PRIu64 "/%zu\n", step, step_bytes);
fprintf(f, "# Steps: %ju\n", ((uintmax_t) nsectors / step) + 1);
fprintf(f, "#\n");
fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
}
src = (backward ? from + nsectors : from) * ss;
dst = (backward ? to + nsectors : to) * ss;
buf = xmalloc(step_bytes);
DBG(MISC, ul_debug(" initial: src=%ju dst=%ju", src, dst));
gettimeofday(&prev_time, NULL);
prev = 0;
for (cc = 1, i = 0; i < nsectors && nbytes > 0; i += step, cc++) {
if (nbytes < step_bytes) {
DBG(MISC, ul_debug("aligning step #%05zu from %zu to %ju",
cc, step_bytes, nbytes));
step_bytes = nbytes;
}
nbytes -= step_bytes;
if (backward)
src -= step_bytes, dst -= step_bytes;
DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst));
if (!sf->noact) {
/* read source */
if (lseek(fd, src, SEEK_SET) == (off_t) -1 ||
read_all(fd, buf, step_bytes) != (ssize_t) step_bytes) {
if (f)
fprintf(f, "%05zu: read error %12ju %12ju\n", cc, src, dst);
fdisk_warn(sf->cxt,
_("cannot read at offset: %ju; continue"), src);
ioerr++;
goto next;
}
/* write target */
if (lseek(fd, dst, SEEK_SET) == (off_t) -1 ||
write_all(fd, buf, step_bytes) != 0) {
if (f)
fprintf(f, "%05zu: write error %12ju %12ju\n", cc, src, dst);
fdisk_warn(sf->cxt,
_("cannot write at offset: %ju; continue"), dst);
ioerr++;
goto next;
}
if (sf->movefsync && fsync(fd) != 0)
fdisk_warn(sf->cxt,
_("cannot fsync at offset: %ju; continue"), dst);
}
/* write log */
if (f)
fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
if (progress && i % 10 == 0) {
unsigned int elapsed = 0; /* usec */
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
if (cur_time.tv_sec - prev_time.tv_sec > 1) {
elapsed = ((cur_time.tv_sec - prev_time.tv_sec) * 1000000) +
(cur_time.tv_usec - prev_time.tv_usec);
bytes_per_sec = ((i - prev) * ss) / elapsed; /* per usec */
bytes_per_sec *= 1000000; /* per sec */
prev_time = cur_time;
prev = i;
}
if (bytes_per_sec)
fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
i + 1, nsectors,
100.0 / ((double) nsectors/(i+1)),
(double) (bytes_per_sec / (1024 * 1024)));
else
fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%)."),
i + 1, nsectors,
100.0 / ((double) nsectors/(i+1)));
fflush(stdout);
fputc('\r', stdout);
}
next:
if (!backward)
src += step_bytes, dst += step_bytes;
}
if (progress && nsectors) {
int x = get_terminal_width(80);
for (; x > 0; x--)
fputc(' ', stdout);
fflush(stdout);
fputc('\r', stdout);
if (i > nsectors)
/* see for() above; @i has to be greater than @nsectors
* on success due to i += step */
i = nsectors;
fprintf(stdout, _("Moved %ju from %ju sectors (%.0f%%)."),
i, nsectors,
100.0 / ((double) nsectors/(i+1)));
fputc('\n', stdout);
}
rc = 0;
done:
if (f)
fclose(f);
free(buf);
free(typescript);
if (sf->noact)
fdisk_info(sf->cxt, _("Your data has not been moved (--no-act)."));
if (ioerr) {
fdisk_info(sf->cxt, _("%zu I/O errors detected!"), ioerr);
rc = -EIO;
} else if (rc)
warn(_("%s: failed to move data"), devname);
free(devname);
return rc;
}
static int write_changes(struct sfdisk *sf)
{
int rc = 0;
if (sf->noact)
fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act)."));
else
rc = fdisk_write_disklabel(sf->cxt);
if (rc == 0 && sf->movedata && sf->orig_pa)
rc = move_partition_data(sf, sf->partno, sf->orig_pa);
if (!sf->noact && !rc) {
fdisk_info(sf->cxt, _("\nThe partition table has been altered."));
if (!sf->notell) {
/* Let's wait a little bit. It's possible that our
* system is still busy with a previous re-read
* ioctl (on sfdisk start) or with another task
* related to the write to the device.
*/
xusleep(250000);
fdisk_reread_partition_table(sf->cxt);
}
}
if (!rc)
rc = fdisk_deassign_device(sf->cxt,
sf->noact || sf->notell); /* no-sync */
return rc;
}
/*
* sfdisk --list [<device ..]
*/
static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
{
int fail = 0;
fdisk_enable_listonly(sf->cxt, 1);
if (argc) {
int i;
for (i = 0; i < argc; i++)
if (print_device_pt(sf->cxt, argv[i], 1, sf->verify, i) != 0)
fail++;
} else
print_all_devices_pt(sf->cxt, sf->verify);
return fail;
}
/*
* sfdisk --list-free [<device ..]
*/
static int command_list_freespace(struct sfdisk *sf, int argc, char **argv)
{
int fail = 0;
fdisk_enable_listonly(sf->cxt, 1);
if (argc) {
int i;
for (i = 0; i < argc; i++)
if (print_device_freespace(sf->cxt, argv[i], 1, i) != 0)
fail++;
} else
print_all_devices_freespace(sf->cxt);
return fail;
}
/*
* sfdisk --list-types
*/
static int command_list_types(struct sfdisk *sf)
{
const struct fdisk_parttype *t;
struct fdisk_label *lb;
const char *name;
size_t i = 0;
int codes;
assert(sf);
assert(sf->cxt);
name = sf->label ? sf->label : "dos";
lb = fdisk_get_label(sf->cxt, name);
if (!lb)
errx(EXIT_FAILURE, _("unsupported label '%s'"), name);
codes = fdisk_label_has_code_parttypes(lb);
fputs(_("Id Name\n\n"), stdout);
while ((t = fdisk_label_get_parttype(lb, i++))) {
if (codes)
printf("%2x %s\n", fdisk_parttype_get_code(t),
fdisk_parttype_get_name(t));
else
printf("%s %s\n", fdisk_parttype_get_string(t),
fdisk_parttype_get_name(t));
}
return 0;
}
static int verify_device(struct sfdisk *sf, const char *devname)
{
int rc = 1;
fdisk_enable_listonly(sf->cxt, 1);
assign_device(sf, devname, 1);
color_scheme_enable("header", UL_COLOR_BOLD);
fdisk_info(sf->cxt, "%s:", devname);
color_disable();
if (!fdisk_has_label(sf->cxt))
fdisk_info(sf->cxt, _("unrecognized partition table type"));
else
rc = fdisk_verify_disklabel(sf->cxt);
fdisk_deassign_device(sf->cxt, 1);
return rc;
}
/*
* sfdisk --verify [<device ..]
*/
static int command_verify(struct sfdisk *sf, int argc, char **argv)
{
int nfails = 0, ct = 0;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (i)
fdisk_info(sf->cxt, " ");
if (verify_device(sf, argv[i]) < 0)
nfails++;
}
} else {
FILE *f = NULL;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (ct)
fdisk_info(sf->cxt, " ");
if (verify_device(sf, dev) < 0)
nfails++;
free(dev);
ct++;
}
}
return nfails;
}
static int get_size(const char *dev, int silent, uintmax_t *sz)
{
int fd, rc = 0;
fd = open(dev, O_RDONLY);
if (fd < 0) {
if (!silent)
warn(_("cannot open %s"), dev);
return -errno;
}
if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) {
if (!silent)
warn(_("Cannot get size of %s"), dev);
rc = -errno;
}
close(fd);
return rc;
}
/*
* sfdisk --show-size [<device ..]
*
* (silly, but just for backward compatibility)
*/
static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
int argc, char **argv)
{
uintmax_t sz;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (get_size(argv[i], 0, &sz) == 0)
printf("%ju\n", sz / 2);
}
} else {
FILE *f = NULL;
uintmax_t total = 0;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (get_size(dev, 1, &sz) == 0) {
printf("%s: %9ju\n", dev, sz / 2);
total += sz / 2;
}
free(dev);
}
if (total)
printf(_("total: %ju blocks\n"), total);
}
return 0;
}
static int print_geom(struct sfdisk *sf, const char *devname)
{
fdisk_enable_listonly(sf->cxt, 1);
assign_device(sf, devname, 1);
fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
devname,
(uintmax_t) fdisk_get_geom_cylinders(sf->cxt),
(uintmax_t) fdisk_get_geom_heads(sf->cxt),
(uintmax_t) fdisk_get_geom_sectors(sf->cxt));
fdisk_deassign_device(sf->cxt, 1);
return 0;
}
/*
* sfdisk --show-geometry [<device ..]
*/
static int command_show_geometry(struct sfdisk *sf, int argc, char **argv)
{
int nfails = 0;
if (argc) {
int i;
for (i = 0; i < argc; i++) {
if (print_geom(sf, argv[i]) < 0)
nfails++;
}
} else {
FILE *f = NULL;
char *dev;
while ((dev = next_proc_partition(&f))) {
if (print_geom(sf, dev) < 0)
nfails++;
free(dev);
}
}
return nfails;
}
/*
* sfdisk --activate <device> [<partno> ...]
*/
static int command_activate(struct sfdisk *sf, int argc, char **argv)
{
int rc, nparts, i, listonly;
struct fdisk_partition *pa = NULL;
const char *devname = NULL;
if (argc < 1)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
/* --activate <device> */
listonly = argc == 1;
assign_device(sf, devname, listonly);
if (fdisk_is_label(sf->cxt, GPT)) {
if (fdisk_gpt_is_hybrid(sf->cxt))
errx(EXIT_FAILURE, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
/* Switch from GPT to PMBR */
sf->cxt = fdisk_new_nested_context(sf->cxt, "dos");
if (!sf->cxt)
err(EXIT_FAILURE, _("cannot switch to PMBR"));
fdisk_info(sf->cxt, _("Activation is unsupported for GPT -- entering nested PMBR."));
} else if (!fdisk_is_label(sf->cxt, DOS))
errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only"));
nparts = fdisk_get_npartitions(sf->cxt);
for (i = 0; i < nparts; i++) {
char *data = NULL;
/* note that fdisk_get_partition() reuses the @pa pointer, you
* don't have to (re)allocate it */
if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
continue;
/* sfdisk --activate list bootable partitions */
if (listonly) {
if (!fdisk_partition_is_bootable(pa))
continue;
if (fdisk_partition_to_string(pa, sf->cxt,
FDISK_FIELD_DEVICE, &data) == 0) {
printf("%s\n", data);
free(data);
}
/* deactivate all active partitions */
} else if (fdisk_partition_is_bootable(pa))
fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE);
}
/* sfdisk --activate <partno> [..] */
for (i = 1; i < argc; i++) {
int n;
if (i == 1 && strcmp(argv[1], "-") == 0)
break;
n = strtou32_or_err(argv[i], _("failed to parse partition number"));
rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE);
if (rc)
errx(EXIT_FAILURE,
_("%s: partition %d: failed to toggle bootable flag"),
devname, i + 1);
}
fdisk_unref_partition(pa);
if (listonly)
rc = fdisk_deassign_device(sf->cxt, 1);
else
rc = write_changes(sf);
return rc;
}
/*
* sfdisk --delete <device> [<partno> ...]
*/
static int command_delete(struct sfdisk *sf, int argc, char **argv)
{
size_t i;
const char *devname = NULL;
if (argc < 1)
errx(EXIT_FAILURE, _("no disk device specified"));
devname = argv[0];
assign_device(sf, devname, 0);
/* delete all */
if (argc == 1) {