Fix failure to recognise whole disk file systems in certain cases (#743181)

When the following conditions were met GParted would fail to recognise a
newly created whole disk device file system, and instead show an unknown
file system filling the disk:

1) Disk was previously partitioned and contained at least one partition.

2) Using libparted version 2.0 to 3.0 inclusive.

Initial status:
    # blkid | fgrep sdc
    # fgrep sdc /proc/partitions
       8       32  976762584 sdc
       8       33  104857600 sdc1
    # parted /dev/sdc
    GNU Parted 2.4
    Using /dev/sdc
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) print
    Model: ATA ST1000LM024 HN-M (scsi)
    Disk /dev/sdc: 1000GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: msdos

    Number  Start   End    Size   Type     File system  Flags
     1      1049kB  107GB  107GB  primary

When creating the loop partition table libparted would not inform the
kernel to delete the old partitions.  /proc/partitions still contained
the details of the old partitions.
    (parted) mktable loop
    Warning: The existing disk label on /dev/sdc will be destroyed and
    all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes
    (parted) print
    Model: ATA ST1000LM024 HN-M (scsi)
    Disk /dev/sdc: 1000GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: loop

    Number  Start  End  Size  File system  Flags

    (parted) quit
    # fgrep sdc /proc/partitions
       8       32  976762584 sdc
       8       33  104857600 sdc1

Creation of the whole disk device file system goes unnoticed by blkid
because the kernel and therefore blkid's cache have stale partition
information.
    # mkfs.xfs -f /dev/sdc
    # blkid | fgrep sdc

NOTE:
On a Linux Software RAID array, as opposed to a hard disk, blkid does
notice creation of the whole disk device file system.  However the
kernel still has old partition details.

This was fixed in libparted 3.1 by commit:

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=f5c909c0cd50ed52a48dae6d35907dc08b137e88
    libparted: remove has_partitions check to allow loopback partitions

Fix by deleting old partitions before creating the loop table when
compiled with a broken version of libparted.  The GParted UI provides
no feedback while a new partition table is created, and with some
versions of GTK the UI become unresponsive too, so it is important to be
as fast as possible.  Evaluated three different methods, deleting 15 and
22 MSDOS partitions on a physical 5400 RPM hard drive using libparted
2.4:

M1) Delete and commit one partition at a time.
    Takes up to 24 seconds to delete 15 partitions.  With 22 partitions
    libparted always reports finding some of the partitions busy and
    unable to inform the kernel about the modifications.
    Too slow and doesn't work.

M2) Delete all partitions in one go and commit once.
    Takes up to 1.4 seconds to delete either 15 or 22 partitions.  Never
    removes partitions 17 and higher from the kernel.
    Doesn't work.

M3) Write GPT table (letting libparted delete any old partitions).
    Takes up to 0.8 seconds to delete either 15 or 22 partitions.
    Fast and works.

Use method 3 - write a GPT table thus using libparted code to inform the
kernel of the old partition deletions.

Bug 743181 - Add unpartitioned drive read-write support
This commit is contained in:
Mike Fleetwood 2015-02-21 16:43:42 +00:00 committed by Curtis Gedak
parent 4087cb2e2b
commit 9c1a833a0d
3 changed files with 44 additions and 4 deletions

View file

