Class: OsCtld::TrashBin
- Inherits:
-
Object
- Object
- OsCtld::TrashBin
- Includes:
- OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
- Defined in:
- lib/osctld/trash_bin.rb
Instance Attribute Summary collapse
- #pool ⇒ Pool readonly
Class Method Summary collapse
Instance Method Summary collapse
- #add_dataset(dataset) ⇒ Object
-
#initialize(pool) ⇒ TrashBin
constructor
A new instance of TrashBin.
- #log_type ⇒ Object
- #prune ⇒ Object
- #prune_datasets ⇒ Object protected
- #run_gc ⇒ Object protected
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #trash_path(dataset) ⇒ Object protected
Constructor Details
#initialize(pool) ⇒ TrashBin
Returns a new instance of TrashBin.
19 20 21 22 23 24 |
# File 'lib/osctld/trash_bin.rb', line 19 def initialize(pool) @pool = pool @trash_dataset = OsCtl::Lib::Zfs::Dataset.new(pool.trash_bin_ds) @queue = OsCtl::Lib::Queue.new @stop = false end |
Instance Attribute Details
Class Method Details
.add_dataset(pool, dataset) ⇒ Object
8 9 10 |
# File 'lib/osctld/trash_bin.rb', line 8 def self.add_dataset(pool, dataset) pool.trash_bin.add_dataset(dataset) end |
Instance Method Details
#add_dataset(dataset) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/osctld/trash_bin.rb', line 49 def add_dataset(dataset) # Set canmount on the dataset and umount it with and all its descendants dataset.list.reverse_each do |ds| zfs(:set, 'canmount=noauto', ds.name) # We ignore errors, because the dataset may belong to # a hung container, etc. begin zfs(:unmount, nil, ds.name) rescue SystemCommandFailed => e unless e.output.include?('not currently mounted') log(:warn, "Unable to unmount #{ds}: #{e.}") end end end # Move the dataset to trash trash_ds, t = trash_path(dataset) zfs(:rename, nil, "#{dataset} #{trash_ds}") # Set metadata properties = { original_name: dataset.name, trashed_at: t.to_i # see check_property below when changing properties } zfs( :set, .map { |k, v| "org.vpsadminos.osctl.trash-bin:#{k}=#{v}" }.join(' '), trash_ds ) end |
#log_type ⇒ Object
82 83 84 |
# File 'lib/osctld/trash_bin.rb', line 82 def log_type "#{pool.name}:trash" end |
#prune ⇒ Object
44 45 46 |
# File 'lib/osctld/trash_bin.rb', line 44 def prune @queue << :prune end |
#prune_datasets ⇒ Object (protected)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/osctld/trash_bin.rb', line 98 def prune_datasets txg_timeout = File.read('/sys/module/zfs/parameters/zfs_txg_timeout').strip.to_i # Check the property that is set when the dataset is moved to trash check_property = 'org.vpsadminos.osctl.trash-bin:trashed_at' @trash_dataset.list( depth: 1, include_self: false, properties: %W[name #{check_property}] ).each do |ds| break if @stop unless ds.name.start_with?("#{@trash_dataset}/") raise "programming error: refusing to destroy dataset #{ds.name.inspect}" end if ds.properties[check_property] == '-' || ds.properties[check_property].to_i <= 0 log(:debug, "Skipping #{ds} as it is still being trashed") next end log(:info, "Destroying #{ds}") begin ds.destroy!(recursive: true) rescue SystemCommandFailed => e log(:warn, "Unable to destroy #{ds}: #{e.}") next end break if @stop sleep([txg_timeout, 5].max) end end |
#run_gc ⇒ Object (protected)
88 89 90 91 92 93 94 95 96 |
# File 'lib/osctld/trash_bin.rb', line 88 def run_gc loop do v = @queue.pop(timeout: Daemon.get.config.trash_bin.prune_interval) return if v == :stop log(:info, 'Pruning') prune_datasets end end |
#start ⇒ Object
26 27 28 29 |
# File 'lib/osctld/trash_bin.rb', line 26 def start @stop = false @thread = Thread.new { run_gc } end |
#started? ⇒ Boolean
40 41 42 |
# File 'lib/osctld/trash_bin.rb', line 40 def started? !@stop end |
#stop ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/osctld/trash_bin.rb', line 31 def stop return unless @thread @stop = true @queue << :stop @thread.join @thread = nil end |
#trash_path(dataset) ⇒ Object (protected)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/osctld/trash_bin.rb', line 135 def trash_path(dataset) t = Time.now path = File.join( @trash_dataset.name, [ dataset.name.split('/')[1..].join('-'), t.to_i, SecureRandom.hex(3) ].join('.') ) [path, t] end |