-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbd2vhd.py
1386 lines (1209 loc) · 61.2 KB
/
rbd2vhd.py
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python -u
#
# Copyright (C) Roman V. Posudnevskiy ([email protected])
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; version 2.1 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth math.floor, Boston, MA 02110-1301 USA
from __future__ import print_function
from struct import *
import uuid
import sys, getopt
import re
import time
import socket
import select
import threading
verbose = False
debug = False
SECTOR_SIZE = 512
VHD_DEFAULT_BLOCK_SIZE = 2097152
VHD_DYNAMIC_HARDDISK_TYPE = 0x00000003# Dynamic hard disk
VHD_DIFF_HARDDISK_TYPE = 0x00000004# Differencing hard disk
VDI_PREFIX = "VHD-"
SNAPSHOT_PREFIX = "SNAP-"
#-- VHD FOOTTER FIELDs --#
_vhd_footter_cookie_ = 0
_vhd_footter_features_ = 1
_vhd_footter_file_format_version_ = 2
_vhd_footter_data_offset_ = 3
_vhd_footter_time_stamp_ = 4
_vhd_footter_creator_application_ = 5
_vhd_footter_creator_version_ = 6
_vhd_footter_creator_host_os_ = 7
_vhd_footter_original_size_ = 8
_vhd_footter_current_size_ = 9
_vhd_footter_disk_geometry_ = 10
_vhd_footter_disk_type_ = 11
_vhd_footter_checksum_ = 12
_vhd_footter_unique_iq_ = 13
_vhd_footter_saved_state_ = 14
_vhd_footter_hidden_ = 15
_vhd_footter_rbd_image_uuid_ = 16
#-- VHD FOOTTER FIELDs --#
#-- VHD DISK GEOMETRY FIELs --#
_vhd_disk_geometry_cylinders_ = 0
_vhd_disk_geometry_heads_ = 1
_vhd_disk_geometry_sectors_per_cylinder_ = 2
#-- VHD DISK GEOMETRY FIELs --#
#-- DYNAMIC DISK HEADER FIELDs --#
_dynamic_disk_header_cookie_ = 0
_dynamic_disk_header_data_offset_ = 1
_dynamic_disk_header_table_offset_ = 2
_dynamic_disk_header_header_version_ = 3
_dynamic_disk_header_max_table_entries_ = 4
_dynamic_disk_header_block_size_ = 5
_dynamic_disk_header_checksum_ = 6
_dynamic_disk_header_parent_unique_id_ = 7
_dynamic_disk_header_parent_time_stamp_ = 8
_dynamic_disk_header_parent_unicode_name_ = 10
_dynamic_disk_header_parent_locator_entry_1_ = 11
_dynamic_disk_header_parent_locator_entry_2_ = 12
_dynamic_disk_header_parent_locator_entry_3_ = 13
_dynamic_disk_header_parent_locator_entry_4_ = 14
_dynamic_disk_header_parent_locator_entry_5_ = 15
_dynamic_disk_header_parent_locator_entry_6_ = 16
_dynamic_disk_header_parent_locator_entry_7_ = 17
_dynamic_disk_header_parent_locator_entry_8_ = 18
#-- DYNAMIC DISK HEADER FIELDs --#
#-- BATMAP FIELDs --#
_batmap_cookie_ = 0
_batmap_offset_ = 1
_batmap_size_ = 2
_batmap_version_ = 3
_batmap_checksum_ = 4
_batmap_marker_ = 5
#-- BATMAP FIELDs --#
#-- PARENT LOCATOR ENTRY FIELD --#
_parent_locator_platform_code_ = 0
_parent_locator_platform_data_space_ = 1
_parent_locator_platform_data_length_ = 2
_parent_locator_platform_data_offset_ = 4
#-- PARENT LOCATOR ENTRY FIELD --#_
#-- DISK TYPES --#
_disk_type_none = 0
_disk_type_fixed_hard_disk = 2
_disk_type_dynamic_hard_disk = 3
_disk_type_differencing_hard_disk = 4
#-- DISK TYPES --#
#-- PLATFORM CODEs --#
_platform_code_None_ = 0x0
_platform_code_Wi2r_ = 0x57693272
_platform_code_Wi2k_ = 0x5769326B
_platform_code_W2ru_ = 0x57327275
_platform_code_W2ku_ = 0x57326B75
_platform_code_Mac_ = 0x4D616320
_platform_code_MacX_ = 0x4D616358
#-- PLATFORM CODEs --#
VHD_FOTTER_FORMAT = "!8sIIQI4sIIQQ4sII16sBB16s410s"
VHD_FOTTER_RECORD_SIZE = 512
VHD_DISK_GEOMETRY_FORMAT = "!HBB"
VHD_DISK_GEOMETRY_RECORD_SIZE = 4
VHD_DYNAMIC_DISK_HEADER_FORMAT = "!8sQQIIII16sII512s24s24s24s24s24s24s24s24s256s"
VHD_DYNAMIC_DISK_HEADER_RECORD_SIZE = 1024
VHD_PARENT_LOCATOR_ENTRY_FORMAT = "!IIIIQ"
VHD_PARENT_LOCATOR_ENTRY_RECORD_SIZE = 24
VHD_PARENT_LOCATORS_COUNT = 9
VHD_BATMAP_HEADER_FORMAT = "!8sQIIIB483s"
VHD_BATMAP_HEADER_SIZE = 512
#-- RBD DIFF v1 META AND DATA FIELDs --#
RBD_HEADER = "rbd diff v1\n"
RBD_DIFF_META_ENDIAN_PREFIX = "<"#bigendian ! or littleendian <
RBD_DIFF_META_RECORD_TAG = "c"
RBD_DIFF_META_RECORD_TAG_SIZE = 1
RBD_DIFF_META_SNAP = "I"
RBD_DIFF_META_SNAP_SIZE = 4
RBD_DIFF_META_SIZE = "Q"
RBD_DIFF_META_SIZE_SIZE = 8
RBD_DIFF_DATA = "QQ"
RBD_DIFF_DATA_SIZE = 16
#-- RBD DIFF v1 META AND DATA FIELDs --#
NBD_INIT_PASSWD = 'NBDMAGIC'
NBD_INIT_PASSWD_HEX = 0x4e42444d41474943
NBD_CLISERVER_MAGIC = 0x00420281861253 #cliserv_magic
NBD_REQUEST_MAGIC = 0x25609513 #NBD_REQUEST_MAGIC
NBD_REPLY_MAGIC = 0x67446698 #NBD_REPLY_MAGIC
NBD_NEGOTIATION_FORMAT = "!8sQQHH124s"
NBD_NEGOTIATION_SIZE = 152
NBD_REQUEST_HEADER_FORMAT = "!LHHQQL"
NBD_REQUEST_HEADER_SIZE = 28
NBD_REPLY_HEADER_FORMAT = "!LLQ"
NBD_REPLY_HEADER_SIZE = 16
NBD_CHUNK_SIZE = SECTOR_SIZE*1024
_nbd_negotiation_init_passwd_ = 0
_nbd_negotiation_cliserver_magic_ = 1
_nbd_negotiation_export_size_ = 2
_nbd_negotiation_handshake_flags_ = 3
_nbd_negotiation_transmission_flags_ = 4
_nbd_negotiation_reserved_ = 5
# NBD Handshake flags
NBD_FLAG_FIXED_NEWSTYLE = 1
NBD_FLAG_NO_ZEROES = 2
#NBD Transmission flags
NBD_FLAG_HAS_FLAGS = 1
NBD_FLAG_READ_ONLY = 2
NBD_FLAG_SEND_FLUSH = 4
NBD_FLAG_SEND_FUA = 8
NBD_FLAG_ROTATIONAL = 16
NBD_FLAG_SEND_TRIM = 32
NBD_FLAG_SEND_WRITE_ZEROES = 64
NBD_FLAG_SEND_DF = 128 #defined by the experimental STRUCTURED_REPLY extension.
NBD_FLAG_CAN_MULTI_CONN = 256
NBD_FLAG_SEND_BLOCK_STATUS = 512 # defined by the experimental BLOCK_STATUS extension.
NBD_FLAG_SEND_RESIZE = 1024 #defined by the experimental RESIZE extension.
#NBD Command flags
NBD_CMD_FLAG_FUA = 1
NBD_CMD_FLAG_NO_HOLE = 2
NBD_CMD_FLAG_DF = 4 # defined by the experimental STRUCTURED_REPLY extension.
#NBD Request types
NBD_CMD_READ = 0
NBD_CMD_WRITE = 1
NBD_CMD_DISC = 2
NBD_CMD_FLUSH = 3
NBD_CMD_TRIM = 4
NBD_CMD_WRITE_ZEROES = 6
NBD_CMD_BLOCK_STATUS = 7 # Defined by the experimental BLOCK_STATUS extension.
NBD_CMD_RESIZE = 8 # Defined by the experimental RESIZEextension.
#NBD Error values
EPERM = 1 # Operation not permitted.
EIO = 5 # Input/output error.
ENOMEM = 12 # Cannot allocate memory.
EINVAL = 22 # Invalid argument.
ENOSPC = 28 # No space left on device.
EOVERFLOW = 75 # defined in the experimental STRUCTURED_REPLY extension.
ESHUTDOWN = 108 # Server is in the process of being shut down.
_nbd_request_magic_ = 0
_nbd_request_cmd_flags_ = 1
_nbd_request_type_ = 2
_nbd_request_handle_ = 3
_nbd_request_offset_ = 4
_nbd_request_length = 5
_nbd_reply_magic_ = 0
_nbd_reply_error_ = 1
_nbd_reply_handle_ = 2
#-------------------------------------------------------------------------------------------------------------------------------------------------------#
def hexdump(s):
return "".join("{:02x}".format(ord(c)) for c in s)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.stderr.flush
def INFO(string):
calling_func = sys._getframe(1).f_code.co_name
if (verbose is True) or (debug is True):
eprint("[INFO][%s]: %s" % (calling_func, string))
def DEBUG(string):
calling_func = sys._getframe(1).f_code.co_name
if debug is True:
eprint("[DEBUG][%s]: %s" % (calling_func, string))
def ERROR(string):
calling_func = sys._getframe(1).f_code.co_name
if verbose is True:
eprint("[ERROR][%s]: %s" % (calling_func, string))
elif debug is True:
eprint("[ERROR][%s]: %s" % (calling_func, string))
else:
eprint("[ERROR][%s]: %s" % (calling_func, string))
def MROUTPUT(string):
length = len(string)
if length>0:
print(pack("!8B",0,0,0,0,0,0,0,0),end="")
print(pack("!B",length),end="")
print(pack("!3B",0,0,0),end="")
print(pack("!%ds" % length, string),end="")
else:
print(pack("!8B",0,0,0,0,0,0,0,0),end="")
print(pack("!B",0),end="")
print(pack("!3B",0,0,0),end="")
def modTupleByIndex(tup, index, ins):
return tuple(tup[0:index]) + (ins,) + tuple(tup[index+1:])
def checksum(vhd_record):
checksum = 0
b = bytearray()
b.extend(vhd_record)
for index in range(len(vhd_record)):
checksum += b[index]
checksum = ~checksum + 2**32
return checksum
def get_size_aligned_to_sector_boundary(size):
if size%SECTOR_SIZE>0:
aligned_size = ((size//SECTOR_SIZE)+1)*SECTOR_SIZE
else:
aligned_size = size
return aligned_size
def get_bitmap_size(dynamic_disk_header):
sectors_in_block = dynamic_disk_header[_dynamic_disk_header_block_size_]/SECTOR_SIZE
bitmap_size = sectors_in_block/8
return get_size_aligned_to_sector_boundary(bitmap_size)
def gen_empty_bitarray_for_bitmap(bitmap_size):
bitarray = []
for bitmap_index in range(bitmap_size):
for bit_index in range(8):
bitarray.append(0)
return bitarray
def gen_bitmap_from_bitarray(bitarray):
_bytearray_ = {}
bitmap = ''
for bitarray_index in range(len(bitarray)):
bit = 0
byte_index = bitarray_index//8
bit_in_byte = bitarray_index%8
if bitarray[bitarray_index] == 1:
bit = 128 >> bit_in_byte
if _bytearray_.has_key(byte_index):
_bytearray_[byte_index] = _bytearray_[byte_index] | bit
else:
_bytearray_[byte_index] = bit
for byte_index in range(len(_bytearray_)):
bitmap = bitmap + pack('!c', chr(_bytearray_[byte_index]))
return bitmap
def get_bitarray_from_bitmap(bitmap, bitmap_size):
bitarray = []
_bitmap_ = bytearray()
_bitmap_.extend(bitmap)
for bitmap_index in range(bitmap_size):
for bit_index in range(8):
offset = 128 >> bit_index
if (_bitmap_[bitmap_index] & offset) > 0:
bitarray.append(1)
else:
bitarray.append(0)
return bitarray
def gen_empty_vhd_bat(image_size):
bat_list = []
max_tab_entries = image_size / VHD_DEFAULT_BLOCK_SIZE
for bat_index in range(max_tab_entries):
bat_list.append(0xffffffff)
return bat_list
def gen_empty_batmap():
return pack("!%ds" % (SECTOR_SIZE*2), '')
def gen_batmap_header(batmap):
reserved = ''
for i in range(483):
reserved = reserved + pack('!c', chr(0))
batmap_header_struct = ('tdbatmap', 0, 0, 0x00010002, 0, 0, reserved) #empty
return batmap_header_struct
def pack_vhd_bat(bat_list):
max_tab_entries = len(bat_list)
bat = ''
for bat_index in range(max_tab_entries):
bat = bat + pack("!I", bat_list[bat_index])
if max_tab_entries < SECTOR_SIZE/4:
bat = bat + pack("!%ds" % (SECTOR_SIZE - max_tab_entries*4), '')
return bat
def gen_vhd_geometry_struct(image_size):
totalSectors = image_size / SECTOR_SIZE
if totalSectors > 65535*16*255:
totalSectors = 65535*16*255
if totalSectors >= 65535*16*255:
sectorsPerTrack = 255
heads = 16
cylinderTimesHeads = totalSectors / sectorsPerTrack
else:
sectorsPerTrack = 17
cylinderTimesHeads = totalSectors / sectorsPerTrack
heads = (cylinderTimesHeads + 1023) / 1024
if heads < 4:
heads = 4
if (cylinderTimesHeads >= (heads * 1024)) or (heads > 16):
sectorsPerTrack = 31
heads = 16
cylinderTimesHeads = totalSectors / sectorsPerTrack
if cylinderTimesHeads >= (heads * 1024):
sectorsPerTrack = 63
heads = 16
cylinderTimesHeads = totalSectors / sectorsPerTrack
cylinders = cylinderTimesHeads / heads
geometry_struct = (cylinders, heads, sectorsPerTrack)
#INFO("[gen_vhd_geometry_struct]: totalSectors = %d, Cyliders = %d, heads = %d, sectors per track = %d" % (totalSectors, cylinders, heads, sectorsPerTrack))
return geometry_struct
def gen_vhd_footer_struct(disk_type, image_size, vhd_uuid, rbd_uuid, checksum):
vhd_geometry_struct = gen_vhd_geometry_struct(image_size)
vhd_geometry = pack(VHD_DISK_GEOMETRY_FORMAT, vhd_geometry_struct[0], vhd_geometry_struct[1], vhd_geometry_struct[2])
reserved = ''
for i in range(410):
reserved = reserved + pack('!c', chr(0))
vhd_footer_struct = ('conectix', 0x00000002, 0x00010000, 0x00000200, time.time()-946684800, 'tap', 0x00010003,
0x00000000, image_size, image_size, vhd_geometry, disk_type, checksum, vhd_uuid, 0, 0,
rbd_uuid, reserved)
return vhd_footer_struct
def gen_vhd_dynamic_disk_header_struct(table_offset, image_size, checksum, parent_uuid, parent_unicode_name):
max_tab_entries = image_size / VHD_DEFAULT_BLOCK_SIZE
reserved = ''
for i in range(256):
reserved = reserved + pack('!c', chr(0))
parent_locator_entry_empty_struct = (0x00000000, 0, 0, 0, 0) #empty
parent_locator_entry_empty = pack(VHD_PARENT_LOCATOR_ENTRY_FORMAT, *parent_locator_entry_empty_struct)
dynamic_disk_header_struct = ('cxsparse', 0xffffffffffffffff, table_offset, 0x00010000, max_tab_entries,
VHD_DEFAULT_BLOCK_SIZE, checksum, parent_uuid, 0, 0x00000000, parent_unicode_name.encode("UTF-16BE"),
parent_locator_entry_empty, parent_locator_entry_empty, parent_locator_entry_empty,
parent_locator_entry_empty, parent_locator_entry_empty, parent_locator_entry_empty,
parent_locator_entry_empty, parent_locator_entry_empty,
reserved)
return dynamic_disk_header_struct
def get_sector_bitmap_and_data(vhdfile, data_block_offset, block_size):
sectors_in_block = block_size/SECTOR_SIZE
bitmap_size = sectors_in_block/8
if bitmap_size%512>0:
bitmap_size=((bitmap_size//512)+1)*512
_format_ = "!%is%is" % (bitmap_size,block_size)
vhdfile.seek((data_block_offset)*512, 0)
BUFFER=vhdfile.read(bitmap_size+block_size)
bitmap_and_data=unpack(_format_,BUFFER)
_format_ = "!"
for i in range(sectors_in_block):
_format_ = _format_ + "512s"
data = unpack(_format_,bitmap_and_data[1])
return [bitmap_and_data[0],data]
def get_raw_byte_offset_of_sector(block_number, sector_in_block, block_size, sector_size):
return block_number*block_size+sector_in_block*sector_size
def get_raw_sector_offset_of_sector(block_number, sector_in_block, block_size, sector_size):
sector_per_block = block_size / sector_size
return block_number*sector_per_block+sector_in_block
def nbd_close_channel(sock, handle):
INFO("NBD: Going to send disconnect request with handle %d" % handle)
flags = 0
request_header = pack(NBD_REQUEST_HEADER_FORMAT, NBD_REQUEST_MAGIC, flags, NBD_CMD_DISC, handle, 0, 0)
DEBUG("NBD: Request header: %s" % hexdump(request_header))
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing")
break
else:
DEBUG("NBD: Socket isn't ready for writing")
sock.sendall(request_header)
INFO("NBD: Disconnect request has been sent")
sock.close()
INFO("NBD: Socket has been cosed")
def nbd_open_channel(uri):
uri_pattern = "(.+)://(.+)/services/SM/nbd/(.+)/(.+)/(.+)\?session_id=OpaqueRef\%3a(.+)"
re_pattern = re.compile(uri_pattern)
re_result = re_pattern.search(uri)
proto = re_result.group(1)
server = re_result.group(2)
sr_uuid = re_result.group(3)
vdi_uuid = re_result.group(4)
dp_uuid = re_result.group(5)
session_id = re_result.group(6)
if (proto == 'http'):
port = 80
else:
ERROR("NBD: Unsupported protocol '%s'" % proto)
sys.exit(3)
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port on server
INFO("NBD: Going to connect to server %s port %s" % (server, port))
sock.connect((server, port))
DEBUG("NBD: Going to send HTTP PUT request")
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing")
break
else:
DEBUG("NBD: Socket isn't ready for writing")
#eprint("PUT /services/SM/nbd/%s/%s/%s?session_id=OpaqueRef%%3a%s HTTP/1.1\r\nHost: %s\r\n\r\n" % (sr_uuid, vdi_uuid, dp_uuid, session_id, server))
sock.sendall("PUT /services/SM/nbd/%s/%s/%s?session_id=OpaqueRef%%3a%s HTTP/1.1\r\nHost: %s\r\n\r\n" % (sr_uuid, vdi_uuid, dp_uuid, session_id, server))
DEBUG("NBD: Waiting for reply")
while True:
ready = select.select([sock],[],[])
if ready[0]:
DEBUG("NBD: Socket ready for reading")
break
else:
DEBUG("NBD: Socket isn't ready for reading")
reply = sock.recv(102)
DEBUG("NBD: Reply has been received")
#eprint(reply)
re_pattern = re.compile("(.*) (\d+) (\w+)")
re_result = re_pattern.search(reply)
if (re_result.group(3) == "OK"):
re_pattern = re.compile("Transfer-encoding: (\w+)")
re_result = re_pattern.search(reply)
return (sock, re_result.group(1))
else:
ERROR("NBD: Invalid HTTP response %s" % reply)
sock.close()
sys.exit(4)
DEBUG("NBD: Server has been connected")
def nbd_negotiate(sock):
INFO("NBD: Negotiation has been started")
while True:
ready = select.select([sock],[],[])
if ready[0]:
DEBUG("NBD: Socket ready for reading")
break
else:
DEBUG("NBD: Socket isn't ready for reading")
reply = sock.recv(NBD_NEGOTIATION_SIZE+1)
negotiate_reply = unpack(NBD_NEGOTIATION_FORMAT, reply)
DEBUG("NBD: Negotiation reply size = %d" % len(reply))
DEBUG("NBD: Size = %d" % negotiate_reply[_nbd_negotiation_export_size_])
DEBUG("NBD: Init_passwd = %s" % negotiate_reply[_nbd_negotiation_init_passwd_])
DEBUG("NBD: Magic = 0x%016x" % negotiate_reply[_nbd_negotiation_cliserver_magic_])
DEBUG("NBD: Handshake flags = 0x%08x" % negotiate_reply[_nbd_negotiation_handshake_flags_])
DEBUG("NBD: Transmission flags = 0x%08x" % negotiate_reply[_nbd_negotiation_transmission_flags_])
if (negotiate_reply[_nbd_negotiation_init_passwd_] != NBD_INIT_PASSWD):
ERROR("NBD: Bad magic in negotiate")
sock.close()
sys.exit(6)
if (negotiate_reply[_nbd_negotiation_cliserver_magic_] != NBD_CLISERVER_MAGIC):
ERROR("NBD: Bad cliserver magic in negotiate")
sock.close()
sys.exit(7)
INFO("NBD: Negotiation has been finished")
return (negotiate_reply[_nbd_negotiation_export_size_], negotiate_reply[_nbd_negotiation_transmission_flags_])
def nbd_send_write(sock, handle, request_handles, offset, length, data):
INFO("NBD: Going to send write data request(s) with handle %d" % handle)
DEBUG("NBD: Length from rbd = %d, length of data to send = %d" % (length, len(data)))
flags = 0
buffer_offset = 0
while length > 0:
DEBUG("NBD: Going to send request with handle = %d" % handle)
if length >= NBD_CHUNK_SIZE:
_buffer_ = data[buffer_offset:buffer_offset+NBD_CHUNK_SIZE]
request_header = pack(NBD_REQUEST_HEADER_FORMAT, NBD_REQUEST_MAGIC, flags, NBD_CMD_WRITE, handle, offset+buffer_offset, NBD_CHUNK_SIZE)
DEBUG("NBD: buffer_offset = %d, offset = %d, length = %d, calculated length of data to send = %d,length of _buffer_ %d" % (buffer_offset, offset, length, NBD_CHUNK_SIZE, len(_buffer_)))
else:
_buffer_ = data[buffer_offset:]
request_header = pack(NBD_REQUEST_HEADER_FORMAT, NBD_REQUEST_MAGIC, flags, NBD_CMD_WRITE, handle, offset+buffer_offset, length)
DEBUG("NBD: buffer_offset = %d, offset = %d, length = %d, calculated length of data to send = %d,length of _buffer_ %d" % (buffer_offset, offset, length, length, len(_buffer_)))
DEBUG("NBD: Request header: %s" % hexdump(request_header))
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing request header")
break
else:
DEBUG("NBD: Socket isn't ready for writing request header")
sock.sendall(request_header)
INFO("NBD: Going to send body for request with handle %d" % handle)
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing body")
break
else:
DEBUG("NBD: Socket isn't ready for writing body")
sock.sendall(_buffer_)
INFO("NBD: Write data request with handle %d has been sent" % handle)
buffer_offset += NBD_CHUNK_SIZE
length -= NBD_CHUNK_SIZE
request_handles[handle] = True
handle += 1
return (handle, request_handles)
def nbd_send_write_zeros(sock, handle, offset, length):
INFO("NBD: Going to send write zeros request with handle %d" % handle)
flags = 0
request_header = pack(NBD_REQUEST_HEADER_FORMAT, NBD_REQUEST_MAGIC, flags, NBD_CMD_WRITE_ZEROES, handle, offset, length)
DEBUG("NBD: Request header: %s" % hexdump(request_header))
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing request header")
break
else:
DEBUG("NBD: Socket isn't ready for writing request header")
sock.sendall(request_header)
INFO("NBD: Wrire zeros request with handle %d has been sent" % handle)
def nbd_send_read(sock, handle, offset, length):
INFO("NBD: Going to send read request with handle %d" % handle)
flags = 0
request_header = pack(NBD_REQUEST_HEADER_FORMAT, NBD_REQUEST_MAGIC, flags, NBD_CMD_READ, handle, offset, length)
DEBUG("NBD: Request header: %s" % hexdump(request_header))
while True:
ready = select.select([],[sock],[])
if ready[1]:
DEBUG("NBD: Socket ready for writing")
break
else:
DEBUG("NBD: Socket isn't ready for writing")
sock.sendall(request_header)
INFO("NBD: Read request with handle %d has been sent" % handle)
#-------------------------------------------------------------------------------------------------------------------------------------------------------#
def rbd2nbd(rbd, uri, progress, mrout):
if rbd == "-":
RBDDIFF_FH = sys.stdin
else:
RBDDIFF_FH = open(rbd, "rb")
rbd_meta_read_finished = 0
_prev_percent_ = 0
_offset_ = 0
handle_index = 10
finished = False
request_handles = {}
#request_handles_lock = threading.Lock()
(sock, encoding) = nbd_open_channel(uri)
if (encoding != 'nbd'):
ERROR("NBD: Unsupported encoding `%s`" % encoding)
nbd_close_channel(sock, 0)
sys.exit(5)
else:
INFO("NBD: Encoding: `%s`" % encoding)
def nbd_receive_reply(sock):
INFO("NBD: Replies reciver thread has been started")
DEBUG("NBD: len(request_handles)=%d, finished=%s" % (len(request_handles), finished))
while (len(request_handles)>0 or (finished == False & len(request_handles)==0)):
while True:
ready = select.select([sock],[],[])
if ready[0]:
DEBUG("NBD: Socket ready for reading")
break
else:
DEBUG("NBD: Socket isn't ready for reading")
reply = unpack(NBD_REPLY_HEADER_FORMAT, sock.recv(NBD_REPLY_HEADER_SIZE))
if reply[_nbd_reply_magic_] != NBD_REPLY_MAGIC:
ERROR("NBD: Bad magic in received reply")
INFO("NBD: Recived reply for handle %d" % reply[_nbd_reply_handle_])
#request_handles_lock.acquire()
request_handles.pop(reply[_nbd_reply_handle_])
#request_handles_lock.release()
INFO("NBD: Replies reciver thread has been finished")
rbd_header = RBDDIFF_FH.read(len(RBD_HEADER))
if (progress):
if (mrout):
MROUTPUT("Progress: 0")
else:
eprint("Progress: 0")
(nbd_size, nbd_trans_flags) = nbd_negotiate(sock)
DEBUG("NBD: Prepare replies reciver thread")
t = threading.Thread(target=nbd_receive_reply, args=(sock,))
t.start()
DEBUG("RBD: Start RBD diff reading")
while True:
record_tag = RBDDIFF_FH.read(RBD_DIFF_META_RECORD_TAG_SIZE)
#record_tag = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_RECORD_TAG), record)
if not record_tag:
INFO("RBD: Unexpected EOF")
break
else:
INFO("RBD: Record TAG = \'%c\'" % record_tag)
if record_tag == "e":
INFO("RBD: Got EOF record TAG")
break
if record_tag == "f":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
from_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
from_snap_name = regex.sub('', from_snap_name)
INFO("RBD: From snap = %s" % from_snap_name)
elif record_tag == "t":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
to_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
to_snap_name = regex.sub('', to_snap_name)
INFO("RBD: To snap = %s" % to_snap_name)
elif record_tag == "s":
record = RBDDIFF_FH.read(RBD_DIFF_META_SIZE_SIZE)
image_size = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SIZE), record)[0])
INFO("RBD: Image size = %d" % image_size)
elif record_tag == "w":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
elif record_tag == "z":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Zero data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
else:
ERROR("RBD: Error while reading rbd_diff file")
nbd_close_channel(sock, 0)
sys.exit(2)
if (rbd_meta_read_finished == 1):
#request_handles_lock.acquire()
if record_tag == "w":
_buffer_ = RBDDIFF_FH.read(length)
(handle_index, request_handles) = nbd_send_write(sock, handle_index, request_handles, offset, length, _buffer_)
elif record_tag == "z":
if (nbd_trans_flags & NBD_FLAG_SEND_WRITE_ZEROES):
nbd_send_write_zeros(sock, handle_index, offset, length)
else:
_buffer_ = pack("!%ds" % length, '')
(handle_index, request_handles) = nbd_send_write(sock, handle_index, request_handles, offset, length, _buffer_)
#request_handles_lock.release()
_offset_ = offset + length
#time.sleep(0.05)
if (progress):
_percent_ = (100*_offset_)//image_size
if _prev_percent_ != _percent_ :
_prev_percent_ = _percent_
if (mrout):
MROUTPUT("Progress: %d" % _percent_)
else:
eprint("Progress: %d" % _percent_)
finished = True
t.join()
if (progress):
if (mrout):
MROUTPUT("Progress: 100")
MROUTPUT("")
else:
eprint("Progress: 100")
if RBDDIFF_FH is not sys.stdin:
RBDDIFF_FH.close
nbd_close_channel(sock, handle_index)
return 0
#-------------------------------------------------------------------------------------------------------------------------------------------------------#
def rbd2raw(rbd, raw, progress, mrout):
RAW_FH = open(raw, "wb")
if rbd == "-":
RBDDIFF_FH = sys.stdin
else:
RBDDIFF_FH = open(rbd, "rb")
rbd_meta_read_finished = 0
_prev_percent_ = 0
_offset_ = 0
rbd_header = RBDDIFF_FH.read(len(RBD_HEADER))
if (progress):
if (mrout):
MROUTPUT("Progress: 0")
else:
eprint("Progress: 0")
while True:
record_tag = RBDDIFF_FH.read(RBD_DIFF_META_RECORD_TAG_SIZE)
#record_tag = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_RECORD_TAG), record)
if not record_tag:
INFO("RBD: Unexpected EOF")
break
else:
INFO("RBD: Record TAG = \'%c\'" % record_tag)
if record_tag == "e":
INFO("RBD: Got EOF record TAG")
break
if record_tag == "f":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
from_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
from_snap_name = regex.sub('', from_snap_name)
INFO("RBD: From snap = %s" % from_snap_name)
elif record_tag == "t":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
to_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
to_snap_name = regex.sub('', to_snap_name)
INFO("RBD: To snap = %s" % to_snap_name)
elif record_tag == "s":
record = RBDDIFF_FH.read(RBD_DIFF_META_SIZE_SIZE)
image_size = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SIZE), record)[0])
INFO("RBD: Image size = %d" % image_size)
elif record_tag == "w":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
elif record_tag == "z":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Zero data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
else:
ERROR("RBD: Error while reading rbd_diff file")
sys.exit(2)
if (rbd_meta_read_finished == 1):
if record_tag == "w":
_buffer_ = RBDDIFF_FH.read(length)
elif record_tag == "z":
_buffer_ = pack("!%ds" % length, '')
if (_offset_ == 0):
RAW_FH.seek(offset,1)
_offset_ = offset + length
RAW_FH.write(_buffer_)
else:
RAW_FH.seek(offset-_offset_,1)
_offset_ = offset + length
RAW_FH.write(_buffer_)
if (progress):
_percent_ = (100*_offset_)//image_size
if _prev_percent_ != _percent_ :
_prev_percent_ = _percent_
if (mrout):
MROUTPUT("Progress: %d" % _percent_)
else:
eprint("Progress: %d" % _percent_)
if (progress):
if (mrout):
MROUTPUT("Progress: 100")
MROUTPUT("")
else:
eprint("Progress: 100")
RAW_FH.close
if RBDDIFF_FH is not sys.stdin:
RBDDIFF_FH.close
return 0
#-------------------------------------------------------------------------------------------------------------------------------------------------------#
def rbd2vhd(rbd, vhd, rbd_image_uuid, progress, mrout):
VHD_FH = open(vhd, "wb")
if rbd == "-":
RBDDIFF_FH = sys.stdin
else:
RBDDIFF_FH = open(rbd, "rb")
rbd_header = RBDDIFF_FH.read(len(RBD_HEADER))
rbd_meta_read_finished = 0
vhd_headers_written = 0
blocks_bitmaps = {}
from_snap_name = ''
to_snap_name = ''
parent_exists = False
rbd_eof = False
rbd_data_exists = False
allocated_block_count=0
last_written_sector_in_block = 0
_prev_percent_ = 0
while True:
record_tag = RBDDIFF_FH.read(RBD_DIFF_META_RECORD_TAG_SIZE)
#record_tag = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_RECORD_TAG), record)
if not record_tag:
INFO("RBD: Unexpected EOF")
break
else:
INFO("RBD: Record TAG = \'%c\'" % record_tag)
if record_tag == "e":
INFO("RBD: Got EOF record TAG")
rbd_eof = True
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
if record_tag == "f":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
from_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
from_snap_name = regex.sub('', from_snap_name)
INFO("RBD: From snap = %s" % from_snap_name)
elif record_tag == "t":
record = RBDDIFF_FH.read(RBD_DIFF_META_SNAP_SIZE)
snap_name_length = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SNAP), record)[0])
record = RBDDIFF_FH.read(snap_name_length)
to_snap_name = unpack("%s%ds" % (RBD_DIFF_META_ENDIAN_PREFIX, snap_name_length),record)[0]
regex = re.compile(SNAPSHOT_PREFIX)
to_snap_name = regex.sub('', to_snap_name)
INFO("RBD: To snap = %s" % to_snap_name)
elif record_tag == "s":
record = RBDDIFF_FH.read(RBD_DIFF_META_SIZE_SIZE)
image_size = int(unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_META_SIZE), record)[0])
INFO("RBD: Image size = %d" % image_size)
elif record_tag == "w":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
rbd_data_exists = True
elif record_tag == "z":
record = RBDDIFF_FH.read(RBD_DIFF_DATA_SIZE)
_record_ = unpack("%s%s" % (RBD_DIFF_META_ENDIAN_PREFIX, RBD_DIFF_DATA), record)
offset = _record_[0]
length = _record_[1]
INFO("RBD: Zero data offset = 0x%08x and length = %d" % (offset, length))
if rbd_meta_read_finished == 0:
rbd_meta_read_finished = 1
rbd_data_exists = True
elif (rbd_eof == False):
ERROR("RBD: Error while reading rbd_diff file")
sys.exit(2)
if (rbd_meta_read_finished == 1) & (vhd_headers_written == 0):
if rbd_image_uuid == '':
ERROR("RBD: RBD image UUID is not specified")
sys.exit(1)
if from_snap_name:
parent_uuid = uuid.UUID(from_snap_name)
parent_exists = True
else:
parent_uuid = uuid.UUID('00000000-0000-0000-0000-000000000000')
parent_exists = False
if to_snap_name:
vhd_uuid = uuid.UUID(to_snap_name)
rbd_uuid = uuid.UUID(rbd_image_uuid)
else:
vhd_uuid = uuid.UUID(rbd_image_uuid)
rbd_uuid = uuid.UUID(rbd_image_uuid)
if parent_exists:
vhd_footer_struct = gen_vhd_footer_struct(VHD_DIFF_HARDDISK_TYPE, image_size, vhd_uuid.bytes, rbd_uuid.bytes, 0)
else:
vhd_footer_struct = gen_vhd_footer_struct(VHD_DYNAMIC_HARDDISK_TYPE, image_size, vhd_uuid.bytes, rbd_uuid.bytes, 0)
VHD_FOOTER = pack(VHD_FOTTER_FORMAT, *vhd_footer_struct)
vhd_footer_struct = modTupleByIndex(vhd_footer_struct, _vhd_footter_checksum_, checksum(VHD_FOOTER))
VHD_FOOTER = pack(VHD_FOTTER_FORMAT, *vhd_footer_struct)
if parent_exists:
vhd_dynamic_disk_header_struct = gen_vhd_dynamic_disk_header_struct(VHD_FOTTER_RECORD_SIZE+VHD_DYNAMIC_DISK_HEADER_RECORD_SIZE, image_size, 0, parent_uuid.bytes, "%s.vhd" % str(parent_uuid))
vhd_dynamic_disk_header_struct = modTupleByIndex(vhd_dynamic_disk_header_struct, _dynamic_disk_header_parent_time_stamp_, time.time()-946684800) #????
else:
vhd_dynamic_disk_header_struct = gen_vhd_dynamic_disk_header_struct(VHD_FOTTER_RECORD_SIZE+VHD_DYNAMIC_DISK_HEADER_RECORD_SIZE, image_size, 0, '', '')
VHD_DYNAMIC_DISK_HEADER = pack(VHD_DYNAMIC_DISK_HEADER_FORMAT, *vhd_dynamic_disk_header_struct)
vhd_dynamic_disk_header_struct = modTupleByIndex(vhd_dynamic_disk_header_struct, _dynamic_disk_header_checksum_, checksum(VHD_DYNAMIC_DISK_HEADER))
VHD_DYNAMIC_DISK_HEADER = pack(VHD_DYNAMIC_DISK_HEADER_FORMAT, *vhd_dynamic_disk_header_struct)
vhd_bat_list = gen_empty_vhd_bat(image_size)
VHD_BAT = pack_vhd_bat(vhd_bat_list)
vhd_file_offset = 0
VHD_FH.write(VHD_FOOTER)
vhd_file_offset += VHD_FOTTER_RECORD_SIZE
VHD_FH.write(VHD_DYNAMIC_DISK_HEADER)
vhd_file_offset += VHD_DYNAMIC_DISK_HEADER_RECORD_SIZE
VHD_FH.write(VHD_BAT)