]>
Commit | Line | Data |
---|---|---|
e4fc8228 AD |
1 | package PVE::Storage::SheepdogPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use PVE::Tools qw(run_command trim); | |
7 | use PVE::Storage::Plugin; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | ||
10 | use base qw(PVE::Storage::Plugin); | |
11 | ||
12 | ||
13 | sub sheepdog_ls{ | |
14 | my ($scfg, $storeid) = @_; | |
15 | ||
16 | my $portal = $scfg->{portal}; | |
17 | my ($server, $port) = split(':', $portal); | |
18 | my $cmd = ['/usr/sbin/collie', 'vdi', 'list', '-a', $server ]; | |
19 | my $list = {}; | |
20 | ||
21 | ||
22 | run_command($cmd,outfunc => sub { | |
23 | my $line = shift; | |
24 | $line = trim($line); | |
25 | if( $line =~ /(vm-(\d+)-\S+)\s+(\d+)\s+([\.0-9]*)\s(\w+)\s+([\.0-9]*)\s(\w+)\W+([\.0-9]*)\s(\w+)\s+([\-0-9]*)\s([:0-9]*)\W+/ ) { | |
26 | ||
27 | my $image = $1; | |
28 | my $owner = $2; | |
29 | my $size = $4; | |
30 | ||
31 | $list->{$storeid}->{$image} = { | |
32 | name => $image, | |
33 | size => $size, | |
34 | vmid => $owner | |
35 | }; | |
36 | ||
37 | ||
38 | ||
39 | } | |
40 | }); | |
41 | ||
42 | return $list; | |
43 | ||
44 | } | |
45 | ||
46 | # Configuration | |
47 | ||
48 | ||
49 | sub type { | |
50 | return 'sheepdog'; | |
51 | } | |
52 | ||
53 | sub plugindata { | |
54 | return { | |
55 | content => [ {images => 1}, { images => 1 }], | |
56 | }; | |
57 | } | |
58 | ||
59 | ||
60 | sub options { | |
61 | return { | |
62 | portal => { fixed => 1 }, | |
63 | content => { optional => 1 }, | |
64 | }; | |
65 | } | |
66 | ||
67 | # Storage implementation | |
68 | ||
69 | sub parse_volname { | |
70 | my ($class, $volname) = @_; | |
71 | ||
72 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
73 | return ('images', $1, $2); | |
74 | } | |
75 | ||
76 | die "unable to parse rbd volume name '$volname'\n"; | |
77 | } | |
78 | ||
79 | sub path { | |
80 | my ($class, $scfg, $volname, $storeid) = @_; | |
81 | ||
82 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
83 | ||
84 | my $portal = $scfg->{portal}; | |
85 | ||
86 | my $path = "sheepdog:$portal:$name"; | |
87 | ||
88 | return ($path, $vmid, $vtype); | |
89 | } | |
90 | ||
91 | sub alloc_image { | |
92 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
93 | ||
94 | ||
95 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" | |
96 | if $name && $name !~ m/^vm-$vmid-/; | |
97 | my $portal = $scfg->{portal}; | |
98 | my ($server, $port) = split(':', $portal); | |
99 | ||
100 | if (!$name) { | |
101 | my $sheepdog = sheepdog_ls($scfg, $storeid); | |
102 | ||
103 | for (my $i = 1; $i < 100; $i++) { | |
104 | my $tn = "vm-$vmid-disk-$i"; | |
105 | if (!defined ($sheepdog->{$storeid}->{$tn})) { | |
106 | $name = $tn; | |
107 | last; | |
108 | } | |
109 | } | |
110 | } | |
111 | ||
112 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" | |
113 | if !$name; | |
114 | my $cmd = ['/usr/sbin/collie', 'vdi', 'create' , $name , $size.'KB', '-a', $server ]; | |
115 | run_command($cmd, errmsg => "sheepdog create $name' error"); | |
116 | ||
117 | return $name; | |
118 | } | |
119 | ||
120 | sub free_image { | |
121 | my ($class, $storeid, $scfg, $volname) = @_; | |
122 | ||
123 | my $portal = $scfg->{portal}; | |
124 | my ($server, $port) = split(':', $portal); | |
125 | ||
126 | my $cmd = ['/usr/sbin/collie', 'vdi', 'delete' , $volname, '-a', $server ]; | |
127 | ||
128 | run_command($cmd, errmsg => "sheepdog delete $volname' error"); | |
129 | ||
130 | return undef; | |
131 | } | |
132 | ||
133 | sub list_images { | |
134 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
135 | ||
136 | $cache->{sheepdog} = sheepdog_ls($scfg, $storeid) if !$cache->{sheepdog}; | |
137 | my $res = []; | |
138 | ||
139 | if (my $dat = $cache->{sheepdog}->{$storeid}) { | |
140 | foreach my $image (keys %$dat) { | |
141 | ||
142 | my $volname = $dat->{$image}->{name}; | |
143 | ||
144 | my $volid = "$storeid:$volname"; | |
145 | ||
146 | ||
147 | my $owner = $dat->{$volname}->{vmid}; | |
148 | if ($vollist) { | |
149 | my $found = grep { $_ eq $volid } @$vollist; | |
150 | next if !$found; | |
151 | } else { | |
152 | next if defined ($vmid) && ($owner ne $vmid); | |
153 | } | |
154 | ||
155 | my $info = $dat->{$volname}; | |
156 | $info->{volid} = $volid; | |
157 | ||
158 | push @$res, $info; | |
159 | } | |
160 | } | |
161 | ||
162 | return $res; | |
163 | } | |
164 | ||
165 | ||
166 | sub status { | |
167 | my ($class, $storeid, $scfg, $cache) = @_; | |
168 | ||
169 | my $total = 0; | |
170 | my $free = 0; | |
171 | my $used = 0; | |
172 | my $active = 1; | |
173 | return ($total,$free,$used,$active); | |
174 | ||
175 | return undef; | |
176 | } | |
177 | ||
178 | sub activate_storage { | |
179 | my ($class, $storeid, $scfg, $cache) = @_; | |
180 | return 1; | |
181 | } | |
182 | ||
183 | sub deactivate_storage { | |
184 | my ($class, $storeid, $scfg, $cache) = @_; | |
185 | return 1; | |
186 | } | |
187 | ||
188 | sub activate_volume { | |
189 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
190 | return 1; | |
191 | } | |
192 | ||
193 | sub deactivate_volume { | |
194 | my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_; | |
195 | return 1; | |
196 | } | |
197 | ||
198 | 1; |