Merge lp:~yuningdodo/ubuntu/trusty/util-linux/util-linux.backport-wipefs-partition-table-erasing-support into lp:ubuntu/trusty-updates/util-linux

Proposed by Yu Ning
Status: Needs review
Proposed branch: lp:~yuningdodo/ubuntu/trusty/util-linux/util-linux.backport-wipefs-partition-table-erasing-support
Merge into: lp:ubuntu/trusty-updates/util-linux
Diff against target: 710 lines (+338/-140)
11 files modified
debian/changelog (+17/-0)
libblkid/src/blkid.h.in (+3/-0)
libblkid/src/blkid.sym (+8/-0)
libblkid/src/blkidP.h (+2/-0)
libblkid/src/partitions/gpt.c (+9/-0)
libblkid/src/partitions/partitions.c (+12/-4)
libblkid/src/partitions/ultrix.c (+10/-0)
libblkid/src/probe.c (+133/-0)
libblkid/src/superblocks/superblocks.c (+0/-15)
libblkid/src/superblocks/superblocks.h (+0/-2)
misc-utils/wipefs.c (+144/-119)
To merge this branch: bzr merge lp:~yuningdodo/ubuntu/trusty/util-linux/util-linux.backport-wipefs-partition-table-erasing-support
Reviewer Review Type Date Requested Status
Mathieu Trudel-Lapierre Needs Information
Review via email: mp+237898@code.launchpad.net

Description of the change

  * wipefs: backport partition table erasing support from
    git://git.debian.org/git/collab-maint/pkg-util-linux.git
    - 6611a3dd783006fd8c6b924a76c4820287278d36
      wipefs: improve -a, use blkid_do_wipe()
    - 2b89be6c802bdbdf6830dbd060c96e33f179b135
      libblkid: add blkid_do_wipe()
    - 476b508e043b6b4c71d84b4f2fdeeffa68c204b3
      libblkid: add BLKID_PARTS_MAGIC to blkid_do_wipe() docs
    - 3c83b3b22f33fc7b17b504158e4a4db7b567bbe8
      libblkid: add BLKID_PARTS_MAGIC
    - 44765fdd841fb1369cf68f360131ed076f3a2771
      libblkid: export info about PT magic strings

To post a comment you must log in.
Revision history for this message
Timo Jyrinki (timo-jyrinki) wrote :

I think this was workarounded in usb-creator instead, am I wrong? With the reasoning that this new feature backport would be a little too heavy in a core part like util-linux.

The latest usb-creator in 14.04 LTS is this https://launchpad.net/ubuntu/+source/usb-creator/0.2.56.3

So I'd mark this as rejected, please re-open if you think this should still be targeted towards trusty.

Revision history for this message
Timo Jyrinki (timo-jyrinki) wrote :

To be noted though for any core dev considering this, this version of the backport is much less invasive than the previous one https://code.launchpad.net/~yuningdodo/ubuntu/trusty/util-linux/util-linux.backport-utopic-2.25-8ubuntu1/+merge/232848

Revision history for this message
Timo Jyrinki (timo-jyrinki) wrote :

Hmm, reopening. As noted on the bug, the disk utility program would still need this.

Revision history for this message
Mathieu Trudel-Lapierre (cyphermox) wrote :

How does this affect https://launchpad.net/bugs/1046665? Have you also tested that this bug isn't regressed by these cherry-picks? What packages might be broken by the changes to libblkid?

It also seems to me like some of these commits probably aren't strictly required to support the changes to wipefs, since that's what is at stake here. Do you have a deep enough understanding of the changes here to know exactly why each of these commits are required? It looks to me like only a subset of these changes might be required to fix the targetted bug, but I only looked really quickly.

For example, it seems to me like the fix should work without the last three commits, and just the addition of blkid_do_wipe in libblkid/src/probe.c from 2b89be6c802bdbdf6830dbd060c96e33f179b135.

review: Needs Information
Revision history for this message
Yu Ning (yuningdodo) wrote :

Thanks for the review.

I checked the ubuntu branch, lp:ubuntu/util-linux, bug #1046665 was firstly fixed in rev90 by retrying the operation several times. Later in rev110 the code was entirely merged with debian experimental, and the previous retry was now implemented by calling blkid_do_probe() multiple times until it returns false. What I proposed to do for the trusty branch is similar, first retire the previous retrying logic then introduce the new implementation, so I believe it will not regress bug #1046665. About libblkid, here is a summary of what have been changed:

