Unattended Ubuntu 16.04 Setup using PowerShell

It was back in 2003-04 when I had worked on linux with RedHat 2x & 3x, Fedora 2x and Knoppix 3x. After that for almost a decade I didn’t use linux.
In last few years (since late 2014) I have been playing Ubuntu releases as part of my experiments, learning DevOps tools and data science topics.
I have another laptop at home with Windows and Ubuntu dual boot which I keep switching between. At times when I am travelling, my preferred option is using Hyper-v virtual machines.
In this post, I will share my recipe building Ubuntu 16.04 virtual machine in unattended mode using PowerShell script.

Disclaimer

  • This script is output my experiment with Hyper-V, PowerShell & Bash on Windows 10 PRO. I have not added extensive error/exception handling.
  • To keep post focused on experiment and outcome, I have drafted much about Kickstart and Preseeding in Linux. I have provided reference to get good enough idea about it.
  • I ran this script 7 times after creating and it produced working VM in an average 35 minutes.

Ingredients

Steps

  1. Run the script to build iso for unattended setup.
  2. Connect to VM (last step in script hence actually not extra step)
  3. Connect through SSH and work with VM

Script Walk-through (High Level Steps)

  • Setting up required variables ( line number 2 to 9 )
  • Ask the user questions about user preferences ( line number 12 to 16 )
  • Check if the passwords match and then generate encrypted hash to use in preseed file ( line number 19 to 29 )
  • Check if the root passwords match and generate encrypted hash to use in ks.cfg file ( line number 32 to 42 )
  • Creating/Verifying Required Folder Structure is available to work ( line number 45 to 69 )
  • Mounting C:\LabSources\ISOs\ubuntu-16.04.1-server-amd64.iso file and copy content locally ( line number 72 to 82 )
  • Copy baseline Kickstart Configuration File [ks.cfg] To Working Folder ( line number 85 )
  • Copy baseline Seed File [mshende.seed in my case] (answers for unattended setup) To Working Folder ( line number 88 )
  • Update the ks.cfg file to reflect encrypted root password hash ( line number 91 )
  • Update the mshende.seed file to reflect the user’s choices ( line number 94 to 97 )
  • Update the isolinux.cfg file to reflect boot time choices ( line number 100 to 101 )
  • Building installer menu choice to make it default and use ks.cfg and mshende.seed files ( line number 104 to 111 )
  • Creating new ISO file at C:\LabSources\ISOs\ubuntu-16.04.1-server-amd64-unattended.iso ( line number 116 )
  • Create Hyper-V Virtual Machine ( line number 119 to 128 )
  • Add host entry on my physical machine pointing to VM ip address to connect VM over ssh ( line number 130 )
  • Starting Virtual Machine and Connecting through VM console ( line number 135 to 136 )
  • Connect to server once ready using putty ( line number 139, type manually once VM is ready after setup)

Links

Screenshots

This slideshow requires JavaScript.

References

Lessons Learnt

  • I am not expert in Linux, however being conceptually aware on diverse topics, I was able to use PowerShell to get what I want.
  • There are many options (both cloud and on-premise) available these days to host virtual machines. This is not about what is better v/s bad. This is about using available resources.
  • While writing this I couldn’t find best suitable alternatives for mkpasswd (line number 36) and mkisofs.exe ( line number 116 ) natively in/with PowerShell. However, because PowerShell can work with variety of applications, I was able to proceed without getting stuck.
  • In this case mkpasswd ( line number 36 ), I am calling bash command from Bash on Windows 10 and use in my script.
  • In this case of mkisofs which is available in bash shell, but I was not able to get it working like in the case of line number 36. I found Windows platform download at https://code.google.com/archive/p/mkisofs-md5/downloads.