gparted/src/BlockSpecial.cc

113 lines
3.3 KiB
C++
Raw Normal View History

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
2016-06-22 10:32:01 -06:00
/* 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>
#include <map>
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
2016-06-22 10:32:01 -06:00
namespace GParted
{
struct MM_Number
{
unsigned long m_major;
unsigned long m_minor;
};
typedef std::map<Glib::ustring, MM_Number> MMNumberMapping;
// Associative array caching name to major, minor number pairs
// E.g.
// mm_number_cache["/dev/sda"] = {8, 0}
// mm_number_cache["/dev/sda1"] = {8, 1}
// mm_number_cache["proc"] = {0, 0}
// mm_number_cache["sysfs"] = {0, 0}
static MMNumberMapping mm_number_cache;
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
2016-06-22 10:32:01 -06:00
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 )
{
MMNumberMapping::const_iterator mm_num_iter = mm_number_cache.find( name );
if ( mm_num_iter != mm_number_cache.end() )
{
// Use already cached major, minor pair
m_major = mm_num_iter->second.m_major;
m_minor = mm_num_iter->second.m_minor;
return;
}
MM_Number pair = {0UL, 0UL};
// Call stat(name, ...) to get the major, minor pair
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
2016-06-22 10:32:01 -06:00
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 );
pair.m_major = m_major;
pair.m_minor = m_minor;
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
2016-06-22 10:32:01 -06:00
}
// Add new cache entry for name to major, minor pair
mm_number_cache[name] = pair;
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
2016-06-22 10:32:01 -06:00
}
BlockSpecial::~BlockSpecial()
{
}
void BlockSpecial::clear_cache()
{
mm_number_cache.clear();
}
void BlockSpecial::register_block_special( const Glib::ustring & name,
unsigned long major, unsigned long minor )
{
MM_Number pair;
pair.m_major = major;
pair.m_minor = minor;
// Add new, or update existing, cache entry for name to major, minor pair
mm_number_cache[name] = pair;
}
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
2016-06-22 10:32:01 -06:00
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;
}
Add BlockSpecial into mount_info and fstab_info (#767842) On some distributions having btrfs on top of LUKS encrypted partitions, adding a second device and removing the first device used to mount the file system causes GParted to no longer be able to report the file system as busy or the mount points themselves. For example, on CentOS 7, create a single btrfs file system and mount it. The provided /dev/mapper/sdb1_crypt name is reported, via /proc/mounts, as the mounting device: # cryptsetup luksFormat --force-password /dev/sdb1 # cryptsetup luksOpen /dev/sdb1 sdb1_crypt # mkfs.btrfs -L encrypted-btrfs /dev/mapper/sdb1_crypt # mount /dev/mapper/sdb1_crypt /mnt/1 # ls -l /dev/mapper total 0 lrwxrwxrwx. 1 root root 7 Jul 2 14:15 centos-root -> ../dm-1 lrwxrwxrwx. 1 root root 7 Jul 2 14:15 centos-swap -> ../dm-0 crw-------. 1 root root 10, 236 Jul 2 14:15 control lrwxrwxrwx. 1 root root 7 Jul 2 15:14 sdb1_crypt -> ../dm-2 # fgrep btrfs /proc/mounts /dev/mapper/sdb1_crypt /mnt/1 btrfs rw,seclabel,relatime,space_cache 0 0 Add a second device to the btrfs file system: # cryptsetup luksFormat --force-password /dev/sdb2 # cryptsetup luksOpen /dev/sdb2 sdb2_crypt # btrfs device add /dev/mapper/sdb2_crypt /mnt/1 # ls -l /dev/mapper ... lrwxrwxrwx. 1 root root 7 Jul 2 15:12 sdb2_crypt -> ../dm-3 # btrfs filesystem show /dev/mapper/sdb1_crypt Label: 'encrypted-btrfs' uuid: 45d7b1ef-820c-4ef8-8abd-c70d928afb49 Total devices 2 FS bytes used 32.00KiB devid 1 size 1022.00MiB used 12.00MiB path /dev/mapper/sdb1_crypt devid 2 size 1022.00MiB used 0.00B path /dev/mapper/sdb2_crypt Remove the first mounting device from the btrfs file system. Now the non-canonical name /dev/dm-3 is reported, via /proc/mounts, as the mounting device: # btrfs device delete /dev/mapper/sdb1_crypt /mnt/1 # btrfs filesystem show /dev/mapper/sdb2_crypt Label: 'encrypted-btrfs' uuid: 45d7b1ef-820c-4ef8-8abd-c70d928afb49 Total devices 1 FS bytes used 96.00KiB devid 2 size 1022.00MiB used 144.00MiB path /dev/mapper/sdb2_crypt # fgrep btrfs /proc/mounts /dev/dm-3 /mnt/1 btrfs rw,seclabel,relatime,space_cache 0 0 # ls -l /dev/dm-3 brw-rw----. 1 root disk 253, 3 Jul 2 15:12 /dev/dm-3 GParted loads the mount_info mapping from /proc/mounts and with it the /dev/dm-3 name. When GParted is determining if the encrypted btrfs file system is mounted or getting the mount points it is using the /dev/mapper/sdb2_crypt name. Therefore no information is found and the file system is incorrectly reported as unmounted. Fix by changing mount_info and fstab_info to use BlockSpecial objects instead of strings so that matching is performed by major, minor device numbers rather than by string compare. Note that as BlockSpecial objects are used as the key of std::map [1] mappings operator<() [2] needs to be provided to order the key values. [1] std::map http://www.cplusplus.com/reference/map/map/ [2] std::map::key_comp http://www.cplusplus.com/reference/map/map/key_comp/ Bug 767842 - File system usage missing when tools report alternate block device names
2016-06-25 01:29:17 -06:00
bool operator<( const BlockSpecial & lhs, const BlockSpecial & rhs )
{
if ( lhs.m_major == 0 && rhs.m_major == 0 && lhs.m_minor == 0 && rhs.m_minor == 0 )
// Two non-block special files are ordered by name.
return lhs.m_name < rhs.m_name;
else
// Block special files are ordered by major, minor device numbers.
return lhs.m_major < rhs.m_major || ( lhs.m_major == rhs.m_major && lhs.m_minor < rhs.m_minor );
}
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
2016-06-22 10:32:01 -06:00
} //GParted