summaryrefslogtreecommitdiff
path: root/old-school/samizdat-cdrom-copy
blob: d4920b9bf5a5e9734cf4ee6c51d4f04b71a9c8d2 (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
73
74
75
#!/bin/sh
md_name=$1
lv_name=$2
lv_dev=$3
cdrom_loopdev=$4
cdrom_dev=$5

. lvm-create.sh

mdadm_wait_remove()
{
  # We should perhaps use mdadm --monitor's RebuildFinished event.

  local dev="$1" disk="$2" tries
  if ! mdadm --wait "$dev"; then
    tries=1000
    while ! mdadm --detail --test "$dev"; do
      [ $tries -gt 0 ] || return 1
      sleep 1
      tries=$((tries-1))
    done
  fi

  mdadm "$dev" --fail "$disk" || return 1
  tries=100
  while ! mdadm "$dev" --remove "$disk"; do
    [ $tries -gt 0 ] || return 1
    sleep 1
    tries=$((tries-1))
  done
  return 0
}


Done()
{
  mdadm --grow "$md_name" -n 1 --force
  lvm lvrename "$lv_name".tmp "${lv_name#*/}"
  losetup -d "$cdrom_loopdev"
  [ -e /etc/mtab ] || ln -sf /proc/mounts /etc/mtab
  eject "$cdrom_dev"
  echo "[$$] Done."
}

exec >>/var/log/samizdat-cdrom-copy.log 2>&1
echo "[$$] Waiting for $cdrom_loopdev ($cdrom_dev) to be removed from $md_name."

if mdadm_wait_remove "$md_name" "$cdrom_loopdev"; then
  Done;
else
  echo "[$$] Warning: mdadm_wait_remove() returned $?.  Doing manual copy with sg_dd (using continue-on-error)."
  mdadm -D "$md_name"
  mdadm "$md_name" --fail "$lv_dev".tmp
  mdadm -D "$md_name"
  mdadm "$md_name" --remove "$lv_dev".tmp
  mdadm -D "$md_name"
  mdadm  /dev/md55 --grow -n1 --force
  mdadm -D "$md_name"

  sizelimit=$(get_cdrom_sizelimit "$cdrom_dev")

  if sg_dd bs=2048 ${sizelimit:+count=$((sizelimit/2048))} iflag=coe,coe,coe if="$cdrom_dev" of="$lv_dev".tmp retries=42; then
    echo "[$$] sg_dd succeeded."
    mdadm "$md_name" --grow -n 2 --assume-clean --add "$lv_dev".tmp
    mdadm -D "$md_name"
    if mdadm_wait_remove "$md_name" "$cdrom_loopdev"; then
      Done;
    else
      echo "[$$] Error: mdadm_wait_remove() returned $? after sg_dd.  Cannot eject CDROM!"
    fi
  else
    echo "[$$] Error: sg_dd returned $?.  Cannot eject CDROM!"
  fi
fi