#!/bin/sh
# see also http://www.altlinux.org/Autoinstall

message() { echo "vm-profile: $*" >>/tmp/vm-profile.log; }

mem="$(sed -n '/^MemTotal/s/[^0-9]//gp' /proc/meminfo)" # in kB

finded_disks_size="$(find /sys/block/{hd,sd,vd,nvme,mmc}*/size 2> /dev/null)"
[ -n "$finded_disks_size" ] &&
	max_disk="$(sort -rn $finded_disks_size | head -1)" # in 512-byte sectors

# feel free to suggest better defaults
if [ "$mem" -le 262144 ]; then
	swap="$[ 2*$mem ]"
elif [ "$mem" -le 524288 ]; then
	swap="$mem"
else
	swap=1048576
fi

# don't do RAID in a VM, reduce swap to a minimum
if grep -qE '(101300b8)|(80eebeef)|(14ad0405)' /proc/bus/pci/devices; then
	methods='plain'
	swap=131072
else
	methods='raid plain'
fi

if [ -n "$max_disk" ] && [ "$max_disk" -le 20971520 ]; then
	base=1048576	# less than 10G HDD, choose 1G partitions
elif [ "$max_disk" -le 52428800 ]; then
	base=2097152	# 2G up to 25G HDD
elif [ "$max_disk" -le 209715200 ]; then
	base=10485760	# 10G up to 100G HDD
else
	base=20971520	# 20G otherwise
fi

message "mem=$mem k"
message "swap=$swap k"
message "max_disk=$max_disk"
message "base=$base k"

# EVMS deals with sectors
swap="$[ 2*$swap ]"
base="$[ 2*$base ]"

# The Plan:
# - provide a few more or less equivalent server partitioning profiles
# - give larger swap to build servers (RAM sized so tmpfs can get swapped out)

cat > /var/cache/alterator/vm-profile.scm << _EOF_
((srv
  (title . "Generic server (large /srv)")
  (action . trivial)
  (actiondata
   ("swap" (size . $swap) (fsim . "SWAPFS") (methods $methods))
   ("/"    (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var" (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/srv" (size $base . #t) (fsim . "Ext2/3") (methods $methods))))
 (vz
  (title . "OpenVZ HN server (large /var/lib/vz)")
  (action . trivial)
  (actiondata
   ("swap" (size . $swap) (fsim . "SWAPFS") (methods $methods))
   ("/"    (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var/lib/vz" (size $base . #t) (fsim . "Ext2/3") (methods $methods))))
 (kvm
  (title . "KVM HN server (large /var/lib/libvirt)")
  (action . trivial)
  (actiondata
   ("swap" (size . $[2*$swap]) (fsim . "SWAPFS") (methods $methods))
   ("/"    (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var/lib/libvirt" (size $base . #t) (fsim . "Ext2/3") (methods $methods))))
 (var
  (title . "Office server (large /var)")
  (action . trivial)
  (actiondata
   ("swap" (size . $swap) (fsim . "SWAPFS") (methods $methods))
   ("/"    (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var" (size $base . #t) (fsim . "Ext2/3") (methods $methods))))
 (home
  (title . "Terminal/Samba server (large /home)")
  (action . trivial)
  (actiondata
   ("swap"  (size . $swap) (fsim . "SWAPFS") (methods $methods))
   ("/"     (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var"  (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/home" (size $base . #t) (fsim . "Ext2/3") (methods $methods))))
 (build
  (title . "Build server (larger swap for tmpfs)")
  (action . trivial)
  (actiondata
   ("swap"  (size $mem . $[2*$mem]) (fsim . "SWAPFS") (methods $methods))
   ("/"     (size $base . $[2*$base]) (fsim . "Ext2/3") (methods $methods))
   ("/var"  (size . $base) (fsim . "Ext2/3") (methods $methods))
   ("/home" (size $base . #t) (fsim . "Ext2/3") (methods $methods)))))
_EOF_
