Cómo compactar y exportar una máquina virtual de VirtualBox#
Note
- Todos estos comandos son opcionales y ayudan a reducir aún más el tamaño del disco de la máquina virtual
- Estos comandos son específicos para Debian, Ubuntu y derivados
- Sólo funcionan en distribuciones de GNU/Linux que tengan el gestor de paquetes APT
Configurar máquina virtual#
Warning
- Ejecutar estos comandos DENTRO de la máquina virtual
Configurar locale#
Seleccionar los siguientes idiomas para el sistema operativo
root@debian-11:~# dpkg-reconfigure -p low locales ⬅️
-
en-US.UTF-8
-
es-MX.UTF-8
Seleccionar el locale predeterminado según sea el caso
Instalar herramientas#
root@debian-11:~# apt install zerofree localepurge ⬅️
Note
Configurar localepurge
con las opciones predeterminadas
Eliminar caché de APT#
root@debian-11:~# apt clean ⬅️
root@debian-11:~# apt purge ⬅️
root@debian-11:~# apt autoremove ⬅️
/var/cache/apt/archives
/var/cache/apt/archives/partial
Compactar el sistema de archivos en la máquina virtual#
Iniciar la máquina virtual en modo de rescate#
Iniciar la máquina virtual y seleccionar las opciones avanzadas de inicio.
GNU GRUB version 2.04-20
+----------------------------------------------------------------------------+
| Debian GNU/Linux |
|■ Advanced options for Debian GNU/Linux ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| ⬅️
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
+----------------------------------------------------------------------------+
Use the ^ and v keys to select which entry is highlighted.
Press enter to boot the selected OS, `e' to edit the commands
before booting or `c' for a command-line.
Seleccionar el modo de recuperación para iniciar en un ambiente reducido.
GNU GRUB version 2.04-20
+----------------------------------------------------------------------------+
| Debian GNU/Linux, with Linux 5.10.0-17-amd64 |
|■ Debian GNU/Linux, with Linux 5.10.0-17-amd64 (recovery mode) ■■■■■■■■■■■■■| ⬅️
| |
| |
| |
| |
| |
| |
| |
| |
| |
+----------------------------------------------------------------------------+
Use the ^ and v keys to select which entry is highlighted.
Press enter to boot the selected OS, `e' to edit the commands
before booting or `c' for a command-line. ESC to return
previous menu.
Escribir la contraseña del usuario root
, no se mostrará en la pantalla.
Booting a command list
Loading Linux 5.10.0-17-amd64 ...
Loading initial ramdisk ...
/dev/sda1: clean, 64094/593344 files, 543653/2371072 blocks
You are in rescue mode. After logging in, type "journalctl -xb" to view
system logs, "systemctl reboot" to reboot, "systemctl default" or "exit"
to boot into default mode.
Give root password for maintenance ✅
(or press Control-D to continue):
Reducir programas en ejecución#
Listar los procesos en ejecución.
root@debian-11:~# ps afx ⬅️
PID TTY STAT TIME COMMAND
2 ? S 0:00 [kthreadd]
... ... ... ... ... ... ...
1 ? Ss 0:01 /sbin/init single
182 ? Ss 0:00 /lib/systemd/systemd-journald
202 ? Ss 0:00 /lib/systemd/systemd-udevd
268 ? Ssl 0:00 /lib/systemd/systemd-timesyncd
291 ? Ssl 0:00 /sbin/dhclient -4 -v -i -pf /run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases -I -df /var/lib/dhcp/dhclient6.eth0.leases eth0
292 ? Ssl 0:00 /sbin/dhclient -4 -v -i -pf /run/dhclient.eth1.pid -lf /var/lib/dhcp/dhclient.eth1.leases -I -df /var/lib/dhcp/dhclient6.eth1.leases eth1
390 ttyS0 Ss 0:00 /lib/systemd/systemd-sulogin-shell rescue
391 ttyS0 S 0:00 \_ /sbin/sulogin
392 ttyS0 S 0:00 \_ bash
398 ttyS0 R+ 0:00 | \_ ps afx
399 ttyS0 S+ 0:00 | \_ more
393 tty1 Ss+ 0:00 \_ /sbin/sulogin
Detener los servicios de SystemD.
root@debian-11:~# systemctl stop systemd-journald systemd-udevd systemd-timesyncd ⬅️
... ... ... ... ... ... ...
Warning: Stopping systemd-journald.service, but it can still be activated by:
systemd-journald-dev-log.socket
systemd-journald-audit.socket
systemd-journald.socket
Warning: Stopping systemd-udevd.service, but it can still be activated by:
systemd-udevd-control.socket
systemd-udevd-kernel.socket
Listar los procesos del cliente de DHCP y detenerlos.
root@debian-11:~# pgrep dhclient ⬅️
291 292
root@debian-11:~# pkill dhclient ⬅️
Listar los procesos restantes.
root@debian-11:~# ps afx ⬅️
PID TTY STAT TIME COMMAND
2 ? S 0:00 [kthreadd]
... ... ... ... ... ... ...
1 ? Ss 0:01 /sbin/init single
390 ttyS0 Ss 0:00 /lib/systemd/systemd-sulogin-shell rescue
391 ttyS0 S 0:00 \_ /sbin/sulogin
392 ttyS0 S 0:00 \_ bash
416 ttyS0 R+ 0:00 | \_ ps afx
393 tty1 Ss+ 0:00 \_ /sbin/sulogin
Montar el sistema de archivos en modo de sólo lectura#
Identificar los discos y particiones que tiene la máquina virtual.
root@debian-11:~# lsblk ⬅️
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
...
sda 8:0 0 10G 0 disk
├─sda1 8:1 0 9G 0 part
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 975M 0 part
sr0 11:0 1 1M 0 rom
Identificar la partición que tiene el sistema de archivos raíz.
root@debian-11:~# df -m / ⬅️
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/sda1 9017 1879 6660 23% /
Verificar si el sistema de archivos está montado como lectura-escritura (rw
).
root@debian-11:~# mount | grep '/dev/sda1' ⬅️
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)
Remontar el sistema de archivos en modo sólo lectura (ro
).
root@debian-11:~# mount -o remount,ro / ⬅️
root@debian-11:~# mount | grep '/dev/sda1'
/dev/sda1 on / type ext4 (ro,relatime,errors=remount-ro) ✅
Compactar el sistema de archivos raíz#
Identificar la ruta al programa zerofree
y las opciones con las que se ejecuta
root@debian-11:~# which zerofree ⬅️
/usr/sbin/zerofree ✅
root@debian-11:~# zerofree ⬅️
usage: zerofree [-n] [-v] [-f fillval] filesystem
Ejecutar el programa zerofree
para rellenar el sistema de archivos con 0x00
.
root@debian-11:~# zerofree -v /dev/sda1 ⬅️
0.0%
...
100.0% ✅
12280/1827419/2371072
Apagar la máquina inmediatamente.
# poweroff ⬅️
Manejo de imagenes OVA en la máquina física#
Warning
- Ejecutar estos comandos en la máquina física
Compactar el disco de la máquina virtual#
Listar el tamaño del disco de la máquina virtual.
tonejito@laptop:~/VirtualBox VMs/Debian-11$ ls -l *.vdi ⬅️
-rw------- 1 tonejito tonejito 4707319808 oct 24 12:32 Debian-11.vdi
Tamaño disco original VDI |
---|
![]() |
Ejecutar el comando para compactar el disco VDI.
tonejito@laptop:~/VirtualBox VMs/Debian-11$ VBoxManage modifyhd "Debian-11.vdi" -compact ⬅️
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Verificar que el disco de la máquina virtual haya sido compactado.
tonejito@laptop:~/VirtualBox VMs/Debian-11$ ls -l *.vdi ⬅️
-rw------- 1 tonejito tonejito 2360344576 oct 25 22:33 Debian-11.vdi ✅
Tamaño disco VDI compactado |
---|
![]() |
Exportar la máquina virtual#
Exportar la máquina virtual a una imagen OVA
Pantalla principal de VirtualBox |
---|
![]() |
Menú exportar |
---|
![]() |
Seleccionar máquina virtual |
---|
![]() |
Configuración OVA |
---|
![]() |
Información OVA |
---|
![]() |
Exportando imagen OVA… |
---|
![]() |
Tamaño disco original VDI |
---|
![]() |
Tamaño imagen OVA |
---|
![]() |
Importar imagen OVA#
Importar la máquina virtual desde la imagen OVA
Menú importar |
---|
![]() |
Seleccionar imagen OVA |
---|
![]() |
Buscar imagen OVA |
---|
![]() |
Configuración OVA |
---|
![]() |
Importando imagen OVA… |
---|
![]() |
Referencias#
- https://wiki.debian.org/Locale
- https://manpages.debian.org/bullseye/coreutils/dd.1.en.html
- https://manpages.debian.org/bullseye/zerofree/zerofree.8.en.html
- https://manpages.debian.org/bullseye/localepurge/localepurge.8.en.html
- https://www.debian.org/releases/bullseye/amd64/ch05s03.en.html
- https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifymedium
- https://www.virtualbox.org/wiki/Serial_redirect
- http://blog.tonejito.org/2011/11/zerofree-zero-out-space-on-virtual.html
- http://blog.tonejito.org/2012/05/virtualbox-serial-console-on-mac-os-x.html
- https://www.cyberciti.biz/faq/linux-unix-dd-command-show-progress-while-coping/
- https://www.cyberciti.biz/faq/howto-setup-serial-console-on-debian-linux/
- https://www.gonwan.com/2014/04/07/setting-up-serial-console-on-virtualbox/
- https://unix.stackexchange.com/questions/42015/mount-is-busy-when-trying-to-mount-as-read-only-so-that-i-can-run-zerofree