178 lines
5.7 KiB
Markdown
178 lines
5.7 KiB
Markdown
|
### 1. 下载openwrt镜像
|
|||
|
|
|||
|
- https://openwrt.org/downloads
|
|||
|
|
|||
|
- 选用中国区地址
|
|||
|
|
|||
|
<img src="https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251652326.png" alt="image-20240525165203213" style="zoom:50%;" />
|
|||
|
|
|||
|
- 选设备类型
|
|||
|
|
|||
|
![image-20240525165319112](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251653170.png)
|
|||
|
|
|||
|
- 选用rootfs.tar.gz,可以直接用于pve的lxc模板
|
|||
|
|
|||
|
![image-20240525165400390](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251654443.png)
|
|||
|
|
|||
|
### 2. 上传lxc模板到pve
|
|||
|
|
|||
|
#### 2.1 如果直接下载的是rootfs.tar.gz文件直接上传即可
|
|||
|
|
|||
|
![image-20240525165516671](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251655726.png)
|
|||
|
|
|||
|
#### 2.2 如果下载的为带rootfs的img.gz文件,则需要解压img文件
|
|||
|
|
|||
|
- 通过 ssh 工具将文件上传到pve服务器上
|
|||
|
|
|||
|
- 安装img文件解压工具
|
|||
|
|
|||
|
```bash
|
|||
|
apt install squashfs-tools
|
|||
|
```
|
|||
|
|
|||
|
- 解压 img文件,需要在img文件存储路径操作
|
|||
|
|
|||
|
![image-20240525173410855](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251734920.png)
|
|||
|
|
|||
|
```bash
|
|||
|
unsquashfs openwrt-23.05.2-x86-legacy-generic-squashfs-rootfs.img
|
|||
|
```
|
|||
|
|
|||
|
- 解压成功后会在当前目录生成`squashfs-root`文件夹
|
|||
|
|
|||
|
![image-20240525173526262](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251735315.png)
|
|||
|
|
|||
|
- 进入生成的文件夹内,打包解压出的内容到pve的lxc模板存储路径内
|
|||
|
|
|||
|
```bash
|
|||
|
tar zcf /var/lib/vz/template/cache/OpenWRT-x86-64-generic-rootfs.tar.gz ./*
|
|||
|
```
|
|||
|
|
|||
|
也可以将`/var/lib/vz/template/cache/`替换为其他的pve存储目录对应的路径,一般在`/mnt/pve`下边
|
|||
|
|
|||
|
#### 2.3 如果下载的文件为img.gz文件,则需要挂载打包处理
|
|||
|
|
|||
|
- 开启 nbd模块
|
|||
|
|
|||
|
```bash
|
|||
|
modprobe nbd
|
|||
|
```
|
|||
|
|
|||
|
- 挂载img文件到设备
|
|||
|
|
|||
|
```bash
|
|||
|
qemu-nbd -c /dev/nbd0 -f raw OpenWRT-x86-64-generic-squashfs-combined.img
|
|||
|
```
|
|||
|
|
|||
|
- 查看挂载的分区
|
|||
|
|
|||
|
```bash
|
|||
|
lsblk -f /dev/nbd0
|
|||
|
```
|
|||
|
|
|||
|
![image-20240525182033136](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251820206.png)
|
|||
|
|
|||
|
- 挂载带有squashfs的文件
|
|||
|
|
|||
|
```bash
|
|||
|
mkdir /mnt/openwrt
|
|||
|
mount /dev/nbd0p2 /mnt/openwrt
|
|||
|
```
|
|||
|
|
|||
|
- 进入挂载的openwrt目录,打包lxc模板
|
|||
|
|
|||
|
```bash
|
|||
|
cd /mnt/openwrt
|
|||
|
tar -czvf /var/lib/vz/template/cache/istoreos.tar.gz *
|
|||
|
```
|
|||
|
|
|||
|
- 模板打包完成后卸载挂载的文件与nbd设备
|
|||
|
|
|||
|
```bash
|
|||
|
cd .. # 移动到openwrt文件夹的上一层
|
|||
|
umount /mnt/openwrt
|
|||
|
qemu-nbd -d /dev/nbd0
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### 3. 创建pve lxc容器
|
|||
|
|
|||
|
```bash
|
|||
|
pct create 102 ssd1t:vztmpl/OpenWRT-x86-64-generic-rootfs.tar.gz --rootfs ssd1t:32 --ostype unmanaged --hostname openwrt --arch amd64 --cores 4 --memory 1024 --swap 0 -net0 bridge=vmbr0,name=eth0
|
|||
|
```
|
|||
|
|
|||
|
- `pct create` 创建命令
|
|||
|
|
|||
|
- `102` pve容器序列号,不要重复
|
|||
|
|
|||
|
- `ssd1t:vztmpl/OpenWRT-x86-64-generic-rootfs.tar.gz`:模板路径,前边ssd1t为pve存储的ID,一定要选中数据中心,然后再点右侧的存储
|
|||
|
|
|||
|
![image-20240525165842480](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251658517.png)
|
|||
|
|
|||
|
- `--rootfs`:指定模板未rootfs文件
|
|||
|
|
|||
|
- `ssd1t:32`:存储路径,ssd1t依然为存储的ID,代表将openwrt的虚拟磁盘存储在ssd1t的硬盘下,32为虚拟磁盘容量大小
|
|||
|
|
|||
|
- `--ostype unmanaged`:指定操作系统类型,暂时只能写`unmanaged`
|
|||
|
|
|||
|
- `--hostname openwrt`:主机名,随便取一个,我用openwrt
|
|||
|
|
|||
|
- `--arch amd64`:虚拟机架构,我是x86的主机所以选择`amd64`,如果不确定可以在网页端创建一个lxc容器然后查看架构
|
|||
|
|
|||
|
![image-20240525170403423](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251704498.png)
|
|||
|
|
|||
|
- `--cores 4`:cpu核心数,随意分配
|
|||
|
|
|||
|
- `--memory 1024`:内存大小,单位`M`
|
|||
|
|
|||
|
- `--swap 0`:swap分区大小,如果不需要设置为0
|
|||
|
|
|||
|
- `-net0 bridge=vmbr0,name=eth0`:网络参数,桥接pve的网卡,并将网卡名字设置为`eth0`,在openwrt里可以直接用
|
|||
|
|
|||
|
### 5. 查看创建的openwrt容器
|
|||
|
|
|||
|
- 概要
|
|||
|
|
|||
|
![image-20240525170740114](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251707164.png)
|
|||
|
|
|||
|
- 资源分配,主要为内存和硬盘和CPU
|
|||
|
|
|||
|
![image-20240525170804348](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251708411.png)
|
|||
|
|
|||
|
- 网络,因为用命令行创建时没有为容器分配ip地址,所以可以用web管理页面,给容器分配ip,我已经分配好了
|
|||
|
|
|||
|
![](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/image-20240525170827336.png)
|
|||
|
|
|||
|
- 容器选项,基本是一些附加功能
|
|||
|
|
|||
|
![image-20240525170948198](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251709253.png)
|
|||
|
|
|||
|
### 6. 启动openwrt
|
|||
|
|
|||
|
- 控制台输出,按回车可以进入openwrt的终端窗口
|
|||
|
|
|||
|
![image-20240525171109336](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251711403.png)
|
|||
|
|
|||
|
- 可以在终端修改网卡配置![image-20240525171246977](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251712024.png)
|
|||
|
|
|||
|
- 修改网卡配置
|
|||
|
|
|||
|
- `vi /etc/config/network`
|
|||
|
- lan接口的ip地址修改为刚刚在pve管理页设置的ip
|
|||
|
|
|||
|
![image-20240525171346917](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251713972.png)
|
|||
|
|
|||
|
### 7. 登录openwrt
|
|||
|
|
|||
|
- 浏览器访问openwrt的lan接口的ip,首次进入root密码不需要数据,登录后会提示设置密码
|
|||
|
|
|||
|
![image-20240525171504464](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251715519.png)
|
|||
|
|
|||
|
- openwrt状态信息
|
|||
|
|
|||
|
![image-20240525171628857](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251716907.png)
|
|||
|
|
|||
|
- ![image-20240525171703528](https://blog-heysq-1255479807.cos.ap-beijing.myqcloud.com/halo2/202405251717626.png)
|