> For the complete documentation index, see [llms.txt](https://docs.verge.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.verge.io/knowledge-base/zh/virtual-machines/import-rhel-centos-vm.md).

# 如何导入 Linux 虚拟机（RHEL、CentOS、SUSE、Debian、Ubuntu）

## 概述

{% hint style="info" %}
**要点**

* Linux 发行版在安装期间只会为检测到的硬件安装驱动程序。
* 由于缺少适用于 VergeOS 硬件的 virtio 驱动程序，导入的 VM 可能无法启动。
* 您可以通过调整 VM 配置并重新生成 initramfs 来解决启动问题。
* 本指南涵盖 RHEL、CentOS、Fedora、SUSE、openSUSE、Debian 和 Ubuntu。
  {% endhint %}

本指南说明如何将其他虚拟机管理程序中的 Linux 虚拟机导入到 VergeOS 中。它解决了迁移后 VM 无法启动或缺少网络连接等潜在问题，并提供了用于重新生成 initramfs 的特定发行版说明。

## 前提条件

* 可访问 VergeOS 和 VergeOS UI。
* 熟悉虚拟机管理程序环境和 VM 配置。
* 导入的 VM 文件必须位于 VergeOS 环境中。

## 步骤

### 1. 更新 VM 硬件配置

1. **将所有硬盘更改为 `virtio-scsi`**:
   * 在 VergeOS UI 中，进入该 VM 的设置。
   * 对于每个硬盘，将接口更改为 `virtio-scsi` 以获得最佳性能和兼容性。
2. **将所有 NIC 更改为 `virtio`**:
   * 确保所有网络接口卡（NIC）都设置为 `virtio` 以增强网络支持。
3. **调整启动顺序**:
   * 确保操作系统磁盘列为 **ID 0** 在启动顺序中。

### 2. 启动进入救援模式

1. **启动 VM**:
   * 打开 VM 电源，并在启动过程中按住 **左 Shift** 键以进入 GRUB 启动菜单。
2. **选择救援模式**:
   * 在 GRUB 菜单中，选择救援模式以启动到最小恢复环境。

{% hint style="success" %}
**SUSE/openSUSE 替代方案**

如果您无法从 GRUB 启动到救援模式（SLES15 和 openSUSE Leap 15 上常见），请将安装 ISO 挂载到 VM 并从其启动。该 ISO 会提供“Rescue System”选项。
{% endhint %}

### 3. 挂载根文件系统并进入 chroot

{% hint style="success" %}
**RHEL 9 及以上版本**

默认情况下，RHEL 9 系列的 VM（例如 RHEL、AlmaLinux 9、Rocky Linux 9、CentOS Stream 9）使用一个 LVM 配置文件，根据硬件 ID 显式将设备列入白名单。如果导入 VM 时未保留磁盘硬件 ID/序列号，请运行 `pvscan --cache` 以及 `vgchange -ay` 以强制 LVM 重新扫描所有块设备。请参阅知识库文章： [RHEL 9 系列虚拟机在克隆或快照还原后无法启动](/knowledge-base/zh/backup-dr/rhel9-boot-failure-clone-snapshot-restore.md) 以获取更多信息。
{% endhint %}

进入救援模式后，以 root 身份登录并挂载根文件系统。

1. **查找根分区**:

   如果您不知道哪个分区包含根文件系统，请列出所有可用分区：

   ```bash
   cat /proc/partitions
   ```

   对于使用 LVM 的系统，列出所有逻辑卷：

   ```bash
   lvdisplay
   ```
2. **挂载根分区**:

   将根分区或逻辑卷挂载到 `/mnt`:

   ```bash
   mount /dev/<device_name> /mnt
   ```

   将 `<device_name>` 并将 \<device\_name> 替换为您的根分区（例如， `sda2`, `mapper/vg0-root`).
3. **验证挂载**:

   通过列出其内容，检查您是否挂载了正确的文件系统：

   ```bash
   ls /mnt
   ```

   您应该会看到如下目录： `/root`, `/boot`, `/home`, `/etc`，并 `/var`.
4. **绑定虚拟文件系统**:

   使用以下 for 循环来绑定所需的虚拟文件系统：

   ```bash
   for i in proc sys dev run; do mount --rbind /$i /mnt/$i; done
   ```

   或者，分别挂载它们：

   ```bash
   mount --rbind /proc /mnt/proc
   mount --rbind /sys /mnt/sys
   mount --rbind /dev /mnt/dev
   mount --rbind /run /mnt/run
   ```

   对于 EFI/UEFI 系统，还要挂载 EFI 分区：

   ```bash
   mount /dev/sdX1 /mnt/boot/efi
   ```

   请将 /dev/sdX1 替换为你的实际 EFI 分区（使用 lsblk 或 fdisk -l 检查）。
5. **chroot 进入已挂载的文件系统**:

   ```bash
   chroot /mnt
   ```
6. **挂载其余文件系统**:

   进入 chroot 后，挂载 fstab 中定义的任何其他分区：

   ```bash
   mount -a
   ```

### 4. 重建 initramfs

进入系统的 chroot 环境后，使用所需的 virtio 驱动程序重新生成 initramfs。

#### RHEL / CentOS / Fedora / SUSE（使用 dracut）

运行以下命令以重新生成 initramfs：

```bash
dracut -f --regenerate-all --add-drivers "virtio_blk virtio_net virtio_pci"
```

这会添加以下驱动程序： `virtio_blk` （块设备）， `virtio_net` （网络设备）和 `virtio_pci` （PCI 总线）到 initramfs。

#### Debian / Ubuntu（使用 update-initramfs）

如果 `dracut` 不可用（Debian 10 和 Ubuntu 上常见），请使用 `update-initramfs` 代替：

1. 将 virtio 模块添加到 initramfs 配置中：

   ```bash
   cat >> /etc/initramfs-tools/modules << EOF
   virtio_pci
   virtio_blk
   virtio_net
   EOF
   ```
2. 重新生成 initramfs：

   ```bash
   update-initramfs -u
   ```

### 5. 重启并验证

1. **退出 chroot 环境**:

   ```bash
   exit
   ```
2. **重启 VM**:

   ```bash
   reboot
   ```
3. **验证启动和网络连接**:
   * 确认 VM 能成功启动，并且通过以下设备网络连接正常： `virtio` NIC。

## 故障排除

{% hint style="warning" %}
**常见问题**

* **VM 无法启动**:
  * **解决方案**：请再次检查 VM 设置中的启动顺序。操作系统磁盘必须设置为 **ID 0**.
* **没有网络连接**:
  * **解决方案**：确保 NIC 设置为 `virtio` 并且 initramfs 已使用相应的网络驱动程序重新构建。
    {% endhint %}

## 其他资源

* [迁移 VM](/run-the-platform/virtual-machines/vm-migration-overview.md)
* [虚拟机最佳实践](/run-the-platform/virtual-machines/vm-best-practices.md)
* [Dracut 维基](https://github.com/dracutdevs/dracut/wiki/)

## 反馈

{% hint style="info" %}
**需要帮助吗？**

如果您在导入 VM 时有任何问题或遇到任何故障，请联系我们的支持团队寻求帮助。
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.verge.io/knowledge-base/zh/virtual-machines/import-rhel-centos-vm.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
