-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateLvm.sh
67 lines (58 loc) · 1.49 KB
/
CreateLvm.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
#!/bin/sh
LVM_GRP=klas
LVM_VOL=lv_opt
LOOP_PATH=/home/lvm_loop
LOOP_SIZE=2000
LOOP_UNIT=1M
CreateLoop () {
dd if=/dev/zero of=${LOOP_PATH} bs=${LOOP_UNIT} count=${LOOP_SIZE}
losetup -f ${LOOP_PATH}
LOOPDEVICE=${LOOP_PATH} python <<EOF
import subprocess
import json
import os
proc = subprocess.Popen(["losetup", "--json"], stdout=subprocess.PIPE)
data = json.loads(proc.stdout.read().decode())["loopdevices"]
for item in data:
if item["back-file"] == os.getenv("LOOPDEVICE"):
print(item["name"])
EOF
}
CreateService () {
cat > /etc/systemd/system/setup-klas-loop.service <<EOF
[Unit]
Description=Setup loop devices for klas
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target
After=systemd-udevd.service home.mount
Required=systemd-udevd.serpvice
[Service]
Type=oneshot
ExecStart=/usr/sbin/kpartx -v -a /home/lvm_loop
ExecStop=/usr/sbin/kpartx -d /home/lvm_loop
TimeoutSec=60
RemainAfterExit=yes
[Install]
WantedBy=local-fs.target
Also=systemd-udevd.service
EOF
systemctl daemon-reload
systemctl enable setup-klas-loop
}
InitlizeLvm () {
vgcreate ${LVM_GRP} ${1}
lvcreate -l +100%FREE ${LVM_GRP} -n ${LVM_VOL}
}
InitlizeXfs () {
mkfs.xfs /dev/mapper/${LVM_GRP}-${LVM_VOL}
grep -q "${LVM_GRP}-${LVM_VOL}" /etc/fstab || echo "/dev/mapper/${LVM_GRP}-${LVM_VOL} /opt xfs defaults 0 0" >> /etc/fstab
mount -a
}
Main () {
loop_name=$(CreateLoop)
CreateService
InitlizeLvm ${loop_name}
InitlizeXfs
}
Main