]>
git.proxmox.com Git - qemu-server.git/blob - qmextract
12 use PVE
::RPCEnvironment
;
16 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
18 die "please run as root\n" if $> != 0;
20 my @std_opts = ('storage=s', 'pool=s', 'info', 'prealloc');
23 print STDERR
"usage: $0 [--storage=<storeid>] [--pool=<poolid>] [--info] [--prealloc] <archive> <vmid>\n\n";
27 if (!GetOptions
($opts, @std_opts)) {
32 PVE
::INotify
::inotify_init
();
34 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
36 $rpcenv->init_request();
37 $rpcenv->set_language($ENV{LANG
});
38 $rpcenv->set_user('root@pam');
41 # NOTE: this is run as tar subprocess (--to-command)
43 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
44 die "interrupted by signal\n";
47 my $filename = $ENV{TAR_FILENAME
};
48 die "got strange environment - no TAR_FILENAME\n" if !$filename;
50 my $filesize = $ENV{TAR_SIZE
};
51 die "got strange file size '$filesize'\n" if !$filesize;
53 my $tmpdir = $ENV{VZDUMP_TMPDIR
};
54 die "got strange environment - no VZDUMP_TMPDIR\n" if !$tmpdir;
56 my $filetype = $ENV{TAR_FILETYPE
} || 'none';
57 die "got strange filetype '$filetype'\n" if $filetype ne 'f';
59 my $vmid = $ENV{VZDUMP_VMID
};
60 PVE
::JSONSchema
::pve_verify_vmid
($vmid);
62 my $user = $ENV{VZDUMP_USER
};
63 $rpcenv->check_user_enabled($user);
66 print STDERR
"reading archive member '$filename'\n";
68 print STDERR
"extracting '$filename' from archive\n";
71 my $conffile = "$tmpdir/qemu-server.conf";
72 my $statfile = "$tmpdir/qmrestore.stat";
74 if ($filename eq 'qemu-server.conf') {
75 my $outfd = IO
::File-
>new($conffile, "w") ||
76 die "unable to write file '$conffile'\n";
78 while (defined(my $line = <>)) {
80 print STDERR
"CONFIG: $line" if $opts->{info
};
89 exec 'dd', 'bs=256K', "of=/dev/null";
90 die "couldn't exec dd: $!\n";
93 my $conffd = IO
::File-
>new($conffile, "r") ||
94 die "unable to read file '$conffile'\n";
97 while (defined(my $line = <$conffd>)) {
98 if ($line =~ m/^\#vzdump\#map:(\S+):(\S+):(\d+):(\S*):$/) {
99 $map->{$2} = { virtdev
=> $1, size
=> $3, storeid
=> $4 };
104 my $statfd = IO
::File-
>new($statfile, "a") ||
105 die "unable to open file '$statfile'\n";
107 if ($filename !~ m/^.*\.([^\.]+)$/){
108 die "got strange filename '$filename'\n";
115 print STDERR
"restoring old style vzdump archive - " .
116 "no device map inside archive\n";
117 die "can't restore old style archive to storage '$opts->{storage}'\n"
118 if defined($opts->{storage
}) && $opts->{storage
} ne 'local';
120 my $dir = "/var/lib/vz/images/$vmid";
123 $path = "$dir/$filename";
125 print $statfd "vzdump::$path\n";
130 my $info = $map->{$filename};
131 die "no vzdump info for '$filename'\n" if !$info;
133 if ($filename !~ m/^vm-disk-$info->{virtdev}\.([^\.]+)$/){
134 die "got strange filename '$filename'\n";
137 if ($filesize != $info->{size
}) {
138 die "detected size difference for '$filename' " .
139 "($filesize != $info->{size})\n";
142 # check permission for all used storages
143 my $pool = $opts->{pool
};
144 if ($user ne 'root@pam') {
145 if (defined($opts->{storage
})) {
146 my $sid = $opts->{storage
} || 'local';
147 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
149 foreach my $fn (keys %$map) {
150 my $fi = $map->{$fn};
151 my $sid = $fi->{storeid
} || 'local';
152 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
158 if (defined($opts->{storage
})) {
159 $storeid = $opts->{storage
} || 'local';
161 $storeid = $info->{storeid
} || 'local';
164 my $cfg = PVE
::Storage
::config
();
165 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
167 my $alloc_size = int(($filesize + 1024 - 1)/1024);
168 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
169 # hack: we just alloc a small file (32K) - we overwrite it anyways
172 die "unable to restore '$filename' to storage '$storeid'\n" .
173 "storage type '$scfg->{type}' does not support format '$format\n"
177 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $vmid,
178 $format, undef, $alloc_size);
180 print STDERR
"new volume ID is '$volid'\n";
182 print $statfd "vzdump:$info->{virtdev}:$volid\n";
185 $path = PVE
::Storage
::path
($cfg, $volid);
188 print STDERR
"restore data to '$path' ($filesize bytes)\n";
190 if ($opts->{prealloc
} || $format ne 'raw' || (-b
$path)) {
191 exec 'dd', 'ibs=256K', 'obs=256K', "of=$path";
192 die "couldn't exec dd: $!\n";
194 exec '/usr/lib/qemu-server/sparsecp', $path;
195 die "couldn't exec sparsecp: $!\n";
200 if (scalar(@ARGV) == 2) {
204 # fixme: use API call
205 PVE
::JSONSchema
::pve_verify_vmid
($vmid);
207 PVE
::Cluster
::check_cfs_quorum
();
209 PVE
::QemuServer
::restore_archive
($archive, $vmid, 'root@pam', $opts);
211 } elsif (scalar(@ARGV) == 0 && $ENV{TAR_FILENAME
}) {