summaryrefslogtreecommitdiff
path: root/old-school/mdadm-dup.sh
diff options
context:
space:
mode:
Diffstat (limited to 'old-school/mdadm-dup.sh')
-rw-r--r--old-school/mdadm-dup.sh217
1 files changed, 0 insertions, 217 deletions
diff --git a/old-school/mdadm-dup.sh b/old-school/mdadm-dup.sh
deleted file mode 100644
index 70163a5..0000000
--- a/old-school/mdadm-dup.sh
+++ /dev/null
@@ -1,217 +0,0 @@
1LoSetup()
2{
3 local losetup_binary="$(which LoSetup)"
4 if [ "$losetup_binary" ]; then
5 "$losetup_binary" "$@"
6 else
7 losetup "$@"
8 fi
9}
10
11dm_snapshot()
12{
13 # TODO: eliminate duplication; this function exists elsewhere in a less generalized form
14 local ro_file rw_file cutoff_size
15 ro_file=$1
16 rw_file=$2
17 cutoff_size=$3
18
19 local ro_dev rw_dev size new_dev_name persist chunksize
20
21 if [ -b "$ro_file" ];
22 then ro_dev=$ro_file
23 else ro_dev=$(LoSetup -r -f --show "$ro_file") || return
24 fi
25
26 if [ -b "$rw_file" ];
27 then rw_dev=$rw_file
28 else rw_dev=$(LoSetup -f --show "$rw_file") || return
29 fi
30
31 if [ "$cutoff_size" -a "$cutoff_size" -gt 0 ]; then
32 size=$cutoff_size
33 else
34 size=$(blockdev --getsz "$ro_dev") || return
35 fi
36
37 new_dev_name=${ro_dev##*/}
38 persist=p
39 chunksize=16
40 dmsetup create "$new_dev_name" --table "0 $size snapshot $ro_dev $rw_dev $persist $chunksize" || return
41 wait_for_dm_device /dev/mapper/"$new_dev_name"
42 echo /dev/mapper/"$new_dev_name"
43}
44
45dm_snapshot_teardown()
46{
47 local dev="$1"
48 case "$dev" in
49 /dev/dm-*)
50 dmsetup table "$dev" | (
51 read _ _ snapshot ro_dev rw_dev _ crypt_dev _
52 case "$snapshot" in
53 snapshot)
54 dmsetup remove "$dev" || exit 1
55 # errors ignored because the loop dev can be configured to be
56 # automatically removed upon disuse
57 losetup -d /dev/block/"$rw_dev" || true
58 eject /dev/block/"$ro_dev" || true
59 ;;
60 crypt)
61 cryptsetup remove "$dev" || exit 1
62 losetup -d /dev/block/"$crypt_dev" || true
63 ;;
64 esac
65 ) || return
66 ;;
67 *) return 1 ;;
68 esac
69}
70
71wait_for_dm_device()
72{
73 # TODO: improve
74 while ! [ -e "$1" ]; do
75 sleep 1
76 done
77}
78
79dup_mount_cdrom()
80{
81 local cdrom_dev="$1" mountpoint="$2"
82
83 local sectors md_dev=/dev/md55 cdrom_rw_file=/"${cdrom_dev##*/}".rw
84
85 sectors=$(get_cdrom_sizelimit "$cdrom_dev") || return
86
87 # TODO: do we even need this backing file? We do need to trick mdadm into
88 # thinking that this is a RW device, but previously we got away with just
89 # creating a loopback device.
90 dd if=/dev/zero of="$cdrom_rw_file" bs=1K count=32 || return
91 cdrom_rw_dev=$(dm_snapshot "$cdrom_dev" "$cdrom_rw_file" "$sectors") || return
92 mdadm_dup "$cdrom_rw_dev" "$md_dev" "$sectors" || return
93 mount -t iso9660 -r $md_dev "$mountpoint"
94}
95
96get_cdrom_sizelimit()
97{
98 # returns 512-byte sectors
99 local dev="$1" sectors
100 sectors=$(blockdev --getsz "$dev") || return
101
102 # Check if we can read the last 8 sectors. With a TAO CDROM, we can't --
103 # these sectors are faux, and not part of the ISO fs. If mdadm is allowed to
104 # read them, it will mark the device failed.
105 if dd count=2 if="$dev" bs=2048 skip=$((sectors/4 - 2)) of=/dev/null 2>/dev/null; then
106 echo $sectors
107 else
108 echo $((sectors - 8))
109 fi
110}
111
112mdadm_dup()
113{
114 local input_dev="$1" md_name="$2" sectors="$3"
115
116 mdadm --build "$md_name" "${sectors:+--size=$((sectors / 2))}" \
117 --level=1 --raid-devices=1 --force --write-mostly "$input_dev" || return
118}
119
120mdadm_subdevices()
121{
122 local md_dev="$1"
123 mdadm -D "$md_dev" -Y | sed -ne 's/^MD_DEVICE_.*_DEV=//p'
124}
125
126cryptsetup_temp()
127{
128 local sectors="$1" cryptname="$2" temp_file="$3" parms=$- secret
129 set +x
130 # Add 4096 sectors for LUKS header
131 truncate -s $(((sectors + 4096) * 512)) "$temp_file" || return
132 cleartext_dev=$(LoSetup -f --show "$temp_file") || return
133 secret="$(head -c256 /dev/urandom)" || return
134 printf %s "$secret" |
135 cryptsetup luksFormat "$cleartext_dev" - || return
136 printf %s "$secret" |
137 cryptsetup --key-file - luksOpen "$cleartext_dev" "$cryptname" || return
138 unset secret
139 set "$parms"
140
141 wait_for_dm_device /dev/mapper/"$cryptname"
142 rm "$temp_file"
143 echo /dev/mapper/"$cryptname"
144}
145
146mdadm_copy_eject_crypt()
147{
148 local md_dev="$1" temp_file="$2"
149
150 [ -b "$md_dev" ] || return
151
152 local output_dev sectors
153
154 old_subdev=$(mdadm_subdevices "$md_dev"|head -n1) || return
155 [ -b "$old_subdev" ] || return
156 # TODO: truncate to the ISO fs size if the device is larger
157 sectors=$(blockdev --getsz "$md_dev") || return
158
159 output_dev=$(cryptsetup_temp "$sectors" samizdatiso "$temp_file") || return
160
161 mdadm "$md_dev" --add "$output_dev" || return
162 mdadm "$md_dev" --grow -n2 || return
163
164 mdadm_wait_remove "$md_dev" "$old_subdev" || return
165
166 mdadm "$md_dev" --grow -n1 --force || return
167 dm_snapshot_teardown "$old_subdev"
168}
169
170mdadm_copy_eject()
171{
172 local md_dev="$1" output_file="$2"
173
174 [ -b "$md_dev" ] || return
175 [ ! -e "$output_file" ] || return
176
177 local output_dev sectors
178
179 old_subdev=$(mdadm_subdevices "$md_dev"|head -n1) || return
180 [ -b "$old_subdev" ] || return
181 sectors=$(blockdev --getsz "$md_dev") || return
182
183 truncate -s $((sectors * 512)) "$output_file" || return
184 output_dev=$(LoSetup -f --show "$output_file") || return
185
186 mdadm "$md_dev" --add "$output_dev" || return
187 mdadm "$md_dev" --grow -n2 || return
188
189 mdadm_wait_remove "$md_dev" "$old_subdev" || return
190
191 mdadm "$md_dev" --grow -n1 --force || return
192 dm_snapshot_teardown "$old_subdev"
193}
194
195mdadm_wait_remove()
196{
197 # We should perhaps use mdadm --monitor's RebuildFinished event.
198
199 local dev="$1" disk="$2" tries
200 if ! mdadm --wait "$dev"; then
201 tries=1000
202 while ! mdadm --detail --test "$dev"; do
203 [ $tries -gt 0 ] || return 1
204 sleep 1
205 tries=$((tries-1))
206 done
207 fi
208
209 mdadm "$dev" --fail "$disk" || return 1
210 tries=100
211 while ! mdadm "$dev" --remove "$disk"; do
212 [ $tries -gt 0 ] || return 1
213 sleep 1
214 tries=$((tries-1))
215 done
216 return 0
217}