Create BlockSpecial class and use in LVM2_PV_Info (#767842)

In some cases creating an LVM2 Physical Volume on top of a DMRaid array
reports no usage information and this partition warning:
    Unable to read the contents of this file system!
    Because of this some operations may be unavailable.
    The cause might be a missing software package.
    The following list of software packages is required for lvm2
    pv file system support: lvm2.

For example on Ubuntu 14.04 LTS (with GParted built with
--enable-libparted-dmraid) create an LVM2 PV in a DMRaid array
partition.  GParted uses this command:
    # lvm pvcreate -M 2 /dev/mapper/isw_bacdehijbd_MyArray0p2

But LVM reports the PV having a different name:
    # lvm pvs
      PV                                                VG   Fmt  Attr PSize PFree
      /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2      lvm2 a--  1.00g 1.00g

This alternate name is loaded into the LVM2_PV_Info module cache.  Hence
when GParted queries partition /dev/mapper/isw_bacdehijbd_MyArray0p2 it
has no PV information against that name and reports unknown usage.

However they are actually the same block special device; major 252,
minor 2:
    # ls -l /dev/mapper/isw_bacdehijbd_MyArray0p2
    brw-rw---- 1 root disk 252, 2 Jul  2 11:09 /dev/mapper/isw_bacdehijbd_MyArray0p2

    # ls -l /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2
    lrwxrwxrwx 1 root root 10 Jul  2 11:09 /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2 -> ../../dm-2
    # ls -l /dev/dm-2
    brw-rw---- 1 root disk 252, 2 Jul  2 11:09 /dev/dm-2

To determine if two names refer to the same block special device their
major, minor numbers need to be compared, instead of string comparing
their names.

Implement class BlockSpecial which encapsulates the name and major,
minor numbers for a block special device.  Also performs comparison as
needed.  See bug 767842 comments 4 and 5 for further investigation and
decision for choosing to implement a class.

Replace name strings in the LVM2_PV_Info module with BlockSpecial
objects performing correct block special device comparison.

Bug 767842 - File system usage missing when tools report alternate block
             device names
This commit is contained in:
Mike Fleetwood 2016-06-22 17:32:01 +01:00 committed by Curtis Gedak
parent 0f4df8dfd1
commit ab2d4f5ee6
7 changed files with 132 additions and 14 deletions

54
include/BlockSpecial.h Normal file
View file

@ -0,0 +1,54 @@
/* Copyright (C) 2016 Mike Fleetwood
*
* 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 2 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/>.
*/
/* BlockSpecial
*
* Representation of a POSIX block special file, e.g. /dev/sda1. Also
* tracks the major, minor device numbers so that different names for
* the same block device can be compared equal.
* Refs: mknod(1) and mknod(2).
*/
#ifndef GPARTED_BLOCKSPECIAL_H
#define GPARTED_BLOCKSPECIAL_H
#include <glibmm/ustring.h>
namespace GParted
{
class BlockSpecial
{
public:
BlockSpecial();
BlockSpecial( const Glib::ustring & name );
~BlockSpecial();
Glib::ustring m_name; // E.g. Block special file {"/dev/sda1", 8, 1},
unsigned long m_major; // plain file {"FILENAME", 0, 0} and empty object
unsigned long m_minor; // {"", 0, 0}.
};
// Operator overloading > The Decision between Member and Non-member
// http://stackoverflow.com/questions/4421706/operator-overloading/4421729#4421729
// "2. If a binary operator treats both operands equally (it leaves them unchanged),
// implement this operator as a non-member function."
bool operator==( const BlockSpecial & lhs, const BlockSpecial & rhs );
}//GParted
#endif /* GPARTED_BLOCKSPECIAL_H */

View file

@ -25,14 +25,18 @@
#ifndef GPARTED_LVM2_PV_INFO_H
#define GPARTED_LVM2_PV_INFO_H
#include "../include/BlockSpecial.h"
#include "../include/Utils.h"
#include <glibmm/ustring.h>
#include <vector>
namespace GParted
{
struct LVM2_PV
{
Glib::ustring pv_name;
BlockSpecial pv_name;
Byte_Value pv_size;
Byte_Value pv_free;
Glib::ustring vg_name;

View file

@ -1,6 +1,7 @@
gparted_includedir = $(pkgincludedir)
EXTRA_DIST = \
BlockSpecial.h \
Copy_Blocks.h \
DMRaid.h \
Device.h \

View file

@ -3,6 +3,7 @@
gparted.appdata.xml.in
gparted.desktop.in.in
include/Utils.h
src/BlockSpecial.cc
src/Copy_Blocks.cc
src/Dialog_Base_Partition.cc
src/Dialog_Disklabel.cc

55
src/BlockSpecial.cc Normal file
View file

@ -0,0 +1,55 @@
/* Copyright (C) 2016 Mike Fleetwood
*
* 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 2 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/>.
*/
#include "../include/BlockSpecial.h"
#include <glibmm/ustring.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace GParted
{
BlockSpecial::BlockSpecial() : m_name( "" ), m_major( 0UL ), m_minor( 0UL )
{
}
BlockSpecial::BlockSpecial( const Glib::ustring & name ) : m_name( name ), m_major( 0UL ), m_minor( 0UL )
{
struct stat sb;
if ( stat( name.c_str(), &sb ) == 0 && S_ISBLK( sb.st_mode ) )
{
m_major = major( sb.st_rdev );
m_minor = minor( sb.st_rdev );
}
}
BlockSpecial::~BlockSpecial()
{
}
bool operator==( const BlockSpecial & lhs, const BlockSpecial & rhs )
{
if ( lhs.m_major > 0UL && lhs.m_minor > 0UL )
// Match block special files by major, minor device numbers.
return lhs.m_major == rhs.m_major && lhs.m_minor == rhs.m_minor;
else
// For non-block special files fall back to name string compare.
return lhs.m_name == rhs.m_name;
}
} //GParted

View file

@ -15,6 +15,7 @@
*/
#include "../include/LVM2_PV_Info.h"
#include "../include/BlockSpecial.h"
namespace GParted
{
@ -37,13 +38,13 @@ enum LV_BIT
// lvm_found - Is the "lvm" command available?
// lvm2_pv_cache - Vector of PV fields: pv_name, pv_size, pv_free, vg_name.
// E.g.
// //pv_name , pv_size , pv_free , vg_name
// [{"/dev/sda10", 1073741824, 1073741824, "" },
// {"/dev/sda11", 1069547520, 1069547520, "Test-VG1"},
// {"/dev/sda12", 1069547520, 335544320, "Test_VG2"},
// {"/dev/sda13", 1069547520, 0, "Test_VG3"},
// {"/dev/sda14", 1069547520, 566231040, "Test_VG3"},
// {"/dev/sda15", 1069547520, 545259520, "Test-VG4"}
// //pv_name , pv_size , pv_free , vg_name
// [{BlockSpecial("/dev/sda10"), 1073741824, 1073741824, "" },
// {BlockSpecial("/dev/sda11"), 1069547520, 1069547520, "Test-VG1"},
// {BlockSpecial("/dev/sda12"), 1069547520, 335544320, "Test_VG2"},
// {BlockSpecial("/dev/sda13"), 1069547520, 0, "Test_VG3"},
// {BlockSpecial("/dev/sda14"), 1069547520, 566231040, "Test_VG3"},
// {BlockSpecial("/dev/sda15"), 1069547520, 545259520, "Test-VG4"}
// ]
// lvm2_vg_cache - Vector storing VG fields: vg_name, vg_attr, lv_name, lv_attr.
// See vgs(8) and lvs(8) for details of vg_attr and lv_attr respectively.
@ -145,7 +146,7 @@ std::vector<Glib::ustring> LVM2_PV_Info::get_vg_members( const Glib::ustring & v
{
if ( vgname == lvm2_pv_cache[i].vg_name )
{
members.push_back( lvm2_pv_cache[i].pv_name );
members.push_back( lvm2_pv_cache[i].pv_name.m_name );
}
}
@ -252,10 +253,10 @@ void LVM2_PV_Info::load_lvm2_pv_info_cache()
Utils::split( Utils::trim( lines[i] ), fields, "," );
if ( fields.size() < PVFIELD_COUNT )
continue; // Not enough fields
LVM2_PV pv;
pv.pv_name = fields[PVFIELD_PV_NAME];
if ( pv.pv_name.size() == 0 )
if ( fields[PVFIELD_PV_NAME] == "" )
continue; // Empty PV name
LVM2_PV pv;
pv.pv_name = BlockSpecial( fields[PVFIELD_PV_NAME] );
pv.pv_size = lvm2_pv_size_to_num( fields[PVFIELD_PV_SIZE] );
pv.pv_free = lvm2_pv_size_to_num( fields[PVFIELD_PV_FREE] );
pv.vg_name = fields[PVFIELD_VG_NAME];
@ -331,12 +332,13 @@ void LVM2_PV_Info::load_lvm2_pv_info_cache()
// Returns found cache entry or not found substitute.
const LVM2_PV & LVM2_PV_Info::get_pv_cache_entry_by_name( const Glib::ustring & pvname )
{
BlockSpecial bs_pvname( pvname );
for ( unsigned int i = 0 ; i < lvm2_pv_cache.size() ; i ++ )
{
if ( pvname == lvm2_pv_cache[i].pv_name )
if ( bs_pvname == lvm2_pv_cache[i].pv_name )
return lvm2_pv_cache[i];
}
static LVM2_PV pv = {"", -1, -1, ""};
static LVM2_PV pv = {BlockSpecial(), -1, -1, ""};
return pv;
}

View file

@ -12,6 +12,7 @@ AM_CXXFLAGS = -Wall
sbin_PROGRAMS = gpartedbin
gpartedbin_SOURCES = \
BlockSpecial.cc \
Copy_Blocks.cc \
DMRaid.cc \
Device.cc \