]>
Commit | Line | Data |
---|---|---|
390889ab TL |
1 | package Proxmox::Install::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use Carp; | |
7 | use JSON qw(from_json to_json); | |
8 | ||
e90bb4ef TL |
9 | use Proxmox::Log; |
10 | ||
390889ab | 11 | use Proxmox::Install::ISOEnv; |
7f3941d1 | 12 | use Proxmox::Sys::Net; |
390889ab | 13 | |
8e66eb8e TL |
14 | my sub parse_kernel_cmdline { |
15 | my ($cfg) = @_; | |
16 | ||
17 | my $cmdline = Proxmox::Install::RunEnv::get('kernel_cmdline'); | |
18 | ||
a02a78a8 | 19 | if ($cmdline =~ s/\b(ext4|xfs)\s?//i) { |
8e66eb8e TL |
20 | $cfg->{filesys} = $1; |
21 | } | |
22 | ||
a02a78a8 | 23 | if ($cmdline =~ s/\bhdsize=(\d+(\.\d+)?)\s?//i) { |
8e66eb8e TL |
24 | $cfg->{hdsize} = $1; |
25 | } | |
26 | ||
a02a78a8 | 27 | if ($cmdline =~ s/\bswapsize=(\d+(\.\d+)?)\s?//i) { |
8e66eb8e TL |
28 | $cfg->{swapsize} = $1; |
29 | } | |
30 | ||
a02a78a8 | 31 | if ($cmdline =~ s/\bmaxroot=(\d+(\.\d+)?)\s?//i) { |
8e66eb8e TL |
32 | $cfg->{maxroot} = $1; |
33 | } | |
34 | ||
a02a78a8 | 35 | if ($cmdline =~ s/\bminfree=(\d+(\.\d+)?)\s?//i) { |
8e66eb8e TL |
36 | $cfg->{minfree} = $1; |
37 | } | |
38 | ||
39 | my $iso_env = Proxmox::Install::ISOEnv::get(); | |
40 | if ($iso_env->{product} eq 'pve') { | |
a02a78a8 | 41 | if ($cmdline =~ s/\bmaxvz=(\d+(\.\d+)?)\s?//i) { |
8e66eb8e TL |
42 | $cfg->{maxvz} = $1; |
43 | } | |
44 | } | |
45 | ||
a02a78a8 SI |
46 | $cmdline =~ s/(?:BOOT_IMAGE|root|ramdisk_size|splash|vga)=\S+\s?//gi; |
47 | $cmdline =~ s/ro|rw|quiet|proxdebug|proxtui|nomodeset//gi; | |
48 | ||
49 | $cfg->{target_cmdline}= $cmdline; | |
4c1473ce | 50 | |
8e66eb8e TL |
51 | return $cfg; |
52 | } | |
53 | ||
54 | ||
390889ab | 55 | my sub init_cfg { |
74041d68 TL |
56 | my $iso_env = Proxmox::Install::ISOEnv::get(); |
57 | ||
58 | my $country = Proxmox::Install::RunEnv::get('country'); | |
55c2fb99 | 59 | if (defined($country) && !defined($iso_env->{locales}->{country}->{$country})) { |
74041d68 TL |
60 | log_warn("ignoring detected country '$country', invalid or unknown\n"); |
61 | $country = undef; | |
62 | } | |
63 | ||
64 | my $initial = { | |
390889ab TL |
65 | # installer behavior related |
66 | autoreboot => 1, | |
67 | ||
68 | # disk and filesystem related | |
69 | filesys => 'ext4', | |
70 | hdsize => undef, | |
71 | swapsize => undef, | |
72 | maxroot => undef, | |
73 | minfree => undef, | |
74 | maxvz => undef, | |
6edd095a TL |
75 | zfs_opts => { |
76 | ashift => 12, | |
77 | compress => 'on', | |
78 | checksum => 'on', | |
79 | copies => 1, | |
42aa2fa7 | 80 | arc_max => Proxmox::Install::RunEnv::default_zfs_arc_max(), # in MiB |
6edd095a | 81 | }, |
5bcefda0 TL |
82 | # TODO: single disk selection config |
83 | target_hd => undef, | |
84 | disk_selection => {}, | |
ec7b6356 | 85 | lvm_auto_rename => 0, |
74041d68 TL |
86 | |
87 | # locale | |
88 | country => $country, | |
2959225b | 89 | timezone => 'Europe/Vienna', |
b3bcf70e | 90 | keymap => 'en-us', |
a024147b TL |
91 | |
92 | # root credentials & details | |
93 | password => undef, | |
8924c145 | 94 | mailto => 'mail@example.invalid', |
9a5b563d | 95 | root_ssh_keys => [], |
8a7e31ce TL |
96 | |
97 | # network related | |
98 | mngmt_nic => undef, | |
44e93c11 TL |
99 | # FIXME: fix call sites and remove below, it's just an ugly relict of GTK GUI and time |
100 | # pressure on creating the single source of truth for installation config | |
8a7e31ce | 101 | mngmt_nic_id => undef, |
b7ad1ad4 TL |
102 | hostname => undef, |
103 | domain => undef, | |
7f3941d1 TL |
104 | cidr => undef, |
105 | gateway => undef, | |
106 | dns => undef, | |
a02a78a8 | 107 | target_cmdline => undef, |
390889ab | 108 | }; |
74041d68 | 109 | |
8e66eb8e TL |
110 | $initial = parse_kernel_cmdline($initial); |
111 | ||
74041d68 | 112 | return $initial; |
390889ab TL |
113 | } |
114 | ||
115 | # merge a $new hash into the current config, with $new taking precedence | |
116 | sub merge { | |
117 | my ($new) = @_; | |
118 | ||
119 | my $current = get(); | |
120 | ||
121 | for my $k (sort keys $new->%*) { # first check all | |
122 | croak "unknown key '$k'" if !exists($current->{$k}) | |
123 | } | |
124 | $current->{$_} = $new->{$_} for sort keys $new->%*; # then merge | |
125 | ||
126 | return $current; | |
127 | } | |
128 | ||
129 | my $_cfg = undef; # NOTE: global singleton | |
130 | sub get { | |
131 | my ($k) = @_; | |
132 | $_cfg = init_cfg() if !defined($_cfg); | |
133 | return defined($k) ? $_cfg->{$k} : $_cfg; | |
134 | } | |
135 | ||
136 | sub set_key { | |
137 | my ($k, $v) = @_; | |
138 | my $cfg = get(); | |
139 | croak "unknown key '$k'" if !exists($cfg->{$k}); | |
140 | $cfg->{$k} = $v; | |
141 | } | |
142 | ||
0a3ac982 TL |
143 | sub set_autoreboot { set_key('autoreboot', $_[0]); } |
144 | sub get_autoreboot { return get('autoreboot'); } | |
145 | ||
cd1a45ad TL |
146 | sub set_filesys { set_key('filesys', $_[0]); } |
147 | sub get_filesys { return get('filesys'); } | |
148 | ||
a8a14c4d TL |
149 | sub set_hdsize { set_key('hdsize', $_[0]); } |
150 | sub get_hdsize { return get('hdsize'); } | |
151 | ||
ef41b049 TL |
152 | sub set_swapsize { set_key('swapsize', $_[0]); } |
153 | sub get_swapsize { return get('swapsize'); } | |
154 | ||
b4ab3f19 TL |
155 | sub set_maxroot { set_key('maxroot', $_[0]); } |
156 | sub get_maxroot { return get('maxroot'); } | |
157 | ||
35e7bf16 TL |
158 | sub set_minfree { set_key('minfree', $_[0]); } |
159 | sub get_minfree { return get('minfree'); } | |
160 | ||
140f2e85 TL |
161 | sub set_maxvz { set_key('maxvz', $_[0]); } |
162 | sub get_maxvz { return get('maxvz'); } | |
163 | ||
6edd095a TL |
164 | sub set_zfs_opt { |
165 | my ($k, $v) = @_; | |
166 | my $zfs_opts = get('zfs_opts'); | |
167 | croak "unknown zfs opts key '$k'" if !exists($zfs_opts->{$k}); | |
168 | $zfs_opts->{$k} = $v; | |
169 | } | |
170 | sub get_zfs_opt { | |
171 | my ($k) = @_; | |
172 | my $zfs_opts = get('zfs_opts'); | |
173 | return defined($k) ? $zfs_opts->{$k} : $zfs_opts; | |
174 | } | |
175 | ||
5bcefda0 TL |
176 | sub set_target_hd { set_key('target_hd', $_[0]); } |
177 | sub get_target_hd { return get('target_hd'); } | |
178 | ||
179 | sub set_disk_selection { | |
180 | my ($id, $v) = @_; | |
181 | my $disk_selection = get('disk_selection'); | |
182 | $disk_selection->{$id} = $v; | |
183 | } | |
184 | sub get_disk_selection { | |
185 | my ($id) = @_; | |
186 | my $disk_selection = get('disk_selection'); | |
187 | return defined($id) ? $disk_selection->{$id} : $disk_selection; | |
188 | } | |
189 | ||
74041d68 TL |
190 | sub set_country { set_key('country', $_[0]); } |
191 | sub get_country { return get('country'); } | |
192 | ||
2959225b TL |
193 | sub set_timezone { set_key('timezone', $_[0]); } |
194 | sub get_timezone { return get('timezone'); } | |
195 | ||
b3bcf70e TL |
196 | sub set_keymap { set_key('keymap', $_[0]); } |
197 | sub get_keymap { return get('keymap'); } | |
198 | ||
a024147b TL |
199 | sub set_password { set_key('password', $_[0]); } |
200 | sub get_password { return get('password'); } | |
201 | ||
8924c145 TL |
202 | sub set_mailto { set_key('mailto', $_[0]); } |
203 | sub get_mailto { return get('mailto'); } | |
204 | ||
9a5b563d CH |
205 | sub set_root_ssh_keys { set_key('root_ssh_keys', $_[0]); } |
206 | sub get_root_ssh_keys { return get('root_ssh_keys'); } | |
207 | ||
8a7e31ce TL |
208 | sub set_mngmt_nic { set_key('mngmt_nic', $_[0]); } |
209 | sub get_mngmt_nic { return get('mngmt_nic'); } | |
210 | ||
211 | sub set_mngmt_nic_id { set_key('mngmt_nic_id', $_[0]); } | |
212 | sub get_mngmt_nic_id { return get('mngmt_nic_id'); } | |
213 | ||
e02f38dc TL |
214 | sub set_hostname { set_key('hostname', $_[0]); } |
215 | sub get_hostname { return get('hostname'); } | |
216 | ||
217 | sub set_domain { set_key('domain', $_[0]); } | |
218 | sub get_domain { return get('domain'); } | |
219 | ||
220 | sub get_fqdn { # virtual config | |
221 | my ($hostname, $domain) = (get('hostname'), get('domain')); | |
222 | return defined($hostname) && defined($domain) ? "${hostname}.${domain}" : undef; | |
223 | } | |
224 | ||
7f3941d1 TL |
225 | sub set_cidr { set_key('cidr', $_[0]); } |
226 | sub get_cidr { return get('cidr'); } | |
227 | ||
228 | sub get_ip_addr { #'virtual config | |
229 | my $cidr = get('cidr') // return; | |
230 | my ($ip, $mask) = split('/', $cidr); | |
231 | return $ip; | |
232 | } | |
233 | sub get_ip_version { # virtual config | |
234 | my $ip = get_ip_addr() // return; | |
235 | return Proxmox::Sys::Net::parse_ip_address($ip); | |
236 | } | |
237 | ||
238 | sub set_gateway { set_key('gateway', $_[0]); } | |
239 | sub get_gateway { return get('gateway'); } | |
240 | ||
241 | sub set_dns { set_key('dns', $_[0]); } | |
242 | sub get_dns { return get('dns'); } | |
243 | ||
a02a78a8 SI |
244 | sub set_target_cmdline { set_key('target_cmdline', $_[0]); } |
245 | sub get_target_cmdline { return get('target_cmdline'); } | |
4c1473ce | 246 | |
ec7b6356 CH |
247 | sub set_lvm_auto_rename { set_key('lvm_auto_rename', $_[0]); } |
248 | sub get_lvm_auto_rename { return get('lvm_auto_rename'); } | |
4c1473ce | 249 | |
390889ab | 250 | 1; |