weixin_39984661
weixin_39984661
2020-12-02 00:43

'vagrant up' fails on generic/alpine310 if the option hostname is needed

Vagrant version

2.2.5

Host operating system

Ubuntu 18.04 LTS (Bionic Beaver)

Guest operating system

Alpine Linux 3.10

Vagrantfile

ruby
Vagrant.configure("2") do |config|  
  config.vm.box = "generic/alpine310"
  config.vm.hostname = "alpine.example.org" 
end

Expected behavior

Alpine VM is up and requested hostname is set.

Actual behavior

vagrant up fails on Setting hostname....

sh
==> default: Setting hostname...                                                                                                                                                              
The following SSH command responded with a non-zero exit status.                                                                                                                              
Vagrant assumes that this means the command failed!                                                                                                                                           

# Save current hostname saved in /etc/hosts                                                                                                                                                   
CURRENT_HOSTNAME_FULL="$(hostname -f)"                                                                                                                                                        
CURRENT_HOSTNAME_SHORT="$(hostname -s)"                                                                                                                                                       

# New hostname to be saved in /etc/hosts                                                                                                                                                      
NEW_HOSTNAME_FULL='alpine.exampler.org'                                                                                                                                                       
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"                                                                                                                                                 

# Update sysconfig                                                                                                                                                                            
sed -i 's/\(HOSTNAME=\).*/\1alpine.exampler.org/' /etc/sysconfig/network                                                                                                                      

# Set the hostname - use hostnamectl if available
if command -v hostnamectl; then
  hostnamectl set-hostname --static 'alpine.exampler.org'
  hostnamectl set-hostname --transient 'alpine.exampler.org'
else
  hostname 'alpine.exampler.org'
fi

# Update ourselves in /etc/hosts
if grep -w "$CURRENT_HOSTNAME_FULL" /etc/hosts; then
  sed -i -e "s/( )$CURRENT_HOSTNAME_FULL( )/$NEW_HOSTNAME_FULL/g" -e "s/( )$CURRENT_HOSTNAME_FULL$/$NEW_HOSTNAME_FULL/g" /etc/hosts
fi
if grep -w "$CURRENT_HOSTNAME_SHORT" /etc/hosts; then
  sed -i -e "s/( )$CURRENT_HOSTNAME_SHORT( )/$NEW_HOSTNAME_SHORT/g" -e "s/( )$CURRENT_HOSTNAME_SHORT$/$NEW_HOSTNAME_SHORT/g" /etc/hosts
fi

# Restart network
service network restart


Stdout from the command:

127.0.0.1       localhost.lavabit.com localhost localhost.localdomain localhost
127.0.0.1       localhost.lavabit.com localhost localhost.localdomain localhost
::1             localhost localhost.localdomain


Stderr from the command:

sed: /etc/sysconfig/network: No such file or directory
 * service: service `network' does not exist

Steps to reproduce

Run vagrant up with the provided Vagrantfile

References

The same error is mentioned on hashicorp/vagrant: #10584

该提问来源于开源项目:lavabit/robox

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • weixin_39738273 weixin_39738273 5月前

    sadly this isn't a bug that I can fix. I believe checked already, but setting the box type to :linux causes the network provisioning to break. While setting the box type to :alt fixes the networking issue, but leads to this hostname problem.

    You have two solutions. You can install the vagrant-alpine plugin, and then override the default type, by setting it to :alpine or you can track down the vagrant code that runs the above commands, and where it says:

    bash                                                                                                                                                                                    
    # Update sysconfig
    sed -i 's/\(HOSTNAME=\).*/\1alpine.exampler.org/' /etc/sysconfig/network
    

    Change it to:

    bash
    if [ -f /etc/sysconfig/network ]; then
      # Update sysconfig
      sed -i 's/\(HOSTNAME=\).*/\1alpine.exampler.org/' /etc/sysconfig/network
    fi
    

    That should sidestep the bug at the cost of preventing the hostname change from persisting across reboots. An else which runs sed against the appropriate file would fix that. I'd do it myself, but the internet connection where I am is currently running at 10 kb/s with 40% packet loss, so I couldn't boot Alpine to check the file.

    点赞 评论 复制链接分享
  • weixin_39738273 weixin_39738273 5月前

    I created a vagrant pull request ... https://github.com/hashicorp/vagrant/pull/11000 ...

    or can either of you test it?

    点赞 评论 复制链接分享
  • weixin_39798579 weixin_39798579 5月前

    The PR is not really ideal, I left a comment with my reasoning there.

    点赞 评论 复制链接分享
  • weixin_39984661 weixin_39984661 5月前

    I've also tested the alpine 3.8 vagrant box from maier who wrote the vagrant-alpine plugin:

    ruby
    Vagrant.configure("2") do |config|  
      config.vm.box = "maier/alpine-3.8-x86_64"
      config.vm.hostname = "alpine.example.org" 
    end
    

    I don't know what he changed in his alpine vagrant box, but setting the hostname works without problems. Maybe you'll find out what he's changed to make it work.

    点赞 评论 复制链接分享
  • weixin_39738273 weixin_39738273 5月前

    He could have done a couple of things. He might be setting the box type to :linux which will fix the hostname issue, but can cause problems when you try to provision network resources. He could have also created dummy files so the provisioner doesn't fail. I'm considering giving the latter a try, at least until things get worked out upstream.

    点赞 评论 复制链接分享
  • weixin_39738273 weixin_39738273 5月前

    This issue has been discussed at length, and hopefully fixed with the forthcoming vagrant v2.2.6 release.

    点赞 评论 复制链接分享
  • weixin_39984661 weixin_39984661 5月前

    Yes, it's fixed with the Vagrant v2.2.6 release.

    点赞 评论 复制链接分享

相关推荐