]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
1 package PVE
::LXC
::Setup
;
8 use PVE
::LXC
::Setup
::Debian
;
9 use PVE
::LXC
::Setup
::Ubuntu
;
10 use PVE
::LXC
::Setup
::Redhat
;
11 use PVE
::LXC
::Setup
::Fedora
;
12 use PVE
::LXC
::Setup
::SUSE
;
13 use PVE
::LXC
::Setup
::ArchLinux
;
14 use PVE
::LXC
::Setup
::Alpine
;
17 debian
=> 'PVE::LXC::Setup::Debian',
18 ubuntu
=> 'PVE::LXC::Setup::Ubuntu',
19 redhat
=> 'PVE::LXC::Setup::Redhat',
20 fedora
=> 'PVE::LXC::Setup::Fedora',
21 opensuse
=> 'PVE::LXC::Setup::SUSE',
22 archlinux
=> 'PVE::LXC::Setup::ArchLinux',
23 alpine
=> 'PVE::LXC::Setup::Alpine',
26 my $autodetect_type = sub {
29 my $lsb_fn = "$rootdir/etc/lsb-release";
31 my $data = PVE
::Tools
::file_get_contents
($lsb_fn);
32 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
37 if (-f
"$rootdir/etc/debian_version") {
39 } elsif (-f
"$rootdir/etc/SuSE-brand" || -f
"$rootdir/etc/SuSE-release") {
41 } elsif (-f
"$rootdir/etc/fedora-release") {
43 } elsif (-f
"$rootdir/etc/redhat-release") {
45 } elsif (-f
"$rootdir/etc/arch-release") {
47 } elsif (-f
"$rootdir/etc/alpine-release") {
50 die "unable to detect OS distribution\n";
54 my ($class, $conf, $rootdir, $type) = @_;
56 die "no root directory\n" if !$rootdir || $rootdir eq '/';
58 my $self = bless { conf
=> $conf, rootdir
=> $rootdir};
60 if ($conf->{ostype
} && $conf->{ostype
} eq 'unmanaged') {
62 } elsif (!defined($type)) {
63 # try to autodetect type
64 $type = &$autodetect_type($rootdir);
65 my $expected_type = $conf->{ostype
} || $type;
67 die "got unexpected ostype ($type != $expected_type)\n"
68 if $type ne $expected_type;
71 my $plugin_class = $plugins->{$type} ||
72 "no such OS type '$type'\n";
74 my $plugin = $plugin_class->new($conf, $rootdir);
75 $self->{plugin
} = $plugin;
76 $self->{in_chroot
} = 0;
78 # Cache some host files we need access to:
79 $plugin->{host_resolv_conf
} = PVE
::INotify
::read_file
('resolvconf');
81 # pass on user namespace information:
82 my ($id_map, $rootuid, $rootgid) = PVE
::LXC
::parse_id_maps
($conf);
84 $plugin->{id_map
} = $id_map;
85 $plugin->{rootuid
} = $rootuid;
86 $plugin->{rootgid
} = $rootgid;
93 my ($self, $sub) = @_;
95 die "internal error" if !$self->{plugin
};
98 return $sub->() if $self->{in_chroot
};
100 my $rootdir = $self->{rootdir
};
101 if (!-d
"$rootdir/dev" && !mkdir("$rootdir/dev")) {
102 die "failed to create temporary /dev directory: $!\n";
106 die "fork failed: $!\n" if !defined($child);
109 # avoid recursive forks
110 $self->{in_chroot
} = 1;
111 $self->{plugin
}->{in_chroot
} = 1;
113 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
114 chdir('/') or die "failed to change to root directory\n";
123 while (waitpid($child, 0) != $child) {}
125 my $method = (caller(1))[3];
126 die "error in setup task $method\n";
133 return if !$self->{plugin
}; # unmanaged
136 $self->{plugin
}->template_fixup($self->{conf
});
138 $self->protected_call($code);
144 return if !$self->{plugin
}; # unmanaged
147 $self->{plugin
}->setup_network($self->{conf
});
149 $self->protected_call($code);
155 return if !$self->{plugin
}; # unmanaged
158 $self->{plugin
}->set_hostname($self->{conf
});
160 $self->protected_call($code);
166 return if !$self->{plugin
}; # unmanaged
169 $self->{plugin
}->set_dns($self->{conf
});
171 $self->protected_call($code);
177 return if !$self->{plugin
}; # unmanaged
180 $self->{plugin
}->setup_init($self->{conf
});
182 $self->protected_call($code);
185 sub set_user_password
{
186 my ($self, $user, $pw) = @_;
188 return if !$self->{plugin
}; # unmanaged
191 $self->{plugin
}->set_user_password($self->{conf
}, $user, $pw);
193 $self->protected_call($code);
196 sub rewrite_ssh_host_keys
{
199 return if !$self->{plugin
}; # unmanaged
201 my $conf = $self->{conf
};
202 my $plugin = $self->{plugin
};
203 my $rootdir = $self->{rootdir
};
205 return if ! -d
"$rootdir/etc/ssh";
208 rsa1
=> 'ssh_host_key',
209 rsa
=> 'ssh_host_rsa_key',
210 dsa
=> 'ssh_host_dsa_key',
211 ecdsa
=> 'ssh_host_ecdsa_key',
212 ed25519
=> 'ssh_host_ed25519_key',
215 my $hostname = $conf->{hostname
} || 'localhost';
216 $hostname =~ s/\..*$//;
218 # Create temporary keys in /tmp on the host
221 foreach my $keytype (keys %$keynames) {
222 my $basename = $keynames->{$keytype};
223 my $file = "/tmp/$$.$basename";
224 print "Creating SSH host key '$basename' - this may take some time ...\n";
225 my $cmd = ['ssh-keygen', '-q', '-f', $file, '-t', $keytype,
226 '-N', '', '-C', "root\@$hostname"];
227 PVE
::Tools
::run_command
($cmd);
228 $keyfiles->{"/etc/ssh/$basename"} = [PVE
::Tools
::file_get_contents
($file), 0600];
229 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE
::Tools
::file_get_contents
("$file.pub"), 0644];
234 # Write keys out in a protected call
237 foreach my $file (keys %$keyfiles) {
238 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
241 $self->protected_call($code);
247 return if !$self->{plugin
}; # unmanaged
250 # Create /fastboot to skip run fsck
251 $self->{plugin
}->ct_file_set_contents('/fastboot', '');
253 $self->{plugin
}->pre_start_hook($self->{conf
});
255 $self->protected_call($code);
258 sub post_create_hook
{
259 my ($self, $root_password) = @_;
261 return if !$self->{plugin
}; # unmanaged
264 $self->{plugin
}->post_create_hook($self->{conf
}, $root_password);
266 $self->protected_call($code);
267 $self->rewrite_ssh_host_keys();