]>
git.proxmox.com Git - pve-storage.git/blob - PVE/API2/Storage/Content.pm
1 package PVE
::API2
::Storage
::Content
;
7 use PVE
::Cluster
qw(cfs_read_file);
10 use PVE
::Exception
qw(raise_param_exc);
11 use PVE
::RPCEnvironment
;
13 use PVE
::JSONSchema
qw(get_standard_option);
15 use base
qw(PVE::RESTHandler);
17 my @ctypes = qw(images vztmpl iso backup);
19 __PACKAGE__-
>register_method ({
23 description
=> "List storage content.",
25 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
30 additionalProperties
=> 0,
32 node
=> get_standard_option
('pve-node'),
33 storage
=> get_standard_option
('pve-storage-id'),
35 description
=> "Only list content of this type.",
36 type
=> 'string', format
=> 'pve-storage-content',
39 vmid
=> get_standard_option
41 description
=> "Only list images for this VM",
56 links
=> [ { rel
=> 'child', href
=> "{volid}" } ],
61 my $cts = $param->{content
} ?
[ $param->{content
} ] : [ @ctypes ];
63 my $storeid = $param->{storage
};
65 my $cfg = cfs_read_file
("storage.cfg");
67 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
70 foreach my $ct (@$cts) {
72 if ($ct eq 'images' || defined($param->{vmid
})) {
73 $data = PVE
::Storage
::vdisk_list
($cfg, $storeid, $param->{vmid
});
74 } elsif ($ct eq 'iso') {
75 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'iso');
76 } elsif ($ct eq 'vztmpl') {
77 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'vztmpl');
78 } elsif ($ct eq 'backup') {
79 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
82 next if !$data || !$data->{$storeid};
84 foreach my $item (@{$data->{$storeid}}) {
85 $item->{content
} = $ct;
93 __PACKAGE__-
>register_method ({
97 description
=> "Allocate disk images.",
99 check
=> ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']],
104 additionalProperties
=> 0,
106 node
=> get_standard_option
('pve-node'),
107 storage
=> get_standard_option
('pve-storage-id'),
109 description
=> "The name of the file to create.",
112 vmid
=> get_standard_option
('pve-vmid', { description
=> "Specify owner VM" } ),
114 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
116 pattern
=> '\d+[MG]?',
120 enum
=> ['raw', 'qcow2'],
127 description
=> "Volume identifier",
133 my $storeid = $param->{storage
};
134 my $name = $param->{filename
};
135 my $sizestr = $param->{size
};
138 if ($sizestr =~ m/^\d+$/) {
140 } elsif ($sizestr =~ m/^(\d+)M$/) {
142 } elsif ($sizestr =~ m/^(\d+)G$/) {
143 $size = $1 * 1024 * 1024;
145 raise_param_exc
({ size
=> "unable to parse size '$sizestr'" });
148 # extract FORMAT from name
149 if ($name =~ m/\.(raw|qcow2)$/) {
152 raise_param_exc
({ format
=> "different storage formats ($param->{format} != $fmt)" })
153 if $param->{format
} && $param->{format
} ne $fmt;
155 $param->{format
} = $fmt;
158 my $cfg = cfs_read_file
('storage.cfg');
160 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $param->{vmid
},
167 # we allow to pass volume names (without storage prefix) if the storage
168 # is specified as separate parameter.
169 my $real_volume_id = sub {
170 my ($storeid, $volume) = @_;
174 if ($volume =~ m/:/) {
176 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume);
177 raise_param_exc
({ storage
=> "storage ID missmatch" })
178 if $storeid && $sid ne $storeid;
181 raise_param_exc
({ volume
=> $@}) if $@;
184 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
187 $volid = "$storeid:$volume";
193 __PACKAGE__-
>register_method ({
197 description
=> "Get volume attributes",
199 check
=> ['perm', '/storage/{storage}', ['Datastore.Audit', 'Datastore.AllocateSpace'], any
=> 1],
204 additionalProperties
=> 0,
206 node
=> get_standard_option
('pve-node'),
207 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
209 description
=> "Volume identifier",
214 returns
=> { type
=> 'object' },
218 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
220 my $cfg = cfs_read_file
('storage.cfg');
222 my $path = PVE
::Storage
::path
($cfg, $volid);
223 my ($size, $format, $used) = PVE
::Storage
::file_size_info
($path);
225 # fixme: return more attributes?
233 __PACKAGE__-
>register_method ({
237 description
=> "Delete volume",
239 check
=> ['perm', '/storage/{storage}', ['Datastore.AllocateSpace']],
244 additionalProperties
=> 0,
246 node
=> get_standard_option
('pve-node'),
247 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
249 description
=> "Volume identifier",
254 returns
=> { type
=> 'null' },
258 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
260 my $cfg = cfs_read_file
('storage.cfg');
262 PVE
::Storage
::vdisk_free
($cfg, $volid);
267 __PACKAGE__-
>register_method ({
271 description
=> "Copy a volume. This is experimental code - do not use.",
275 additionalProperties
=> 0,
277 node
=> get_standard_option
('pve-node'),
278 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
280 description
=> "Source volume identifier",
284 description
=> "Target volume identifier",
287 target_node
=> get_standard_option
('pve-node', {
288 description
=> "Target node. Default is local node.",
299 my $rpcenv = PVE
::RPCEnvironment
::get
();
301 my $user = $rpcenv->get_user();
303 my $target_node = $param->{target_node
} || PVE
::INotify
::nodename
();
305 # cd /nodes/localhost/storage/local/content
306 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
307 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
309 my $src_volid = &$real_volume_id($param->{storage
}, $param->{volume
});
310 my $dst_volid = &$real_volume_id($param->{storage
}, $param->{target
});
312 print "DEBUG: COPY $src_volid TO $dst_volid\n";
314 my $cfg = cfs_read_file
('storage.cfg');
316 # do all parameter checks first
318 # then do all short running task (to raise errors befor we go to background)
320 # then start the worker task
324 print "DEBUG: starting worker $upid\n";
326 my ($target_sid, $target_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
327 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
329 # you need to get this working (fails currently, because storage_migrate() uses
330 # ssh to connect to local host (which is not needed
331 PVE
::Storage
::storage_migrate
($cfg, $src_volid, $target_node, $target_sid, $target_volname);
333 print "DEBUG: end worker $upid\n";
337 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);