/
fsck.rb
680 lines (518 loc) · 17.3 KB
/
fsck.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
if !ONE_LOCATION
LOG_LOCATION = "/var/log/one"
else
LOG_LOCATION = ONE_LOCATION + "/var"
end
LOG = LOG_LOCATION + "/onedb-fsck.log"
require "rexml/document"
include REXML
require 'ipaddr'
require 'set'
require 'nokogiri'
require 'opennebula'
require 'fsck/pool_control'
require 'fsck/user'
require 'fsck/group'
require 'fsck/cluster'
require 'fsck/host'
require 'fsck/datastore'
require 'fsck/network'
require 'fsck/image'
require 'fsck/marketplaceapp'
require 'fsck/marketplace'
require 'fsck/vm'
require 'fsck/cluster_vnc_bitmap'
require 'fsck/history'
require 'fsck/vrouter'
require 'fsck/template'
require 'fsck/quotas'
module OneDBFsck
VERSION = "5.12.0"
LOCAL_VERSION = "5.12.0"
def db_version
if defined?(@db_version) && @db_version
@db_version
else
@db_version = read_db_version
end
end
def check_db_version()
# db_version = read_db_version()
if ( db_version[:version] != VERSION ||
db_version[:local_version] != LOCAL_VERSION )
raise <<-EOT
Version mismatch: fsck file is for version
Shared: #{VERSION}, Local: #{LOCAL_VERSION}
Current database is version
Shared: #{db_version[:version]}, Local: #{db_version[:local_version]}
EOT
end
end
def one_version
"OpenNebula #{VERSION}"
end
# def db_version
# one_version()
# end
VM_BIN = 0x0000001000000000
NET_BIN = 0x0000004000000000
VROUTER_BIN = 0x0004000000000000
HOLD = 0xFFFFFFFF
TABLES = ["group_pool", "user_pool", "acl", "image_pool", "host_pool",
"network_pool", "template_pool", "vm_pool", "cluster_pool",
"datastore_pool", "document_pool", "zone_pool", "secgroup_pool",
"vdc_pool", "vrouter_pool", "marketplace_pool",
"marketplaceapp_pool"].freeze
FEDERATED_TABLES = ["group_pool", "user_pool", "acl", "zone_pool",
"vdc_pool", "marketplace_pool", "marketplaceapp_pool", "db_versioning"].freeze
def tables
TABLES
end
def federated_tables
FEDERATED_TABLES
end
def add_element(elem, name)
return elem.add_child(elem.document.create_element(name))
end
def add_cdata(elem, name, text)
# The cleaner doc.create_cdata(txt) is not supported in
# old versions of nokogiri
return add_element(elem, name).add_child(
Nokogiri::XML::CDATA.new(elem.document(), text))
end
########################################################################
# Acl
########################################################################
########################################################################
# Users
#
# USER/GNAME
########################################################################
########################################################################
# Datastore
#
# DATASTORE/UID
# DATASTORE/UNAME
# DATASTORE/GID
# DATASTORE/GNAME
# DATASTORE/SYSTEM ??
########################################################################
########################################################################
# VM Template
#
# VMTEMPLATE/UID
# VMTEMPLATE/UNAME
# VMTEMPLATE/GID
# VMTEMPLATE/GNAME
########################################################################
########################################################################
# Documents
#
# DOCUMENT/UID
# DOCUMENT/UNAME
# DOCUMENT/GID
# DOCUMENT/GNAME
########################################################################
########################################################################
# VM
#
# VM/UID
# VM/UNAME
# VM/GID
# VM/GNAME
#
# VM/STATE <--- Check transitioning states?
# VM/LCM_STATE <---- Check consistency state/lcm_state ?
########################################################################
########################################################################
# Image
#
# IMAGE/UID
# IMAGE/UNAME
# IMAGE/GID
# IMAGE/GNAME
########################################################################
########################################################################
# VNet
#
# VNET/UID
# VNET/UNAME
# VNET/GID
# VNET/GNAME
########################################################################
def counters
if !defined?(@counters)
@counters = {}
@counters[:host] = {}
@counters[:image] = {}
@counters[:vnet] = {}
@counters[:vrouter] = {}
end
@counters
end
# Initialize all the vrouter counters to 0
def init_vrouter_counters
@db.fetch("SELECT oid FROM vrouter_pool") do |row|
counters[:vrouter][row[:oid]] = {
:vms => Set.new
}
end
end
def fsck
init_log_time()
@errors = 0
@repaired_errors = 0
@unrepaired_errors = 0
puts
db_version = read_db_version()
########################################################################
# pool_control
########################################################################
check_pool_control
fix_pool_control
log_time()
########################################################################
# Groups
#
# GROUP/USERS/ID
########################################################################
########################################################################
# Users
#
# USER/GID
########################################################################
check_user
fix_user
log_time
check_group
fix_group
log_time
########################################################################
# Clusters
#
# CLUSTER/SYSTEM_DS
# CLUSTER/HOSTS/ID
# CLUSTER/DATASTORES/ID
# CLUSTER/VNETS/ID
########################################################################
# Datastore
#
# DATASTORE/CLUSTER_ID
# DATASTORE/CLUSTER
########################################################################
# VNet
#
# VNET/CLUSTER_ID
# VNET/CLUSTER
########################################################################
# Hosts
#
# HOST/CLUSTER_ID
# HOST/CLUSTER
########################################################################
init_cluster
check_host_cluster
fix_host_cluster
log_time
check_datastore_cluster
fix_datastore_cluster
log_time
check_network_cluster
fix_network_cluster
log_time
check_fix_cluster
log_time
check_fix_cluster_relations
log_time
########################################################################
# Datastore
#
# DATASTORE/IMAGES/ID
########################################################################
# Image
#
# IMAGE/DATASTORE_ID
# IMAGE/DATASTORE
########################################################################
init_datastore_counters
log_time
check_datastore_image
fix_datastore_image
log_time
check_fix_datastore
log_time
########################################################################
# VM Counters for host, image and vnet usage
########################################################################
init_host_counters
log_time
init_image_counters
log_time
init_network_counters
log_time
init_vrouter_counters
log_time
check_vm
fix_vm
log_time
# VNC
# DATA: VNC Bitmap
check_cluster_vnc_bitmap
fix_cluster_vnc_bitmap
log_time
# history
check_history
fix_history
log_time
########################################################################
# Virtual Routers
#
# VROUTER/VMS/ID
########################################################################
check_vrouter
fix_vrouter
log_time
########################################################################
# DATA: Hosts
#
# HOST/HOST_SHARE/MEM_USAGE
# HOST/HOST_SHARE/CPU_USAGE
# HOST/HOST_SHARE/RUNNING_VMS
# HOST/VMS/ID
########################################################################
check_host
fix_host
log_time
########################################################################
# DATA: Marketplace
#
# MARKETPLACE/MARKETPLACEAPPS/ID
########################################################################
# DATA: App
#
# MARKETPLACEAPP/MARKETPLACE_ID
# MARKETPLACEAPP/MARKETPLACE
# MARKETPLACEAPP/ORIGIN_ID
########################################################################
check_marketplaceapp
fix_marketplaceapp
log_time()
check_marketplace
fix_marketplace
log_time()
########################################################################
# DATA: Image
#
# IMAGE/RUNNING_VMS
# IMAGE/VMS/ID
#
# IMAGE/CLONING_OPS
# IMAGE/CLONES/ID
# IMAGE/APP_CLONES/ID
#
# IMAGE/CLONING_ID
#
# IMAGE/STATE
########################################################################
check_image
fix_image
log_time
########################################################################
# VNet
#
# LEASES
########################################################################
init_network_lease_counters
check_network
fix_network
log_time
########################################################################
# Users
#
# USER QUOTAS
########################################################################
check_fix_quotas('user')
check_fix_quotas('user', 'running')
log_time
########################################################################
# Groups
#
# GROUP QUOTAS
########################################################################
check_fix_quotas('group')
check_fix_quotas('group', 'running')
log_time
########################################################################
# VM Templates
#
# TEMPLATE/OS/BOOT
########################################################################
check_template
fix_template
log_total_errors
return true
end
def log_error(message, repaired=true)
@errors += 1
if repaired
@repaired_errors += 1
else
@unrepaired_errors += 1
end
if !repaired
message = "[UNREPAIRED] " + message
end
log_msg(message)
end
def log_msg(message)
@log_file ||= File.open(LOG, "w")
puts message
@log_file.puts(message)
@log_file.flush
end
def log_total_errors()
puts
log_msg "Total errors found: #{@errors}"
log_msg "Total errors repaired: #{@repaired_errors}"
log_msg "Total errors unrepaired: #{@unrepaired_errors}"
puts "A copy of this output was stored in #{LOG}"
end
def mac_s_to_i(mac)
return nil if mac.nil? || mac.empty?
return mac.split(":").map {|e|
e.to_i(16).to_s(16).rjust(2,"0")}.join("").to_i(16)
end
def mac_i_to_s(mac)
mac_s = mac.to_s(16).rjust(12, "0")
return "#{mac_s[0..1]}:#{mac_s[2..3]}:#{mac_s[4..5]}:"<<
"#{mac_s[6..7]}:#{mac_s[8..9]}:#{mac_s[10..11]}"
end
def ip6_prefix_s_to_i(prefix)
return nil if prefix.empty?
return prefix.split(":", 4).map {|e|
e.to_i(16).to_s(16).rjust(4, "0")}.join("").to_i(16)
end
# Copied from AddressRange::set_ip6 in AddressRange.cc
def mac_to_ip6_suffix(mac_i)
mac = [
mac_i & 0x00000000FFFFFFFF,
(mac_i & 0xFFFFFFFF00000000) >> 32
]
mlow = mac[0]
eui64 = [
4261412864 + (mlow & 0x00FFFFFF),
((mac[1]+512)<<16) + ((mlow & 0xFF000000)>>16) + 0x000000FF
]
return (eui64[1] << 32) + eui64[0]
end
def lease_to_s(lease)
return lease[:ip].nil? ? lease[:mac].to_s : lease[:ip].to_s
end
# Returns the ID of the # disk of a type
# Params:
# +type+:: type name of the disk, can be “hd” or “cdrom”
# +doc+:: Nokogiri::XML::Node describing the VM template
def get_disk_id(type, index, doc, uid)
found_i = -1
doc.root.xpath("TEMPLATE/DISK").each_with_index do |disk, disk_i|
id = disk.at_xpath("IMAGE_ID")
if id
begin
image = get_image_from_id(id.content)
rescue => e
template_id = doc.root.at_xpath("ID").content
STDERR.puts "Error processing template: #{template_id}"
raise e
end
else
image = get_image_from_name(disk, uid)
end
next if image.nil?
if is_image_type_matching?(image, type)
found_i += 1
if (found_i == index)
return disk_i
end
end
end
return nil
end
# Returns a Nokogiri::XML::Node describing an image
# Params:
# +id+:: ID of the image
def get_image_from_id(id)
# if IMAGE_ID has a non positive integer (>=0) return nil
return nil if id !~ /^\d+$/
begin
sql_cmd = "SELECT body FROM image_pool WHERE oid='#{id}'"
row = @db.fetch(sql_cmd).first
rescue => e
STDERR.puts "Error executing: #{sql_cmd}"
raise e
end
# No image found, so unable to get image TYPE
return nil if row.nil?
image = nokogiri_doc(row[:body], 'image_pool')
return image
end
# Returns a Nokogiri::XML::Node describing an image
# Params:
# +disk+:: Nokogiri::XML::Node describing a disk used by a template
def get_image_from_name(disk, template_uid)
name = disk.at_xpath("IMAGE") && disk.at_xpath("IMAGE").content
return nil if name.nil?
uid = disk.at_xpath("IMAGE_UID")
uname = disk.at_xpath("IMAGE_UNAME")
if uid
uid = uid.content
else
if uname
uid = get_user_id(uname.content)
else
uid = template_uid
end
end
return nil if uid.nil?
row = @db.fetch("SELECT body FROM image_pool WHERE name=\"#{name}\" AND uid=#{uid}").first
# No image found, so unable to get image TYPE
return nil if row.nil?
image = nokogiri_doc(row[:body], 'image_pool')
return image
end
# Returns the ID of a user name
# Params:
# +name+:: name of a user
def get_user_id(name)
row = @db.fetch("SELECT uid from user_pool WHERE name=\"#{name}\"").first
return nil if row.nil?
return row[:uid]
end
# Check if an image type match the type used in template BOOT
# Params:
# +image_type+:: doc
# +wanted_type+:: string type extracted from VM template BOOT
def is_image_type_matching?(image, wanted_type)
return false if image.nil? || image.at_xpath("IMAGE/TYPE").nil?
img_type = OpenNebula::Image::IMAGE_TYPES[image.at_xpath("IMAGE/TYPE").text.to_i]
if wanted_type == "hd"
return img_type == "OS" || img_type == "DATABLOCK"
else
return img_type == "CDROM"
end
end
end