Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Reproduce the functionality of virt-sysprep for use with Packer builds

License

Notifications You must be signed in to change notification settings

ruzickap/packer-virt-sysprep

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

91 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Packer-Virt-Sysprep


Scripts to clean and prepare a VM for cloning à la libguestfs's virt-sysprep but from within a running guest.

The intention is to provide virt-sysprep style operations such as log removal, removal of a guests host ssh keys, deletion of custom firewall rules etc for use with automated build tools such as packer.

Currently libguestfs is not available for all host platforms. Additionally virt-sysprep requires that the guest VM be shutdown prior to use.


Packer-Virt-Sysprep Operations

  • sysprep-op-bash-history: Delete bash history for root and all users under /home
  • sysprep-op-crash-data: Delete any crash data created by kexec-tools
  • sysprep-op-dhcp-client-state: Delete any DHCP lease information
  • sysprep-op-firewall-rules: Delete custom rules and firewall customisations
  • sysprep-op-logfiles: Ensures the resultant image is devoid of log files
  • sysprep-op-machine-id: Deletes the machine-id. This ensures a unique ID is created the next time the machine is booted.
  • sysprep-op-mail-spool: Removes any mail from the local spool
  • sysprep-op-package-manager-cache: Removes cache files associated with the guests package manager. Should work for apt, dnf, yum and zypper.
  • sysprep-op-rpm-db: Removes host specific RPM database files. RPM will recreate these files automatically when needed. Clearly intended for RPM based distro's (but should be safe to run against non RPM distro's as well)
  • sysprep-op-ssh-hostkeys: Delete the host ssh keys. A new set of keys will be auto-generated by the host at next boot.
  • sysprep-op-tmp-files: Ensures the resultant image is devoid of any temp files
  • sysprep-op-yum-uuid: Remove the yum package manager UUID associated with the guest. A new UUID will be automatically generated the next time yum is run

Coverage

The scripts provided have been tested on CentOS 7.x and Debian 9.x. While not tested, the scripts or 'operations' should also work on Red Hat 7 without issue.

Usage on any other OS, such as SUSE or Ubuntu, may work but will require thorough testing. Let me know if you find, after thorough testing, that the scripts work for you on another OS. Similarly, if you adapt the scripts to make them work on another platform, or simply make some improvements, please feel free to issue a PR to incorporate the changes.


Basic Usage

Some basic familiarity with Packer is assumed. If something below doesn't make sense, please read through the Packer documentation.

Generally speaking the packer-virt-sysprep operations should be among the last provisioning scripts you run against your build before shutting it down.

Each script or virt-sysprep style operation can be used individually or in conjuction with any or all of the other operations.

The example below will run the operations that ensure each machine created from the generated image will have a unique machine-id and host ssh keys. Be warned that you may need to change the "execute_command" to fit with how you do things in your build e.g. run with sudo. Additionally, note that all packer-virt-sysprep scripts expect to be run within a Bash shell!

{
  "builders": [
    {
        ...
        VMware ISO builder
        Virtualbox ISO builder
        etc
        ...
    }
  ],

  "provisioners": [
    {
      "type": "shell"
      "execute_command": "{{ .Vars }} $(command -v bash) '{{ .Path }}'",
      "scripts": [
        "scripts/packer-virt-sysprep/sysprep-op-machine-id.sh",
        "scripts/packer-virt-sysprep/sysprep-op-ssh-hostkeys.sh"
      ]
    }
  ]
}

Please see the packer-virt-sysprep-example repository for example usage and further details.


Forked from: https://github.com/DanHam/packer-virt-sysprep

About

Reproduce the functionality of virt-sysprep for use with Packer builds

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 100.0%