2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: src/sbin/gpt/remove.c,v 1.10 2006/10/04 18:20:25 marcel Exp $");
35 #include <sys/types.h>
49 static off_t block
, size
;
50 static unsigned int entry
;
52 const char removemsg1
[] = "remove -a device ...";
53 const char removemsg2
[] = "remove [-b lba] [-i index] [-s lba] "
54 "[-t type] device ...";
63 getprogname(), removemsg1
, getprogname(), removemsg2
);
78 gpt
= map_find(MAP_TYPE_PRI_GPT_HDR
);
80 warnx("%s: error: no primary GPT header; run create or recover",
85 tpg
= map_find(MAP_TYPE_SEC_GPT_HDR
);
87 warnx("%s: error: no secondary GPT header; run recover",
92 tbl
= map_find(MAP_TYPE_PRI_GPT_TBL
);
93 lbt
= map_find(MAP_TYPE_SEC_GPT_TBL
);
94 if (tbl
== NULL
|| lbt
== NULL
) {
95 warnx("%s: error: run recover -- trust me", device_name
);
99 /* Remove all matching entries in the map. */
100 for (m
= map_first(); m
!= NULL
; m
= m
->map_next
) {
101 if (m
->map_type
!= MAP_TYPE_GPT_PART
|| m
->map_index
< 1)
103 if (entry
> 0 && entry
!= m
->map_index
)
105 if (block
> 0 && block
!= m
->map_start
)
107 if (size
> 0 && size
!= m
->map_size
)
110 i
= m
->map_index
- 1;
113 ent
= (void*)((char*)tbl
->map_data
+ i
*
114 le32toh(hdr
->hdr_entsz
));
115 le_uuid_dec(&ent
->ent_type
, &uuid
);
116 if (!uuid_is_nil(&type
, NULL
) &&
117 !uuid_equal(&type
, &uuid
, NULL
))
120 /* Remove the primary entry by clearing the partition type. */
121 uuid_create_nil((uuid_t
*)&ent
->ent_type
, NULL
);
123 hdr
->hdr_crc_table
= htole32(crc32(tbl
->map_data
,
124 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
125 hdr
->hdr_crc_self
= 0;
126 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
132 ent
= (void*)((char*)lbt
->map_data
+ i
*
133 le32toh(hdr
->hdr_entsz
));
135 /* Remove the secundary entry. */
136 uuid_create_nil((uuid_t
*)&ent
->ent_type
, NULL
);
138 hdr
->hdr_crc_table
= htole32(crc32(lbt
->map_data
,
139 le32toh(hdr
->hdr_entries
) * le32toh(hdr
->hdr_entsz
)));
140 hdr
->hdr_crc_self
= 0;
141 hdr
->hdr_crc_self
= htole32(crc32(hdr
, le32toh(hdr
->hdr_size
)));
146 printf("%sp%u removed\n", device_name
, m
->map_index
);
149 printf("partition %d removed from %s\n", m
->map_index
,
156 cmd_remove(int argc
, char *argv
[])
161 /* Get the remove options */
162 while ((ch
= getopt(argc
, argv
, "ab:i:s:t:")) != -1) {
172 block
= strtoll(optarg
, &p
, 10);
173 if (*p
!= 0 || block
< 1)
179 entry
= strtoul(optarg
, &p
, 10);
180 if (*p
!= 0 || entry
< 1)
186 size
= strtoll(optarg
, &p
, 10);
187 if (*p
!= 0 || size
< 1)
191 if (!uuid_is_nil(&type
, NULL
))
193 if (parse_uuid(optarg
, &type
) != 0)
202 (block
> 0 || entry
> 0 || size
> 0 || !uuid_is_nil(&type
, NULL
)))
208 while (optind
< argc
) {
209 fd
= gpt_open(argv
[optind
++]);
211 warn("unable to open device '%s'", device_name
);