Jump to content

Photo
- - - - -

Autoscript to pass module config data to the guest


  • Please log in to reply
No replies to this topic

#1 Richard

Richard

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 02 January 2019 - 11:14 PM

Hi all,

 

I have built on a number of auto scripts in the forum - my needs were to create a file on the guest before first run-up which was then included in /etc/rc.local to set up a local user, hostname and so on. I have not included my /etc/rc.local as it has a significant number of VestaCP based setup commands which aren't relevant to this auto script. The relevant portion of my /etc/rc.local looks like this:

[ -e /etc/vps/create-boot.sh ] && . /etc/vps/create-boot.sh
rm /etc/vps/create-boot.sh
exit 0

Note that I have a non-standard directory in the guest template called /etc/vps

 

Once this script is copied to the proxmox server(s) (I put it in the directory /opt/whmcs), I enabled it in WHMCS via the menus/tabs -

  • Addons
  • Proxmox Manager
  • Auto Provisioning VM Template
  • Edit
  • Scripts tab
    • Enable
    • Script0 URL: /opt/whmcs/create-pre-boot.sh
    • Run On: post-Create-pre-Start and post-Reinstall-pre-Start
  • Save

The auto script queries the Proxmox server using pvesm to find which disk storage location has the disk (and path) for the module supplied VMID.

#!/bin/bash

# DATE: 02/01/2019 Based on other scripts in the Modules Factory Autoscript forums
# by Richard Ham QXTN, rich at qxtn.net
# Note - this does NOT work on LVM partitions

# This script serves the purpose to work with Modules Factory Proxmox module 
# for WHMCS to do the following things:

# 1. This script finds the KVM template image files according to VMID and mounts partition 1
# 2. This script assumes qemu-nbd is installed on the Proxmox host;
# 3. The virtual machine needs a reboot for the setup to be complete;
# 4. The script creates a file called create-boot.sh in /etc/vps which can be included in /etc/rc.local using " [ -e /etc/vps/create-boot.sh ] && . /etc/vps/create-boot.sh "
# 5. The script sets the hostname, hosts file and if it is an Ubuntu server, refreshes the SSH host keys;
# 6. The password for user 'admin' is set to the password supplied by the user during ordering.

# Parts of this are stolen from the other scripts in the forums - thank-you for the inspiration!

IN=$@

IFS=',' read varvmid varhostname varusername varpassword varmac varip varnode <<< "$IN";IFS='=' read var1 vmid <<< "$varvmid";IFS='=' read var2 hostname <<< "$varhostname";IFS='=' read var3 password <<< "$varpassword";IFS='=' read var4 username <<< "$varusername";IFS='=' read var5 macs <<< "$varmac";IFS='=' read var6 ips <<< "$varip";IFS='=' read var7 node <<< "$varnode"

mp=$((vmid%10))

function volumeID()
{

   cat /etc/pve/storage.cfg | grep ': ' | cut -f 2 -d ':' | while read vmdiskStores ; do
      # Find which store the vmid disk-1 is in
      diskID=''
      diskID=`pvesm list $vmdiskStores | grep vm-${vmid}-disk-1 | cut -f 1 -d ' '`
      [ ! -z "$diskID" ] && echo "$diskID" && break
   done

}

diskID=$(volumeID)
# exit if no diskID
[ -z "$diskID" ] && echo "Disk for VMID ${vmid} not found" && exit 1

diskPath=`pvesm path "$diskID"`

mkdir -p /tmp/${vmid}
qemu-nbd -c /dev/nbd${mp} "$diskPath"
pvscan --cache /dev/nbd${mp}
mount /dev/nbd${mp}p1 /tmp/${vmid}

### Start of script ############################
cat > /tmp/${vmid}/etc/vps/create-boot.sh <<- EOF

export vpspasswd=${password}
export vpsusername=vpsadmin
export vpsip=${ips}
export fqdn=${hostname}
export vpshostname=`echo $hostname | cut -f 1 -d '.'`
export vpsdomain=`echo $hostname | cut -f 2- -d '.'`

echo \$vpshostname > /etc/hostname
echo "$ips \$fqdn \$vpshostname" >> /etc/hosts

if [ -e /etc/debian_version ] ; then
/bin/rm -v /etc/ssh/ssh_host_*
dpkg-reconfigure -fnoninteractive openssh-server
systemctl restart ssh
fi
EOF
### END ########################################

if [ ! -z "$password" ] ; then

   export password
   perl -pe 's|(?<=admin:)[^:]*|crypt($ENV{password},"\$6\$$ENV{password}\$")|e' /tmp/${vmid}/etc/shadow > /tmp/${vmid}/root/shadow
   unset password
   cp /tmp/${vmid}/root/shadow /tmp/${vmid}/etc/
   rm /tmp/${vmid}/root/shadow

fi

umount /tmp/${vmid}
rmdir /tmp/${vmid}
qemu-nbd -d /dev/nbd${mp}







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Check your Pings