【问题标题】:vagrant provisionning in packer build打包器构建中的流浪者配置
【发布时间】:2016-04-01 13:34:31
【问题描述】:

我想用 Packer 创建一个 Vagrant 盒子。为此,我在 github 上使用了一个打包器项目:Packer ubuntu 14.04 LTS

“packer build”命令运行正常,我可以正常使用我的 vagrant box。但是我想用我的配置和工具预安装来构建一个 Vagrant 盒子。

为此,我创建了一个新的脚本 sh 文件,该文件在配置器脚本打包器模板中运行。

  "provisioners": [
    {
        "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'",
        "override": {
            "virtualbox-iso": {
                "scripts": [
                    "scripts/base.sh",
                    "scripts/system.sh",
                    "scripts/vagrant.sh",
                    "scripts/python.sh",
                    "scripts/virtualbox.sh",
                    "scripts/docker.sh",
                    "scripts/test-perso.sh",
                    "scripts/cleanup.sh"
                ]
            }
        },
        "type": "shell"
    }
]

以及该文件的包含:

#!/bin/bash -x

apt-get -y install aptitude
apt-get -y install nano
apt-get -y install apache2
apt-get -y install php5
apt-get -y install libapache2-mod-php5

所有打包程序构建运行正常,没有错误。但是,当使用此框创建我的 Vagrant 并启动 cd /etc/apache2 时,它找不到它。没有安装 apache2。

因为当在我的流浪汉上手动运行这个命令'apt-get -y install apache2'时,它会返回我

无法打开文件锁 /var/lib/dpkg/lock - open (13: Permission not allowed) 无法锁定管理目录 (/var/lib/dpkg/)。你有超级用户权限吗?

我认为有这个问题是因为用于配置我的 vagrant 的 vagrant 用户没有此权限。但我不明白为什么,因为我已经有了这个:

base.sh 脚本:

# Set up sudo
echo 'vagrant ALL=NOPASSWD:ALL' > /etc/sudoers.d/vagrant

vagrant.sh 脚本:

# Create the user vagrant with password vagrant
useradd -G sudo -p $(perl -e'print crypt("vagrant", "vagrant")') -m -s /bin/bash -N vagrant

# Install vagrant keys
mkdir -pm 700 /home/vagrant/.ssh
curl -Lo /home/vagrant/.ssh/authorized_keys \
  'https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub'
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh

template.json 打包器中的构建器:

  "builders": [
    {
      "boot_command": [
        "<esc><wait>",
        "<esc><wait>",
        "<enter><wait>",
        "/install/vmlinuz<wait>",
        " auto<wait>",
        " console-setup/ask_detect=false<wait>",
        " console-setup/layoutcode=fr<wait>",
        " console-setup/modelcode=pc105<wait>",
        " debconf/frontend=noninteractive<wait>",
        " debian-installer=fr_FR<wait>",
        " fb=false<wait>",
        " initrd=/install/initrd.gz<wait>",
        " kbd-chooser/method=fr<wait>",
        " keyboard-configuration/layout=fr<wait>",
        " keyboard-configuration/variant=fr<wait>",
        " locale=fr_FR<wait>",
        " netcfg/get_domain=vm<wait>",
        " netcfg/get_hostname=vagrant<wait>",
        " noapic<wait>",
        " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg<wait>",
        " -- <wait>",
        "<enter><wait>"
      ],
      "boot_wait": "10s",
      "disk_size": 20480,
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "guest_os_type": "Ubuntu_64",
      "http_directory": "http",
      "headless": true,
      "iso_checksum": "0501c446929f713eb162ae2088d8dc8b6426224a",
      "iso_checksum_type": "sha1",
      "iso_url": "http://mirrors.mit.edu/ubuntu-releases/trusty/ubuntu-14.04.3-server-amd64.iso",
      "output_directory": "packer-ubuntu-14.04-amd64-virtualbox",
      "shutdown_command": "echo 'vagrant'|sudo -S shutdown -P now",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_port": 22,
      "ssh_wait_timeout": "10000s",
      "type": "virtualbox-iso",
      "vboxmanage": [
        [
          "modifyvm",
          "{{.Name}}",
          "--memory",
          "1024"
        ],
        [
          "modifyvm",
          "{{.Name}}",
          "--cpus",
          "1"
        ]
      ],
      "virtualbox_version_file": ".vbox_version",
      "vm_name": "packer-ubuntu-14.04-amd64"
    }
  ]

还有我的 preseed.cfg ubuntu:

choose-mirror-bin mirror/http/proxy string
d-i base-installer/kernel/override-image string linux-server
d-i clock-setup/utc boolean true
d-i clock-setup/utc-auto boolean true
d-i finish-install/reboot_in_progress note
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/method string lvm
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm_write_new_label boolean true
d-i passwd/user-fullname string vagrant
d-i passwd/user-uid string 900
d-i passwd/user-password password vagrant
d-i passwd/user-password-again password vagrant
d-i passwd/username string vagrant
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev linux-source dkms nfs-common
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select unattended-upgrades
d-i pkgsel/upgrade select full-upgrade
d-i time/zone string UTC
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
tasksel tasksel/first multiselect standard, ubuntu-server

你能告诉我如何在我的 packer build 的配置 vagrant 中使用具有 sudo 访问权限的用户或 root 用户吗?我想在全面启动之前管理我的 vagrant box。

操作系统主机:windows 7 pro 操作系统访客:Ubuntu 14.04 LTS 虚拟机技术:VirtualBox 5.0.10 + Vagrant 1.7.4 VM 工具生成器:Packer 0.8.6

编辑:

我的调试日志告诉我 apache 正确安装并重新启动...

但是当一个vagrant up我觉得那个参数

config.vm.box_url

无法正确运行,因为当找到一个不存在的文件时,它会正确启动一个 virtualbox。

在这种情况下,我认为 vagrant up 不是好盒子,而且我的 vagrant 中没有安装 apache

【问题讨论】:

  • 你的输出是什么?特别是您将脚本设置为调试模式。我唯一能想到的就是在脚本的开头添加apt-get update,这通常是运行安装之前的最佳实践
  • 你也可以用apt-get install -y代替apt-get -y install
  • 我尝试将apt-get -y install 更改为apt-get install -y,但结果相同。明天我将尝试输出详细的构建打包程序...
  • 在输出调试中,一切似乎都很好。它安装apache,配置它并重新启动它。但是当连接到 vagrant 时,永远找不到安装 apache 的文件......非常非常奇怪

标签: linux ubuntu vagrant packer


【解决方案1】:

执行以下操作:

#vagrant box list

删除所有或不需要的框:

#vagrant box remove <box>

用新盒子运行 vagrant:

#vagrant up --provision

或者直接启动盒子而不进行配置:

#vagrant up --no-provision

最后:

#vagrant ssh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2016-05-16
    • 2022-01-26
    相关资源
    最近更新 更多