summaryrefslogtreecommitdiffstats
path: root/scripts/zpool-config/lo-faulty-raidz.sh
blob: 2f1f08aa65d2c65032cebcc98fbb5cc56da80329 (plain)
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
#!/bin/bash
#
# 4 loopback devices using the md faulty level for easy
# fault injection on top of which is layered raidz.
#
#     zpool-vdev0    zpool-vdev1    zpool-vdev2    zpool-vdev3
#     loop0          loop1          loop2          loop3
#     md0 (faulty)   md1 (faulty)   md2 (faulty)   md3 (faulty)
#     <--------------------- raidz zpool --------------------->
#

FILES="/tmp/zpool-vdev0  \
       /tmp/zpool-vdev1  \
       /tmp/zpool-vdev2  \
       /tmp/zpool-vdev3"
LODEVICES=""
MDDEVICES=""

zpool_create() {
	check_loop_utils
	check_md_utils
	check_md_partitionable || die "Error non-partitionable md devices"

	for FILE in ${FILES}; do
		LODEVICE=`unused_loop_device`
		MDDEVICE=`unused_md_device`

		rm -f ${FILE} || exit 1
		dd if=/dev/zero of=${FILE} bs=1M count=0 seek=256 \
			&>/dev/null || die "Error $? creating ${FILE}"

		# Setup the loopback device on the file.
		msg "Creating ${LODEVICE} using ${FILE}"
		${LOSETUP} ${LODEVICE} ${FILE} || \
			die "Error $? creating ${LODEVICE} using ${FILE}"

		LODEVICES="${LODEVICES} ${LODEVICE}"

		# Setup the md device on the loopback device.
		msg "Creating ${MDDEVICE} using ${LODEVICE}"
		${MDADM} --build ${MDDEVICE} --level=faulty                  \
			--raid-devices=1 ${LODEVICE} &>/dev/null ||          \
			(destroy_md_devices "${MDDEVICES}" &&                \
			destroy_loop_devices "${LODEVICES}" &&               \
			die "Error $? creating ${MDDEVICE} using ${LODEVICE}")
		wait_udev ${MDDEVICE} 30 ||                                  \
			(destroy_md_devices "${MDDEVICES}" &&                \
			destroy_loop_devices "${LODEVICES}" &&               \
			die "Error udev never created ${MDDEVICE}")

		# Create empty GPT/EFI partition table.
		${PARTED} --script ${MDDEVICE} mklabel gpt
		MDDEVICES="${MDDEVICES} ${MDDEVICE}"
	done

	msg ${ZPOOL} create ${FORCE_FLAG} ${ZPOOL_NAME} raidz ${MDDEVICES}
	${ZPOOL} create ${FORCE_FLAG} ${ZPOOL_NAME} raidz ${MDDEVICES} ||    \
		(destroy_md_devices "${MDDEVICES}" &&                        \
		destroy_loop_devices "${LODEVICES}" && exit 1)

	echo "$LODEVICES" >/tmp/zpool-lo.txt
	echo "$MDDEVICES" >/tmp/zpool-md.txt
}

zpool_destroy() {
	msg ${ZPOOL} destroy ${ZPOOL_NAME}
	${ZPOOL} destroy ${ZPOOL_NAME}
	destroy_md_devices "`cat /tmp/zpool-md.txt`"
	destroy_loop_devices "`cat /tmp/zpool-lo.txt`"

	rm -f /tmp/zpool-md.txt /tmp/zpool-lo.txt
}