# Créer une Vagrant box



## VirtualBox

https://www.engineyard.com/blog/building-a-vagrant-box
https://www.ceos3c.com/open-source/install-linux-mint-19-virtualbox/

1. #### Créer une nouvelle machine virtuelle

  Nom: Linux Mint 19 xfce
  Type: Linux
  Version: Ubuntu (64-bit)

  - mémoire: 2Go
  - disque dur: créer un disque dur maintenant
  - type: VMDK  VDI (VirtualBox Disk Image)
  - stockage: Dynamiquement alloué
  - emplacement et taille: /Users/bruno/VirtualBox VMs/Linux Mint 19 xfce/Linux Mint 19 xfce.vdi 20Go

2. #### Configurer le hardware

  - désactiver audio et USB
  - la carte réseau 1 doit être réglée sur NAT
  - ajouter cette règle de redirection de ports:
    Nom: SSH Protocole: TCP IP Hote: <blanc> Port hôte: 2222 IP invité <blanc> Port invité: 22

3. #### Installer Linux Mint 19

  - Démarrer la machine
  - Choisir l'ISO: linuxmint-19-xfce-64bit-v2.iso
  - Linux Mint 19 démarre dans son environnement Live. Double-clic sur Install Linux Mint
  - Choisir le language
  - Choisir le clavier
  - Ne pas installer les logiciels tiers
  - Effacer le disque et installer Linux Mint
  - Installer maintenant
  - Choisir le fuseau horaire
  - Utilisateur: vagrant Mot de passe: vagrant

4. #### Redémarrer:

   - Régler le mot de passe root: 
     `sudo passwd root (vagrant)`

   - Régler le super utilisateur: sudo visudo -f /etc/sudoers.d/vagrant
     Ajouter:

     ```bash
     add vagrant user
     vagrant ALL=(ALL) NOPASSWD:ALL
     ```

   - Sauver et quitter.

   - Tester:
     `sudo pwd`

5. #### Mettre à jour l'OS:

   ```bash
   sudo apt-get update -y
   sudo apt-get upgrade -y
   #et redémarrer
   sudo shutdown -r now
   ```

6. #### Reconfigurer le clavier:

```bash
sudo apt-get install keyboard-configuration
sudo dpkg-reconfigure keyboard-configuration
Keyboard-configuration choices for MackBookPro French keyboard:

-> MacBook / MacBookPro (Intl)
-> French
-> French (again)
-> Right Alt (AltGr)
-> No Compose Key
```

7. #### Installer la Clé Vagrant:

```bash
mkdir -p /home/vagrant/.ssh
chmod 0700 /home/vagrant/.ssh
wget --no-check-certificate \
    https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub \
    -O /home/vagrant/.ssh/authorized_keys
chmod 0600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
```

8. #### Installer le serveur OpenSSH:

```bash
sudo apt-get install -y openssh-server
sudo nano /etc/ssh/sshd_config
# Décommenter la ligne AuthorizedKeysFile %h/.ssh/authorized_keys
# Redémarrer ssh
sudo service ssh restart
```

9. #### Installer Guest Tools:

```bash
sudo apt-get install -y gcc build-essential linux-headers-server
#Dans VirtualBox, menu Périphériques, cliquer sur Insérer l'image CD des Additions Invité
sudo mount /dev/cdrom /mnt 
cd /mnt
sudo ./VBoxLinuxAdditions.run
```

10. #### Finaliser la Box:

```bash
“zero out” the drive.
sudo dd if=/dev/zero of=/EMPTY bs=1M
sudo rm -f /EMPTY
```

11. #### Créer un dossier pour les Box:

```bash
mkdir ~/vagrant_boxes
cd ~/vagrant_boxes
```

12. #### Créer le paquet (tar.gz et fichiers Vagrantfile , metadata.json 

```bash
$ vagrant package --base  Linux\ Mint\ 19\ xfce
==> Linux Mint 19 xfce: Attempting graceful shutdown of VM...
==> Linux Mint 19 xfce: Clearing any previously set forwarded ports...
==> Linux Mint 19 xfce: Exporting VM...
==> Linux Mint 19 xfce: Compressing package to: /Users/bruno/vagrant_boxes/package.box
```

13. #### Tester la box

```bash
$ vagrant box add mint19-64 package.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'mint19-64' (v0) for provider:
    box: Unpacking necessary files from: file:///Users/bruno/vagrant_boxes/package.box
==> box: Successfully added box 'mint19-64' (v0) for 'virtualbox'!
```



```bash
$ vagrant init mint19-64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
```

```bash
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'mint19-64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_boxes_default_1535276878752_78916
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

  https://www.virtualbox.org/manual/ch04.html#sharedfolders
```

```bash
This option can be disabled globally with an environment variable:

  VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

  config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/bruno/vagrant_boxes
```



```bash
$ vagrant ssh
vagrant@vagrant-VirtualBox:~$ ls
Bureau  Documents  Images  Modèles  Musique  Public  Téléchargements  Vidéos
```

```bash
vagrant@vagrant-VirtualBox:~$ exit
déconnexion
Connection to 127.0.0.1 closed.
```

