2022-12-03 17:04:13 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# We built on top of tiny-cloud
|
2023-07-04 15:10:30 +00:00
|
|
|
. /etc/tiny-cloud.conf
|
2022-12-03 17:04:13 +00:00
|
|
|
|
|
|
|
# archive orig /var, mount new var on top and restore orig var
|
|
|
|
copy_and_mount() {
|
|
|
|
local dev=$1
|
|
|
|
|
|
|
|
tar cf /tmp/var.tar /var 2>/dev/null
|
|
|
|
mount -t xfs -o noatime "$dev" /var
|
|
|
|
tar xf /tmp/var.tar -C / && rm -f /tmp/var.tar
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_var() {
|
2023-07-04 15:10:30 +00:00
|
|
|
for d in $(find /dev/xvd[a-z] /dev/sd[a-z]); do
|
2022-12-03 17:04:13 +00:00
|
|
|
# resolve to a valid block device
|
|
|
|
dev="$(realpath "$d")"
|
|
|
|
[ -b "$dev" ] || continue
|
|
|
|
|
|
|
|
# already mounted
|
2023-04-26 17:22:52 +00:00
|
|
|
mount | grep -q "$d" && continue
|
2022-12-03 17:04:13 +00:00
|
|
|
|
|
|
|
case "$CLOUD" in
|
|
|
|
aws)
|
2023-07-04 15:10:30 +00:00
|
|
|
# on AWS look for sdx/xvdx
|
|
|
|
if [ "$d" = "/dev/sdx" -o "$d" = "/dev/xvdx" ]; then
|
2022-12-03 17:04:13 +00:00
|
|
|
# check volume for existing filesystem
|
|
|
|
type=$(file -Lbs $d)
|
|
|
|
if [[ "$type" =~ "XFS filesystem" ]]; then
|
|
|
|
xfs_repair $d >/dev/null 2>&1
|
2023-04-26 17:22:52 +00:00
|
|
|
mount -t xfs -o noatime "$d" /var
|
2022-12-03 17:04:13 +00:00
|
|
|
else
|
|
|
|
mkfs.xfs -qf $d >/dev/null
|
2023-04-26 17:22:52 +00:00
|
|
|
copy_and_mount "$d"
|
2022-12-03 17:04:13 +00:00
|
|
|
fi
|
2023-04-26 17:22:52 +00:00
|
|
|
grep -q "$d" /etc/fstab || echo "$d /var xfs defaults,noatime,nofail 0 2" >> /etc/fstab
|
2022-12-03 17:04:13 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
2023-04-26 17:22:52 +00:00
|
|
|
ewarn "Unsupported cloud: $CLOUD"
|
|
|
|
return 1
|
2022-12-03 17:04:13 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
attach_ebs() {
|
|
|
|
local volId="$1"
|
|
|
|
local device="$2"
|
|
|
|
|
|
|
|
local tries=30
|
|
|
|
while true; do
|
|
|
|
_json="$(aws ec2 describe-volumes --volume-ids $volId --region $REGION --output json)"
|
|
|
|
rc=$?; [ $rc -ne 0 ] && return $rc
|
|
|
|
|
|
|
|
vol_status=$(echo "$_json" | jq -r .Volumes[].State)
|
|
|
|
attachId=$(echo "$_json" | jq -r .Volumes[].Attachments[].InstanceId)
|
|
|
|
|
|
|
|
[ "$attachId" = "$INSTANCE_ID" ] && break
|
|
|
|
|
|
|
|
if [ "$vol_status" = "available" ]; then
|
|
|
|
aws ec2 attach-volume --volume-id "$volId" --instance-id "$INSTANCE_ID" --region "$REGION" --device "$device" > /dev/null
|
|
|
|
rc=$?; [ $rc -ne 0 ] && return $rc
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
# if attached but not to us -> detach
|
|
|
|
if [ "$vol_status" = "in-use" ]; then
|
|
|
|
aws ec2 detach-volume --volume-id "$volId" --region "$REGION" --force
|
|
|
|
rc=$?; [ $rc -ne 0 ] && return $rc
|
|
|
|
fi
|
|
|
|
|
|
|
|
((tries=tries-1))
|
|
|
|
[ $tries -eq 0 ] && return 1
|
|
|
|
sleep 5
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
_parse_volume() {
|
|
|
|
# Todo: proper checks once all is yaml
|
|
|
|
# For now just replace ':'
|
|
|
|
echo $1 | sed -e 's/:/ /g'
|
|
|
|
}
|
|
|
|
|
|
|
|
# mount optional remote volumes
|
|
|
|
mount_volumes() {
|
|
|
|
local volumes="$1"
|
|
|
|
|
|
|
|
for vol in $volumes; do
|
|
|
|
# Todo: check volume type and call matching func
|
|
|
|
read volType volId volDevice volPath < <(_parse_volume $vol)
|
|
|
|
|
|
|
|
[ "$volType" != "ebs" ] && { echo "Unknown volume type $volType"; break; }
|
|
|
|
attach_ebs $volId $volDevice
|
|
|
|
rc=$?
|
|
|
|
[ $rc -ne 0 ] && { ewarn "error trying to attach $volId"; break; }
|
|
|
|
|
|
|
|
# wait for the block device to become available
|
|
|
|
while true; do
|
|
|
|
mdev -s
|
|
|
|
test -b $volDevice && break
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
# check volume for existing filesystem
|
|
|
|
type=$(file -Lbs $volDevice)
|
|
|
|
if [[ "$type" =~ "XFS filesystem" ]]; then
|
|
|
|
xfs_repair $volDevice >/dev/null 2>&1
|
|
|
|
else
|
|
|
|
mkfs.xfs -qf $volDevice >/dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
# mount
|
|
|
|
mkdir -p $volPath
|
|
|
|
mount -t xfs -o noatime $volDevice $volPath
|
2023-04-26 17:22:52 +00:00
|
|
|
|
|
|
|
ebegin "mounting $volDevice at $volPath"
|
2022-12-03 17:04:13 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount_volumes() {
|
|
|
|
local volumes="$1"
|
|
|
|
|
|
|
|
for vol in $volumes; do
|
|
|
|
read volType volId volDevice volPath < <(_parse_volume $vol)
|
|
|
|
|
|
|
|
umount $volPath && aws ec2 detach-volume --volume-id "$volId" --instance-id $INSTANCE_ID --region $REGION > /dev/null
|
|
|
|
done
|
|
|
|
}
|