Skip to content

Commit

Permalink
Merge pull request #1174 from ancorgs/tmpfs_wrapper
Browse files Browse the repository at this point in the history
Initial wrapper for Storage::Tmpfs
  • Loading branch information
ancorgs committed Nov 27, 2020
2 parents 4c4c044 + 7f059d5 commit e80d08e
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package/yast2-storage-ng.changes
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Thu Nov 26 15:29:54 UTC 2020 - Ancor Gonzalez Sosa <ancor@suse.com>

- Y2Storage wrapper for the new Tmpfs class from libstorage-ng
(related to jsc#SLE-11308)

-------------------------------------------------------------------
Wed Nov 25 12:13:14 UTC 2020 - José Iván López González <jlopez@suse.com>

Expand Down
6 changes: 6 additions & 0 deletions src/lib/y2storage/devicegraph.rb
Expand Up @@ -28,6 +28,7 @@
require "y2storage/fake_device_factory"
require "y2storage/filesystems/base"
require "y2storage/filesystems/blk_filesystem"
require "y2storage/filesystems/tmpfs"
require "y2storage/filesystems/nfs"
require "y2storage/lvm_lv"
require "y2storage/lvm_vg"
Expand Down Expand Up @@ -303,6 +304,11 @@ def multidevice_btrfs_filesystems
btrfs_filesystems.select(&:multidevice?)
end

# @return [Array<Filesystem::Tmpfs>]
def tmp_filesystems
Filesystems::Tmpfs.all(self)
end

# @return [Array<Filesystem::Nfs>]
def nfs_mounts
Filesystems::Nfs.all(self)
Expand Down
1 change: 1 addition & 0 deletions src/lib/y2storage/filesystems.rb
Expand Up @@ -3,5 +3,6 @@
require "y2storage/filesystems/btrfs"
require "y2storage/filesystems/type"
require "y2storage/filesystems/mount_by_type"
require "y2storage/filesystems/tmpfs"
require "y2storage/filesystems/nfs"
require "y2storage/filesystems/legacy_nfs"
2 changes: 1 addition & 1 deletion src/lib/y2storage/filesystems/base.rb
Expand Up @@ -34,7 +34,7 @@ module Filesystems
# This is a wrapper for Storage::Filesystem
class Base < Mountable
wrap_class Storage::Filesystem,
downcast_to: ["Filesystems::BlkFilesystem", "Filesystems::Nfs"]
downcast_to: ["Filesystems::BlkFilesystem", "Filesystems::Nfs", "Filesystems::Tmpfs"]

# @!method self.all(devicegraph)
# @param devicegraph [Devicegraph]
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2storage/filesystems/nfs.rb
Expand Up @@ -28,7 +28,7 @@ module Filesystems
# The class does not provide functions to change the server or path since
# that would create a completely different filesystem.
#
# This a wrapper for Storage::Nfs
# This is a wrapper for Storage::Nfs
class Nfs < Base
wrap_class Storage::Nfs

Expand Down
89 changes: 89 additions & 0 deletions src/lib/y2storage/filesystems/tmpfs.rb
@@ -0,0 +1,89 @@
# 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 SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2storage/storage_class_wrapper"
require "y2storage/filesystems/base"

module Y2Storage
module Filesystems
# Class to represent a tmpfs.
#
# The Tmpfs object should always have MountPoint as child. So the Tmpfs
# must be deleted whenever the MountPoint is removed.
#
# This is a wrapper for Storage::Tmpfs
class Tmpfs < Base
wrap_class Storage::Tmpfs

# @!method self.create(devicegraph)
# @param devicegraph [Devicegraph]
# @return [Tmpfs]
storage_class_forward :create, as: "Filesystems::Tmpfs"

# @!method self.all(devicegraph)
# @param devicegraph [Devicegraph]
# @return [Array<Tmpfs>] all the tmp filesystems in the given devicegraph
storage_class_forward :all, as: "Filesystems::Tmpfs"

# Size of the filesystem, given by the corresponding mount option
#
# @return [DiskSize] zero if the size couldn't be determined
def size
DiskSize.parse(size_from_mount_options, legacy_units: true)
rescue StandardError
DiskSize.zero
end

protected

# Name of the mount option used to determine the size of the tmpfs
# @return [String]
SIZE_OPT = "size".freeze
private_constant :SIZE_OPT

# @see Device#is?
def types_for_is
super << :tmpfs
end

# Mount options used to define the size of the temporary filesystem
#
# For a sane tmpfs, this should be an array with just one element. But it could be empty
# if the size is not defined or could contain several entries if the "size=" argument is
# specified more than once.
#
# @return [Array<String>]
def size_mount_options
return [] unless mount_point

mount_point.mount_options.select { |opt| opt =~ /#{SIZE_OPT}=/i }
end

# String representation of the filesystem size, as specified in the mount options
#
# @return [String, nil] nil if no size is given in the mount options
def size_from_mount_options
option = size_mount_options.last
return nil unless option

option.split("=").last
end
end
end
end
23 changes: 23 additions & 0 deletions test/y2storage/devicegraph_test.rb
Expand Up @@ -187,6 +187,29 @@ def with_sda2_deleted(initial_graph)
end
end

describe "#tmp_filesystems" do
before do
Y2Storage::StorageManager.create_test_instance

tmp1 = Y2Storage::Filesystems::Tmpfs.create(fake_devicegraph)
tmp1.mount_path = "/mnt/tmp1"

tmp2 = Y2Storage::Filesystems::Tmpfs.create(fake_devicegraph)
tmp2.mount_path = "/mnt/tmp2"
end

subject(:list) { fake_devicegraph.tmp_filesystems }

it "returns an array of tmp filesystems" do
expect(list).to be_a Array
expect(list.map { |i| i.is?(:tmpfs) }).to all(be(true))
end

it "finds all the tmp filesystems from the devicegraph" do
expect(list.map(&:mount_path)).to contain_exactly("/mnt/tmp1", "/mnt/tmp2")
end
end

describe "#filesystems" do
before { fake_scenario("complex-lvm-encrypt") }
subject(:list) { fake_devicegraph.filesystems }
Expand Down
47 changes: 47 additions & 0 deletions test/y2storage/filesystems/tmpfs_test.rb
@@ -0,0 +1,47 @@
#!/usr/bin/env rspec
# 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 SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../spec_helper"
require "y2storage"

describe Y2Storage::Filesystems::Tmpfs do
before do
Y2Storage::StorageManager.create_test_instance
end
subject(:filesystem) { described_class.create(fake_devicegraph) }

describe "#size" do
it "returns the size specified as mount option" do
filesystem.mount_path = "/tmp"
filesystem.mount_point.mount_options = ["rw", "size=128M", "noatime"]

expect(filesystem.size).to eq Y2Storage::DiskSize.MiB(128)
end

it "returns zero if there is no mount point" do
expect(filesystem.size).to eq Y2Storage::DiskSize.zero
end

it "returns zero if there are no mount options" do
filesystem.mount_path = "/tmp"
expect(filesystem.size).to eq Y2Storage::DiskSize.zero
end
end
end

0 comments on commit e80d08e

Please sign in to comment.