jpsecher
jpsecher

Reputation: 4881

Packer creation of Ubuntu VirtualBox fails to shutdown

I am trying to create an Ubuntu VirtualBox using Packer.

The VirtualBox is created fine, but Packer cannot shut it down and complains

==> virtualbox: Gracefully halting virtual machine...
    virtualbox: bash:  sudo: command not found
==> virtualbox: Timeout while waiting for machine to shut down.

If I manually log into the VirtualBox under construction at the time the above message appears, I can use sudo and shutdown with no problems.

A minimal Packer configuration to reproduce the problem is

{
    "variables": {
        "ssh_name": "ubuntu",
        "ssh_pass": "ubuntu",
        "host_name": "tahr"
    },
    "builders": [{
        "name": "virtualbox",
        "type": "virtualbox-iso",
        "guest_os_type": "Ubuntu_64",
        "vboxmanage": [
            ["modifyvm","{{.Name}}","--vram","32"]
        ],
        "disk_size": 10000,
        "iso_url": "http://releases.ubuntu.com/trusty/ubuntu-14.04.3-server-amd64.iso",
        "iso_checksum": "0501c446929f713eb162ae2088d8dc8b6426224a",
        "iso_checksum_type": "sha1",
        "http_directory": "virtualbox",
        "http_port_min": "9001",
        "http_port_max": "9001",
        "ssh_username": "{{user `ssh_name`}}",
        "ssh_password": "{{user `ssh_pass`}}",
        "ssh_pty" : "true",
        "ssh_wait_timeout": "15m",
        "shutdown_command": "echo {{user `ssh_pass`}} | sudo -S shutdown -P now",
        "boot_command": [
            "<esc><esc><enter><wait>",
            "/install/vmlinuz noapic ",
            "preseed/url=http://{{.HTTPIP}}:{{.HTTPPort}}/preseed.cfg ",
            "debian-installer=en_US auto locale=en_US kbd-chooser/method=us ",
            "hostname={{user `host_name`}} ",
            "fb=false debconf/frontend=noninteractive ",
            "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA ",
            "keyboard-configuration/variant=USA console-setup/ask_detect=false ",
            "initrd=/install/initrd.gz -- <enter>"
        ]
    }]
}

with the virtualbox/preseed.cfg containing

d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i localechooser/preferred-locale string en_US.UTF-8
d-i localechooser/supported-locales en_US.UTF-8
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layout select USA
d-i keyboard-configuration/variant select USA
d-i keyboard-configuration/modelcode string pc105
d-i netcfg/get_hostname string this-host
d-i netcfg/get_domain string this-host
d-i time/zone string UTC
d-i clock-setup/utc-auto boolean true
d-i clock-setup/utc boolean true
d-i debconf debconf/frontend select Noninteractive
d-i pkgsel/install-language-support boolean false
tasksel tasksel/first multiselect standard, ubuntu-server
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman/confirm_write_new_label boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto-lvm/guided_size string max
d-i mirror/http/proxy string
d-i passwd/user-fullname string ubuntu
d-i passwd/username string ubuntu
d-i passwd/user-password password ubuntu
d-i passwd/user-password-again password ubuntu
d-i user-setup/encrypt-home boolean false
d-i user-setup/allow-password-weak boolean true
d-i pkgsel/install-language-support boolean false
d-i pkgsel/include string build-essential ssh
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select safe-upgrade
d-i grub-installer/only_debian boolean true
d-i finish-install/reboot_in_progress note

The above Packer setup is intentionally minimised for clarification. The final goal is to have the Packer setup also create AWS instances (which actually works fine) for production.

If I replace

"shutdown_command": "echo {{user `ssh_pass`}} | sudo -S shutdown -P now"

with

"shutdown_command": "env"

then I get the output

virtualbox: XDG_SESSION_ID=1
virtualbox: TERM=xterm
virtualbox: SHELL=/bin/bash
virtualbox: SSH_CLIENT=10.0.2.2 63390 22
virtualbox: SSH_TTY=/dev/pts/0
virtualbox: USER=ubuntu
virtualbox: SSH_AUTH_SOCK=/tmp/ssh-xO4Ad2K13w/agent.825
virtualbox: MAIL=/var/mail/ubuntu
virtualbox: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
virtualbox: PWD=/home/ubuntu
virtualbox: LANG=en_US.UTF-8
virtualbox: SHLVL=1
virtualbox: HOME=/home/ubuntu
virtualbox: LOGNAME=ubuntu
virtualbox: SSH_CONNECTION=10.0.2.2 63390 10.0.2.15 22
virtualbox: XDG_RUNTIME_DIR=/run/user/1000
virtualbox: _=/usr/bin/env 

Upvotes: 1

Views: 2113

Answers (1)

jpsecher
jpsecher

Reputation: 4881

It turns out there was non-breaking space instead of a regular blank in the shutdown_command. For OSX, it is possible to disable non-breaking spaces, and for Sublime Text there is a Unicode highlighter.

Upvotes: 1

Related Questions