]> git.proxmox.com Git - dab.git/blobdiff - DAB.pm
debian 11: use short ostype
[dab.git] / DAB.pm
diff --git a/DAB.pm b/DAB.pm
index 9cd9bf7ecf7b2b0b61b02a4a818f684cbe714d93..26db7ad9eb3b97263f3de6b5ab617e2a87222a30 100644 (file)
--- a/DAB.pm
+++ b/DAB.pm
@@ -9,6 +9,8 @@ use IO::Select;
 use IPC::Open2;
 use IPC::Open3;
 use POSIX qw (LONG_MAX);
+use UUID;
+use Cwd;
 
 # fixme: lock container ?
 
@@ -40,6 +42,8 @@ mynetworks = 127.0.0.0/8
 inet_interfaces = loopback-only
 recipient_delimiter = +
 
+compatibility_level = 2
+
 EOD
 
 # produce apt compatible filenames (/var/lib/apt/lists)
@@ -53,6 +57,140 @@ sub __url_to_filename {
     return $url;
 }
 
+# defaults:
+#  origin: debian
+my $supported_suites = {
+    'bullseye' => {
+       ostype => "debian-11",
+    },
+    'buster' => {
+       ostype => "debian-10",
+    },
+    'stretch' => {
+       ostype => "debian-9.0",
+    },
+    'jessie' => {
+       ostype => "debian-8.0",
+    },
+    'wheezy' => {
+       ostype => "debian-7.0",
+    },
+    'squeeze' => {
+       ostype => "debian-6.0",
+    },
+    'lenny' => {
+       ostype => "debian-5.0",
+    },
+    'etch' => {
+       ostype => "debian-4.0",
+    },
+
+# DEVUAN
+    'devuan-jessie' => {
+       suite => 'jessie',
+       ostype => "devuan-1.0",
+    },
+    'devuan-ascii' => {
+       suite => 'ascii',
+       ostype => "devuan-2.0",
+    },
+    'ascii' => {
+       ostype => "devuan-2.0",
+    },
+    'beowulf' => {
+       ostype => "devuan-3.0",
+    },
+    'chimaera' => {
+       ostype => "devuan-4.0",
+    },
+
+# UBUNTU
+    'hardy' => {
+       ostype => "ubuntu-8.04",
+       origin => 'ubuntu',
+    },
+    'intrepid' => {
+       ostype => "ubuntu-8.10",
+       origin => 'ubuntu',
+    },
+    'jaunty' => {
+       ostype => "ubuntu-9.04",
+       origin => 'ubuntu',
+    },
+    'precise' => {
+       ostype => "ubuntu-12.04",
+       origin => 'ubuntu',
+    },
+    'trusty' => {
+       ostype => "ubuntu-14.04",
+       origin => 'ubuntu',
+    },
+    'vivid' => {
+       ostype => "ubuntu-15.04",
+       origin => 'ubuntu',
+    },
+    'wily' => {
+       ostype => "ubuntu-15.10",
+       origin => 'ubuntu',
+    },
+    'xenial' => {
+       ostype => "ubuntu-16.04",
+       origin => 'ubuntu',
+    },
+    'yakkety' => {
+       ostype => "ubuntu-16.10",
+       origin => 'ubuntu',
+    },
+    'zesty' => {
+       ostype => "ubuntu-17.04",
+       origin => 'ubuntu',
+    },
+    'artful' => {
+       ostype => "ubuntu-17.10",
+       origin => 'ubuntu',
+    },
+    'bionic' => {
+       ostype => "ubuntu-18.04",
+       origin => 'ubuntu',
+    },
+    'cosmic' => {
+       ostype => "ubuntu-18.10",
+       origin => 'ubuntu',
+    },
+    'disco' => {
+       ostype => "ubuntu-19.04",
+       origin => 'ubuntu',
+    },
+    'eoan' => {
+       ostype => "ubuntu-19.10",
+       origin => 'ubuntu',
+    },
+    'focal' => {
+       ostype => "ubuntu-20.04",
+       origin => 'ubuntu',
+    },
+    'groovy' => {
+       ostype => "ubuntu-20.10",
+       origin => 'ubuntu',
+    },
+    'hirsute' => {
+       ostype => "ubuntu-21.04",
+       origin => 'ubuntu',
+    },
+};
+
+sub get_suite_info {
+    my ($suite) = @_;
+
+    my $suiteinfo = $supported_suites->{$suite} || die "unsupported suite '$suite'!\n";
+
+    # set defaults
+    $suiteinfo->{origin} //= 'debian';
+    $suiteinfo->{suite} //= $suite;
+
+    return $suiteinfo;
+}
+
 sub download {
     my ($self, $url, $path) = @_;
 
@@ -228,73 +366,22 @@ sub writelog {
 }
 
 sub __sample_config {
-    my ($self, $mem) = @_;
-
-    my $max = LONG_MAX;
-    my $nolimit = "\"$max:$max\"";
-
-    my $defaults = {
-       128 => {},
-       256 => {},
-       512 => {},
-       1024 => {},
-       2048 => {},
-    };
-
-    die "unknown memory size" if !defined ($defaults->{$mem});
+    my ($self) = @_;
 
     my $data = '';
+    my $arch = $self->{config}->{architecture};
 
-    $data .= "# DAB default config for ${mem}MB RAM\n\n";
-
-    $data .= "ONBOOT=\"no\"\n";
-
-    $data .= "\n# Primary parameters\n";
-    $data .= "NUMPROC=\"1024:1024\"\n";
-    $data .= "NUMTCPSOCK=$nolimit\n";
-    $data .= "NUMOTHERSOCK=$nolimit\n";
-
-    my $vmguarpages = int ($mem*1024/4);
-    $data .= "VMGUARPAGES=\"$vmguarpages:$max\"\n";
-
-    $data .= "\n# Secondary parameters\n";
-
-    $data .= "KMEMSIZE=$nolimit\n";
-
-    my $privmax = int ($vmguarpages*1.1);
-    $privmax = $vmguarpages + 12500 if ($privmax-$vmguarpages) > 12500;
-    $data .= "OOMGUARPAGES=\"$vmguarpages:$max\"\n";
-    $data .= "PRIVVMPAGES=\"$vmguarpages:$privmax\"\n";
-
-    $data .= "TCPSNDBUF=$nolimit\n";
-    $data .= "TCPRCVBUF=$nolimit\n";
-    $data .= "OTHERSOCKBUF=$nolimit\n";
-    $data .= "DGRAMRCVBUF=$nolimit\n";
-
-    $data .= "\n# Auxiliary parameters\n";
-    $data .= "NUMFILE=$nolimit\n";
-    $data .= "NUMFLOCK=$nolimit\n";
-    $data .= "NUMPTY=\"255:255\"\n";
-    $data .= "NUMSIGINFO=\"1024:1024\"\n";
-    $data .= "DCACHESIZE=$nolimit\n";
-    $data .= "LOCKEDPAGES=$nolimit\n";
-    $data .= "SHMPAGES=$nolimit\n";
-    $data .= "NUMIPTENT=$nolimit\n";
-    $data .= "PHYSPAGES=\"0:$max\"\n";
-
-    $data .= "\n# Disk quota parameters\n";
-    $data .= "DISK_QUOTA=\"no\"\n";
-    $data .= "DISKSPACE=$nolimit\n";
-    $data .= "DISKINODES=$nolimit\n";
-    $data .= "QUOTATIME=\"0\"\n";
-    $data .= "QUOTAUGIDLIMIT=\"0\"\n";
-
-    $data .= "\n# CPU fair sheduler parameter\n";
-    $data .= "CPUUNITS=\"1000\"\n\n";
+    my $ostype = $self->{config}->{ostype};
 
-    $data .= "\n# Template parameter\n";
-    $data .= "OSTEMPLATE=\"$self->{targetname}\"\n";
-    $data .= "HOSTNAME=\"localhost\"\n";
+    if ($ostype =~ m/^de(bi|vu)an-/) {
+       $data .= "lxc.include = /usr/share/lxc/config/debian.common.conf\n";
+    } elsif ($ostype =~ m/^ubuntu-/) {
+       $data .= "lxc.include = /usr/share/lxc/config/ubuntu.common.conf\n";
+    } else {
+       die "unknown os type '$ostype'\n";
+    }
+    $data .= "lxc.uts.name = localhost\n";
+    $data .= "lxc.rootfs.path = $self->{rootfs}\n";
     
     return $data;
 }
@@ -309,48 +396,39 @@ sub __allocate_ve {
        close ($fd);
     }
 
-    my $cfgdir = "/etc/pve/openvz";
+
+    $self->{working_dir} = getcwd;
+    $self->{veconffile} = "$self->{working_dir}/config";
+    $self->{rootfs} = "$self->{working_dir}/rootfs";
 
     if ($cid) {
        $self->{veid} = $cid;
-       $self->{veconffile} = "$cfgdir/$cid.conf";
        return $cid;
     }
 
-    my $cdata = $self->__sample_config (1024);
-
-    my $veid;
-    my $startid = 90000;
-    for (my $id = $startid; $id < ($startid + 100); $id++) {
-
-       my $tmpfn = "$cfgdir/$id.conf.tmp$$";
-       my $target = "$cfgdir/$id.conf";
-
-       next if -f $target;
-
-       my $fh = IO::File->new ($target, O_WRONLY | O_CREAT | O_EXCL, 0644);
-
-       next if !$fh;
-
-       print $fh $cdata;
-       close ($fh);
-       $veid = $id;
-       last;
-    }
-
-    die "unable to allocate VE\n" if !$veid;
+    my $uuid;
+    my $uuid_str;
+    UUID::generate($uuid);
+    UUID::unparse($uuid, $uuid_str);
+    $self->{veid} = $uuid_str;
 
     my $fd = IO::File->new (">.veid") ||
        die "unable to write '.veid'\n";
-    print $fd "$veid\n";
+    print $fd "$self->{veid}\n";
     close ($fd);
 
-    $self->logmsg ("allocated VE $veid\n");
+    my $cdata = $self->__sample_config();
 
-    $self->{veid} = $veid;
-    $self->{veconffile} = "$cfgdir/$veid.conf";
+    my $fh = IO::File->new ($self->{veconffile}, O_WRONLY|O_CREAT|O_EXCL) ||
+       die "unable to write lxc config file '$self->{veconffile}' - $!";
+    print $fh $cdata;
+    close ($fh);
 
-    return $veid;
+    mkdir $self->{rootfs} || die "unable to create rootfs - $!";
+
+    $self->logmsg ("allocated VE $self->{veid}\n");
+
+    return $self->{veid};
 }
 
 sub new {
@@ -377,25 +455,10 @@ sub new {
        if $arch !~ m/^(i386|amd64)$/;
 
     my $suite = $config->{suite} || die "no 'suite' specified\n";
-    if ($suite eq 'jessie') {
-         $config->{ostype} = "debian-8.0";
-    } elsif ($suite eq 'wheezy') {
-         $config->{ostype} = "debian-7.0";
-    } elsif ($suite eq 'squeeze') {
-       $config->{ostype} = "debian-6.0";
-    } elsif ($suite eq 'lenny') { 
-       $config->{ostype} = "debian-5.0";
-    } elsif ($suite eq 'etch') { 
-       $config->{ostype} = "debian-4.0";
-    } elsif ($suite eq 'hardy') { 
-       $config->{ostype} = "ubuntu-8.04";
-    } elsif ($suite eq 'intrepid') { 
-       $config->{ostype} = "ubuntu-8.10";
-    } elsif ($suite eq 'jaunty') { 
-       $config->{ostype} = "ubuntu-9.04";
-    } else {
-       die "unsupported debian suite '$suite'\n";
-    }
+
+    my $suiteinfo = get_suite_info($suite);
+    $suite = $suiteinfo->{suite};
+    $config->{ostype} = $suiteinfo->{ostype};
 
     my $name = $config->{name} || die "no 'name' specified\n";
 
@@ -416,17 +479,34 @@ sub new {
     }
 
     if (!$config->{source}) {
-       if ($suite eq 'etch' || $suite eq 'lenny' || $suite eq 'squeeze' || 
-           $suite eq 'wheezy' || $suite eq 'jessie' ) {
-           push @{$config->{source}}, "http://ftp.debian.org/debian SUITE main contrib";
-           push @{$config->{source}}, "http://ftp.debian.org/debian SUITE-updates main contrib"
-               if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie');
-           push @{$config->{source}}, "http://security.debian.org SUITE/updates main contrib";
-       } elsif ($suite eq 'hardy' || $suite eq 'intrepid' || $suite eq 'jaunty') {
+       if (lc($suiteinfo->{origin}) eq 'debian') {
+           if ($suite eq 'etch' || $suite eq 'lenny') {
+               push @{$config->{source}}, (
+                   'http://ftp.debian.org/debian SUITE main contrib',
+                   'http://security.debian.org SUITE/updates main contrib',
+               );
+           } elsif ($suite eq 'bullseye') {
+               push @{$config->{source}}, (
+                   "http://ftp.debian.org/debian SUITE main contrib",
+                   "http://ftp.debian.org/debian SUITE-updates main contrib",
+                   "http://security.debian.org SUITE-security main contrib",
+               );
+           } else {
+               push @{$config->{source}}, (
+                   "http://ftp.debian.org/debian SUITE main contrib",
+                   "http://ftp.debian.org/debian SUITE-updates main contrib",
+                   "http://security.debian.org SUITE/updates main contrib",
+               );
+           }
+       } elsif (lc($suiteinfo->{origin}) eq 'ubuntu') {
            my $comp = "main restricted universe multiverse";
-           push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE $comp"; 
-           push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-updates $comp"; 
-           push @{$config->{source}}, "http://archive.ubuntu.com/ubuntu SUITE-security $comp";
+           push @{$config->{source}}, (
+               "http://archive.ubuntu.com/ubuntu SUITE $comp",
+               "http://archive.ubuntu.com/ubuntu SUITE-updates $comp",
+               "http://archive.ubuntu.com/ubuntu SUITE-security $comp",
+           );
+       } else {
+           die "implement me";
        }
     }
 
@@ -486,7 +566,20 @@ sub new {
 
     # ubuntu has too many dependencies on udev, so
     # we cannot exclude it (instead we disable udevd)
-    if ($suite eq 'hardy') {
+
+    if ($suite eq 'vivid' || $suite eq 'wily' || $suite eq 'xenial' ||
+       $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
+       $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
+       $suite eq 'eoan' || $suite eq 'focal' || $suite eq 'groovy'
+       || $suite eq 'hirsute'
+    ) {
+       push @$incl, 'isc-dhcp-client';
+       push @$excl, qw(libmodule-build-perl);
+    } elsif ($suite eq 'trusty') {
+       push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+);
+   } elsif ($suite eq 'precise') {
+       push @$excl, qw(systemd systemd-services libpam-systemd libsystemd-daemon0 memtest86+ ubuntu-standard);
+    } elsif ($suite eq 'hardy') {
        push @$excl, qw(kbd);
        push @$excl, qw(apparmor apparmor-utils ntfs-3g
                        friendly-recovery);
@@ -498,6 +591,9 @@ sub new {
        push @$incl, 'libperl4-corelibs-perl'; # to make lsof happy
        push @$excl, qw(systemd systemd-sysv udev module-init-tools pciutils hdparm 
                        memtest86+ parted);
+    } elsif ($suite eq 'stretch' || $suite eq 'buster' || $suite eq 'bullseye') {
+       push @$excl, qw(module-init-tools pciutils hdparm
+                       memtest86+ parted);
      } else {
        push @$excl, qw(udev module-init-tools pciutils hdparm 
                        memtest86+ parted);
@@ -522,6 +618,18 @@ sub initialize {
     my $logfd = $self->{logfd} = IO::File->new (">$self->{logfile}") ||
        die "unable to open log file";
 
+    # FIXME: seems a bit like a hacky way??
+    my $COMPRESSOR = {
+       ext => 'gz',
+       decomp => 'gzip -d',
+    };
+    if ($self->{config}->{suite} eq 'bullseye') {
+       $COMPRESSOR = {
+           ext => 'xz',
+           decomp => 'xz -d',
+       };
+    }
+
     foreach my $ss (@{$self->{sources}}) {
        my $src = $ss->{mirror} || $ss->{source};
        my $path = "dists/$ss->{suite}/Release";
@@ -536,12 +644,13 @@ sub initialize {
            print $logfd $@; 
            warn "Release info ignored\n";
        };
+
        foreach my $comp (@{$ss->{comp}}) {
-           $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.gz";
+           $path = "dists/$ss->{suite}/$comp/binary-$arch/Packages.$COMPRESSOR->{ext}";
            $target = "$infodir/" . __url_to_filename ("$ss->{source}/$path");
            my $pkgsrc = "$src/$path";
            $self->download ($pkgsrc, $target);
-           $self->run_command ("gzip -d '$target'");
+           $self->run_command ("$COMPRESSOR->{decomp} '$target'");
        }
     }
 }
@@ -555,7 +664,7 @@ sub write_config {
 
     $data .= "Name: $config->{name}\n";
     $data .= "Version: $config->{version}\n";
-    $data .= "Type: openvz\n";
+    $data .= "Type: lxc\n";
     $data .= "OS: $config->{ostype}\n";
     $data .= "Section: $config->{section}\n";
     $data .= "Maintainer: $config->{maintainer}\n";
@@ -584,7 +693,8 @@ sub finalize {
     my $instpkgs = $self->read_installed ();
     my $pkginfo = $self->pkginfo();
     my $veid = $self->{veid};
-    my $rootdir = $self->vz_root_dir();
+    my $conffile = $self->{veconffile};
+    my $rootdir = $self->{rootfs};
 
     my $vestat = $self->ve_status();
     die "ve not running - unable to finalize\n" if !$vestat->{running};
@@ -649,8 +759,7 @@ sub finalize {
     $self->ve_divert_remove ("/sbin/init"); 
 
     # finally stop the VE
-    $self->run_command ("vzctl stop $veid --fast");
-    $rootdir = $self->vz_priv_dir();
+    $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
 
     unlink "$rootdir/sbin/defenv";
 
@@ -664,15 +773,18 @@ sub finalize {
 
     write_file ("", "$rootdir/var/log/syslog");
 
-    $self->logmsg ("detecting final size: ");
+    my $get_path_size = sub {
+       my ($path) = @_;
+       my $sizestr = $self->run_command ("du -sm $path", undef, 1);
+       if ($sizestr =~ m/^(\d+)\s+\Q$path\E$/) {
+           return int($1);
+       } else {
+           die "unable to detect size for '$path'\n";
+       }
+    };
 
-    my $sizestr = $self->run_command ("du -sm $rootdir", undef, 1);
-    my $size;
-    if ($sizestr =~ m/^(\d+)\s+\Q$rootdir\E$/) {
-       $size = $1;
-    } else {
-       die "unable to detect size\n";
-    }
+    $self->logmsg ("detecting final appliance size: ");
+    my $size = $get_path_size->($rootdir);
     $self->logmsg ("$size MB\n");
 
     $self->write_config ("$rootdir/etc/appliance.info", $size);
@@ -680,18 +792,44 @@ sub finalize {
     $self->logmsg ("creating final appliance archive\n");
 
     my $target = "$self->{targetname}.tar";
+
+    my $compressor = $opts->{compressor} // 'gz';
+    my $compressor2cmd_map = {
+       gz => 'gzip',
+       gzip => 'gzip',
+       zst => 'zstd -9',
+       zstd => 'zstd -9',
+       'zstd-max' => 'zstd -19 -T0', # maximal level where the decompressor can still run efficiently
+    };
+    my $compressor2ending = {
+       gzip => 'gz',
+       zstd => 'zst',
+       'zstd-max' => 'zst',
+    };
+    my $compressor_cmd = $compressor2cmd_map->{$compressor};
+    die "unkown compressor '$compressor', use one of: ". join(', ', sort keys %$compressor2cmd_map)
+       if !defined($compressor_cmd);
+
+    my $ending = $compressor2ending->{$compressor} // $compressor;
+    my $final_archive = "${target}.${ending}";
     unlink $target;
-    unlink "$target.gz";
+    unlink $final_archive;
 
     $self->run_command ("tar cpf $target --numeric-owner -C '$rootdir' ./etc/appliance.info");
     $self->run_command ("tar rpf $target --numeric-owner -C '$rootdir' --exclude ./etc/appliance.info .");
-    $self->run_command ("gzip $target");
+    $self->run_command ("$compressor_cmd $target");
+
+    $self->logmsg ("detecting final commpressed appliance size: ");
+    $size = $get_path_size->($final_archive);
+    $self->logmsg ("$size MB\n");
+
+    $self->logmsg ("appliance archive: $final_archive\n");
 }
 
 sub read_installed {
     my ($self) = @_;
 
-    my $rootdir = $self->vz_priv_dir();
+    my $rootdir = $self->{rootfs};
 
     my $pkgfilelist = "$rootdir/var/lib/dpkg/status";
     local $/ = '';
@@ -725,51 +863,45 @@ sub read_installed {
     return $pkglist;
 }
 
-sub vz_root_dir {
+sub ve_status {
     my ($self) = @_;
 
     my $veid = $self->{veid};
 
-    return "/var/lib/vz/root/$veid";
-}
+    my $res = { running => 0 };
 
-sub vz_priv_dir {
-    my ($self) = @_;
+    $res->{exist} = 1 if -d "$self->{rootfs}/usr";
 
-    my $veid = $self->{veid};
+    my $filename = "/proc/net/unix";
 
-    return "/var/lib/vz/private/$veid";
-}
+    # similar test is used by lcxcontainers.c: list_active_containers
+    my $fh = IO::File->new ($filename, "r");
+    return $res if !$fh;
 
-sub ve_status {
-    my ($self) = @_;
-
-    my $veid = $self->{veid};
-    
-    my $res = $self->run_command ("vzctl status $veid", undef, 1);
-    chomp $res;
-
-    if ($res =~ m/^CTID\s+$veid\s+(exist|deleted)\s+(mounted|unmounted)\s+(running|down)$/) {
-       return {
-           exist => $1 eq 'exist',
-           mounted => $2 eq 'mounted',
-           running => $3 eq 'running',
-       };
-    } else {
-       die "unable to parse ve status";
+    while (defined(my $line = <$fh>)) {
+       if ($line =~ m/^[a-f0-9]+:\s\S+\s\S+\s\S+\s\S+\s\S+\s\d+\s(\S+)$/) {
+           my $path = $1;
+           if ($path =~ m!^@/\S+/$veid/command$!) {
+               $res->{running} = 1;
+           }
+       }
     }
+    close($fh);
+    
+    return $res;
 }
 
 sub ve_command {
     my ($self, $cmd, $input) = @_;
 
     my $veid = $self->{veid};
+    my $conffile = $self->{veconffile};
 
     if (ref ($cmd) eq 'ARRAY') {
-       unshift @$cmd, 'vzctl', 'exec2',  $veid, 'defenv';
+       unshift @$cmd, 'lxc-attach', '-n', $veid, '--rcfile', $conffile, '--clear-env', '--', 'defenv';
        $self->run_command ($cmd, $input);      
     } else {
-       $self->run_command ("vzctl exec2 $veid defenv $cmd", $input);
+       $self->run_command ("lxc-attach -n $veid --rcfile $conffile --clear-env -- defenv $cmd", $input);
     }
 }
 
@@ -778,9 +910,10 @@ sub ve_exec {
     my ($self, @cmd) = @_;
 
     my $veid = $self->{veid};
+    my $conffile = $self->{veconffile};
 
     my $reader;
-    my $pid = open2($reader, "<&STDIN", 'vzctl', 'exec2', $veid, 
+    my $pid = open2($reader, "<&STDIN", 'lxc-attach', '-n', $veid,  '--rcfile', $conffile, '--',
                    'defenv', @cmd) || die "unable to exec command";
     
     while (defined (my $line = <$reader>)) {
@@ -802,7 +935,7 @@ sub ve_divert_add {
 sub ve_divert_remove {
     my ($self, $filename) = @_;
 
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
 
     unlink "$rootdir/$filename";
     $self->ve_command ("dpkg-divert --remove --rename '$filename'");
@@ -811,7 +944,7 @@ sub ve_divert_remove {
 sub ve_debconfig_set {
     my ($self, $dcdata) = @_;
 
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
     my $cfgfile = "/tmp/debconf.txt";
     write_file ($dcdata, "$rootdir/$cfgfile");
     $self->ve_command ("debconf-set-selections $cfgfile"); 
@@ -831,7 +964,7 @@ sub ve_dpkg {
 
     my $pkginfo = $self->pkginfo();
 
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
     my $cachedir = $self->{cachedir};
 
     my @files;
@@ -860,48 +993,32 @@ sub ve_destroy {
     my ($self) = @_;
 
     my $veid = $self->{veid}; # fixme
+    my $conffile = $self->{veconffile};
 
     my $vestat = $self->ve_status();
     if ($vestat->{running}) {
-       $self->run_command ("vzctl stop $veid --fast");
-    } elsif ($vestat->{mounted}) {
-       $self->run_command ("vzctl umount $veid");
-    }
-    if ($vestat->{exist}) {
-       $self->run_command ("vzctl destroy $veid");
-    } else {
-       unlink $self->{veconffile};
+       $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
     }
+
+    rmtree $self->{rootfs};
+    unlink $self->{veconffile};
 }
 
 sub ve_init {
     my ($self) = @_;
 
-    my $root = $self->vz_root_dir();
-    my $priv = $self->vz_priv_dir();
-
-    my $veid = $self->{veid}; # fixme
+    my $veid = $self->{veid};
+    my $conffile = $self->{veconffile};
 
     $self->logmsg ("initialize VE $veid\n");
 
-    while (1) {
-       my $vestat = $self->ve_status();
-       if ($vestat->{running}) {
-           $self->run_command ("vzctl stop $veid --fast");
-       } elsif ($vestat->{mounted}) {
-           $self->run_command ("vzctl umount $veid");
-       } else {
-           last;
-       }
-       sleep (1);
-    }
-
-    rmtree $root;
-    rmtree $priv;
-    mkpath $root;
-    mkpath $priv;
+    my $vestat = $self->ve_status();
+    if ($vestat->{running}) {
+       $self->run_command ("lxc-stop -n $veid --rcfile $conffile --kill");
+    } 
 
-    $self->run_command ("vzctl mount $veid");
+    rmtree $self->{rootfs};
+    mkpath $self->{rootfs};
 }
 
 sub __deb_version_cmp {
@@ -1062,18 +1179,19 @@ sub closure {
     # first, record provided packages
     __record_provides ($pkginfo, $closure, $list, 1);
 
-    my $pkgs = {};
+    my $pkghash = {};
+    my $pkglist = [];
 
     # then resolve dependencies
     foreach my $pname (@$list) {
-       __closure_single ($pkginfo, $closure, $pkgs, $pname, $self->{excl});
+       __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $pname, $self->{excl});
     }
 
-    return [ keys %$pkgs ];
+    return $pkglist;
 }
 
 sub __closure_single {
-    my ($pkginfo, $closure, $pkgs, $pname, $excl) = @_;
+    my ($pkginfo, $closure, $pkghash, $pkglist, $pname, $excl) = @_;
 
     $pname =~ s/^\s+//;
     $pname =~ s/\s+$//;
@@ -1090,8 +1208,11 @@ sub __closure_single {
     my $url = $info->{url};
 
     $url || die "$pname: no url for package '$pname'";
-    
-    $pkgs->{$pname} = 1;
+
+    if (!$pkghash->{$pname}) {
+       unshift @$pkglist, $pname;
+       $pkghash->{$pname} = 1;
+    }
 
     __record_provides ($pkginfo, $closure, [$pname]) if $info->{provides};
 
@@ -1127,7 +1248,7 @@ sub __closure_single {
 
       #printf (STDERR "$pname: $p --> $found\n");
          
-      __closure_single ($pkginfo, $closure, $pkgs, $found, $excl);
+      __closure_single ($pkginfo, $closure, $pkghash, $pkglist, $found, $excl);
   }
 }
 
@@ -1168,7 +1289,7 @@ sub install_init_script {
     my ($self, $script, $runlevel, $prio) = @_;
 
     my $suite = $self->{config}->{suite};
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
 
     my $base = basename ($script);
     my $target = "$rootdir/etc/init.d/$base";
@@ -1176,6 +1297,15 @@ sub install_init_script {
     $self->run_command ("install -m 0755 '$script' '$target'");
     if ($suite eq 'etch' || $suite eq 'lenny') {
        $self->ve_command ("update-rc.d $base start $prio $runlevel .");
+    } elsif ($suite eq 'xenial' || $suite eq 'wily' || $suite eq 'vivid' ||
+            $suite eq 'yakkety' || $suite eq 'zesty' || $suite eq 'artful' ||
+            $suite eq 'bionic' || $suite eq 'cosmic' || $suite eq 'disco' ||
+            $suite eq 'eoan' || $suite eq 'focal' || $suite eq 'groovy'
+            || $suite eq 'hirsute'
+    ) {
+       die "unable to install init script (system uses systemd)\n";
+    } elsif ($suite eq 'trusty' || $suite eq 'precise') {
+       die "unable to install init script (system uses upstart)\n";
     } else {
        $self->ve_command ("insserv $base");
     }
@@ -1200,7 +1330,7 @@ sub bootstrap {
        push @$important, "postfix";
     }
 
-    foreach my $p (keys %$pkginfo) {
+    foreach my $p (sort keys %$pkginfo) {
        next if grep { $p eq $_ } @{$self->{excl}};
        my $pri = $pkginfo->{$p}->{priority};
        next if !$pri;
@@ -1251,7 +1381,7 @@ sub bootstrap {
     $self->cache_packages ($important);
     $self->cache_packages ($standard);
  
-    my $rootdir = $self->vz_priv_dir();
+    my $rootdir = $self->{rootfs};
 
     # extract required packages first
     $self->logmsg ("create basic environment\n");
@@ -1291,8 +1421,18 @@ sub bootstrap {
     # avoid warnings about non-existent resolv.conf
     write_file ("", "$rootdir/etc/resolv.conf", 0644);
 
-    $data = "auto lo\niface lo inet loopback\n";
-    write_file ($data, "$rootdir/etc/network/interfaces", 0644);
+    if (
+       $suite eq 'hirsute' || $suite eq 'groovy' || $suite eq 'focal' ||
+       $suite eq 'eoan' || $suite eq 'disco' || $suite eq 'cosmic' ||
+       $suite eq 'bionic' || $suite eq 'artful' ||
+       $suite eq 'zesty' || $suite eq 'yakkety' || $suite eq 'xenial' ||
+       $suite eq 'wily') {
+       # no need to configure loopback device
+    } else {
+       $data = "auto lo\niface lo inet loopback\n";
+       mkdir "$rootdir/etc/network";
+       write_file ($data, "$rootdir/etc/network/interfaces", 0644);
+    }
 
     # setup devices
     $self->run_command ("tar xzf '$devicetar' -C '$rootdir'");
@@ -1306,8 +1446,7 @@ sub bootstrap {
 
     $self->run_command ("cp '$default_env' '$rootdir/sbin/defenv'");
 
-    $self->run_command ("vzctl start $veid");    
-    $rootdir = $self->vz_root_dir(); 
+    $self->run_command ("lxc-start -n $veid -f $self->{veconffile}");
 
     $self->logmsg ("initialize ld cache\n");
     $self->ve_command ("/sbin/ldconfig");
@@ -1330,7 +1469,7 @@ sub bootstrap {
 
     $self->ve_dpkg ('install', 'mawk');
     $self->ve_dpkg ('install', 'debconf');
-    
+
     # unpack required packages
     foreach my $p (@$required) {
        $self->ve_dpkg ('unpack', $p);
@@ -1388,7 +1527,11 @@ EOD
     }
 
     # start loopback
-    $self->ve_command ("ifconfig lo up");
+    if (-x "$rootdir/sbin/ifconfig") {
+       $self->ve_command ("ifconfig lo up");
+    } else {
+       $self->ve_command ("ip link set lo up");
+    }
 
     $self->logmsg ("configure important packages\n");
     $self->ve_command ("dpkg --force-confold --skip-same-version --configure -a");
@@ -1408,9 +1551,6 @@ EOD
     # reset password
     $self->ve_command ("usermod -L root");
 
-    # regenerate sshd host keys
-    $self->install_init_script ($script_ssh_init, 2, 14);
-
     if ($mta eq 'postfix') {
        $data = "postfix postfix/main_mailer_type select No configuration\n";
        $self->ve_debconfig_set ($data);
@@ -1468,18 +1608,16 @@ EOD
        $self->run_command ($cmd);
     }
 
-    if ($suite eq 'intrepid') {
+    if ($suite eq 'intrepid' || $suite eq 'jaunty') {
        # remove sysctl setup (avoid warnings at startup)
        my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
-       $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf";
+       $filelist .= " $rootdir/etc/sysctl.d/10-process-security.conf" if $suite eq 'intrepid';
        $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
        $self->run_command ("rm $filelist");
     }
-    if ($suite eq 'jaunty') {
-       # remove sysctl setup (avoid warnings at startup)
-       my $filelist = "$rootdir/etc/sysctl.d/10-console-messages.conf";
-       $filelist .= " $rootdir/etc/sysctl.d/10-network-security.conf";
-       $self->run_command ("rm $filelist");
+
+    if (-e "$rootdir/lib/systemd/system/sys-kernel-config.mount") {
+       $self->ve_command ("ln -s /dev/null /etc/systemd/system/sys-kernel-debug.mount");
     }
 }
 
@@ -1487,6 +1625,7 @@ sub enter {
     my ($self) = @_;
 
     my $veid = $self->{veid};
+    my $conffile = $self->{veconffile};
 
     my $vestat = $self->ve_status();
 
@@ -1496,10 +1635,10 @@ sub enter {
     }
 
     if (!$vestat->{running}) {
-       $self->run_command ("vzctl start $veid");
+       $self->run_command ("lxc-start -n $veid -f $conffile");
     }
 
-    system ("vzctl enter $veid");
+    system ("lxc-attach -n $veid --rcfile $conffile --clear-env");
 }
 
 sub ve_mysql_command {
@@ -1519,7 +1658,7 @@ sub ve_mysql_bootstrap {
     my $suite = $self->{config}->{suite};
 
     if ($suite eq 'jessie') {
-       my $rootdir = $self->vz_root_dir();
+       my $rootdir = $self->{rootfs};
        $self->run_command ("sed -e 's/^key_buffer\\s*=/key_buffer_size =/' -i $rootdir/etc/mysql/my.cnf");
     }
 
@@ -1566,6 +1705,16 @@ sub task_postgres {
     } elsif ($suite eq 'jessie') {
         @supp = ('9.4');
         $pgversion = '9.4';
+    } elsif ($suite eq 'stretch') {
+        @supp = ('9.6');
+        $pgversion = '9.6';
+    } elsif ($suite eq 'buster') {
+        @supp = ('11');
+        $pgversion = '11';
+    } elsif ($suite eq 'bullseye') {
+       # FIXME update on freeze!
+       @supp = ('12');
+       $pgversion = '12';
     }
 
     $pgversion = $opts->{version} if $opts->{version};
@@ -1573,7 +1722,7 @@ sub task_postgres {
     die "unsupported postgres version '$pgversion'\n" 
        if !grep { $pgversion eq $_; } @supp;
 
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
 
     my $required = $self->compute_required (["postgresql-$pgversion"]);
 
@@ -1582,8 +1731,9 @@ sub task_postgres {
     $self->ve_dpkg ('install', @$required);
 
     my $iscript = "postgresql-$pgversion";
-    if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie') {
-      $iscript = 'postgresql';
+    if ($suite eq 'squeeze' || $suite eq 'wheezy' || $suite eq 'jessie' ||
+       $suite eq 'stretch') {
+       $iscript = 'postgresql';
     }
 
     $self->ve_command ("/etc/init.d/$iscript start") if $opts->{start};
@@ -1593,7 +1743,7 @@ sub task_mysql {
     my ($self, $opts) = @_;
 
     my $password = $opts->{password};
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
 
     my $suite = $self->{config}->{suite};
     
@@ -1602,6 +1752,8 @@ sub task_mysql {
       $ver = '5.1';
     } elsif ($suite eq 'wheezy' || $suite eq 'jessie') {
       $ver = '5.5';
+    } else {
+       die "task_mysql: unsupported suite '$suite'";
     }
 
     my $required = $self->compute_required (['mysql-common', "mysql-server-$ver"]);
@@ -1638,7 +1790,7 @@ sub task_php {
     my ($self, $opts) = @_;
 
     my $memlimit = $opts->{memlimit};
-    my $rootdir = $self->vz_root_dir();
+    my $rootdir = $self->{rootfs};
 
     my $required = $self->compute_required ([qw (php5 php5-cli libapache2-mod-php5 php5-gd)]);