Windows 11 vagrant,virtual box, al2023

# Windows 11 vagrant,virtual box, al2023

## version 

```bash
vagrant --version
Vagrant 2.3.7
```

```bash
set PATH=%PATH%;PATH\VirtualBox
VBoxManage --version
7.0.14r161095
```

## box

[hashicorp](https://portal.cloud.hashicorp.com/vagrant/discover/gbailey/al2023)

teraterm で接続できない場合。

```bash
[root@amazonlinux-2023 ~]# cat /home/vagrant/.ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKpWN2a3QEyUk08llu9zn+4pgAd+j63J2eyd017lkxLB hoge@localost
```

簡単な設定。

```bash
dnf -y install rsyslog
timedatectl set-timezone Asia/Tokyo
/opt/VBoxGuestAdditions-7.1.4/uninstall.sh
```

```bash
ll -h D:/APP_HOME/VirtualBoxVMs/al2023_default_1743900114911_11431 | grep vmdk
-rw-a-- 2.3G Apr  6 09:45:43 box-disk001.vmdk
```

```bash
[root@amazonlinux-2023 ~]# df -h | grep '/dev/sda1'
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        4.0M     0  4.0M   0% /dev
tmpfs           980M     0  980M   0% /dev/shm
tmpfs           392M  5.4M  387M   2% /run
/dev/sda1        25G  2.2G   23G   9% /
[root@amazonlinux-2023 ~]# 
```

利用できるバージョンの確認

```bash
/usr/bin/dnf check-release-update
```

```text
Version 2023.6.20250303:
Run the following command to upgrade to 2023.6.20250303:

    dnf upgrade --releasever=2023.6.20250303

Version 2023.6.20250317:
Run the following command to upgrade to 2023.6.20250317:
    dnf upgrade --releasever=2023.6.20250317

Version 2023.7.20250331:
Run the following command to upgrade to 2023.7.20250331:
    dnf upgrade --releasever=2023.7.20250331
```

```bash
vagrant package
vagrant box add BOX_NAME ./package.box
```

```bash
dnf upgrade --releasever=2023.6.20250303
# Total download size: 136 M
```

```bash
dnf install tpm2-tss tpm2-tools
```

```bash
$ ll -h D:/APP_HOME/VirtualBoxVMs/al2023_default_1743900114911_11431 | grep vmdk
-rw-a-- 2.9G Apr  6 10:01:02 box-disk001.vmdk 
```

```bash
[root@amazonlinux-2023 ~]# df -h | grep '/dev/sda1'
/dev/sda1        25G  2.4G   23G  10% /
/dev/sda128      10M  1.3M  8.7M  13% /boot/efi
```

```bash
dnf upgrade --releasever=2023.6.20250317
# Total download size: 139 M
```

```bash
[root@amazonlinux-2023 ~]# df -h | grep '/dev/sda1'
/dev/sda1        25G  2.5G   23G  10% /
/dev/sda128      10M  1.3M  8.7M  13% /boot/efi
```

```bash
$ ll -h D:/APP_HOME/VirtualBoxVMs/al2023_default_1743900114911_11431 | grep vmdk
-rw-a-- 3.3G Apr  6 10:13:19 box-disk001.vmdk 
```

```bash
dnf upgrade --releasever=2023.7.20250331
# Total download size: 142 M
```

```bash
[root@amazonlinux-2023 ~]# df -h | grep '/dev/sda1'
/dev/sda1        25G  2.5G   23G  10% /
/dev/sda128      10M  1.3M  8.7M  13% /boot/efi
```

```bash
$ ll -h D:/APP_HOME/VirtualBoxVMs/al2023_default_1743900114911_11431 | grep vmdk
-rw-a-- 3.4G Apr  6 10:18:07 box-disk001.vmdk 
```

[Vagrantで容量が膨れ上がったvmdkを圧縮する方法(Vagrant + VirtualBox)](https://qiita.com/Gushi_maru/items/3efd3e5ac497f25f7606)


```bash
# al2023
dd if=/dev/zero of=zero bs=4k; \rm zero
```

```bash
[root@amazonlinux-2023 ~]# df -h
/dev/sda1        25G  2.6G   23G  11% /
/dev/sda128      10M  1.3M  8.7M  13% /boot/efi
```

## windows

```bash
set PATH=%PATH%;PATH\VirtualBox
```

```bash
VBoxManage.exe list hdds
```

```text
UUID:           fbc87567-d5fa-4336-8c7b-19bb3e7e78bd
Parent UUID:    base
State:          locked write
Type:           normal (base)
Location:       D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk
Storage format: VMDK
Capacity:       25600 MBytes
Encryption:     disabled
```

* copy(clone)

```bash
# vm の停止
vagrant halt
# VBoxManage.exe clonehd [inputFile] [outputFile]
# ^ = 改行
VBoxManage.exe clonehd "D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk" ^
"D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi" --format vdi
```

* windows

```bash
$ ll -h D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk
-rw-a-- 3.4G Apr  6 13:15:47 D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk 
$ ll -h D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi
-rw-a-- 2.8G Apr  6 13:18:17 D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi 
```

* copy 圧縮

```bash
# VBoxManage.exe modifyhd [inputFile] --compact
VBoxManage.exe modifyhd "D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi" --compact
# 変わらず。
$ ll -h D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi
-rw-a-- 2.8G Apr  6 13:21:06 D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi 
```

* vdiファイルをvmdk形式に変換

```bash
# .\VBoxManage.exe clonehd [inputFile] [outputFile] VDI形式からVMDK形式のクローンを作成します。
VBoxManage.exe clonehd "D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi" ^
"D:\APP_HOME\VirtualBoxVMs\al2023-clone\al2023-clone.vmdk" --format vmdk
# --variant Split2G をつけるとなんかファイルがいっぱいできたのでつけつけない方向で
```

```text
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone medium created in format 'vmdk'. UUID: e142b3f7-ec38-4658-b094-8ed10aecc3a0
```

```bash
$ VBoxManage.exe list vms
"ubuntu2204_default_1743337068182_62785" {7ee3852a-fcb8-4403-a987-de2efb0aa073}
"al2023_default_1743900114911_11431" {265c00af-9196-4a42-97f8-5a52ad9dbb00}
```

* デタッチ

```bash
VBoxManage.exe storageattach 265c00af-9196-4a42-97f8-5a52ad9dbb00 ^
--storagectl "SATA" ^
--port 0 --type hdd --medium emptydrive
```

* アタッチ

```bash
VBoxManage.exe storageattach 265c00af-9196-4a42-97f8-5a52ad9dbb00 ^
--storagectl "SATA" --port 0 --type hdd ^
--medium "D:\APP_HOME\VirtualBoxVMs\al2023-clone\al2023-clone.vmdk"
```

```bash
VBoxManage.exe list hdds
```

```bash
# VBoxManage.exe closemedium disk [ DISKs_UUID ]
# Location:       D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi
# vmdk => vdi に変換したももの
VBoxManage.exe closemedium disk ba33dea4-a00f-4c02-80d4-ba70c2cebcbc
# Location:       D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy-clone.vmdk
# 間違えて作成した disk
VBoxManage.exe closemedium disk 6b1ce84f-d419-41ca-acf2-81fb9b700a8f
# Location:       D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk
# 元々圧縮元ディスク
VBoxManage.exe closemedium disk fbc87567-d5fa-4336-8c7b-19bb3e7e78bd
```

```bash
rm "D:\APP_HOME\VirtualBoxVMs\al2023-copy\copy.vdi"
```

```bash
VBoxManage.exe storageattach 265c00af-9196-4a42-97f8-5a52ad9dbb00 ^
--storagectl "SATA" ^
--port 0 --type hdd --medium emptydrive
```

```bash
mkdir D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\compression
mv "D:\APP_HOME\VirtualBoxVMs\al2023-clone\al2023-clone.vmdk" "D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\compression" 
```

```bash
# 新しいdisk 情報削除
VBoxManage.exe closemedium disk e142b3f7-ec38-4658-b094-8ed10aecc3a0
```

```bash
VBoxManage.exe storageattach 265c00af-9196-4a42-97f8-5a52ad9dbb00 ^
--storagectl "SATA" --port 0 --type hdd ^
--medium "D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\compression\al2023-clone.vmdk"
```

```bash
rm "D:\APP_HOME\VirtualBoxVMs\al2023_default_1743900114911_11431\box-disk001.vmdk"
```


```bash
vagrant halt
vagrant package
ll -h package.box
# 878M
vagrant box add lo-al2023 ./package.box
```