]>
git.proxmox.com Git - qemu-server.git/blob - qmextract
11 use PVE
::Cluster
qw(cfs_read_file);
12 use PVE
::RPCEnvironment
;
15 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
17 die "please run as root\n" if $> != 0;
19 my @std_opts = ('storage=s', 'pool=s', 'info', 'prealloc');
22 print STDERR
"usage: $0 [--storage=<storeid>] [--pool=<poolid>] [--info] [--prealloc] <archive> <vmid>\n\n";
26 if (!GetOptions
($opts, @std_opts)) {
31 PVE
::INotify
::inotify_init
();
33 my $rpcenv = PVE
::RPCEnvironment-
>init('cli');
35 $rpcenv->init_request();
36 $rpcenv->set_language($ENV{LANG
});
37 $rpcenv->set_user('root@pam');
40 # NOTE: this is run as tar subprocess (--to-command)
42 $SIG{INT
} = $SIG{TERM
} = $SIG{QUIT
} = $SIG{HUP
} = $SIG{PIPE
} = sub {
43 die "interrupted by signal\n";
46 my $filename = $ENV{TAR_FILENAME
};
47 die "got strange environment - no TAR_FILENAME\n" if !$filename;
49 my $filesize = $ENV{TAR_SIZE
};
50 die "got strange file size '$filesize'\n" if !$filesize;
52 my $tmpdir = $ENV{VZDUMP_TMPDIR
};
53 die "got strange environment - no VZDUMP_TMPDIR\n" if !$tmpdir;
55 my $filetype = $ENV{TAR_FILETYPE
} || 'none';
56 die "got strange filetype '$filetype'\n" if $filetype ne 'f';
58 my $vmid = $ENV{VZDUMP_VMID
};
59 PVE
::JSONSchema
::pve_verify_vmid
($vmid);
61 my $user = $ENV{VZDUMP_USER
};
62 $rpcenv->check_user_enabled($user);
65 print STDERR
"reading archive member '$filename'\n";
67 print STDERR
"extracting '$filename' from archive\n";
70 my $conffile = "$tmpdir/qemu-server.conf";
71 my $statfile = "$tmpdir/qmrestore.stat";
73 if ($filename eq 'qemu-server.conf') {
74 my $outfd = IO
::File-
>new($conffile, "w") ||
75 die "unable to write file '$conffile'\n";
77 while (defined(my $line = <>)) {
79 print STDERR
"CONFIG: $line" if $opts->{info
};
88 exec 'dd', 'bs=256K', "of=/dev/null";
89 die "couldn't exec dd: $!\n";
92 my $conffd = IO
::File-
>new($conffile, "r") ||
93 die "unable to read file '$conffile'\n";
96 while (defined(my $line = <$conffd>)) {
97 if ($line =~ m/^\#vzdump\#map:(\S+):(\S+):(\d+):(\S*):$/) {
98 $map->{$2} = { virtdev
=> $1, size
=> $3, storeid
=> $4 };
103 my $statfd = IO
::File-
>new($statfile, "a") ||
104 die "unable to open file '$statfile'\n";
106 if ($filename !~ m/^.*\.([^\.]+)$/){
107 die "got strange filename '$filename'\n";
114 print STDERR
"restoring old style vzdump archive - " .
115 "no device map inside archive\n";
116 die "can't restore old style archive to storage '$opts->{storage}'\n"
117 if defined($opts->{storage
}) && $opts->{storage
} ne 'local';
119 my $dir = "/var/lib/vz/images/$vmid";
122 $path = "$dir/$filename";
124 print $statfd "vzdump::$path\n";
129 my $info = $map->{$filename};
130 die "no vzdump info for '$filename'\n" if !$info;
132 if ($filename !~ m/^vm-disk-$info->{virtdev}\.([^\.]+)$/){
133 die "got strange filename '$filename'\n";
136 if ($filesize != $info->{size
}) {
137 die "detected size difference for '$filename' " .
138 "($filesize != $info->{size})\n";
141 # check permission for all used storages
142 my $pool = $opts->{pool
};
143 if ($user ne 'root@pam') {
144 if (defined($opts->{storage
})) {
145 my $sid = $opts->{storage
} || 'local';
146 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
148 foreach my $fn (keys %$map) {
149 my $fi = $map->{$fn};
150 my $sid = $fi->{storeid
} || 'local';
151 $rpcenv->check($user, "/storage/$sid", ['Datastore.AllocateSpace']);
157 if (defined($opts->{storage
})) {
158 $storeid = $opts->{storage
} || 'local';
160 $storeid = $info->{storeid
} || 'local';
163 my $cfg = cfs_read_file
('storage.cfg');
164 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
166 my $alloc_size = int(($filesize + 1024 - 1)/1024);
167 if ($scfg->{type
} eq 'dir' || $scfg->{type
} eq 'nfs') {
168 # hack: we just alloc a small file (32K) - we overwrite it anyways
171 die "unable to restore '$filename' to storage '$storeid'\n" .
172 "storage type '$scfg->{type}' does not support format '$format\n"
176 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $vmid,
177 $format, undef, $alloc_size);
179 print STDERR
"new volume ID is '$volid'\n";
181 print $statfd "vzdump:$info->{virtdev}:$volid\n";
184 $path = PVE
::Storage
::path
($cfg, $volid);
187 print STDERR
"restore data to '$path' ($filesize bytes)\n";
189 if ($opts->{prealloc
} || $format ne 'raw' || (-b
$path)) {
190 exec 'dd', 'ibs=256K', 'obs=256K', "of=$path";
191 die "couldn't exec dd: $!\n";
193 exec '/usr/lib/qemu-server/sparsecp', $path;
194 die "couldn't exec sparsecp: $!\n";
199 if (scalar(@ARGV) == 2) {
203 # fixme: use API call
204 PVE
::JSONSchema
::pve_verify_vmid
($vmid);
206 PVE
::Cluster
::check_cfs_quorum
();
208 PVE
::QemuServer
::restore_archive
($archive, $vmid, 'root@pam', $opts);
210 } elsif (scalar(@ARGV) == 0 && $ENV{TAR_FILENAME
}) {