/
setup_lvm.sh
executable file
·90 lines (74 loc) · 2.28 KB
/
setup_lvm.sh
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
#!/bin/bash -uex
# setup_lvm.sh: set up lvm on a loopback file
# Copyright (C) 2017 Cisco Inc
#
# 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.
#set -x
. $(dirname $0)/common.sh
loopdev=""
if [ -f .lpack.lvm.loopdev ]; then
loopdev=$(cat .lpack.lvm.loopdev)
fi
dokpartx() {
local kpartx_ret=$(sudo kpartx -vas $1)
[ -z "$kpartx_ret" ] && {
log "Failed to map image partitions into LVM"
exit 1
}
local kpartx_ret=$(sudo kpartx -vas $1)
local loopparts=( `echo ${kpartx_ret} | fmt -w 1 | grep ^loop` )
loopdev=${loopparts[0]::${#loopparts[0]}-2}
echo $loopdev > .lpack.lvm.loopdev
}
id_check
needattach=1
createdfile=0
# This is dangerous and insufficient - we need a way
# to make sure that *our* lofile is attached to this device
if [ -f .lpack.lvm.loopdev ]; then
dev=$(cat .lpack.lvm.loopdev)
sz=$(cat /sys/block/${dev}/size)
if [ $sz -ne 0 ]; then
loopdev="$dev"
needattach=0
fi
fi
if [ ! -f "${lofile}" ]; then
createdfile=1
truncate -s "${lvsize}" "${lofile}"
sfdisk "${lofile}" << EOF
, 2G;
,,8e;
EOF
sync
fi
if [ "$needattach" = "1" ]; then
echo "setting up loopback file: ${lofile}"
dokpartx "${lofile}"
fi
if [ "$createdfile" = "1" ]; then
pvcreate "/dev/mapper/${loopdev}p2"
vgcreate "${vg}" "/dev/mapper/${loopdev}p2"
# create the thinpool
# datalv
lvcreate -n ThinDataLV -L "${thinsize}" "${vg}"
# metadata lv
lvcreate -n MetaDataLV -L 1G "${vg}"
lvconvert -y --type thin-pool --poolmetadata "${vg}/MetaDataLV" "${vg}/ThinDataLV"
# Now we can create thin lvs using:
# lvcreate -n thin1 -V 10G --thinpool ThinDataLV "${vg}"
# and snapshot it using:
# lvcreate -n thin2 --snapshot ${vg}/thin1
# lvchange -ay -K ${vg}/thin2
fi
mkdir -p "$lvbasedir"