* new flag: BLKID_PARTS_MAGIC.
* new public method blkid_probe_wipe().
* declaration of blkid_probe_set_magic() is moved to libblkid/src/blkid.h from libblkid/src/superblocks/superblocks.h, so it will become a public method.

Besides these API changes, there are also some internal changes. I think they won't break the API backward compatibility of libblkid.

About the picked patches, I can't say I have a deep enough understanding about the patches, actually I picked them based on the test results of this simple testcase:

dd if=/dev/zero of=data bs=1M count=4
parted data --script -- mklabel msdos
parted data --script -- mkpart primary fat16 0 -0
wipefs -a data # it's expected to output "2 bytes were erased at offset 0x000001fe (dos): 55 aa"

With the first two patches it's not enough to erase the "55aa" signature. In fact the key patch is the last one, 44765fdd841fb1369cf68f360131ed076f3a2771, the other 4 patches are just its dependences. If we want to only pick the key patch, we may have to modify it a lot and causes future conflicts when other patches are being backported.

Revision history for this message
Amr Ibrahim (amribrahim1987) wrote :

Any updates on this?

Unmerged revisions

114. By Yu Ning

44765fdd841fb1369cf68f360131ed076f3a2771 libblkid: export info about PT magic strings

113. By Yu Ning

3c83b3b22f33fc7b17b504158e4a4db7b567bbe8 libblkid: add BLKID_PARTS_MAGIC

112. By Yu Ning

476b508e043b6b4c71d84b4f2fdeeffa68c204b3 libblkid: add BLKID_PARTS_MAGIC to blkid_do_wipe() docs

111. By Yu Ning

2b89be6c802bdbdf6830dbd060c96e33f179b135 libblkid: add blkid_do_wipe()

110. By Yu Ning

