From 51a028e6348f5b2ac49e48206ae2234bba6f9014 Mon Sep 17 00:00:00 2001 From: Michael Trip Date: Tue, 18 Feb 2020 11:01:57 +0100 Subject: [PATCH] initial commit --- README.md | 45 ++++++++++++++++++++++- vm-firstrun | Bin 0 -> 4541 bytes vm-firstrun.example-config | 10 +++++ vm-firstrun.service | 11 ++++++ vm-firstrun.sh | 73 +++++++++++++++++++++++++++++++++++++ vm-seal.sh | 44 ++++++++++++++++++++++ 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 vm-firstrun create mode 100644 vm-firstrun.example-config create mode 100644 vm-firstrun.service create mode 100644 vm-firstrun.sh create mode 100644 vm-seal.sh diff --git a/README.md b/README.md index f14460c..b74dfd5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ -# template-config +# EL7 VM Templatization for Proxmox +## Description + +These are some handy tools to turn a VM into a template, so that creating a new VM is as simple as (full) clone and boot. There are several assumptions made that may not necessarily match with anyone else's environment: + - CentOS 7 minimal install (will probably work on any flavor of EL7) + - DHCP server available + - rootfs (/) is on the last partition of the primary disk, and is a primary partition + - a `centos` user exists on the VM (this is not a hard requirement, nothing bad will happen if it's not true) + +Right now, the main things it will do is on the first time a new VM boots it will: + - generate a new hostname (configurable, defaults to using UUIDs) + - grow the rootfs + +## Setup + + 1. Create a new VM (with a very small disk, like <=8GB) and install CentOS 7 minimal + 1. Customize new install with whatever software/users/ssh keys you will want on *every* VM by default + 1. Copy each of the four files to the location specified in the comment at the top: + - `cp ./vm-{seal,firstrun}.sh /usr/local/sbin/` + - `cp ./vm-firstrun.example-config /etc/sysconfig/vm-firstrun` + - `cp ./vm-firstrun.service /etc/systemd/system/vm-firstrun.service` + 1. Make the two .sh scripts executable: `chmod +x /usr/local/sbin/vm-{seal,firstrun}.sh` + 1. Let systemd see the new unit file: `systemctl daemon-reload` + 1. Once all your customizations are done and you're ready to turn it into a template, run: `/usr/local/sbin/vm-seal.sh` + 1. The VM should shutdown, then in Proxmox you can just right-click and convert to template + +## Usage + +After doing the setup, to create a new VM: + 1. do a full clone of the template + 1. grow the size of the disk if needed + 1. start the VM + +If you ever need to make changes to your template: + 1. follow the above steps to create a new VM from the template + 1. make your changes on the new VM that will become the new template + 1. when done making changes, run `/usr/local/sbin/vm-seal.sh` + 1. after the new VM stops, convert it to a template and delete the old template + +Note that because it touches `/.autorelabel`, the first boot can take a few minutes while the SELinux contexts are re-applied or whatever, if SELinux is disabled in your environment this may not matter. + +## How it works + +It's very simple, `vm-seal.sh` basically de-configures the parts of the system that should be unique, then removes any log files or anything that shouldn't really be on a newly-installed system. Then it enables `vm-firstrun.service` in systemd and does a shutdown. At the next boot, systemd starts the `vm-firstrun.service` which just runs `vm-firstrun.sh`, which generates a new hostname and grows the rootfs. Then it disables the `vm-firstrun.service` so that it won't run again next time the VM is rebooted. \ No newline at end of file diff --git a/vm-firstrun b/vm-firstrun new file mode 100644 index 0000000000000000000000000000000000000000..75438897d10447b74ce3fd6a4126f68cc32aa943 GIT binary patch literal 4541 zcmcJTXH-*L8iu17fzXR|klt$oNR^sPQF;-O8WK7o1*3o<(gguQKsrhXFDONd(nPvI zXi}w$R0Bdp7!Vk|cjme-GqcwG(C@6h&-wQ~>s@=j`+JY^6_Rrx!1p?YA+P@N@vluE zKn{Sw<(-_s@`|$35IF<{0*5)nq-EjqU>Mk0Mo~@{At3{EMkvb4D!}BOVPF>pI7~(k zrU-^PffW%jh^!Ob*##_vfXgDJEX;v`Gk9Eyjq$%4lKs(;yXiS44G;jJqyPZSeriKg zsD`!yRMH#%SLayE+4vE8+MwBHSbjjA@=LW5A<>b`#_GUT;bQe1e8yGz4JF+3tiJOj z$Bnb&{WGm)#&jHy;3~wv+q1oU(e~4-1k7B$(zO0H*vOgS0^1TMd5&&y4=BalpplYvY_OpL+%9Z4TFclz zgM1VAZm8IqBJ$xLf?-dIb}IC1KhEnz6>u=xey;ZB#TAholWWu()l?>|T9@)V@H4N< zCR;zOe9JZ#ugmKwCh=He6It=b5Vv3bufJL>} zl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zD$1-Fr9Bz&7x`QZLztL47f`Te_W4x8o8A-I%@Cx z@FU~GwlEkNug_P|qTHGAgsH)}ZqT162wtyA*eKpNEmrY;kL4sGq-}K>b;v9=IeZ;C zQxy7pLGlI}ALM>lKz}i|O-LznPI?0^+T6&&-`qEV&UST`8<@ZD(~S?Xj9BRcO^jC) zbTB<(E>qX6z0BPGc}%N}f_UDoOAG6eHwA16t z4>W$#Uxjvhcsb2 /etc/machine-id +hostnamectl set-hostname localhost.localdomain +systemctl enable vm-firstrun + +rm -f /etc/ssh/ssh_host_* + +rm -rf /root/.ssh/ +rm -f /root/anaconda-ks.cfg +rm -f /root/.bash_history + +rm -f /home/centos/.bash_history +rm -f /home/centos/.ssh/known_hosts + +rm -f /var/log/boot.log +rm -f /var/log/cron +rm -f /var/log/dmesg +rm -f /var/log/grubby +rm -f /var/log/lastlog +rm -f /var/log/maillog +rm -f /var/log/messages +rm -f /var/log/secure +rm -f /var/log/spooler +rm -f /var/log/tallylog +rm -f /var/log/wpa_supplicant.log +rm -f /var/log/wtmp +rm -f /var/log/yum.log +rm -f /var/log/audit/audit.log +rm -f /var/log/tuned/tuned.log + +updatedb + +touch /.autorelabel + +sys-unconfig +