[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#16231: [PATCH] [PATCH] Fix loop labels
From: |
Phillip Susi |
Subject: |
bug#16231: [PATCH] [PATCH] Fix loop labels |
Date: |
Sun, 29 Dec 2013 00:27:54 -0500 |
Loop labels were incorrectly identifying the device
for the fictional partition as $dev1 instead of just $dev.
This caused other programs like gparted to be confused,
and caused parted to fail to identify the partition as busy
due to the fact that it was looking for the wrong device.
Parted also actually created the partition device so your
raw fs on $dev gained an alias as $dev1.
---
NEWS | 2 +
include/parted/device.in.h | 1 +
libparted/arch/linux.c | 17 ++++++--
libparted/disk.c | 2 +
libparted/labels/loop.c | 25 ++----------
partprobe/partprobe.c | 4 +-
tests/Makefile.am | 1 +
tests/t1102-loop-label.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 120 insertions(+), 28 deletions(-)
create mode 100644 tests/t1102-loop-label.sh
diff --git a/NEWS b/NEWS
index 935fa33..816fb57 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,8 @@ GNU parted NEWS -*-
outline -*-
boot partition type.
** Bug Fixes
+ Fix several bugs with loop labels ( whole disk filesystems )
+
Fix gpt to correctly handle non ASCII charcters in partition names
If a drive was 100 times an even multiple of two, sizes specified as
diff --git a/include/parted/device.in.h b/include/parted/device.in.h
index 7c06a66..1c1765a 100644
--- a/include/parted/device.in.h
+++ b/include/parted/device.in.h
@@ -86,6 +86,7 @@ struct _PedDevice {
int external_mode;
int dirty;
int boot_dirty;
+ int loop; /* using "loop" partition table */
PedCHSGeometry hw_geom;
PedCHSGeometry bios_geom;
diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index f43eae1..8a09763 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -48,6 +48,7 @@
#include "../architecture.h"
#include "dirname.h"
#include "xstrtol.h"
+#include "xalloc.h"
#if ENABLE_NLS
# include <libintl.h>
@@ -2315,10 +2316,14 @@ _device_get_part_path (PedDevice const *dev, int num)
? dm_canonical_path (dev) : dev->path);
size_t path_len = strlen (devpath);
char *result;
+
+ /* bare device, no partitions */
+ if (dev->loop)
+ result = xstrdup(devpath);
/* Check for devfs-style /disc => /partN transformation
unconditionally; the system might be using udev with devfs rules,
and if not the test is harmless. */
- if (5 < path_len && !strcmp (devpath + path_len - 5, "/disc")) {
+ else if (5 < path_len && !strcmp (devpath + path_len - 5, "/disc")) {
/* replace /disc with /part%d */
result = zasprintf ("%.*s/part%d",
(int) (path_len - 5), devpath, num);
@@ -2814,7 +2819,10 @@ _disk_sync_part_table (PedDisk* disk)
int i;
/* remove old partitions first */
for (i = 1; i <= lpn; i++) {
- PedPartition *part = ped_disk_get_partition (disk, i);
+ PedPartition *part;
+ if (disk->dev->loop)
+ part = 0;
+ else part = ped_disk_get_partition (disk, i);
if (part) {
unsigned long long length;
unsigned long long start;
@@ -2830,7 +2838,10 @@ _disk_sync_part_table (PedDisk* disk)
}
}
for (i = 1; i <= lpn; i++) {
- PedPartition *part = ped_disk_get_partition (disk, i);
+ PedPartition *part;
+ if (disk->dev->loop)
+ part = 0;
+ else part = ped_disk_get_partition (disk, i);
if (part) {
unsigned long long length;
unsigned long long start;
diff --git a/libparted/disk.c b/libparted/disk.c
index ce71bfc..0eecd63 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -197,6 +197,7 @@ ped_disk_new (PedDevice* dev)
disk = ped_disk_new_fresh (dev, type);
if (!disk)
goto error_close_dev;
+ dev->loop = 0;
if (!type->ops->read (disk))
goto error_destroy_disk;
disk->needs_clobber = 0;
@@ -497,6 +498,7 @@ ped_disk_commit_to_dev (PedDisk* disk)
goto error_close_dev;
disk->needs_clobber = 0;
}
+ disk->dev->loop = 0;
if (!disk->type->ops->write (disk))
goto error_close_dev;
ped_device_close (disk->dev);
diff --git a/libparted/labels/loop.c b/libparted/labels/loop.c
index ea8f007..79b817c 100644
--- a/libparted/labels/loop.c
+++ b/libparted/labels/loop.c
@@ -118,18 +118,12 @@ loop_read (PedDisk* disk)
int found_sig = !strncmp (buf, LOOP_SIGNATURE, strlen
(LOOP_SIGNATURE));
free (buf);
-
- if (found_sig) {
- ped_constraint_destroy (constraint_any);
- return 1;
- }
-
geom = ped_geometry_new (dev, 0, dev->length);
if (!geom)
goto error;
fs_type = ped_file_system_probe (geom);
- if (!fs_type)
+ if (!fs_type && !found_sig)
goto error_free_geom;
part = ped_partition_new (disk, PED_PARTITION_NORMAL,
@@ -137,11 +131,11 @@ loop_read (PedDisk* disk)
ped_geometry_destroy (geom);
if (!part)
goto error;
- part->fs_type = fs_type;
if (!ped_disk_add_partition (disk, part, constraint_any))
goto error;
ped_constraint_destroy (constraint_any);
+ dev->loop = 1;
return 1;
error_free_geom:
@@ -159,20 +153,7 @@ loop_write (const PedDisk* disk)
char *buf = ped_malloc (buflen);
if (buf == NULL)
return 0;
-
- if (ped_disk_get_partition (disk, 1)) {
- if (!ped_device_read (disk->dev, buf, 0, 1)) {
- free (buf);
- return 0;
- }
- if (strncmp (buf, LOOP_SIGNATURE, strlen (LOOP_SIGNATURE)) !=
0) {
- free (buf);
- return 1;
- }
- memset (buf, 0, strlen (LOOP_SIGNATURE));
- return ped_device_write (disk->dev, buf, 0, 1);
- }
-
+ disk->dev->loop = 1;
memset (buf, 0, buflen);
strcpy (buf, LOOP_SIGNATURE);
diff --git a/partprobe/partprobe.c b/partprobe/partprobe.c
index 4da4fb7..8b744b5 100644
--- a/partprobe/partprobe.c
+++ b/partprobe/partprobe.c
@@ -106,9 +106,7 @@ process_dev (PedDevice* dev)
PedDisk* disk;
disk_type = ped_disk_probe (dev);
- if (disk_type && !strcmp (disk_type->name, "loop"))
- return 1;
- else if (!disk_type) {
+ if (!disk_type) {
/* Partition table not found, so create dummy,
empty one */
disk_type = ped_disk_type_get("msdos");
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7a6fe8f..fc0b5ec 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,6 +40,7 @@ TESTS = \
t0501-duplicate.sh \
t1100-busy-label.sh \
t1101-busy-partition.sh \
+ t1102-loop-label.sh \
t1700-probe-fs.sh \
t2200-dos-label-recog.sh \
t2201-pc98-label-recog.sh \
diff --git a/tests/t1102-loop-label.sh b/tests/t1102-loop-label.sh
new file mode 100644
index 0000000..b26addd
--- /dev/null
+++ b/tests/t1102-loop-label.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+# make sure that loop labels do busy detection and don't
+# create an actual partition
+
+# Copyright (C) 2013 Free Software Foundation, Inc.
+
+# 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 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+path_prepend_ ../partprobe
+require_root_
+require_scsi_debug_module_
+ss=$sector_size_
+
+scsi_debug_setup_ sector_size=$ss dev_size_mb=90 > dev-name ||
+ skip_ 'failed to create scsi_debug device'
+dev=$(cat dev-name)
+
+mke2fs -F $dev
+parted -s "$dev" print > out 2>&1 || fail=1
+cat <<EOF > exp
+Model: Linux scsi_debug (scsi)
+Disk DEVICE: 94.4MB
+Sector size (logical/physical): 512B/512B
+Partition Table: loop
+Disk Flags:
+
+Number Start End Size File system Flags
+ 1 0.00B 94.4MB 94.4MB ext2
+
+EOF
+mv out o2 && sed -e "s,$dev,DEVICE,;" o2 > out
+
+compare exp out || fail=1
+parted -s $dev rm 1 mkpart ext2 0% 100% || fail=1
+if [ -e ${dev}1 ]; then
+ echo "Partition should not exist on loop device"
+ fail=1
+fi
+partprobe $dev || fail=1
+if [ -e ${dev}1 ]; then
+ echo "Partition should not exist on loop device"
+ fail=1
+fi
+
+mount_point="`pwd`/mnt"
+
+# Be sure to unmount upon interrupt, failure, etc.
+cleanup_fn_() { umount "$mount_point" > /dev/null 2>&1; }
+
+# create mount point dir. and mount the just-created partition on it
+mkdir $mount_point || fail=1
+mount -t ext2 "${dev}" $mount_point || fail=1
+
+# now that a partition is mounted, mklabel attempt must fail
+parted -s "$dev" mklabel msdos > out 2>&1; test $? = 1 || fail=1
+
+# create expected output file
+echo "Error: Partition(s) on $dev are being used." > exp
+compare exp out || fail=1
+
+# make sure partition busy check works ( mklabel checks whole disk )
+parted -s "$dev" rm 1 > out 2>&1; test $? = 1 || fail=1
+# create expected output file
+echo "Error: Partition /dev/sde is being used. You must unmount it before you
modify \
+it with Parted." > exp
+compare exp out || fail=1
+
+umount "$mount_point"
+
+# make sure partprobe cleans up stale partition devices
+parted -s $dev mklabel msdos mkpart primary ext2 0% 100%
+if [ ! -e ${dev}1 ]; then
+ echo "Partition doesn't exist on loop device"
+ fail=1
+fi
+
+mke2fs -F $dev
+partprobe $dev
+if [ -e ${dev}1 ]; then
+ echo "Partition should not exist on loop device"
+ fail=1
+fi
+
+Exit $fail
--
1.8.3.2