aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/adapters/cobbler/snippets/keep_cfengine_keys
blob: 61c8eb51a8e40b65e6d25e840e6109f446e6f0a0 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#raw
# Nifty trick to restore cfengine keys without using a nochroot %post

echo "Saving cfengine  keys..." > /dev/ttyS0

SEARCHDIR=/var/cfengine/ppkeys
TEMPDIR=cfengine
PATTERN=localhost

keys_found=no
# /var could be a separate partition
SHORTDIR=${SEARCHDIR#/var}
if [ $SHORTDIR = $SEARCHDIR ]; then
	SHORTDIR=''
fi	
insmod /lib/jbd.o
insmod /lib/ext3.o

mkdir -p /tmp/$TEMPDIR

function findkeys
{
 for disk in $DISKS; do
    name=$(basename $disk)
    tmpdir=$(mktemp -d $name.XXXXXX)
    mkdir -p /tmp/$tmpdir
    mount $disk /tmp/$tmpdir
    if [ $? -ne 0 ]; then # Skip to the next partition if the mount fails
      rm -rf /tmp/$tmpdir
      continue
    fi
    # Copy current host keys out to be reused
    if [ -d /tmp/$tmpdir$SEARCHDIR ] && cp -a /tmp/$tmpdir$SEARCHDIR/${PATTERN}* /tmp/$TEMPDIR; then
        keys_found="yes"
	umount /tmp/$tmpdir
	rm -r /tmp/$tmpdir
	break
    elif [ -n "$SHORTDIR" ] && [ -d /tmp/$tmpdir$SHORTDIR ] && cp -a /tmp/$tmpdir$SHORTDIR/${PATTERN}* /tmp/$TEMPDIR; then
	keys_found="yes"
        umount /tmp/$tmpdir
	rm -r /tmp/$tmpdir
        break
    fi
    umount /tmp/$tmpdir
    rm -r /tmp/$tmpdir
 done
}

DISKS=$(awk '{if ($NF ~ "^[a-zA-Z].*[0-9]$" && $NF !~ "c[0-9]+d[0-9]+$" && $NF !~ "^loop.*") print "/dev/"$NF}'  /proc/partitions)
# In the awk line above we want to make list of partitions, but not devices/controllers
# cciss raid controllers have partitions like /dev/cciss/cNdMpL, where N,M,L - some digits, we want to make sure 'pL' is there
# No need to scan loopback niether.
# Try to find the keys on ordinary partitions

findkeys

# Try software RAID
if [ "$keys_found" = "no" ]; then
  if mdadm -As; then
      DISKS=$(awk '/md/{print "/dev/"$1}' /proc/mdstat)
      findkeys
  fi
fi


# Try LVM if that didn't work
if [ "$keys_found" = "no" ]; then
    lvm lvmdiskscan
    vgs=$(lvm vgs | tail -n +2 | awk '{ print $1 }')
    for vg in $vgs; do
        # Activate any VG we found
        lvm vgchange -ay $vg
    done
    
    DISKS=$(lvm lvs | tail -n +2 | awk '{ print "/dev/" $2 "/" $1 }')
    findkeys    

    # And clean up..
    for vg in $vgs; do
        lvm vgchange -an $vg
    done
fi

# Loop until the corresponding rpm is installed
if [ "$keys_found" = "yes" ]; then
    while : ; do
        sleep 10
        if [ -d /mnt/sysimage$SEARCHDIR ] ; then
            cp -af /tmp/$TEMPDIR/${PATTERN}* /mnt/sysimage$SEARCHDIR
            logger "keys copied to newly installed system"
            break
        fi
    done &
fi
#end raw