* wipefs: backport partition table erasing support from
  git://git.debian.org/git/collab-maint/pkg-util-linux.git
  - 6611a3dd783006fd8c6b924a76c4820287278d36
    wipefs: improve -a, use blkid_do_wipe()

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'debian/changelog'
--- debian/changelog 2014-06-02 13:36:20 +0000
+++ debian/changelog 2014-10-10 06:30:46 +0000
@@ -1,3 +1,20 @@
1util-linux (2.20.1-5.1ubuntu20.1~test1) UNRELEASED; urgency=medium
2
3 * wipefs: backport partition table erasing support from
4 git://git.debian.org/git/collab-maint/pkg-util-linux.git
5 - 6611a3dd783006fd8c6b924a76c4820287278d36
6 wipefs: improve -a, use blkid_do_wipe()
7 - 2b89be6c802bdbdf6830dbd060c96e33f179b135
8 libblkid: add blkid_do_wipe()
9 - 476b508e043b6b4c71d84b4f2fdeeffa68c204b3
10 libblkid: add BLKID_PARTS_MAGIC to blkid_do_wipe() docs
11 - 3c83b3b22f33fc7b17b504158e4a4db7b567bbe8
12 libblkid: add BLKID_PARTS_MAGIC
13 - 44765fdd841fb1369cf68f360131ed076f3a2771
14 libblkid: export info about PT magic strings
15
16 -- Yu Ning <ning.yu@canonical.com> Fri, 10 Oct 2014 14:16:52 +0800
17
1util-linux (2.20.1-5.1ubuntu20.1) trusty-proposed; urgency=medium18util-linux (2.20.1-5.1ubuntu20.1) trusty-proposed; urgency=medium
219
3 * fstrim-all: Fix typo that made unwanted stderr output from hdparm appear.20 * fstrim-all: Fix typo that made unwanted stderr output from hdparm appear.
421
=== modified file 'libblkid/src/blkid.h.in'
--- libblkid/src/blkid.h.in 2011-11-03 15:38:23 +0000
+++ libblkid/src/blkid.h.in 2014-10-10 06:30:46 +0000
@@ -278,6 +278,7 @@
278/* partitions probing flags */278/* partitions probing flags */
279#define BLKID_PARTS_FORCE_GPT (1 << 1)279#define BLKID_PARTS_FORCE_GPT (1 << 1)
280#define BLKID_PARTS_ENTRY_DETAILS (1 << 2)280#define BLKID_PARTS_ENTRY_DETAILS (1 << 2)
281#define BLKID_PARTS_MAGIC (1 << 3)
281extern int blkid_probe_set_partitions_flags(blkid_probe pr, int flags);282extern int blkid_probe_set_partitions_flags(blkid_probe pr, int flags);
282283
283/* binary interface */284/* binary interface */
@@ -319,6 +320,8 @@
319 const char **data, size_t *len);320 const char **data, size_t *len);
320extern int blkid_probe_has_value(blkid_probe pr, const char *name);321extern int blkid_probe_has_value(blkid_probe pr, const char *name);
321322
323extern int blkid_do_wipe(blkid_probe pr, int dryrun);
324
322/*325/*
323 * Deprecated functions/macros326 * Deprecated functions/macros
324 */327 */
325328
=== modified file 'libblkid/src/blkid.sym'
--- libblkid/src/blkid.sym 2011-11-03 15:38:23 +0000
+++ libblkid/src/blkid.sym 2014-10-10 06:30:46 +0000
@@ -140,3 +140,11 @@
140 blkid_superblocks_get_name;140 blkid_superblocks_get_name;
141} BLKID_2.18;141} BLKID_2.18;
142142
143/*
144 * symbols since util-linux 2.21
145 */
146BLKID_2.21 {
147global:
148 blkid_do_wipe;
149} BLKID_2.20;
150
143151
=== modified file 'libblkid/src/blkidP.h'
--- libblkid/src/blkidP.h 2011-11-03 15:38:23 +0000
+++ libblkid/src/blkidP.h 2014-10-10 06:30:46 +0000
@@ -426,6 +426,8 @@
426 const char *fmt, va_list ap);426 const char *fmt, va_list ap);
427extern int blkid_probe_sprintf_value(blkid_probe pr, const char *name,427extern int blkid_probe_sprintf_value(blkid_probe pr, const char *name,
428 const char *fmt, ...);428 const char *fmt, ...);
429extern int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
430 size_t len, unsigned char *magic);
429431
430extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len);432extern void blkid_unparse_uuid(const unsigned char *uuid, char *str, size_t len);
431extern size_t blkid_rtrim_whitespace(unsigned char *str);433extern size_t blkid_rtrim_whitespace(unsigned char *str);
432434
=== modified file 'libblkid/src/partitions/gpt.c'
--- libblkid/src/partitions/gpt.c 2011-11-03 15:38:23 +0000
+++ libblkid/src/partitions/gpt.c 2014-10-10 06:30:46 +0000
@@ -26,6 +26,7 @@
2626
27/* Signature - “EFI PART” */27/* Signature - “EFI PART” */
28#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL28#define GPT_HEADER_SIGNATURE 0x5452415020494645ULL
29#define GPT_HEADER_SIGNATURE_STR "EFI PART"
2930
30/* basic types */31/* basic types */
31typedef uint16_t efi_char16_t;32typedef uint16_t efi_char16_t;
@@ -326,6 +327,14 @@
326 if (!tab)327 if (!tab)
327 goto err;328 goto err;
328329
330 blkid_probe_set_magic(pr, lba << 9,
331 sizeof(GPT_HEADER_SIGNATURE_STR) - 1,
332 (unsigned char *) GPT_HEADER_SIGNATURE_STR);
333
334 blkid_probe_set_magic(pr, lba << 9,
335 sizeof(GPT_HEADER_SIGNATURE_STR) - 1,
336 (unsigned char *) GPT_HEADER_SIGNATURE_STR);
337
329 ssf = blkid_probe_get_sectorsize(pr) / 512;338 ssf = blkid_probe_get_sectorsize(pr) / 512;
330339
331 fu = le64_to_cpu(h->first_usable_lba);340 fu = le64_to_cpu(h->first_usable_lba);
332341
=== modified file 'libblkid/src/partitions/partitions.c'
--- libblkid/src/partitions/partitions.c 2011-11-03 15:38:23 +0000
+++ libblkid/src/partitions/partitions.c 2014-10-10 06:30:46 +0000
@@ -530,15 +530,17 @@
530 return 1;530 return 1;
531}531}
532532
533static int idinfo_probe(blkid_probe pr, const struct blkid_idinfo *id)533static int idinfo_probe(blkid_probe pr, const struct blkid_idinfo *id,
534 struct blkid_chain *chn)
534{535{
535 const struct blkid_idmag *mag;536 const struct blkid_idmag *mag;
537 blkid_loff_t off;
536 int rc = 1; /* = nothing detected */538 int rc = 1; /* = nothing detected */
537539
538 if (pr->size <= 0 || (id->minsz && id->minsz > pr->size))540 if (pr->size <= 0 || (id->minsz && id->minsz > pr->size))
539 goto nothing; /* the device is too small */541 goto nothing; /* the device is too small */
540542
541 if (blkid_probe_get_idmag(pr, id, NULL, &mag))543 if (blkid_probe_get_idmag(pr, id, &off, &mag))
542 goto nothing;544 goto nothing;
543545
544 /* final check by probing function */546 /* final check by probing function */
@@ -549,9 +551,15 @@
549 if (rc == -1) {551 if (rc == -1) {
550 /* reset after error */552 /* reset after error */
551 reset_partlist(blkid_probe_get_partlist(pr));553 reset_partlist(blkid_probe_get_partlist(pr));
554 if (chn && !chn->binary)
555 blkid_probe_chain_reset_vals(pr, chn);
552 DBG(DEBUG_LOWPROBE, printf(556 DBG(DEBUG_LOWPROBE, printf(
553 "%s probefunc failed\n", id->name));557 "%s probefunc failed\n", id->name));
554 }558 }
559 if (rc == 0 && mag && chn && !chn->binary)
560 blkid_probe_set_magic(pr, off, mag->len,
561 (unsigned char *) mag->magic);
562
555 DBG(DEBUG_LOWPROBE, printf(563 DBG(DEBUG_LOWPROBE, printf(
556 "%s: <--- (rc = %d)\n", id->name, rc));564 "%s: <--- (rc = %d)\n", id->name, rc));
557 }565 }
@@ -594,7 +602,7 @@
594 continue;602 continue;
595603
596 /* apply checks from idinfo */604 /* apply checks from idinfo */
597 if (idinfo_probe(pr, idinfos[i]) != 0)605 if (idinfo_probe(pr, idinfos[i], chn) != 0)
598 continue;606 continue;
599607
600 name = idinfos[i]->name;608 name = idinfos[i]->name;
@@ -678,7 +686,7 @@
678686
679 blkid_probe_set_partlist(prc, ls);687 blkid_probe_set_partlist(prc, ls);
680688
681 rc = idinfo_probe(prc, id);689 rc = idinfo_probe(prc, id, blkid_probe_get_chain(pr));
682690
683 blkid_probe_set_partlist(prc, NULL);691 blkid_probe_set_partlist(prc, NULL);
684 blkid_partlist_set_parent(ls, NULL);692 blkid_partlist_set_parent(ls, NULL);
685693
=== modified file 'libblkid/src/partitions/ultrix.c'
--- libblkid/src/partitions/ultrix.c 2011-11-03 15:38:23 +0000
+++ libblkid/src/partitions/ultrix.c 2014-10-10 06:30:46 +0000
@@ -15,7 +15,9 @@
15#include "partitions.h"15#include "partitions.h"
1616
17#define ULTRIX_MAXPARTITIONS 817#define ULTRIX_MAXPARTITIONS 8
18
18#define ULTRIX_MAGIC 0x03295719#define ULTRIX_MAGIC 0x032957
20#define ULTRIX_MAGIC_STR "\x02\x29\x57"
1921
20/* sector with partition table */22/* sector with partition table */
21#define ULTRIX_SECTOR ((16384 - sizeof(struct ultrix_disklabel)) >> 9)23#define ULTRIX_SECTOR ((16384 - sizeof(struct ultrix_disklabel)) >> 9)
@@ -62,6 +64,14 @@
62 if (!tab)64 if (!tab)
63 goto err;65 goto err;
6466
67 blkid_probe_set_magic(pr, (ULTRIX_SECTOR << 9) + ULTRIX_OFFSET,
68 sizeof(ULTRIX_MAGIC_STR) - 1,
69 (unsigned char *) ULTRIX_MAGIC_STR);
70
71 blkid_probe_set_magic(pr, (ULTRIX_SECTOR << 9) + ULTRIX_OFFSET,
72 sizeof(ULTRIX_MAGIC_STR) - 1,
73 (unsigned char *) ULTRIX_MAGIC_STR);
74
65 for (i = 0; i < ULTRIX_MAXPARTITIONS; i++) {75 for (i = 0; i < ULTRIX_MAXPARTITIONS; i++) {
66 if (!l->pt_part[i].pi_nblocks)76 if (!l->pt_part[i].pi_nblocks)
67 blkid_partlist_increment_partno(ls);77 blkid_partlist_increment_partno(ls);
6878
=== modified file 'libblkid/src/probe.c'
--- libblkid/src/probe.c 2011-11-03 15:38:23 +0000
+++ libblkid/src/probe.c 2014-10-10 06:30:46 +0000
@@ -109,6 +109,7 @@
109#endif109#endif
110110
111#include "blkidP.h"111#include "blkidP.h"
112#include "writeall.h"
112113
113/* chains */114/* chains */
114extern const struct blkid_chaindrv superblocks_drv;115extern const struct blkid_chaindrv superblocks_drv;
@@ -914,6 +915,105 @@
914}915}
915916
916/**917/**
918 * blkid_do_wipe:
919 * @pr: prober
920 * @dryrun: if TRUE then don't touch the device.
921 *
922 * This function erases the current signature detected by @pr. The @pr has to
923 * be open in O_RDWR mode, BLKID_SUBLKS_MAGIC or/and BLKID_PARTS_MAGIC flags
924 * has to be enabled.
925 *
926 * After successful signature removing the @pr prober will be moved one step
927 * back and the next blkid_do_probe() call will again call previously called
928 * probing function.
929 *
930 * <example>
931 * <title>wipe all filesystems or raids from the device</title>
932 * <programlisting>
933 * fd = open(devname, O_RDWR);
934 * blkid_probe_set_device(pr, fd, 0, 0);
935 *
936 * blkid_probe_enable_superblocks(pr, 1);
937 * blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC);
938 *
939 * while (blkid_do_probe(pr) == 0)
940 * blkid_do_wipe(pr, FALSE);
941 * </programlisting>
942 * </example>
943 *
944 * Returns: 0 on success, 1 when probing is done and -1 in case of error.
945 */
946int blkid_do_wipe(blkid_probe pr, int dryrun)
947{
948 const char *off = NULL;
949 size_t len = 0;
950 loff_t offset, l;
951 char buf[BUFSIZ];
952 int fd, rc;
953 struct blkid_chain *chn;
954
955 if (!pr)
956 return -1;
957
958 chn = pr->cur_chain;
959 if (!chn)
960 return -1;
961
962 switch (chn->driver->id) {
963 case BLKID_CHAIN_SUBLKS:
964 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
965 if (!rc)
966 rc = blkid_probe_lookup_value(pr, "SBMAGIC", NULL, &len);
967 break;
968 case BLKID_CHAIN_PARTS:
969 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
970 if (!rc)
971 rc = blkid_probe_lookup_value(pr, "PTMAGIC", NULL, &len);
972 break;
973 default:
974 return 0;
975 }
976
977 if (rc || len == 0 || off == NULL)
978 return 0;
979
980 offset = strtoll(off, NULL, 10);
981 fd = blkid_probe_get_fd(pr);
982 if (fd < 0)
983 return -1;
984
985 if (len > sizeof(buf))
986 len = sizeof(buf);
987
988 DBG(DEBUG_LOWPROBE, printf(
989 "wiping [offset=0x%jx, len=%zd, chain=%s, idx=%d, dryrun=%s]\n",
990 offset, len, chn->driver->name, chn->idx, dryrun ? "yes" : "not"));
991
992 l = lseek(fd, offset, SEEK_SET);
993 if (l == (off_t) -1)
994 return -1;
995
996 memset(buf, 0, len);
997
998 if (!dryrun && len) {
999 if (write_all(fd, buf, len))
1000 return -1;
1001 fsync(fd);
1002
1003 blkid_probe_reset_buffer(pr);
1004
1005 if (chn->idx >= 0) {
1006 chn->idx--;
1007 DBG(DEBUG_LOWPROBE,
1008 printf("wipe: moving %s chain index to %d\n",
1009 chn->driver->name,
1010 chn->idx));
1011 }
1012 }
1013 return 0;
1014}
1015
1016/**
917 * blkid_do_safeprobe:1017 * blkid_do_safeprobe:
918 * @pr: prober1018 * @pr: prober
919 *1019 *
@@ -1124,6 +1224,39 @@
1124 return rc;1224 return rc;
1125}1225}
11261226
1227int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
1228 size_t len, unsigned char *magic)
1229{
1230 int rc = 0;
1231 struct blkid_chain *chn = blkid_probe_get_chain(pr);
1232
1233 if (!chn || !magic || !len || chn->binary)
1234 return 0;
1235
1236 switch (chn->driver->id) {
1237 case BLKID_CHAIN_SUBLKS:
1238 if (!(chn->flags & BLKID_SUBLKS_MAGIC))
1239 return 0;
1240 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
1241 if (!rc)
1242 rc = blkid_probe_sprintf_value(pr,
1243 "SBMAGIC_OFFSET", "%llu", offset);
1244 break;
1245 case BLKID_CHAIN_PARTS:
1246 if (!(chn->flags & BLKID_PARTS_MAGIC))
1247 return 0;
1248 rc = blkid_probe_set_value(pr, "PTMAGIC", magic, len);
1249 if (!rc)
1250 rc = blkid_probe_sprintf_value(pr,
1251 "PTMAGIC_OFFSET", "%llu", offset);
1252 break;
1253 default:
1254 break;
1255 }
1256
1257 return rc;
1258}
1259
1127/**1260/**
1128 * blkid_probe_get_devno:1261 * blkid_probe_get_devno:
1129 * @pr: probe1262 * @pr: probe
11301263
=== modified file 'libblkid/src/superblocks/superblocks.c'
--- libblkid/src/superblocks/superblocks.c 2011-11-03 15:38:23 +0000
+++ libblkid/src/superblocks/superblocks.c 2014-10-10 06:30:46 +0000
@@ -478,21 +478,6 @@
478 return 0;478 return 0;
479}479}
480480
481int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
482 size_t len, unsigned char *magic)
483{
484 int rc = 0;
485 struct blkid_chain *chn = blkid_probe_get_chain(pr);
486
487 if (magic && len && (chn->flags & BLKID_SUBLKS_MAGIC)) {
488 rc = blkid_probe_set_value(pr, "SBMAGIC", magic, len);
489 if (!rc)
490 rc = blkid_probe_sprintf_value(pr, "SBMAGIC_OFFSET",
491 "%llu", offset);
492 }
493 return rc;
494}
495
496int blkid_probe_set_version(blkid_probe pr, const char *version)481int blkid_probe_set_version(blkid_probe pr, const char *version)
497{482{
498 struct blkid_chain *chn = blkid_probe_get_chain(pr);483 struct blkid_chain *chn = blkid_probe_get_chain(pr);
499484
=== modified file 'libblkid/src/superblocks/superblocks.h'
--- libblkid/src/superblocks/superblocks.h 2011-11-03 15:38:23 +0000
+++ libblkid/src/superblocks/superblocks.h 2014-10-10 06:30:46 +0000
@@ -71,8 +71,6 @@
71/*71/*
72 * superblock functions72 * superblock functions
73 */73 */
74extern int blkid_probe_set_magic(blkid_probe pr, blkid_loff_t offset,
75 size_t len, unsigned char *magic);
76extern int blkid_probe_set_version(blkid_probe pr, const char *version);74extern int blkid_probe_set_version(blkid_probe pr, const char *version);
77extern int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...)75extern int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...)
78 __attribute__ ((format (printf, 2, 3)));76 __attribute__ ((format (printf, 2, 3)));
7977
=== modified file 'misc-utils/wipefs.c'
--- misc-utils/wipefs.c 2012-09-06 05:47:03 +0000
+++ misc-utils/wipefs.c 2014-10-10 06:30:46 +0000
@@ -49,6 +49,8 @@
49 char *label; /* FS label */49 char *label; /* FS label */
50 char *uuid; /* FS uuid */50 char *uuid; /* FS uuid */
5151
52 int on_disk;
53
52 struct wipe_desc *next;54 struct wipe_desc *next;
53};55};
5456
@@ -138,73 +140,103 @@
138}140}
139141
140static struct wipe_desc *142static struct wipe_desc *
141get_offset_from_probe(struct wipe_desc *wp, blkid_probe pr, int zap)143get_desc_for_probe(struct wipe_desc *wp, blkid_probe pr)
142{144{
143 const char *off, *type, *usage, *mag;145 const char *off, *type, *mag, *p, *usage = NULL;
144 size_t len;146 size_t len;
145147 loff_t offset;
146 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0 &&148 int rc;
147 blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL) == 0 &&149
148 blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len) == 0 &&150 /* superblocks */
149 blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0) {151 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0) {
150152 rc = blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL);
151 loff_t offset = strtoll(off, NULL, 10);153 if (!rc)
152 const char *p;154 rc = blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len);
153155 if (rc)
154 wp = add_offset(wp, offset, zap);156 return wp;
155 if (!wp)157
156 return NULL;158 /* partitions */
157159 } else if (blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL) == 0) {
160 rc = blkid_probe_lookup_value(pr, "PTMAGIC_OFFSET", &off, NULL);
161 if (!rc)
162 rc = blkid_probe_lookup_value(pr, "PTMAGIC", &mag, &len);
163 if (rc)
164 return wp;
165 usage = "partition table";
166 } else
167 return wp;
168
169 offset = strtoll(off, NULL, 10);
170
171 wp = add_offset(wp, offset, 0);
172 if (!wp)
173 return NULL;
174
175 if (usage || blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0)
158 wp->usage = xstrdup(usage);176 wp->usage = xstrdup(usage);
159 wp->type = xstrdup(type);177
160178 wp->type = xstrdup(type);
161 wp->magic = xmalloc(len);179 wp->on_disk = 1;
162 memcpy(wp->magic, mag, len);180
163 wp->len = len;181 wp->magic = xmalloc(len);
164182 memcpy(wp->magic, mag, len);
165 if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0)183 wp->len = len;
166 wp->label = xstrdup(p);184
167185 if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0)
168 if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0)186 wp->label = xstrdup(p);
169 wp->uuid = xstrdup(p);187
170 }188 if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0)
189 wp->uuid = xstrdup(p);
171190
172 return wp;191 return wp;
173}192}
174193
175static struct wipe_desc *194static blkid_probe
176read_offsets(struct wipe_desc *wp, const char *fname, int zap)195new_probe(const char *devname, int mode)
177{196{
178 blkid_probe pr;197 blkid_probe pr;
179 int rc;
180198
181 if (!fname)199 if (!devname)
182 return NULL;200 return NULL;
183201
184 pr = blkid_new_probe_from_filename(fname);202 if (mode) {
203 int fd = open(devname, mode);
204 if (fd < 0)
205 goto error;
206
207 pr = blkid_new_probe();
208 if (pr && blkid_probe_set_device(pr, fd, 0, 0))
209 goto error;
210 } else
211 pr = blkid_new_probe_from_filename(devname);
212
185 if (!pr)213 if (!pr)
186 errx(EXIT_FAILURE, _("error: %s: probing initialization failed"), fname);214 goto error;
187
188 blkid_probe_enable_superblocks(pr, 0); /* enabled by default ;-( */
189
190 blkid_probe_enable_partitions(pr, 1);
191 rc = blkid_do_fullprobe(pr);
192 blkid_probe_enable_partitions(pr, 0);
193
194 if (rc == 0) {
195 const char *type = NULL;
196 blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL);
197 warnx(_("WARNING: %s: appears to contain '%s' "
198 "partition table"), fname, type);
199 }
200215
201 blkid_probe_enable_superblocks(pr, 1);216 blkid_probe_enable_superblocks(pr, 1);
202 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC |217 blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC |
203 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE |218 BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE |
204 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);219 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);
205220
221 blkid_probe_enable_partitions(pr, 1);
222 blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
223
224 return pr;
225error:
226 err(EXIT_FAILURE, _("error: %s: probing initialization failed"), devname);
227 return NULL;
228}
229
230static struct wipe_desc *
231read_offsets(struct wipe_desc *wp, const char *devname)
232{
233 blkid_probe pr = new_probe(devname, 0);
234
235 if (!pr)
236 return NULL;
237
206 while (blkid_do_probe(pr) == 0) {238 while (blkid_do_probe(pr) == 0) {
207 wp = get_offset_from_probe(wp, pr, zap);239 wp = get_desc_for_probe(wp, pr);
208 if (!wp)240 if (!wp)
209 break;241 break;
210 }242 }
@@ -213,60 +245,54 @@
213 return wp;245 return wp;
214}246}
215247
216static int248static struct wipe_desc *
217do_wipe_offset(int fd, struct wipe_desc *wp, const char *fname, int noact)249do_wipe(struct wipe_desc *wp, const char *devname, int noact, int all)
218{250{
219 char buf[BUFSIZ];251 blkid_probe pr = new_probe(devname, O_RDWR);
220 off_t l;252 struct wipe_desc *w;
221 size_t i, len;253
222254 if (!pr)
223 if (!wp->type) {255 return NULL;
224 warnx(_("no magic string found at offset "256
225 "0x%jx -- ignored"), wp->offset);257 while (blkid_do_probe(pr) == 0) {
226 return 0;258 w = get_desc_for_probe(wp, pr);
227 }259 if (!w)
228260 break;
229 l = lseek(fd, wp->offset, SEEK_SET);261 wp = w;
230 if (l == (off_t) -1)262 if (!wp->on_disk)
231 err(EXIT_FAILURE, _("%s: failed to seek to offset 0x%jx"),263 continue;
232 fname, wp->offset);264 wp->zap = all ? 1 : wp->zap;
233265 if (!wp->zap)
234 len = wp->len > sizeof(buf) ? sizeof(buf) : wp->len;266 continue;
235267
236 memset(buf, 0, len);268 if (blkid_do_wipe(pr, noact))
237 if (noact == 0 && write_all(fd, buf, len))269 warn(_("failed to erase %s magic string at offset 0x%08jx"),
238 err(EXIT_FAILURE, _("%s: write failed"), fname);270 wp->type, wp->offset);
239271 else {
240 printf(_("%zd bytes were erased at offset 0x%jx (%s)\nthey were: "),272 size_t i;
241 wp->len, wp->offset, wp->type);273
242274 printf(_("%zd bytes were erased at offset 0x%08jx (%s): "),
243 for (i = 0; i < len; i++) {275 wp->len, wp->offset, wp->type);
244 printf("%02x", wp->magic[i]);276
245 if (i + 1 < len)277 for (i = 0; i < wp->len; i++) {
246 fputc(' ', stdout);278 printf("%02x", wp->magic[i]);
247 }279 if (i + 1 < wp->len)
248280 fputc(' ', stdout);
249 printf("\n");281 }
250 return 0;282 putchar('\n');
251}283 }
252284 }
253static int285
254do_wipe(struct wipe_desc *wp, const char *fname, int noact)286 for (w = wp; w != NULL; w = w->next) {
255{287 if (!w->on_disk)
256 int fd;288 warnx(_("offset 0x%jx not found"), w->offset);
257289 }
258 fd = open(fname, O_WRONLY);290
259 if (fd < 0)291 fsync(blkid_probe_get_fd(pr));
260 err(EXIT_FAILURE, _("%s: open failed"), fname);292 close(blkid_probe_get_fd(pr));
261293 blkid_free_probe(pr);
262 while (wp) {294
263 if (wp->zap)295 return wp;
264 do_wipe_offset(fd, wp, fname, noact);
265 wp = wp->next;
266 }
267
268 close(fd);
269 return 0;
270}296}
271297
272static void298static void
@@ -323,7 +349,7 @@
323{349{
324 struct wipe_desc *wp = NULL;350 struct wipe_desc *wp = NULL;
325 int c, all = 0, has_offset = 0, noact = 0, mode = 0;351 int c, all = 0, has_offset = 0, noact = 0, mode = 0;
326 const char *fname;352 const char *devname;
327353
328 static const struct option longopts[] = {354 static const struct option longopts[] = {
329 { "all", 0, 0, 'a' },355 { "all", 0, 0, 'a' },
@@ -372,26 +398,25 @@
372 if (optind == argc)398 if (optind == argc)
373 usage(stderr);399 usage(stderr);
374400
375 fname = argv[optind++];401 devname = argv[optind++];
376402
377 if (optind != argc)403 if (optind != argc)
378 errx(EXIT_FAILURE, _("only one device as argument is currently supported."));404 errx(EXIT_FAILURE, _("only one device as argument is currently supported."));
379405
380 /* we need to wipe several times for some file systems like VFAT, see406 if (!all && !has_offset) {
381 * https://launchpad.net/bugs/1046665 */407 /*
382 do {408 * Print only
383 wp = read_offsets(wp, fname, all);409 */
384410 wp = read_offsets(wp, devname);
385 if (wp) {411 if (wp)
386 if (has_offset || all)412 print_all(wp, mode);
387 do_wipe(wp, fname, noact);413 } else {
388 else414 /*
389 print_all(wp, mode);415 * Erase
390416 */
391 free_wipe(wp);417 wp = do_wipe(wp, devname, noact, all);
392 wp = NULL;418 }
393 } else419
394 break;420 free_wipe(wp);
395 } while (!noact && all);
396 return EXIT_SUCCESS;421 return EXIT_SUCCESS;
397}422}

Subscribers

People subscribed via source and target branches

to all changes: