710-setup-hugepages
#!/bin/bash
#
# Enabling Large Page (Huge page) in Guest
#
if [ "$UID" -ne 0 ]
  then echo "This script should be run as root."
  exit 1
fi
#
echo "Note: This script runs on the guest"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
# Define VB User
VB_VM_USER=senthil
# Huge page Group Name
HP_GRP_NAME=grphp
# Number of Huge Pages
NR_HP=500
# Huge Page Mount point
HP_MP=/mnt/hugepages
#
# Current setting
grep -i hugepages /proc/meminfo
# Create a group for ''hugepages'' and retrieve the ''GID''.  Add the VB user to the group.
groupadd $HP_GRP_NAME
HP_GRP_ID=`getent group ${HP_GRP_NAME} | cut -d":" -f3`
echo "Group Name: $HP_GRP_NAME, Group Id:  $HP_GRP_ID"
# Add VB user to group
adduser $VB_VM_USER $HP_GRP_NAME
#
# Add/replace vm.nr_hugepages in /etc/sysctl.conf
# Comment out any existing vm.nr_hugepages setting
sed -i 's/^vm\.nr_hugepages/#vm.nr_hugepages/' /etc/sysctl.conf
# Add new setting
echo "vm.nr_hugepages = $NR_HP" >> /etc/sysctl.conf
#
# Add/replace vm.hugetlb_shm_group in /etc/sysctl.conf
# Comment out any existing vm.hugetlb_shm_group setting
sed -i 's/^vm\.hugetlb_shm_group/#vm.hugetlb_shm_group/' /etc/sysctl.conf
# Add new setting
echo "vm.hugetlb_shm_group = $HP_GRP_ID" >> /etc/sysctl.conf
#
# Create directory (for mounting) hugepages file system
mkdir $HP_MP
#
# Add/replace hugetlbfs in /etc/fstab
# Comment out any existing vm.hugetlb_shm_group setting
sed -i 's/^hugetlbfs/#hugetlbfs/' /etc/fstab
# Add new setting
echo "# Entry for hugetlbfs" >> /etc/fstab
echo "hugetlbfs $HP_MP hugetlbfs mode=1770,gid=$HP_GRP_ID 0 0" >> /etc/fstab
#
# Get current hugepage size
HP_SIZE=`grep Hugepagesize /proc/meminfo|awk '{print $2}'`
# Adjust as required. This is setting it to 16 times pagesize. Some may think that ia a lot!
NEW_ULIMIT=$(( $HP_SIZE * 16 ))
#
# Comment out existing memlock hard and soft limits for user, if it exists
cp /etc/security/limits.conf /tmp
sed -i "/^$VB_VM_USER[ ]*hard[ ]*memlock[ ]*/ s/^/# /" /etc/security/limits.conf
sed -i "/^$VB_VM_USER[ ]*soft[ ]*memlock[ ]*/ s/^/# /" /etc/security/limits.conf
# Set new limits
echo "$VB_VM_USER    hard    memlock    $NEW_ULIMIT" >> /etc/security/limits.conf
echo "$VB_VM_USER    soft    memlock    $NEW_ULIMIT" >> /etc/security/limits.conf
#
diff /etc/security/limits.conf /tmp/limits.conf
#
echo "Reboot system for changes to take effect"
exit