]> git.proxmox.com Git - pve-installer.git/blame - Proxmox/Install/Config.pm
add beginnings of central config module with singleton
[pve-installer.git] / Proxmox / Install / Config.pm
CommitLineData
390889ab
TL
1package Proxmox::Install::Config;
2
3use strict;
4use warnings;
5
6use Carp;
7use JSON qw(from_json to_json);
8
9use Proxmox::Install::ISOEnv;
10
11my sub init_cfg {
12 return {
13 # installer behavior related
14 autoreboot => 1,
15
16 # disk and filesystem related
17 filesys => 'ext4',
18 hdsize => undef,
19 swapsize => undef,
20 maxroot => undef,
21 minfree => undef,
22 maxvz => undef,
23 };
24}
25
26# merge a $new hash into the current config, with $new taking precedence
27sub merge {
28 my ($new) = @_;
29
30 my $current = get();
31
32 for my $k (sort keys $new->%*) { # first check all
33 croak "unknown key '$k'" if !exists($current->{$k})
34 }
35 $current->{$_} = $new->{$_} for sort keys $new->%*; # then merge
36
37 return $current;
38}
39
40my $_cfg = undef; # NOTE: global singleton
41sub get {
42 my ($k) = @_;
43 $_cfg = init_cfg() if !defined($_cfg);
44 return defined($k) ? $_cfg->{$k} : $_cfg;
45}
46
47sub set_key {
48 my ($k, $v) = @_;
49 my $cfg = get();
50 croak "unknown key '$k'" if !exists($cfg->{$k});
51 $cfg->{$k} = $v;
52}
53
54sub parse_kernel_cmdline {
55 my $cfg = get();
56
57 my $cmdline = Proxmox::Install::RunEnv::get('kernel_cmdline');
58
59 if ($cmdline =~ m/\s(ext4|xfs)(\s.*)?$/) {
60 $cfg->{filesys} = $1;
61 }
62
63 if ($cmdline =~ m/hdsize=(\d+(\.\d+)?)[\s\n]/i) {
64 $cfg->{hdsize} = $1;
65 }
66
67 if ($cmdline =~ m/swapsize=(\d+(\.\d+)?)[\s\n]/i) {
68 $cfg->{swapsize} = $1;
69 }
70
71 if ($cmdline =~ m/maxroot=(\d+(\.\d+)?)[\s\n]/i) {
72 $cfg->{maxroot} = $1;
73 }
74
75 if ($cmdline =~ m/minfree=(\d+(\.\d+)?)[\s\n]/i) {
76 $cfg->{minfree} = $1;
77 }
78
79 my $iso_env = Proxmox::Install::ISOEnv::get();
80 if ($iso_env->{product} eq 'pve') {
81 if ($cmdline =~ m/maxvz=(\d+(\.\d+)?)[\s\n]/i) {
82 $cfg->{maxvz} = $1;
83 }
84 }
85
86 return $cfg;
87}
88
891;