@ -99,6 +99,27 @@ int main()
)
dnl Check for libparted 2.0 to 3.0 inclusive for a bug where loop table
dnl creation doesn't delete old partitions.
LIBPARTED_MIN_WANTED_VERSION='2.0'
LIBPARTED_MAX_WANTED_VERSION='3.0'
AC_MSG_CHECKING([for $LIBPARTED_MIN_WANTED_VERSION <= libparted <= $LIBPARTED_MAX_WANTED_VERSION (loop table creation doesn't delete old partitions)])
LIBPARTED_MIN_WANTED_INT=`echo "$LIBPARTED_MIN_WANTED_VERSION" |
$AWK -F. '{print $1 * 10000 + $2 * 100 + $3}'`
LIBPARTED_MAX_WANTED_INT=`echo "$LIBPARTED_MAX_WANTED_VERSION" |
$AWK -F. '{print $1 * 10000 + $2 * 100 + $3}'`
if test "$LIBPARTED_MIN_WANTED_INT" -le "$LIBPARTED_FOUND_INT" -a \
"$LIBPARTED_FOUND_INT" -le "$LIBPARTED_MAX_WANTED_INT"; then
need_loop_delete_old_ptns_workaround=yes
AC_DEFINE([ENABLE_LOOP_DELETE_OLD_PTNS_WORKAROUND], 1,
[Define to 1 to enable deletion of old partitions before creating a loop table workaround])
AC_MSG_RESULT([(cached) yes])
else
need_loop_delete_old_ptns_workaround=no
AC_MSG_RESULT([(cached) no])
fi
dnl Check for libparted >= 2.2 for improved informing the kernel to
dnl re-read the partition table code and support of larger sector sizes
dnl (> 512 bytes).
@ -353,6 +374,8 @@ echo ""
echo " Use native libparted dmraid support? : $enable_libparted_dmraid"
echo ""
echo " --- Features Based On Libparted Version ---"
echo " Need delete old partitions before"
echo " creating a loop table workaround? : $need_loop_delete_old_ptns_workaround"
echo " Need partition table re-read workaround? : $need_pt_reread_workaround"
echo " Supports large sector sizes (> 512 bytes)? : $support_large_sector_sizes"
echo " Have old libparted file system resizing API? : $have_old_lp_fs_resize_api"

View file

@ -48,7 +48,8 @@ public:
bool apply_operation_to_disk( Operation * operation );
bool set_disklabel( const Device & device, const Glib::ustring & disklabel );
bool new_disklabel( const Glib::ustring & device_path, const Glib::ustring & disklabel );
bool new_disklabel( const Glib::ustring & device_path, const Glib::ustring & disklabel,
bool recreate_dmraid_devs = true );
bool toggle_flag( const Partition & partition, const Glib::ustring & flag, bool state ) ;

View file

@ -810,6 +810,21 @@ bool GParted_Core::set_disklabel( const Device & device, const Glib::ustring & d
{
Glib::ustring device_path = device.get_path();
// FIXME: Should call file system specific removal actions
// (to remove LVM2 PVs before deleting the partitions).
#ifdef ENABLE_LOOP_DELETE_OLD_PTNS_WORKAROUND
// When creating a "loop" table with libparted 2.0 to 3.0 inclusive, it doesn't
// inform the kernel to delete old partitions so as a consequence blkid's cache
// becomes stale and it won't report a file system subsequently created on the
// whole disk device. Create a GPT first to use that code in libparted to delete
// any old partitions. Fixed in parted 3.1 by commit:
// f5c909c0cd50ed52a48dae6d35907dc08b137e88
// libparted: remove has_partitions check to allow loopback partitions
if ( disklabel == "loop" )
new_disklabel( device_path, "gpt", false );
#endif
// Ensure that any previous whole disk device file system can't be recognised by
// libparted in preference to the "loop" partition table signature, or by blkid in
// preference to any partition table about to be written.
@ -826,7 +841,8 @@ bool GParted_Core::set_disklabel( const Device & device, const Glib::ustring & d
return new_disklabel( device_path, disklabel );
}
bool GParted_Core::new_disklabel( const Glib::ustring & device_path, const Glib::ustring & disklabel )
bool GParted_Core::new_disklabel( const Glib::ustring & device_path, const Glib::ustring & disklabel,
bool recreate_dmraid_devs )
{
bool return_value = false ;
@ -850,7 +866,7 @@ bool GParted_Core::new_disklabel( const Glib::ustring & device_path, const Glib:
#ifndef USE_LIBPARTED_DMRAID
//delete and recreate disk entries if dmraid
DMRaid dmraid ;
if ( return_value && dmraid .is_dmraid_device( device_path ) )
if ( recreate_dmraid_devs && return_value && dmraid.is_dmraid_device( device_path ) )
{
dmraid .purge_dev_map_entries( device_path ) ;
dmraid .create_dev_map_entries( device_path ) ;
@ -859,7 +875,7 @@ bool GParted_Core::new_disklabel( const Glib::ustring & device_path, const Glib:
return return_value ;
}
bool GParted_Core::toggle_flag( const Partition & partition, const Glib::ustring & flag, bool state )
{
bool succes = false ;