Skip to content

Instantly share code, notes, and snippets.

@machv
Last active February 23, 2022 15:40
Show Gist options
  • Save machv/3d5d729415cbc096f929b166d4617842 to your computer and use it in GitHub Desktop.
Save machv/3d5d729415cbc096f929b166d4617842 to your computer and use it in GitHub Desktop.
Packer for Debian with Hyper-V "SSH Direct" fixes

Packer template for Debian with "SSH Direct" on Hyper-V

Output of this Packer template is Hyper-V Gen2 VM with Debian 10 that supports direct connection via vsock thus no internet connectivity is needed.

How to build

$key = "$env:USERPROFILE\.ssh\lab01_rsa"
$publicKey = Get-Content "$($key).pub"

packer build -force -var "ssh_key=$($publicKey)" packer-debian-10.json

How to connect

$vmName = "packer-deb-buster-tpl"
$key = "$env:USERPROFILE\.ssh\lab01_rsa"
hvc ssh -oLogLevel=ERROR -oStrictHostKeyChecking=no -i $key "packer@$vmName"
{
"builders": [{
"boot_command": [
"<wait>c<wait>",
"linux /install.amd/vmlinuz ",
"auto=true ",
"url=http://{{.HTTPIP}}:{{.HTTPPort}}//preseed.cfg ",
"hostname={{user `vm_name`}} ",
"domain={{user `domain`}} ",
"interface=auto ",
"vga=788 noprompt quiet --<enter>",
"initrd /install.amd/initrd.gz<enter>",
"boot<enter>"
],
"boot_wait": "3s",
"generation": 2,
"headless": false,
"http_directory": "http",
"iso_checksum": "sha256:ab3763d553330e90869487a6843c88f1d4aa199333ff16b653e60e59ac1fc60b",
"iso_url": "https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.4.0-amd64-netinst.iso",
"output_directory": "output-debian-10",
"shutdown_command": "echo 'packer' | sudo -S shutdown -P now",
"ssh_password": "packer",
"ssh_timeout": "1h",
"ssh_username": "packer",
"switch_name": "Default Switch",
"type": "hyperv-iso",
"vm_name": "packer-{{user `vm_name`}}"
}],
"provisioners": [{
"inline": [
"mkdir /home/packer/.ssh/",
"echo '{{ user `ssh_key` }}' | tee /home/packer/.ssh/authorized_keys"
],
"type": "shell"
},
{
"destination": "/tmp/linux.pref",
"source": "scripts/linux.pref",
"type": "file"
},
{
"inline": [
"echo 'deb http://deb.debian.org/debian buster-backports main' | sudo tee /etc/apt/sources.list.d/backports.list",
"sudo mv /tmp/linux.pref /etc/apt/preferences.d/linux.pref",
"sudo apt update",
"sudo apt upgrade -y"
],
"type": "shell"
},
{
"execute_command": "echo 'packer' | {{.Vars}} sudo -S -E bash '{{.Path}}'",
"script": "scripts/efi.sh",
"type": "shell"
},
{
"inline": [
"sudo cp /usr/lib/systemd/system/ssh.socket /usr/lib/systemd/system/ssh_vsock.socket",
"sudo cp /usr/lib/systemd/system/[email protected] /usr/lib/systemd/system/[email protected]",
"sudo sed -i 's/ListenStream=22/ListenStream=vsock::22/' /usr/lib/systemd/system/ssh_vsock.socket",
"sudo systemctl disable ssh.service",
"sudo systemctl enable ssh.socket",
"sudo systemctl enable ssh_vsock.socket"
],
"type": "shell"
},
{
"inline": [
"sudo sed -i -E 's/^#UseDNS.*$/UseDNS no/' /etc/ssh/sshd_config",
"sudo sed -i -E 's/^(127\\.0\\.0\\.1[[:blank:]].*)$/\\1\\tUNKNOWN/' /etc/hosts",
"echo 'hv_sock' | sudo tee /etc/modules-load.d/hv_sock.conf"
],
"type": "shell"
}
],
"variables": {
"domain": "template.local",
"ssh_key": "",
"vm_name": "deb-buster-tpl"
}
}
@mohamed3laa33
Copy link

@machv where is the mentioned scripts/linux.pref

@machv
Copy link
Author

machv commented Feb 23, 2022

Hello @mohamed3laa33, I am sorry for late response, you can find updated Packer template with inline definition of preferences in separate repo I created for templates https://github.com/microsoft/MSLab-templates/blob/main/templates/debian-11/debian-11.pkr.hcl#L105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment