【问题标题】:Packer creation of Ubuntu VirtualBox fails to shutdownUbuntu VirtualBox 的 Packer 创建无法关闭
【发布时间】:2015-10-27 20:22:45
【问题描述】:

我正在尝试使用Packer 创建一个Ubuntu VirtualBox

VirtualBox 创建良好,但 Packer 无法将其关闭并报错

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

如果我在出现上述消息时手动登录正在构建的VirtualBox,我可以毫无问题地使用sudoshutdown

重现问题的最小 Packer 配置是

{
    "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>"
        ]
    }]
}

virtualbox/preseed.cfg 包含

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

为了清楚起见,特意将上述 Packer 设置最小化。最终目标是让 Packer 设置也为生产创建 AWS 实例(实际上工作正常)。

如果我替换

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

"shutdown_command": "env"

然后我得到输出

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 

【问题讨论】:

    标签: ubuntu virtualbox packer


    【解决方案1】:

    原来在shutdown_command 中有non-breaking space 而不是常规的空白。对于 OSX,可以使用 disable non-breaking spaces,对于 Sublime Text,可以使用 Unicode highlighter

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多