From: Arvin Schnell Date: Mon, 31 Aug 2020 15:29:53 +0000 (+0200) Subject: fix LVM setup for LVM with one character long names X-Git-Tag: v0.8.14~31^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F552%2Fhead;p=thirdparty%2Fsnapper.git fix LVM setup for LVM with one character long names --- diff --git a/package/snapper.changes b/package/snapper.changes index 0e1e56f2..0935c990 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,9 @@ +------------------------------------------------------------------- +Mon Aug 31 17:18:14 CEST 2020 - aschnell@suse.com + +- 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 - aschnell@suse.com diff --git a/snapper/Lvm.cc b/snapper/Lvm.cc index a944800d..3a97b829 100644 --- a/snapper/Lvm.cc +++ b/snapper/Lvm.cc @@ -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" @@ -48,6 +49,8 @@ namespace snapper { + using namespace std; + Filesystem* Lvm::create(const string& fstype, const string& subvolume, const string& root_prefix) @@ -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 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); diff --git a/snapper/LvmUtils.cc b/snapper/LvmUtils.cc new file mode 100644 index 00000000..47e76954 --- /dev/null +++ b/snapper/LvmUtils.cc @@ -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 + +#include "snapper/LvmUtils.h" +#include "snapper/Regex.h" +#include "snapper/AppUtil.h" + + +namespace snapper +{ + + namespace LvmUtils + { + + pair + 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); + } + + } +} diff --git a/snapper/LvmUtils.h b/snapper/LvmUtils.h new file mode 100644 index 00000000..3bd4caf2 --- /dev/null +++ b/snapper/LvmUtils.h @@ -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 +#include + + +namespace snapper +{ + using std::string; + using std::pair; + + + namespace LvmUtils + { + + std::pair split_device_name(const string& name); + + } + +} + + +#endif diff --git a/snapper/Makefile.am b/snapper/Makefile.am index 3e8b0c07..988b9e32 100644 --- a/snapper/Makefile.am +++ b/snapper/Makefile.am @@ -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 diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am index 0ff300c6..f79ad66e 100644 --- a/testsuite/Makefile.am +++ b/testsuite/Makefile.am @@ -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 @@ -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 diff --git a/testsuite/lvm-utils.cc b/testsuite/lvm-utils.cc new file mode 100644 index 00000000..ed4360da --- /dev/null +++ b/testsuite/lvm-utils.cc @@ -0,0 +1,25 @@ + +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE basename1 + +#include + +#include + +using namespace snapper; + + +BOOST_AUTO_TEST_CASE(split_device_name) +{ + std::pair n1 = LvmUtils::split_device_name("/dev/mapper/system-root"); + BOOST_CHECK_EQUAL(n1.first, "system"); + BOOST_CHECK_EQUAL(n1.second, "root"); + + std::pair 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 n3 = LvmUtils::split_device_name("/dev/mapper/s-r"); + BOOST_CHECK_EQUAL(n3.first, "s"); + BOOST_CHECK_EQUAL(n3.second, "r"); +}