Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
linux_wiki:os_install_post_install [2016/05/18 23:32] billdozor |
linux_wiki:os_install_post_install [2017/09/12 23:07] billdozor [Post Install Script] |
||
---|---|---|---|
Line 4: | Line 4: | ||
After installing an OS via [[linux_wiki: | 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** | **Checklist** | ||
* Distro(s): Enterprise Linux 6/7 | * Distro(s): Enterprise Linux 6/7 | ||
+ | * Other: NFS Server sharing a post install configuration script | ||
---- | ---- | ||
Line 12: | Line 15: | ||
====== Firstboot ====== | ====== Firstboot ====== | ||
- | Post install configuration | + | * The firstboot script |
+ | * It is baked into the system | ||
+ | * It stays generic and calls other external scripts on remote admin systems | ||
+ | * It also reboots the system and sends an email once it has completed | ||
---- | ---- | ||
Line 20: | Line 26: | ||
This script is meant to run once and then disable itself. It calls other post install script(s) to do the actual work. | 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> | <code bash> | ||
#!/bin/bash | #!/bin/bash | ||
Line 30: | Line 37: | ||
nfs_client_mountpoint="/ | nfs_client_mountpoint="/ | ||
post_install_script=" | post_install_script=" | ||
+ | post_install_log="/ | ||
# Write a successful run file | # Write a successful run file | ||
firstboot_ran_file="/ | firstboot_ran_file="/ | ||
+ | |||
+ | # System Admins Group Email | ||
+ | system_admins_email=' | ||
# Reboot delay in minutes | # Reboot delay in minutes | ||
Line 135: | Line 146: | ||
#### End of Safeguards #### | #### End of Safeguards #### | ||
- | # Email root notification of completion | + | # Email notification of completion |
- | echo -e " | + | echo -e " |
- | echo -e "The firstboot script process has completed for: ' | + | echo -e "The firstboot script process has completed for: ' |
# Allow some time for the email to be sent | # Allow some time for the email to be sent | ||
Line 151: | Line 162: | ||
Firstboot will get executed on CentOS 7 via a custom systemd service unit. | Firstboot will get executed on CentOS 7 via a custom systemd service unit. | ||
- | The following service unit will end up in / | + | Create the following service unit file: / |
<code bash> | <code bash> | ||
[Unit] | [Unit] | ||
Line 170: | Line 181: | ||
CentOS 6 will make use of rc.local to execute the script. | CentOS 6 will make use of rc.local to execute the script. | ||
- | / | + | Append to: / |
<code bash> | <code bash> | ||
/ | / | ||
Line 179: | Line 190: | ||
====== Auto Setup ====== | ====== Auto Setup ====== | ||
- | Now that we have a firstboot script and method of executing (CentOS 7 service or CentOS 6 rc.local), the combination of the two can be added to VM templates or kickstarts for unattended execution. | + | Now that we have a firstboot script and method of executing |
---- | ---- | ||
Line 185: | Line 196: | ||
===== Auto Setup: VM Templates ===== | ===== 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 ===== | ===== 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</ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== Post Install Script ====== | ||
+ | |||
+ | * The post install script is what gets called via the firstboot script. | ||
+ | * This script does all the heavy lifting (system updates, configuration, | ||
+ | |||
+ | ===== Post Install Script: Parent ===== | ||
+ | |||
+ | **Post install script**: Provide logging and error checking | ||
+ | <code bash postinstall.sh> | ||
+ | #!/bin/bash | ||
+ | # Title: postinstall.sh | ||
+ | # Description: | ||
+ | # Last Updated: 2016-10-24 | ||
+ | # Most Recent Changes: | ||
+ | ####################################################################################### | ||
+ | |||
+ | function print_usage | ||
+ | { | ||
+ | echo | ||
+ | echo " Usage: postinstall.sh [-y]" | ||
+ | echo | ||
+ | echo " | ||
+ | echo | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | # | ||
+ | # Get Script Arguments | ||
+ | # | ||
+ | # Reset POSIX variable in case it has been used previously in this shell | ||
+ | OPTIND=1 | ||
+ | |||
+ | # By default, do not force run script. Prompt for running or not. | ||
+ | force_run_script=" | ||
+ | |||
+ | while getopts " | ||
+ | case " | ||
+ | h) # -h (help) argument | ||
+ | print_usage | ||
+ | exit 0 | ||
+ | ;; | ||
+ | y) # -y (yes to running script) argument | ||
+ | force_run_script=" | ||
+ | ;; | ||
+ | *) # invalid argument | ||
+ | print_usage | ||
+ | exit 0 | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | ## | ||
+ | ## Pre-req checks | ||
+ | ## | ||
+ | |||
+ | ## Ensure we are root ## | ||
+ | if [[ $(id --user) -ne 0 ]]; then | ||
+ | echo ">> | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | ## | ||
+ | ## Set Script Variables | ||
+ | ## | ||
+ | |||
+ | # Set base path from executed command (relative or full path works) | ||
+ | base_path=" | ||
+ | |||
+ | # Set log file and script locations | ||
+ | postinstall_log="/ | ||
+ | postinstall_worker=" | ||
+ | |||
+ | ## | ||
+ | ## Setup Logging | ||
+ | ## | ||
+ | echo -e ">> | ||
+ | |||
+ | # Clear log and timestamp the beginning | ||
+ | cat /dev/null > ${postinstall_log} | ||
+ | echo -e "---- Log Started: $(date) ----\n" | ||
+ | |||
+ | ## | ||
+ | ## Execute External Scripts | ||
+ | ## | ||
+ | # Start script, pass base path argument | ||
+ | if [[ ${force_run_script} == " | ||
+ | ${base_path}${postinstall_worker} -d ${base_path} 2>&1 | tee -a ${postinstall_log} | ||
+ | elif [[ ${force_run_script} == " | ||
+ | ${base_path}${postinstall_worker} -d ${base_path} -y 2>&1 | tee -a ${postinstall_log} | ||
+ | else | ||
+ | echo -e ">> | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | ## | ||
+ | ## Close Logs, Show Location | ||
+ | ## | ||
+ | # Ending timestamp | ||
+ | echo -e " | ||
+ | |||
+ | # Reminder of where the log file is at | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | </ | ||
+ | |||
+ | ===== Post Install Script: Worker ===== | ||
+ | |||
+ | **Post install worker**: Perform the actual installations/ | ||
+ | <code bash worker_postinstall.sh> | ||
+ | </ | ||
---- | ---- | ||