]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - libfdisk/src/wipe.c
53099fd44473e995d7f894a19441805024cc5803
11 struct list_head wipes
;
12 uint64_t start
; /* sectors */
13 uint64_t size
; /* sectors */
16 static struct fdisk_wipe
*fdisk_get_wipe_area(
17 struct fdisk_context
*cxt
,
23 if (cxt
== NULL
|| list_empty(&cxt
->wipes
))
26 list_for_each(p
, &cxt
->wipes
) {
27 struct fdisk_wipe
*wp
= list_entry(p
, struct fdisk_wipe
, wipes
);
28 if (wp
->start
== start
&& wp
->size
== size
)
34 void fdisk_free_wipe_areas(struct fdisk_context
*cxt
)
36 while (!list_empty(&cxt
->wipes
)) {
37 struct fdisk_wipe
*wp
= list_entry(cxt
->wipes
.next
,
38 struct fdisk_wipe
, wipes
);
39 DBG(WIPE
, ul_debugobj(wp
, "free [start=%ju, size=%ju]",
40 (uintmax_t) wp
->start
, (uintmax_t) wp
->size
));
46 int fdisk_has_wipe_area(struct fdisk_context
*cxt
,
50 return fdisk_get_wipe_area(cxt
, start
, size
) != NULL
;
53 /* Add/remove new wiping area
55 * Returns: <0 on error, or old area setting (1: enabled, 0: disabled)
57 int fdisk_set_wipe_area(struct fdisk_context
*cxt
,
62 struct fdisk_wipe
*wp
;
64 if (FDISK_IS_UNDEF(start
) || FDISK_IS_UNDEF(size
))
67 wp
= fdisk_get_wipe_area(cxt
, start
, size
);
72 DBG(WIPE
, ul_debugobj(wp
, "disable [start=%ju, size=%ju]",
73 (uintmax_t) start
, (uintmax_t) size
));
78 DBG(WIPE
, ul_debug("not requested"));
84 return 1; /* already enabled */
86 wp
= calloc(1, sizeof(*wp
));
90 DBG(WIPE
, ul_debugobj(wp
, "enable [start=%ju, size=%ju]",
91 (uintmax_t) start
, (uintmax_t) size
));
93 INIT_LIST_HEAD(&wp
->wipes
);
96 list_add_tail(&wp
->wipes
, &cxt
->wipes
);
101 #ifndef HAVE_LIBBLKID
102 int fdisk_do_wipe(struct fdisk_context
*cxt
__attribute__((__unused__
)))
107 int fdisk_do_wipe(struct fdisk_context
*cxt
)
114 assert(cxt
->dev_fd
>= 0);
116 if (list_empty(&cxt
->wipes
))
119 pr
= blkid_new_probe();
123 list_for_each(p
, &cxt
->wipes
) {
124 struct fdisk_wipe
*wp
= list_entry(p
, struct fdisk_wipe
, wipes
);
125 blkid_loff_t start
= (blkid_loff_t
) wp
->start
* cxt
->sector_size
,
126 size
= (blkid_loff_t
) wp
->size
* cxt
->sector_size
;
128 DBG(WIPE
, ul_debugobj(wp
, "initialize libblkid prober [start=%ju, size=%ju]",
129 (uintmax_t) start
, (uintmax_t) size
));
131 rc
= blkid_probe_set_device(pr
, cxt
->dev_fd
, start
, size
);
133 DBG(WIPE
, ul_debugobj(wp
, "blkid_probe_set_device() failed [rc=%d]", rc
));
137 blkid_probe_enable_superblocks(pr
, 1);
138 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_MAGIC
|
139 BLKID_SUBLKS_BADCSUM
);
140 blkid_probe_enable_partitions(pr
, 1);
141 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_MAGIC
|
142 BLKID_PARTS_FORCE_GPT
);
144 while (blkid_do_probe(pr
) == 0) {
145 DBG(WIPE
, ul_debugobj(wp
, " wiping..."));
146 blkid_do_wipe(pr
, FALSE
);
150 blkid_free_probe(pr
);
157 * Please don't call this function if there is already a PT.
159 * Returns: 0 if nothing found, < 0 on error, 1 if found a signature
161 #ifndef HAVE_LIBBLKID
162 int fdisk_check_collisions(struct fdisk_context
*cxt
__attribute__((__unused__
)))
167 int fdisk_check_collisions(struct fdisk_context
*cxt
)
173 assert(cxt
->dev_fd
>= 0);
175 DBG(WIPE
, ul_debugobj(cxt
, "wipe check: initialize libblkid prober"));
177 pr
= blkid_new_probe();
180 rc
= blkid_probe_set_device(pr
, cxt
->dev_fd
, 0, 0);
184 cxt
->pt_collision
= 0;
185 free(cxt
->collision
);
186 cxt
->collision
= NULL
;
188 blkid_probe_enable_superblocks(pr
, 1);
189 blkid_probe_set_superblocks_flags(pr
, BLKID_SUBLKS_TYPE
|
190 BLKID_SUBLKS_BADCSUM
);
191 blkid_probe_enable_partitions(pr
, 1);
192 blkid_probe_set_partitions_flags(pr
, BLKID_PARTS_FORCE_GPT
);
194 /* we care about the first found FS/raid, so don't call blkid_do_probe()
195 * in loop or don't use blkid_do_fullprobe() ... */
196 rc
= blkid_do_probe(pr
);
198 const char *name
= NULL
;
200 if (blkid_probe_lookup_value(pr
, "TYPE", &name
, 0) == 0)
201 cxt
->collision
= strdup(name
);
202 else if (blkid_probe_lookup_value(pr
, "PTTYPE", &name
, 0) == 0) {
203 cxt
->collision
= strdup(name
);
204 cxt
->pt_collision
= 1;
207 if (name
&& !cxt
->collision
)
211 blkid_free_probe(pr
);
212 return rc
< 0 ? rc
: cxt
->collision
? 1 : 0;