#!/usr/bin/env bash

set -euo pipefail

host=`hostname`
vhome_block_device="/dev/disk/by-id/google-${host}-vhome"

if [[ `blkid $vhome_block_device` != "" || `blkid /dev/sdb` != "" ]]; then
    exit 1
fi

mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard $vhome_block_device
echo UUID=`blkid -s UUID -o value $vhome_block_device` /home ext4 discard,defaults 0 0 >> /etc/fstab

h="/home"
h_root="/home_root"

mv $h $h_root
mkdir $h
mount $h
rsync -aq $h_root/ $h
rm -rf $h_root

sed -i 's/^.*ssh-rsa/ssh-rsa/' /root/.ssh/authorized_keys
