]>
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.",
27 additionalProperties
=> 0,
29 node
=> get_standard_option
('pve-node'),
30 storage
=> get_standard_option
('pve-storage-id'),
32 description
=> "Only list content of this type.",
33 type
=> 'string', format
=> 'pve-storage-content',
36 vmid
=> get_standard_option
38 description
=> "Only list images for this VM",
53 links
=> [ { rel
=> 'child', href
=> "{volid}" } ],
58 my $cts = $param->{content
} ?
[ $param->{content
} ] : [ @ctypes ];
60 my $storeid = $param->{storage
};
62 my $cfg = cfs_read_file
("storage.cfg");
64 my $scfg = PVE
::Storage
::storage_config
($cfg, $storeid);
67 foreach my $ct (@$cts) {
69 if ($ct eq 'images' || defined($param->{vmid
})) {
70 $data = PVE
::Storage
::vdisk_list
($cfg, $storeid, $param->{vmid
});
71 } elsif ($ct eq 'iso') {
72 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'iso');
73 } elsif ($ct eq 'vztmpl') {
74 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'vztmpl');
75 } elsif ($ct eq 'backup') {
76 $data = PVE
::Storage
::template_list
($cfg, $storeid, 'backup');
79 next if !$data || !$data->{$storeid};
81 foreach my $item (@{$data->{$storeid}}) {
82 $item->{content
} = $ct;
90 __PACKAGE__-
>register_method ({
94 description
=> "Allocate disk images.",
98 additionalProperties
=> 0,
100 node
=> get_standard_option
('pve-node'),
101 storage
=> get_standard_option
('pve-storage-id'),
103 description
=> "The name of the file to create/upload.",
106 vmid
=> get_standard_option
('pve-vmid', { description
=> "Specify owner VM" } ),
108 description
=> "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
110 pattern
=> '\d+[MG]?',
114 enum
=> ['raw', 'qcow2'],
121 description
=> "Volume identifier",
127 my $storeid = $param->{storage
};
128 my $name = $param->{filename
};
129 my $sizestr = $param->{size
};
132 if ($sizestr =~ m/^\d+$/) {
134 } elsif ($sizestr =~ m/^(\d+)M$/) {
136 } elsif ($sizestr =~ m/^(\d+)G$/) {
137 $size = $1 * 1024 * 1024;
139 raise_param_exc
({ size
=> "unable to parse size '$sizestr'" });
142 # extract FORMAT from name
143 if ($name =~ m/\.(raw|qcow2)$/) {
146 raise_param_exc
({ format
=> "different storage formats ($param->{format} != $fmt)" })
147 if $param->{format
} && $param->{format
} ne $fmt;
149 $param->{format
} = $fmt;
152 my $cfg = cfs_read_file
('storage.cfg');
154 my $volid = PVE
::Storage
::vdisk_alloc
($cfg, $storeid, $param->{vmid
},
161 # we allow to pass volume names (without storage prefix) if the storage
162 # is specified as separate parameter.
163 my $real_volume_id = sub {
164 my ($storeid, $volume) = @_;
168 if ($volume =~ m/:/) {
170 my ($sid, $volname) = PVE
::Storage
::parse_volume_id
($volume);
171 raise_param_exc
({ storage
=> "storage ID missmatch" })
172 if $storeid && $sid ne $storeid;
175 raise_param_exc
({ volume
=> $@}) if $@;
178 raise_param_exc
({ volume
=> "no storage speficied - incomplete volume ID" })
181 $volid = "$storeid:$volume";
187 __PACKAGE__-
>register_method ({
191 description
=> "Get volume attributes",
195 additionalProperties
=> 0,
197 node
=> get_standard_option
('pve-node'),
198 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1 }),
200 description
=> "Volume identifier",
205 returns
=> { type
=> 'object' },
209 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
211 my $cfg = cfs_read_file
('storage.cfg');
213 my $path = PVE
::Storage
::path
($cfg, $volid);
214 my ($size, $format, $used) = PVE
::Storage
::file_size_info
($path);
216 # fixme: return more attributes?
224 __PACKAGE__-
>register_method ({
228 description
=> "Delete volume",
232 additionalProperties
=> 0,
234 node
=> get_standard_option
('pve-node'),
235 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
237 description
=> "Volume identifier",
242 returns
=> { type
=> 'null' },
246 my $volid = &$real_volume_id($param->{storage
}, $param->{volume
});
248 my $cfg = cfs_read_file
('storage.cfg');
250 PVE
::Storage
::vdisk_free
($cfg, $volid);
255 __PACKAGE__-
>register_method ({
259 description
=> "Copy a volume.",
263 additionalProperties
=> 0,
265 node
=> get_standard_option
('pve-node'),
266 storage
=> get_standard_option
('pve-storage-id', { optional
=> 1}),
268 description
=> "Source volume identifier",
272 description
=> "Target volume identifier",
275 target_node
=> get_standard_option
('pve-node', {
276 description
=> "Target node. Default is local node.",
287 my $rpcenv = PVE
::RPCEnvironment
::get
();
289 my $user = $rpcenv->get_user();
291 my $target_node = $param->{target_node
} || PVE
::INotify
::nodename
();
293 # cd /nodes/localhost/storage/local/content
294 # pve:/> create local:103/vm-103-disk-1.raw -target local:103/vm-103-disk-2.raw
295 # pve:/> create 103/vm-103-disk-1.raw -target 103/vm-103-disk-3.raw
297 my $src_volid = &$real_volume_id($param->{storage
}, $param->{volume
});
298 my $dst_volid = &$real_volume_id($param->{storage
}, $param->{target
});
300 print "DEBUG: COPY $src_volid TO $dst_volid\n";
302 my $cfg = cfs_read_file
('storage.cfg');
304 # do all parameter checks first
306 # then do all short running task (to raise errors befor we go to background)
308 # then start the worker task
312 print "DEBUG: starting worker $upid\n";
314 my ($target_sid, $target_volname) = PVE
::Storage
::parse_volume_id
($dst_volid);
315 #my $target_ip = PVE::Cluster::remote_node_ip($target_node);
317 # you need to get this working (fails currently, because storage_migrate() uses
318 # ssh to connect to local host (which is not needed
319 PVE
::Storage
::storage_migrate
($cfg, $src_volid, $target_node, $target_sid, $target_volname);
321 print "DEBUG: end worker $upid\n";
325 return $rpcenv->fork_worker('imgcopy', undef, $user, $worker);