Class: Pvectl::Parsers::LxcMountConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/parsers/lxc_mount_config.rb

Overview

Parses and formats LXC mount configurations for Proxmox containers.

Handles rootfs and mountpoint (mp0, mp1, …) configuration strings. Format: key=value pairs separated by commas.

Examples:

Parsing a rootfs config

config = LxcMountConfig.parse("storage=local-lvm,size=8G")
config[:storage] #=> "local-lvm"
config[:size]    #=> "8G"

Converting to Proxmox API format

LxcMountConfig.to_proxmox({ storage: "local-lvm", size: "8G" })
#=> "local-lvm:8"

LxcMountConfig.to_proxmox({ storage: "local-lvm", size: "32G", mp: "/mnt/data" })
#=> "local-lvm:32,mp=/mnt/data"

Constant Summary collapse

VALID_KEYS =

All recognized mount configuration keys.

%w[storage size mp acl backup quota replicate ro shared].freeze
REQUIRED_KEYS =

Keys that must be present in every mount configuration.

%w[storage size].freeze
OPTIONAL_FLAGS =

Optional flags appended to the Proxmox API string.

%w[acl backup quota replicate ro shared].freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Hash<Symbol, String>

Parses a comma-separated key=value mount config string into a Hash.

Examples:

LxcMountConfig.parse("storage=local-lvm,size=8G")
#=> { storage: "local-lvm", size: "8G" }

Parameters:

  • string (String)

    mount config in “key=value,key=value” format

Returns:

  • (Hash<Symbol, String>)

    parsed configuration

Raises:

  • (ArgumentError)

    if unknown keys are present or required keys are missing



41
42
43
44
45
46
47
# File 'lib/pvectl/parsers/lxc_mount_config.rb', line 41

def self.parse(string)
  pairs = string.split(",").map { |pair| pair.strip.split("=", 2).map(&:strip) }
  config = pairs.to_h { |k, v| [k.to_sym, v] }

  validate!(config)
  config
end

.to_proxmox(config) ⇒ String

Converts a parsed mount config Hash to a Proxmox API string.

The Proxmox API expects mount specifications in the format “storage:size[,flag=val]”. Size is extracted as a numeric value (without the “G” suffix).

Examples:

Rootfs config

LxcMountConfig.to_proxmox({ storage: "local-lvm", size: "8G" })
#=> "local-lvm:8"

Mountpoint with path and flags

LxcMountConfig.to_proxmox({ storage: "local-lvm", size: "32G", mp: "/mnt/data", backup: "1" })
#=> "local-lvm:32,mp=/mnt/data,backup=1"

Parameters:

  • config (Hash<Symbol, String>)

    parsed mount configuration

Returns:

  • (String)

    Proxmox API-compatible mount string



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pvectl/parsers/lxc_mount_config.rb', line 65

def self.to_proxmox(config)
  size_num = config[:size].to_s.gsub(/[^0-9]/, "")
  parts = ["#{config[:storage]}:#{size_num}"]
  parts << "mp=#{config[:mp]}" if config[:mp]

  OPTIONAL_FLAGS.each do |flag|
    parts << "#{flag}=#{config[flag.to_sym]}" if config[flag.to_sym]
  end

  parts.join(",")
end