summaryrefslogtreecommitdiff
path: root/ec2-init-appliance-ebs-volume-mount.sh
blob: de3c63ca883d5c937aad197241e94f1e46e5afd4 (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
#!/bin/sh

if [ -n "$EBSMOUNT_DEBUG" ]
then
    do="echo"
    mktemp_args="-u"
else
    do=""
    mktemp_args=""
fi

if [ "$#" -lt 2 ]
then
	echo "Usage: $0 <EBS device> <path> [<path> [<path>...]]"
	exit 1
fi

ebs_volume_device="$1"
shift

canonicalise_dir() {
    dirname="$1"
    echo "${dirname}" | sed -e 's/[^a-zA-Z0-9]/_/g'
}

# The blkid call will detect whether there's already a filesystem on the EBS volume
if [ -n "$(blkid -p -o udev "${ebs_volume_device}")" ]
then
    $do mkfs.ext3 "${ebs_volume_device}"
fi

tmpdir="$(mktemp -d $mktemp_args --tmpdir=/var/run/ec2)"
$do mount ${ebs_volume_device} ${tmpdir}

for dir in "$@"
do
    ebsdir="${tmpdir}/$(canonicalise_dir "${dir}")"
    if [ ! -d "${ebsdir}" ]
    then
        # We bootstrap the storage with the existing data
        $do mkdir "${ebsdir}"
        $do cp -a ${dir} "${ebsdir}"
        $do chown --reference "${dir}" "${ebsdir}"
        $do chmod --reference "${dir}" "${ebsdir}"
    fi
    # Finally, we mount it on top of the old directory.
    $do mount --bind "${ebsdir}" "${dir}"
done