Skip to content

Instantly share code, notes, and snippets.

@kagurazakakotori
Created February 10, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagurazakakotori/3891261c5799e6f7eb20c52b3409052d to your computer and use it in GitHub Desktop.
Save kagurazakakotori/3891261c5799e6f7eb20c52b3409052d to your computer and use it in GitHub Desktop.
如何在Hyper-V第二代虚拟机下安装OpenWrt

如何在Hyper-V第二代虚拟机下安装OpenWrt

安装前的准备

在安装之前,我们需要准备以下内容:

  • 一台装有Linux发行版的Hyper-V虚拟机
  • 一个空的.vhdx虚拟磁盘(将被OpenWrt使用)
  • 一点Linux操作基础

本教程将以下环境为例

  • Ubuntu 18.04
  • OpenWrt 18.06.2
  • 最大容量为4GB的动态扩展.vhdx虚拟磁盘,此处命名为openwrt.vhdx

开始安装

首先,在Linux虚拟机中下载OpenWrt固件

$ wget https://downloads.openwrt.org/releases/18.06.2/targets/x86/64/openwrt-18.06.2-x86-64-generic-rootfs.tar.gz
$ wget https://downloads.openwrt.org/releases/18.06.2/targets/x86/64/openwrt-18.06.2-x86-64-vmlinuz

接着,连接openwrt.vhd至Linux虚拟机 (此处为/dev/sdb,可以使用dmesg命令查看其盘符)

使用parted初始化/dev/sdb并建立分区

警告:下列命令会擦除/dev/sdb内的所有数据!

# parted /dev/sdb
(parted) unit MiB
(parted) mklabel gpt
(parted) mkpart ESI fat32 1 129
(parted) mkpart primary ext4 129 4095
(parted) set 1 boot on
(parted) quit

然后,格式化新的分区

# mkfs.vfat -F 32 /dev/sdb1
# mkfs.ext4 /dev/sdb2

如果一切正常,那么运行parted /dev/sdb print将会有以下结果

# parted /dev/sdb print
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 4295MB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  135MB   134MB   fat32        ESI      boot, esp
 2      135MB   4294MB  4159MB  ext4         primary

现在,挂载这两个分区

# mkdir -p /tmp/sdb1
# mkdir -p /tmp/sdb2
# mount /dev/sdb1 /tmp/sdb1
# mount /dev/sdb2 /tmp/sdb2

安装systemd-boot/dev/sdb1

# bootctl --path=/tmp/sdb1 install

如果成功,将会是以下结果

# bootctl --path=/tmp/sdb1 install
Created "/tmp/sdb1/EFI".
Created "/tmp/sdb1/EFI/systemd".
Created "/tmp/sdb1/EFI/BOOT".
Created "/tmp/sdb1/loader".
Created "/tmp/sdb1/loader/entries".
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/tmp/sdb1/EFI/systemd/systemd-bootx64.efi".
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/tmp/sdb1/EFI/BOOT/BOOTX64.EFI".
Created EFI boot entry "Linux Boot Manager".

现在,编辑引导信息

# vi /tmp/sdb1/loader/loader.conf
# vi /tmp/sdb1/loader/entries/openwrt.conf

loader.conf

timeout 0       # Loader timeout
default openwrt    # Default boot option, here is openwrt

openwrt.conf

title OpenWrt     # Boot option title to be displayed
linux /vmlinuz    # Path to vmlinuz file
options ro root=/dev/sda2 rootfstype=ext4 rootwait console=tty0

接着,将OpenWrt写入/dev/sdb

# mv ./openwrt-18.06.2-x86-64-vmlinuz /tmp/sdb1/vmlinuz
# tar -zx -C /tmp/sdb2 -f openwrt-18.06.2-x86-64-generic-rootfs.tar.gz

你可以使用ls命令查看是否已经写入成功

最后,解除挂载

# umount /dev/sdb1
# umount /dev/sdb2
# eject /dev/sdb

并在虚拟机设置中删除openwrt.vhd

启动OpenWrt

  1. 新建一个第二代虚拟机
  2. 在“连接虚拟硬盘”时选择“使用现有虚拟硬盘”,并指向刚才的openwrt.vhd
  3. 在虚拟机设置中禁用安全启动
  4. 启动虚拟机

当屏幕中出现以下文字时,证明已经启动成功

Please press Enter to activate this console.

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment