0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

vagrant+hypervでのRhel(subscriptionの自動登録、解除)

Posted at

日本語でまとまっている情報がなかったので書きます。
vagrant使ってsubscription-managerを手動で打っているサイト結構あるけど、
自動化できるのでちゃんと自動化しましょう。

hyper-vとvagrantのインストール

ここを見てください。

rhelのサブスクリプションの申請

developerアカウントなら、無料で使えます。
rhel自体はsubscriptionが無くても実は使えますが、
使えるパッケージがかなり限定されるのでほぼほぼ使えません。
(口述のntfsと連携とるためのライブラリやgroupinstallの"Server With GUI"も使えない。)
rhelの登録をしておきましょう。

rhelのdeveloper用サイト

vagrantのプラグインのインストール

vagrant up時に自動的にrhelにサブスクリプションの登録、
vagrant halt時に解除を行うプラグインです。

プラグインとしてはredhat公式にこれ使えと書かれているので安全かと。

この二つの他は必要ないです。

vagrant plugin install vagrant-service-manager
vagrant plugin install vagrant-registration

Vagrantfile

下記のような感じになります。

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
    # The most common configuration options are documented and commented below.
    # For a complete reference, please see the online documentation at
    # https://docs.vagrantup.com.
  
    # Every Vagrant development environment requires a box. You can search for
    # boxes at https://vagrantcloud.com/search.
    # config.vm.box = "oraclelinux/i"
    config.vm.box = "generic/rhel9"

    # The url from where the 'config.vm.box' box will be fetched if it
    # doesn't already exist on the user's system.
    # config.vm.box_url = "https://oracle.github.io/vagrant-projects/boxes/oraclelinux/7.json"

    # dont set ssh key, if you create vagrant box from this container.
    # this config is being used, firtst time you create and launch container.
    # config.ssh.insert_key = false

    # Disable automatic box update checking. If you disable this, then
    # boxes will only be checked for updates when the user runs
    # `vagrant box outdated`. This is not recommended.
    # config.vm.box_check_update = false
  
    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    # NOTE: This will enable public access to the opened port
    # config.vm.network "forwarded_port", guest: 3000, host: 3000
    # config.vm.network "forwarded_port", guest: 1521, host: 1521
  
    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine and only allow access
    # via 127.0.0.1 to disable public access
    # config.vm.network "forwarded_port", guest: 3000, host: 3000, host_ip: "127.0.0.1"
  
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    # config.vm.network "private_network", ip: "192.168.33.10"
  
    # Create a public network, which generally matched to bridged network.
    # Bridged networks make the machine appear as another physical device on
    # your network.
    # config.vm.network "public_network"
  
    # Share an additional folder to the guest VM. The first argument is
    # the path on the host to the actual folder. The second argument is
    # the path on the guest to mount the folder. And the optional third
    # argument is a set of non-required options.

    # Share an additional folder to the guest VM. The first argument is
    # the path on the host to the actual folder. The second argument is
    # the path on the guest to mount the folder. And the optional third
    # argument is a set of non-required options.
    # config.vm.synced_folder "../data", "/vagrant_data"

    config.registration.username = ENV['REDHAT_USERNAME']
    config.registration.password = ENV['REDHAT_PASSWORD']

    # Provider-specific configuration so you can fine-tune various
    # backing providers for Vagrant. These expose provider-specific options.
    # Example for VirtualBox:
    #
    config.vm.provider "virtualbox" do |vb|
      # Display the VirtualBox GUI when booting the machine
      vb.gui = true

      # Customize the amount of memory on the VM:
        vb.memory = "8192"
    end

    config.vm.provider "hyperv" do |hv|
      # hyper-vでゲストーホスト間でのファイル共有が必要な場合は必須。
      config.vm.synced_folder ".", "/vagrant", type: "smb"
      hv.maxmemory = "8192"
    end
    #
    # View the documentation for the provider you are using for more
    # information on available options.
  
    # Enable provisioning with a shell script. Additional provisioners such as
    # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
    # documentation for more information about their specific syntax and use.
    config.vm.provision "shell", path: "./provision.sh"
end

vagrantのprovision用のファイルは次のようになります。

provision.sh
dnf update -y

# macの日本語キーボードの場合は下のレイアウトを反映させる。
# これを反映させないとhyper-vなどの画面から操作するときに実際のキーボードと
# 違うレイアウトになる。
# mac以外のPCの場合は下記のコマンドでノートパソコンのキーマップで近いものを調べて設定すること。
# localectl list-x11-keymap-models
localectl set-x11-keymap jp apple_laptop

# ゲストーホスト間でファイル共有するなら必要。
dnf install -y cifs-utils

## ここから下はGUIでRhelを使いたい場合のみ必要。
# いらないならコメントアウト
dnf -y groupinstall "Server with GUI"

# guiを起動させるために必要。
systemctl set-default graphical.target

ユーザー名、パスワードを環境変数として設定しておく。

REDHATのユーザー名とパスワードを登録しておきます。
上の設定だと

Set-ItemProperty HKCU:\Environment\ -Name REDHAT_USERNAME -Value <username>
Set-ItemProperty HKCU:\Environment\ -Name REDHAT_PASSWORD -Value <password>

設定しておく。

GUIのユーザーも

Set-ItemProperty HKCU:\Environment\ -Name VAGRANT_USERNAME -Value <username>
Set-ItemProperty HKCU:\Environment\ -Name VAGRANT_PASSWORD -Value <password>

windowsには環境変数のバグがあるので、レジストリから必ず触ってください。
いろいろ意識するの面倒臭いと思うので。

vagrantの起動

Vagrantfile下で

vagrant up --provider hyperv

GUIの場合は最後までインストールできたら、
したのコマンドで再起動させると、GUIの画面でログインできるようになります。
hyper-vのウィンドウだけ立ち上がらないので、その部分だけ手動になります。

vagrant reload

refrerence

Redhat サイトvagrant

robox

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?