dissect: drop partition removal code

This reverts a major chunk of 75d7e04eb4

Now that the loopback device code already destroys the partitions we
don't have to do this here anymore.

I am sure the right place to delete the partitions is in the loopback
code, since we really only should do that for loopback devices, see
bug #24431, and not on "real" block devices.

I am also not convinced dropping partitions the dissection logic doesn't
care about is a good idea, after all. The dissection stuff should
probably not consider itself the "owner" of the block devices it
analyzes, but take a more passive role: figure out what is what, but not
modify it.

Fixes: #24431
This commit is contained in:
Lennart Poettering
2022-09-01 22:05:10 +02:00
parent e8383058b2
commit 234c2e16e5
6 changed files with 5 additions and 52 deletions
-1
View File
@@ -2432,7 +2432,6 @@ int setup_namespace(
}
}
dissected_image_relinquish(dissected_image);
loop_device_relinquish(loop_device);
} else if (root_directory) {
-2
View File
@@ -685,7 +685,6 @@ static int action_mount(DissectedImage *m, LoopDevice *d) {
return log_error_errno(r, "Failed to relinquish DM devices: %m");
}
dissected_image_relinquish(m);
loop_device_relinquish(d);
return 0;
}
@@ -738,7 +737,6 @@ static int action_copy(DissectedImage *m, LoopDevice *d) {
return log_error_errno(r, "Failed to relinquish DM devices: %m");
}
dissected_image_relinquish(m);
loop_device_relinquish(d);
if (arg_action == ACTION_COPY_FROM) {
@@ -772,8 +772,6 @@ static int enumerate_partitions(dev_t devnum) {
r = k;
}
dissected_image_relinquish(m);
return r;
}
+5 -42
View File
@@ -150,20 +150,9 @@ static void check_partition_flags(
}
#endif
static void dissected_partition_done(int fd, DissectedPartition *p) {
assert(fd >= 0);
static void dissected_partition_done(DissectedPartition *p) {
assert(p);
#if HAVE_BLKID
if (p->node && p->partno > 0 && !p->relinquished) {
int r;
r = block_device_remove_partition(fd, p->node, p->partno);
if (r < 0)
log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m");
}
#endif
free(p->fstype);
free(p->node);
free(p->label);
@@ -312,14 +301,9 @@ int dissect_image(
return -ENOMEM;
*m = (DissectedImage) {
.fd = -1,
.has_init_system = -1,
};
m->fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
if (m->fd < 0)
return -errno;
r = sd_device_get_sysname(d, &sysname);
if (r < 0)
return log_debug_errno(r, "Failed to get device sysname: %m");
@@ -775,14 +759,10 @@ int dissect_image(
* scheme in OS images. */
if (!PARTITION_DESIGNATOR_VERSIONED(designator) ||
strverscmp_improved(m->partitions[designator].label, label) >= 0) {
r = block_device_remove_partition(fd, node, nr);
if (r < 0)
log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m");
strverscmp_improved(m->partitions[designator].label, label) >= 0)
continue;
}
dissected_partition_done(fd, m->partitions + designator);
dissected_partition_done(m->partitions + designator);
}
if (fstype) {
@@ -852,12 +832,8 @@ int dissect_image(
const char *sid, *options = NULL;
/* First one wins */
if (m->partitions[PARTITION_XBOOTLDR].found) {
r = block_device_remove_partition(fd, node, nr);
if (r < 0)
log_debug_errno(r, "BLKPG_DEL_PARTITION failed, ignoring: %m");
if (m->partitions[PARTITION_XBOOTLDR].found)
continue;
}
sid = blkid_partition_get_uuid(pp);
if (sid)
@@ -1171,9 +1147,8 @@ DissectedImage* dissected_image_unref(DissectedImage *m) {
return NULL;
for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++)
dissected_partition_done(m->fd, m->partitions + i);
dissected_partition_done(m->partitions + i);
safe_close(m->fd);
free(m->image_name);
free(m->hostname);
strv_free(m->machine_info);
@@ -1183,16 +1158,6 @@ DissectedImage* dissected_image_unref(DissectedImage *m) {
return mfree(m);
}
void dissected_image_relinquish(DissectedImage *m) {
assert(m);
/* Partitions are automatically removed when the underlying loop device is closed. We just need to
* make sure we don't try to remove the partitions early. */
for (PartitionDesignator i = 0; i < _PARTITION_DESIGNATOR_MAX; i++)
m->partitions[i].relinquished = true;
}
static int is_loop_device(const char *path) {
char s[SYS_BLOCK_PATH_MAX("/../loop/")];
struct stat st;
@@ -3046,7 +3011,6 @@ int mount_image_privately_interactively(
return log_error_errno(r, "Failed to relinquish DM devices: %m");
}
dissected_image_relinquish(dissected_image);
loop_device_relinquish(d);
*ret_directory = TAKE_PTR(created_dir);
@@ -3209,7 +3173,6 @@ int verity_dissect_and_mount(
return log_debug_errno(r, "Failed to relinquish decrypted image: %m");
}
dissected_image_relinquish(dissected_image);
loop_device_relinquish(loop_device);
return 0;
-4
View File
@@ -31,7 +31,6 @@ struct DissectedPartition {
char *mount_options;
uint64_t size;
uint64_t offset;
bool relinquished;
};
typedef enum PartitionDesignator {
@@ -204,8 +203,6 @@ typedef enum DissectImageFlags {
} DissectImageFlags;
struct DissectedImage {
int fd; /* Backing fd */
bool encrypted:1;
bool has_verity:1; /* verity available in image, but not necessarily used */
bool has_verity_sig:1; /* pkcs#7 signature embedded in image */
@@ -261,7 +258,6 @@ int dissect_image_and_warn(int fd, const char *name, const VeritySettings *verit
DissectedImage* dissected_image_unref(DissectedImage *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);
void dissected_image_relinquish(DissectedImage *m);
int dissected_image_decrypt(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags, DecryptedImage **ret);
int dissected_image_decrypt_interactively(DissectedImage *m, const char *passphrase, const VeritySettings *verity, DissectImageFlags flags, DecryptedImage **ret);
-1
View File
@@ -585,7 +585,6 @@ static int merge_subprocess(Hashmap *images, const char *workspace) {
return log_error_errno(r, "Failed to relinquish DM devices: %m");
}
dissected_image_relinquish(m);
loop_device_relinquish(d);
break;
}