Skip to content

Commit

Permalink
fix LVM setup for LVM with one character long names
Browse files Browse the repository at this point in the history
  • Loading branch information
aschnell committed Aug 31, 2020
1 parent 812fd86 commit 1b58bd4
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 7 deletions.
6 changes: 6 additions & 0 deletions package/snapper.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Mon Aug 31 17:18:14 CEST 2020 - [email protected]

- fix LVM setup for volume groups and logical volumes with one
character long names (gh#openSUSE/snapper#465)

-------------------------------------------------------------------
Fri Aug 28 11:06:23 CEST 2020 - [email protected]

Expand Down
16 changes: 11 additions & 5 deletions snapper/Lvm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "snapper/Log.h"
#include "snapper/Filesystem.h"
#include "snapper/Lvm.h"
#include "snapper/LvmUtils.h"
#include "snapper/Snapper.h"
#include "snapper/SnapperTmpl.h"
#include "snapper/SystemCmd.h"
Expand All @@ -48,6 +49,8 @@

namespace snapper
{
using namespace std;


Filesystem*
Lvm::create(const string& fstype, const string& subvolume, const string& root_prefix)
Expand Down Expand Up @@ -387,16 +390,19 @@ namespace snapper
bool
Lvm::detectThinVolumeNames(const MtabData& mtab_data)
{
Regex rx("^/dev/mapper/(.+[^-])-([^-].+)$");
if (!rx.match(mtab_data.device))
try
{
pair<string, string> names = LvmUtils::split_device_name(mtab_data.device);

vg_name = names.first;
lv_name = names.second;
}
catch (const runtime_error& e)
{
y2err("could not detect lvm names from '" << mtab_data.device << "'");
return false;
}

vg_name = boost::replace_all_copy(rx.cap(1), "--", "-");
lv_name = boost::replace_all_copy(rx.cap(2), "--", "-");

try
{
cache->add_or_update(vg_name, lv_name);
Expand Down
52 changes: 52 additions & 0 deletions snapper/LvmUtils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) [2011-2014] Novell, Inc.
* Copyright (c) 2020 SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* 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, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/


#include <boost/algorithm/string.hpp>

#include "snapper/LvmUtils.h"
#include "snapper/Regex.h"
#include "snapper/AppUtil.h"


namespace snapper
{

namespace LvmUtils
{

pair<string, string>
split_device_name(const string& name)
{
Regex rx("^/dev/mapper/(.*[^-])-([^-].*)$");
if (!rx.match(name))
throw std::runtime_error("faild to split device name into volume group and "
"logical volume name");

string vg_name = boost::replace_all_copy(rx.cap(1), "--", "-");
string lv_name = boost::replace_all_copy(rx.cap(2), "--", "-");

return make_pair(vg_name, lv_name);
}

}
}
48 changes: 48 additions & 0 deletions snapper/LvmUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) [2011-2014] Novell, Inc.
* Copyright (c) 2020 SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* 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, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/


#ifndef SNAPPER_LVM_UTILS_H
#define SNAPPER_LVM_UTILS_H


#include <utility>
#include <string>


namespace snapper
{
using std::string;
using std::pair;


namespace LvmUtils
{

std::pair<string, string> split_device_name(const string& name);

}

}


#endif
3 changes: 2 additions & 1 deletion snapper/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ endif
if ENABLE_LVM
libsnapper_la_SOURCES += \
Lvm.cc Lvm.h \
LvmCache.cc LvmCache.h
LvmCache.cc LvmCache.h \
LvmUtils.cc LvmUtils.h
endif

if ENABLE_ROLLBACK
Expand Down
4 changes: 3 additions & 1 deletion testsuite/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LDADD = ../snapper/libsnapper.la ../dbus/libdbus.la -lboost_unit_test_framework

check_PROGRAMS = sysconfig-get1.test dirname1.test basename1.test \
equal-date.test dbus-escape.test cmp-lt.test humanstring.test table.test \
csv-formatter.test json-formatter.test getopts.test
csv-formatter.test json-formatter.test getopts.test lvm-utils.test

if ENABLE_BTRFS_QUOTA
check_PROGRAMS += qgroup1.test
Expand All @@ -31,3 +31,5 @@ csv_formatter_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.
json_formatter_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la

getopts_test_LDADD = -lboost_unit_test_framework ../client/utils/libutils.la

lvm_utils_test_LDADD = -lboost_unit_test_framework ../snapper/libsnapper.la
25 changes: 25 additions & 0 deletions testsuite/lvm-utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE basename1

#include <boost/test/unit_test.hpp>

#include <snapper/LvmUtils.h>

using namespace snapper;


BOOST_AUTO_TEST_CASE(split_device_name)
{
std::pair<string, string> n1 = LvmUtils::split_device_name("/dev/mapper/system-root");
BOOST_CHECK_EQUAL(n1.first, "system");
BOOST_CHECK_EQUAL(n1.second, "root");

std::pair<string, string> n2 = LvmUtils::split_device_name("/dev/mapper/vg--system-lv--root");
BOOST_CHECK_EQUAL(n2.first, "vg-system");
BOOST_CHECK_EQUAL(n2.second, "lv-root");

std::pair<string, string> n3 = LvmUtils::split_device_name("/dev/mapper/s-r");
BOOST_CHECK_EQUAL(n3.first, "s");
BOOST_CHECK_EQUAL(n3.second, "r");
}

0 comments on commit 1b58bd4

Please sign in to comment.