forked from lvmguy/thinp-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvconvert_example
executable file
·120 lines (96 loc) · 3.37 KB
/
lvconvert_example
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
#!/usr/bin/env ruby
require 'config'
require 'lib/device-mapper/dm'
require 'lib/log'
require 'lib/utils'
require 'lib/thinp-mixin'
require 'lib/xml_format'
require 'stringio'
#----------------------------------------------------------------
# This example show the steps neccessary to convert an existing lv
# into a new thin pool and thin device.
class LVConvert
include ThinpTestMixin
include TinyVolumeManager
include Utils
include XMLFormat
def initialize
setup
# create a new volume manager
@tvm = VM.new
@tvm.add_allocation_volume(@data_dev, 0, dev_size(@data_dev))
end
def create_linear_metadata(lv_size)
# Create some metadata that contains a mapping for a single thin
# device that corresponds to our lv.
nr_blocks = lv_size / @data_block_size
superblock = Superblock.new("the-uuid", 0, 1, @data_block_size, nr_blocks)
devices = [Device.new(0, 1, 0, 0, 0, [Mapping.new(0, 0, nr_blocks, 0)])]
Metadata.new(superblock, devices)
end
def log_metadata(metadata)
io = StringIO.new
write_xml(metadata, io)
debug io.string
end
def write_metadata(md_dev, metadata)
Utils::with_temp_file('xml_mapping') do |f|
# Write it to a temporary file ...
write_xml(metadata, f)
f.flush
f.close
# ... and restore it to the metadata device
restore_metadata(f.path, md_dev)
end
end
def setup_metadata(md_dev, lv_size)
metadata = create_linear_metadata(lv_size)
log_metadata(metadata)
write_metadata(md_dev, metadata)
end
def run
# We'll just use a little linear mapping for the example lv.
lv_name = 'lv'
lv_size = dev_size(@data_dev) / 2 # leaving space for metadata
@tvm.add_volume(linear_vol(lv_name, lv_size))
# now we activate it ...
with_dev(@tvm.table(lv_name)) do |lv|
debug 'activated original lv'
# The first thing to do is set up a new metadata dev. We can do
# this without suspending the original lv.
@tvm.add_volume(linear_vol('pool_metadata', lv_size / 4))
with_dev(@tvm.table('pool_metadata')) do |md|
debug 'activated metadata device'
setup_metadata(md, lv_size)
debug 'restored metadata'
# Now we setup the data device. This has the same table as
# the currently active lv, so we must suspend the origin lv.
data_table = lv.active_table
lv.suspend
debug 'suspended lv'
with_dev(data_table) do |data|
debug 'activated data dev'
# now we can assemble the pool
pool_table = Table.new(ThinPoolTarget.new(lv_size, md, data, @data_block_size, 1))
with_dev(pool_table) do |pool|
debug 'activated pool'
# finally we can load a new table into the original lv
# device and switch over to it
thin_table = Table.new(ThinTarget.new(lv_size, pool, 0))
lv.load(thin_table)
lv.resume
# do some io to the lv just to check it's ok
wipe_device(lv)
# We have to deactivate the lv before the pool because
# it's now a thin. A crufty side-effect of me using all
# these lexically scoped 'with_' functions.
lv.remove
end
end
end
end
end
end
#----------------------------------------------------------------
convert_example = LVConvert.new
convert_example.run