diff options
Diffstat (limited to 'old-school/samizdat-cdrom-copy')
-rwxr-xr-x | old-school/samizdat-cdrom-copy | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/old-school/samizdat-cdrom-copy b/old-school/samizdat-cdrom-copy new file mode 100755 index 0000000..d4920b9 --- /dev/null +++ b/old-school/samizdat-cdrom-copy | |||
@@ -0,0 +1,75 @@ | |||
1 | #!/bin/sh | ||
2 | md_name=$1 | ||
3 | lv_name=$2 | ||
4 | lv_dev=$3 | ||
5 | cdrom_loopdev=$4 | ||
6 | cdrom_dev=$5 | ||
7 | |||
8 | . lvm-create.sh | ||
9 | |||
10 | mdadm_wait_remove() | ||
11 | { | ||
12 | # We should perhaps use mdadm --monitor's RebuildFinished event. | ||
13 | |||
14 | local dev="$1" disk="$2" tries | ||
15 | if ! mdadm --wait "$dev"; then | ||
16 | tries=1000 | ||
17 | while ! mdadm --detail --test "$dev"; do | ||
18 | [ $tries -gt 0 ] || return 1 | ||
19 | sleep 1 | ||
20 | tries=$((tries-1)) | ||
21 | done | ||
22 | fi | ||
23 | |||
24 | mdadm "$dev" --fail "$disk" || return 1 | ||
25 | tries=100 | ||
26 | while ! mdadm "$dev" --remove "$disk"; do | ||
27 | [ $tries -gt 0 ] || return 1 | ||
28 | sleep 1 | ||
29 | tries=$((tries-1)) | ||
30 | done | ||
31 | return 0 | ||
32 | } | ||
33 | |||
34 | |||
35 | Done() | ||
36 | { | ||
37 | mdadm --grow "$md_name" -n 1 --force | ||
38 | lvm lvrename "$lv_name".tmp "${lv_name#*/}" | ||
39 | losetup -d "$cdrom_loopdev" | ||
40 | [ -e /etc/mtab ] || ln -sf /proc/mounts /etc/mtab | ||
41 | eject "$cdrom_dev" | ||
42 | echo "[$$] Done." | ||
43 | } | ||
44 | |||
45 | exec >>/var/log/samizdat-cdrom-copy.log 2>&1 | ||
46 | echo "[$$] Waiting for $cdrom_loopdev ($cdrom_dev) to be removed from $md_name." | ||
47 | |||
48 | if mdadm_wait_remove "$md_name" "$cdrom_loopdev"; then | ||
49 | Done; | ||
50 | else | ||
51 | echo "[$$] Warning: mdadm_wait_remove() returned $?. Doing manual copy with sg_dd (using continue-on-error)." | ||
52 | mdadm -D "$md_name" | ||
53 | mdadm "$md_name" --fail "$lv_dev".tmp | ||
54 | mdadm -D "$md_name" | ||
55 | mdadm "$md_name" --remove "$lv_dev".tmp | ||
56 | mdadm -D "$md_name" | ||
57 | mdadm /dev/md55 --grow -n1 --force | ||
58 | mdadm -D "$md_name" | ||
59 | |||
60 | sizelimit=$(get_cdrom_sizelimit "$cdrom_dev") | ||
61 | |||
62 | if sg_dd bs=2048 ${sizelimit:+count=$((sizelimit/2048))} iflag=coe,coe,coe if="$cdrom_dev" of="$lv_dev".tmp retries=42; then | ||
63 | echo "[$$] sg_dd succeeded." | ||
64 | mdadm "$md_name" --grow -n 2 --assume-clean --add "$lv_dev".tmp | ||
65 | mdadm -D "$md_name" | ||
66 | if mdadm_wait_remove "$md_name" "$cdrom_loopdev"; then | ||
67 | Done; | ||
68 | else | ||
69 | echo "[$$] Error: mdadm_wait_remove() returned $? after sg_dd. Cannot eject CDROM!" | ||
70 | fi | ||
71 | else | ||
72 | echo "[$$] Error: sg_dd returned $?. Cannot eject CDROM!" | ||
73 | fi | ||
74 | fi | ||
75 | |||