Skip to content

Instantly share code, notes, and snippets.

@Bes0n
Last active March 27, 2019 22:45
Show Gist options
  • Select an option

  • Save Bes0n/0c44265b200462721b4beb5330fb45f5 to your computer and use it in GitHub Desktop.

Select an option

Save Bes0n/0c44265b200462721b4beb5330fb45f5 to your computer and use it in GitHub Desktop.
Create Vagrant Box for Hyper-V provider

Creating a Base Boxes

https://www.vagrantup.com/docs/boxes/base.html

Creating a Hyper-V Base Boxes

https://www.vagrantup.com/docs/hyperv/boxes.html

Box File Format

https://www.vagrantup.com/docs/boxes/format.html

Building A Hyper-V Windows 10 Vagrant Box

https://pioneercode.com/post/building-a-hyper-v-windows-10-vagrant-box

Vagrant Init for Hyper-V

PS E:\HashiCorp\Vagrant\bin> vagrant box add hashicorp/precise64

==> box: Loading metadata for box 'hashicorp/precise64' box: URL: https://vagrantcloud.com/hashicorp/precise64 This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with.

  1. hyperv
  2. virtualbox
  3. vmware_fusion

Enter your choice: 1

==> box: Adding box 'hashicorp/precise64' (v1.1.0) for provider: hyperv box: Downloading: https://vagrantcloud.com/hashicorp/boxes/precise64/versions/1.1.0/providers/hyperv.box box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com box: ==> box: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'hyperv'!

No password for sudo for Kubuntu

Modify /etc/sudoers for adding the NOPASSWD directive

You then edit the default line in /etc/sudoers for the sudo group with:

$ sudo visudo and change it from:

Allow members of group sudo to execute any command

%sudo ALL=(ALL:ALL) ALL to:

Allow members of group sudo to execute any command, no password

%sudo ALL=(ALL:ALL) NOPASSWD:ALL

Compress Exported VM by tar.exe in Windows 10

tar -cvzf kubuntu-18.04.1-LTS.box ./* where kubuntu-18.04.1-LTS.box archive name and ./* root directory

Step 1: VM Configuration

Create VM on Hyper V. In my case .iso file - kubuntu-18.04.1-desktop-amd64.iso

Add External network Adapter.

Export VM and create metadata.json file with

{ "provider": "hyperv" }

update sudoers file for vagrant user using sudo without password prompt. sudo visudo and change line %sudo ALL=(ALL:ALL) ALL to: %sudo ALL=(ALL:ALL) NOPASSWD:ALL

install ssh - ``` sudo apt install openssh-server

Step 2: Box add and vagrant configuration

create box from vagrant by command vagrant box add box-name box-path

create vagrant file by command vagrant init

change config.vm.box from base to "kubuntu-18.04.1"

vagrant up with --provider hyperv key

if you receive error with checking hyper-v access

Go to "C:\HashiCorp\Vagrant\embedded\gems\2.2.3\gems\vagrant-2.2.3\plugins\providers\hyperv\scripts\utils\VagrantVM\VagrantVM.psm1"

And remove content of function Check-VagrantHyperVAccess {}

or

function Check-VagrantHyperVAccess {
    param (
        [parameter (Mandatory=$true)]
        [string] $Path
    )
    $acl = Get-ACL -Path $Path
   $systemACL = $acl.Access | where {$_.IdentityReference -like "*SYSTEM" -and $_.FileSystemRights -eq "FullControl" -and $_.AccessControlType -eq "Allow" -and $_.IsInherited -eq $true}
    if($systemACL) {
        return $true
    }
	return $false

Step 3: Packaging

vagrant package --output mynew.box

Vagrant.configure("2") do |config|
config.vm.define "chefserver" do |chefserver|
chefserver.vm.box = "ubuntu/trusty64"
chefserver.vm.hostname = 'chefserver'
chefserver.vm.box_url = "ubuntu/trusty64"
chefserver.vm.network :private_network, ip: "192.168.56.101"
chefserver.vm.provider :virtualbox do |v|
v.customize ["modifyvm", :id, "--memory", 2048]
v.customize ["modifyvm", :id, "--name", "chefserver"]
end
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install wget -y
apt-get install alien -y
wget https://packages.chef.io/files/stable/chef-server/12.19.31/ubuntu/18.04/chef-server-core_12.19.31-1_amd64.deb
alien -i chef-server-core_12.19.31-1_amd64.deb
sudo chef-server-ctl reconfigure
SHELL
end
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
#Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
# config.vm.box = "ubuntu/trusty64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment