GitLab Runner搭配VirtualBox执行器无法处理并发作业的问题求助
GitLab Runner VirtualBox执行器并发作业失败(VBOX_E_INVALID_OBJECT_STATE 0x80bb0007)
我需要通过预配置的Windows 10基础镜像,使用VirtualBox执行器运行2个及以上并发作业。VirtualBox会创建该基础镜像的两个实例(xyz-concurrent-0和xyz-concurrent-1)并同时启动,但第一个作业运行正常,第二个作业始终失败,报错如下:
正常作业日志(第一个启动的作业)
Running with gitlab-runner 16.10.0 (81ab07f6) on Runner_1 93V7Dm8Bk, system ID: s_a1f2f83ac892 Preparing the "virtualbox" executor 01:55 Using VirtualBox version 7.0.14r161095 executor... Creating new VM... Creating default snapshot... Waiting for VM to become responsive... Starting SSH command... Preparing environment 00:02 Running on WIN10 via EVH11716WS... Getting source from Git repository 00:12 Fetching changes with git depth set to 20... Initialized empty Git repository in C:/Users/gitlab-user/builds/myname/roli-dummy-project/.git/ Created fresh repository. warning: auto-detection of host provider took too long (>2000ms) warning: see https://aka.ms/gcm/autodetect for more information. fatal: Unable to persist credentials with the 'wincredman' credential store. See https://aka.ms/gcm/credstores for more information. Checking out 6cce2e68 as detached HEAD (ref is main)... git-lfs/3.4.1 (GitHub; windows amd64; go 1.20.11; git 0898dcbc) Skipping Git submodules setup Executing "step_script" stage of the job script 00:32 $ powershell -File echo.ps1 WIN10 WIN10 WIN10 Cleaning up project directory and file based variables 00:01 Job succeeded
失败作业日志(第二个启动的作业)
Running with gitlab-runner 16.10.0 (81ab07f6) on Runner_1 93V7Dm8Bk, system ID: s_a1f2f83ac892 Preparing the "virtualbox" executor 00:25 Using VirtualBox version 7.0.14r161095 executor... Creating new VM... ERROR: Preparation failed: VBoxManageOutput error: vboxmanage.exe: error: Locking of attached media failed. A possible reason is that one of the media is attached to a running VM vboxmanage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component SessionMachine, interface IMachine Will be retried in 3s ... Using VirtualBox version 7.0.14r161095 executor... Restoring VM from snapshot... Previous VM failed. Deleting, because VBoxManageOutput error: Creating new VM... ERROR: Preparation failed: VBoxManageOutput error: vboxmanage.exe: error: Locking of attached media failed. A possible reason is that one of the media is attached to a running VM vboxmanage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component SessionMachine, interface IMachine Will be retried in 3s ... Using VirtualBox version 7.0.14r161095 executor... Restoring VM from snapshot... Previous VM failed. Deleting, because VBoxManageOutput error: Creating new VM... ERROR: Preparation failed: VBoxManageOutput error: vboxmanage.exe: error: Locking of attached media failed. A possible reason is that one of the media is attached to a running VM vboxmanage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component SessionMachine, interface IMachine Will be retried in 3s ... ERROR: Job failed (system failure): VBoxManageOutput error: vboxmanage.exe: error: Locking of attached media failed. A possible reason is that one of the media is attached to a running VM vboxmanage.exe: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component SessionMachine, interface IMachine
已尝试的配置
配置1:单个Runner允许2个并发作业
concurrent = 2 check_interval = 0 shutdown_timeout = 0 [session_server] session_timeout = 1800 [[runners]] name = "Runner_1" url = "myurl" id = 1 token = "mytoken" token_obtained_at = 2024-04-19T10:36:11Z token_expires_at = 0001-01-01T00:00:00Z executor = "virtualbox" shell = "powershell" [runners.cache] MaxUploadedArchiveSize = 0 [runners.ssh] user = "gitlab-user" identity_file = "C:\\Users\\myuser\\.ssh\\id_ed25519" disable_strict_host_key_checking = true [runners.virtualbox] base_name = "Win10_prepare" base_folder = "" disable_snapshots = false start_type = ""
配置2:两个Runner各允许2个并发作业
concurrent = 2 check_interval = 0 shutdown_timeout = 0 [session_server] session_timeout = 1800 [[runners]] name = "Runner_1" url = "myurl" id = 1 token = "mytoken" token_obtained_at = 2024-04-19T10:36:11Z token_expires_at = 0001-01-01T00:00:00Z executor = "virtualbox" shell = "powershell" [runners.cache] MaxUploadedArchiveSize = 0 [runners.ssh] user = "gitlab-user" identity_file = "C:\\Users\\myuser\\.ssh\\id_ed25519" disable_strict_host_key_checking = true [runners.virtualbox] base_name = "Win10_prepare" base_folder = "" disable_snapshots = false start_type = "" [[runners]] name = "Runner_2" url = "myurl" id = 2 token = "mytoken" token_obtained_at = 2024-04-19T10:36:11Z token_expires_at = 0001-01-01T00:00:00Z executor = "virtualbox" shell = "powershell" [runners.cache] MaxUploadedArchiveSize = 0 [runners.ssh] user = "gitlab-user" identity_file = "C:\\Users\\myuser\\.ssh\\id_ed25519" disable_strict_host_key_checking = true [runners.virtualbox] base_name = "Win10_prepare" base_folder = "" disable_snapshots = false start_type = ""
两种配置均出现相同问题。
解决方案
1. 强制创建完整克隆而非链接克隆
GitLab Runner默认使用VirtualBox的链接克隆,多个并发VM会共享同一基础磁盘文件,导致锁定冲突。修改Runner配置,添加clone_type = "full"参数:
[runners.virtualbox] base_name = "Win10_prepare" base_folder = "" disable_snapshots = false start_type = "" clone_type = "full"
完整克隆会生成独立的磁盘文件,避免共享锁定问题,但会占用更多磁盘空间。
2. 检查基础镜像的磁盘设置
- 打开VirtualBox管理器,选中
Win10_prepare镜像,进入设置→存储 - 右键虚拟磁盘→属性,确保磁盘未被标记为“不可共享”,或直接设置为可共享
- 停止所有该镜像的运行实例,重新创建干净的快照,确保基础镜像处于完全关闭状态
3. 验证Runner权限与宿主机资源
- 确保GitLab Runner服务运行的用户拥有VirtualBox安装目录及虚拟磁盘文件目录的读写权限
- 宿主机需分配足够的CPU核心、内存资源给每个VM,资源不足可能触发磁盘锁定异常
4. 禁用快照(可选)
如果不需要基于快照恢复,可以尝试禁用快照功能,直接从基础镜像克隆:
[runners.virtualbox] disable_snapshots = true
内容的提问来源于stack exchange,提问作者Roli Mayer
相关产品推荐
相关产品推荐

