Differences
This shows you the differences between two versions of the page.
linux_wiki:os_install_post_install [2016/11/26 23:42] billdozor [Firstboot: The script] |
linux_wiki:os_install_post_install [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== OS Install: Post Install ====== | ||
- | |||
- | **General Information** | ||
- | |||
- | After installing an OS via [[linux_wiki: | ||
- | |||
- | This page demonstrates how to create VM templates and kickstarts that will auto-execute scripts one time for a system' | ||
- | |||
- | **Checklist** | ||
- | * Distro(s): Enterprise Linux 6/7 | ||
- | * Other: NFS Server sharing a post install configuration script | ||
- | |||
- | ---- | ||
- | |||
- | ====== Firstboot ====== | ||
- | |||
- | Post install configuration is normally only needed to be completed the first time a system is booted in order to set it up for the type of environment it is in. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Firstboot: The script ===== | ||
- | |||
- | This script is meant to run once and then disable itself. It calls other post install script(s) to do the actual work. | ||
- | |||
- | / | ||
- | <code bash> | ||
- | #!/bin/bash | ||
- | # Name: firstboot.sh | ||
- | # Description: | ||
- | |||
- | #### Customize These Variables #### | ||
- | nfs_server=" | ||
- | nfs_server_share=" | ||
- | nfs_client_mountpoint="/ | ||
- | post_install_script=" | ||
- | post_install_log="/ | ||
- | |||
- | # Write a successful run file | ||
- | firstboot_ran_file="/ | ||
- | |||
- | # Reboot delay in minutes | ||
- | reboot_delay=" | ||
- | #### End of Customize Variables #### | ||
- | |||
- | # | ||
- | # Functions; Main Starts After | ||
- | # | ||
- | function check_os_type | ||
- | { | ||
- | ## Gather Distro and Major Version | ||
- | if [ -f / | ||
- | distro=$(awk -F: ' | ||
- | major_version=$(awk -F: ' | ||
- | elif [ -f / | ||
- | distro=$(awk ' | ||
- | major_version=$(awk -F. ' | ||
- | fi | ||
- | } | ||
- | |||
- | # | ||
- | # Main Program | ||
- | # | ||
- | |||
- | echo -e " | ||
- | echo -e "#### | ||
- | echo -e " | ||
- | |||
- | # Check to see if script has been run before | ||
- | if [[ -f ${firstboot_ran_file} ]]; then | ||
- | echo -e " | ||
- | echo -e " | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # Discover OS Type | ||
- | check_os_type | ||
- | |||
- | # Start rpcbind service | ||
- | if [[ ${major_version} == " | ||
- | systemctl start rpcbind | ||
- | else | ||
- | service rpcbind start | ||
- | fi | ||
- | |||
- | # Try to reach the NFS server 3 times | ||
- | for index in 1 2 3; do | ||
- | ping -c 1 ${nfs_server} &> /dev/null | ||
- | | ||
- | if [[ $? -eq 0 ]]; then | ||
- | # Successful ping, exit loop | ||
- | break | ||
- | else | ||
- | # Unsuccessful; | ||
- | echo -e " | ||
- | sleep 10 | ||
- | fi | ||
- | done | ||
- | |||
- | # Mount script location | ||
- | echo -e " | ||
- | mount -t nfs ${nfs_server_share} ${nfs_client_mountpoint} | ||
- | |||
- | # Execute post install script | ||
- | echo -e " | ||
- | ${post_install_script} | ||
- | |||
- | if [[ $? -eq 0 ]]; then | ||
- | echo -e " | ||
- | else | ||
- | echo -e " | ||
- | exit 1 | ||
- | fi | ||
- | |||
- | # | ||
- | # Call other post install scripts/ | ||
- | # | ||
- | |||
- | # Unmount nfs share | ||
- | echo -e " | ||
- | umount ${nfs_client_mountpoint} | ||
- | |||
- | #### Safeguards to prevent firstboot.sh from running more than once #### | ||
- | # Create firstboot-ran file | ||
- | echo -e " | ||
- | echo -e " | ||
- | chown -v root:root ${firstboot_ran_file} | ||
- | chmod -v 400 ${firstboot_ran_file} | ||
- | |||
- | # Make script not executable | ||
- | echo -e " | ||
- | chown -v root:root ${0} | ||
- | chmod -v 400 ${0} | ||
- | |||
- | # Disable auto execution | ||
- | if [[ ${major_version} == " | ||
- | systemctl disable firstboot.service | ||
- | else | ||
- | sed -i '/ | ||
- | fi | ||
- | #### End of Safeguards #### | ||
- | |||
- | # Email root notification of completion | ||
- | echo -e " | ||
- | echo -e "The firstboot script process has completed for: ' | ||
- | |||
- | # Allow some time for the email to be sent | ||
- | sleep 5 | ||
- | |||
- | # Reboot system | ||
- | shutdown -r +${reboot_delay} " | ||
- | |||
- | ---- | ||
- | |||
- | ===== Firstboot: CentOS 7 Service ===== | ||
- | |||
- | Firstboot will get executed on CentOS 7 via a custom systemd service unit. | ||
- | |||
- | The following service unit will end up in / | ||
- | <code bash> | ||
- | [Unit] | ||
- | Description=Auto-execute post install scripts | ||
- | After=network.target | ||
- | |||
- | [Service] | ||
- | ExecStart=/ | ||
- | |||
- | [Install] | ||
- | WantedBy=multi-user.target | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Firstboot: CentOS 6 Service ===== | ||
- | |||
- | CentOS 6 will make use of rc.local to execute the script. | ||
- | |||
- | / | ||
- | <code bash> | ||
- | / | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Auto Setup ====== | ||
- | |||
- | Now that we have a firstboot script and method of executing on boot(CentOS 7 service or CentOS 6 rc.local), the combination of the two can be added to VM templates or kickstarts for unattended execution. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Auto Setup: VM Templates ===== | ||
- | |||
- | The modifications for auto execution need to be done on a new template that is a modification of your base VM template. | ||
- | |||
- | **Warning**: | ||
- | |||
- | * Deploy a new VM from your base template ([[linux_wiki: | ||
- | * Make the following modifications to the new system. | ||
- | * **CentOS 6**<code bash>## VM deployed from the base template ## | ||
- | |||
- | ## Create a script directory for root | ||
- | mkdir / | ||
- | |||
- | ## Mount NFS Server and Copy firstboot.sh to the VM | ||
- | mount -t nfs < | ||
- | cp -v / | ||
- | chown -Rv root:root / | ||
- | chmod -Rv 700 / | ||
- | |||
- | ## Create line in rc.local to auto execute firstboot script | ||
- | echo "/ | ||
- | |||
- | ## Unmount NFS server | ||
- | umount / | ||
- | * [[linux_wiki: | ||
- | * **CentOS 7**<code bash>## VM deployed from the base template ## | ||
- | |||
- | ## Create a script directory for root | ||
- | mkdir / | ||
- | |||
- | ## Mount NFS Server and Copy firstboot.sh to the VM | ||
- | mount -t nfs < | ||
- | cp -v / | ||
- | chown -Rv root:root / | ||
- | chmod -Rv 700 / | ||
- | |||
- | ## Copy firstboot.service unit to the VM | ||
- | cp -v / | ||
- | chown -v root:root / | ||
- | chmod -v 644 / | ||
- | systemctl enable firstboot.service | ||
- | |||
- | ## Unmount NFS server | ||
- | umount / | ||
- | * [[linux_wiki: | ||
- | |||
- | ---- | ||
- | |||
- | ===== Auto Setup: Kickstarts ===== | ||
- | |||
- | Kickstart files require a post install section to be edited in order for the firstboot script to be placed on a new system. | ||
- | |||
- | * [[linux_wiki: | ||
- | * Modify the " | ||
- | * **CentOS 6**<code bash> | ||
- | ( | ||
- | |||
- | ## Start rpcbind for NFS | ||
- | service rpcbind start | ||
- | |||
- | ## Mount NFS Server | ||
- | mount -vt nfs 10.1.2.3:/ | ||
- | |||
- | ## Create root's scripts directory | ||
- | mkdir / | ||
- | |||
- | ## Copy the firstboot script to the new directory | ||
- | cp -v / | ||
- | chown -Rv root:root / | ||
- | chmod -Rv 700 / | ||
- | |||
- | ## Create rc.local entry for auto execution on boot | ||
- | echo "/ | ||
- | |||
- | ## Unmount NFS Server | ||
- | umount -v /mnt | ||
- | ) | ||
- | %end</ | ||
- | * **CentOS 7**<code bash> | ||
- | ( | ||
- | |||
- | ## Start rpcbind for NFS | ||
- | systemctl start rpcbind | ||
- | |||
- | ## Mount NFS Server | ||
- | mount -vt nfs 10.1.2.3:/ | ||
- | |||
- | ## Create root's scripts directory | ||
- | mkdir / | ||
- | |||
- | ## Copy the firstboot script to the new directory | ||
- | cp -v / | ||
- | chown -Rv root:root / | ||
- | chmod -Rv 700 / | ||
- | |||
- | ## Copy the firstboot service for auto execution on boot | ||
- | cp -v / | ||
- | chown -v root:root / | ||
- | chmod -v 644 / | ||
- | |||
- | ## Enable firstboot service | ||
- | systemctl enable firstboot.service | ||
- | |||
- | ## Unmount NFS Server | ||
- | umount -v /mnt | ||
- | ) | ||
- | %end</ | ||
- | |||
- | ---- | ||