From 1022a7c4a9ea5da9b102e53ac5886e03236eb174 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 5 Feb 2020 17:14:39 +0100 Subject: [PATCH] systemd unit name escape helpers moved to common, use them Signed-off-by: Thomas Lamprecht --- PVE/API2/Disks/Directory.pm | 30 +++--------------------------- PVE/Storage/CephFSPlugin.pm | 22 ++-------------------- debian/control | 2 +- 3 files changed, 6 insertions(+), 48 deletions(-) diff --git a/PVE/API2/Disks/Directory.pm b/PVE/API2/Disks/Directory.pm index ae9d58a..2decb89 100644 --- a/PVE/API2/Disks/Directory.pm +++ b/PVE/API2/Disks/Directory.pm @@ -7,6 +7,7 @@ use PVE::Diskmanage; use PVE::JSONSchema qw(get_standard_option); use PVE::RESTHandler; use PVE::RPCEnvironment; +use PVE::Systemd; use PVE::Tools qw(run_command trim file_set_contents file_get_contents dir_glob_foreach lock_file); use PVE::API2::Storage::Config; @@ -87,31 +88,6 @@ my $write_ini = sub { file_set_contents($filename, $content); }; -sub systemd_escape { - my ($val, $is_path) = @_; - - # NOTE: this is not complete, but enough for our needs. normally all - # characters which are not alpha-numerical, '.' or '_' would need escaping - $val =~ s/\-/\\x2d/g; - - if ($is_path) { - $val =~ s/^\///g; - $val =~ s/\/$//g; - } - $val =~ s/\//-/g; - - return $val; -} - -sub systemd_unescape { - my ($val) = @_; - - $val =~ s/-/\//g; - $val =~ s/\\x([a-fA-F0-9]{2})/chr(hex($1))/eg; - - return $val; -} - __PACKAGE__->register_method ({ name => 'index', path => '', @@ -163,7 +139,7 @@ __PACKAGE__->register_method ({ dir_glob_foreach('/etc/systemd/system', '^mnt-pve-(.+)\.mount$', sub { my ($filename, $storid) = @_; - $storid = systemd_unescape($storid); + $storid = PVE::Systemd::unescape_unit($storid); my $unitfile = "/etc/systemd/system/$filename"; my $unit = $read_ini->($unitfile); @@ -232,7 +208,7 @@ __PACKAGE__->register_method ({ my $worker = sub { my $path = "/mnt/pve/$name"; - my $mountunitname = systemd_escape($path, 1) . ".mount"; + my $mountunitname = PVE::Systemd::escape_unit($path, 1) . ".mount"; my $mountunitpath = "/etc/systemd/system/$mountunitname"; PVE::Diskmanage::locked_disk_action(sub { diff --git a/PVE/Storage/CephFSPlugin.pm b/PVE/Storage/CephFSPlugin.pm index e6add16..4aa9e96 100644 --- a/PVE/Storage/CephFSPlugin.pm +++ b/PVE/Storage/CephFSPlugin.pm @@ -11,6 +11,7 @@ use PVE::CephConfig; use PVE::JSONSchema qw(get_standard_option); use PVE::ProcFSTools; use PVE::Storage::Plugin; +use PVE::Systemd; use PVE::Tools qw(run_command file_set_contents); use base qw(PVE::Storage::Plugin); @@ -37,25 +38,6 @@ sub cephfs_is_mounted { return undef; } - -# FIXME: duplicate of api/diskmanage one, move to common helper (pve-common's -# Tools or Systemd ?) -sub systemd_escape { - my ($val, $is_path) = @_; - - # NOTE: this is not complete, but enough for our needs. normally all - # characters which are not alpha-numerical, '.' or '_' would need escaping - $val =~ s/\-/\\x2d/g; - - if ($is_path) { - $val =~ s/^\///g; - $val =~ s/\/$//g; - } - $val =~ s/\//-/g; - - return $val; -} - # FIXME: remove in PVE 7.0 where systemd is recent enough to not have those # local-fs/remote-fs dependency cycles generated for _netdev mounts... sub systemd_netmount { @@ -83,7 +65,7 @@ Type=${type} Options=${opts} EOF - my $unit_fn = systemd_escape($where, 1) . ".mount"; + my $unit_fn = PVE::Systemd::escape_unit($where, 1) . ".mount"; my $unit_path = "/run/systemd/system/$unit_fn"; my $daemon_needs_reload = -e $unit_path; diff --git a/debian/control b/debian/control index 513d2ee..d95938f 100644 --- a/debian/control +++ b/debian/control @@ -24,7 +24,7 @@ Depends: cstream, libfile-chdir-perl, libnet-dbus-perl, libpve-cluster-perl, - libpve-common-perl (>= 6.0-9), + libpve-common-perl (>= 6.0-12), librados2-perl, lvm2, nfs-common,