banner
Aki

Aki

太阳能维修,月亮可更换,星星不闪包退换。
twitter

树莓派部署本地apt源

前言#

树莓派正式加入内网环境,准备部署 apt 源,为内网环境的其他 debian 类设备或虚拟机提供源仓库服务,比如 proxmox。如果想模拟企业数据中心环境,要将内外网完全隔离开来,这么做是必要的。

如何理解本地源#

本地源指的是软件包的源
本地源设备为内网环境下无法联网的设备提供源服务,为什么他们不能联网?在数据中心或者其他内网环境下,有些设备出于安全考虑是无法连接互联网的,不是不能,是不可以连,重点是在安全两个字。所以本地源就成了内网环境下设备更新软件包的唯一接入点。
因此需要从网上把源下载到存储本地源的设备。
例如,我在我的服务器上安装了 proxmox,这个虚拟机系统是基于 Debian 操作系统的,它又是在内网环境下,未来要作为内网服务的主力系统,虚拟多个设备提供不同的服务,但是他本身安装好以后存在各种软件缺失问题又无法联网进行安装,因此需要有本地源这么一个设备为他解决安装软件的需求。

如何部署本地源#

首先部署问题网上有教程可以参考,树莓派的系统是卸载 tf 卡上的,一般买的时候都是 32G。这点容量别说部署本地源了,随便塞几个系统镜像包上去就满了。
所以继续给树莓派加个大容量的背包,他才能承担起这项任务。所以在部署本地源之前,需要给树莓派挂载一个移动硬盘。正好手里有一个 512g 的固态,可以作为它的外挂磁盘用。

#查找磁盘
root@raspberrypi:/#fdisk -l
#结果省略前面部分
Disk identifier: 0x6aef5cb3

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   532479   524288  256M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      532480 62333951 61801472 29.5G 83 Linux


Disk /dev/sda: 476.94 GiB, 512110190592 bytes, 1000215216 sectors
Disk model: Generic
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt

Disk identifier: 8C903F36-23B9-44CB-99B8-D122B713E560Device     Start        End    Sectors   Size Type
/dev/sda1   2048 1000215182 1000213135 476.9G Linux filesystem

查找到一个 sda 的磁盘,曾经也用与 linux 的文件系统,先把数据清空掉,重新格式化一遍

root@raspberrypi:/# mkfs -t ext3 /dev/sda
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 125026902 4k blocks and 31260672 inodes
Filesystem UUID: 2898dbe1-7892-4424-9fee-ff8b78b8b32e
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
        102400000

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

创建挂载目录并挂载磁盘

root@raspberrypi:/#mkdir /opt/sda1
root@raspberrypi:/#chmod 777 /opt/sda1
root@raspberrypi:/#mount /dev/sda /opt/sda1

查询磁盘的 ssid

root@raspberrypi:/# blkid
/dev/mmcblk0p1: LABEL_FATBOOT="boot" LABEL="boot" UUID="0F92-BECC" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="6aef5cb3-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="41c98998-6a08-4389-bf74-79c9efcf0739" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="6aef5cb3-02"
/dev/sda: UUID="2898dbe1-7892-4424-9fee-ff8b78b8b32e" BLOCK_SIZE="4096" TYPE="ext3"

设置开机挂载磁盘,毕竟万一宕机重启没挂载上去都不知道哪里出了问题,会很麻烦

root@raspberrypi:/etc# vim fstab
#在fstab最后一行添加下方内容,ssid为上一步查询到的sda信息
UUID=2898dbe1-7892-4424-9fee-ff8b78b8b32e /opt/sda1 ext3 defaults        0       0

磁盘挂载完成,开始部署 apt 本地源
综合来看,搭建本地源通常有两种方式
1、按需拉取,内网设备需要什么包通过远程拷贝的方式安装,参考资料种的 1、2、3 都是这种方法。
2、直接部署本地仓库,拉取绝大部分软件包,参考资料的 4 就是这种方法
既然都准备好大容量的硬盘了,肯定是考虑后者。
又因为目前主要为基于 debian 系统的 proxmox 做服务的,所以需要在部署过程种替换源目标

root@raspberrypi:/# apt install apt-mirror
root@raspberrypi:/# cd /etc/apt
root@raspberrypi:/etc/apt# vim mirror.list
#对mirror.list文件进行修改
#修改完之后执行apt-mirror
root@raspberrypi:/# apt-mirror

文件中的内容如下,原有官方美国的源改为中国源

############# config ##################
#
set base_path    /opt/sda1/pve_mirror
#
#set mirror_path   $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############

deb http://ftp.cn.debian.org/debian unstable main contrib non-free
deb-src http://ftp.cn.debian.org/debian unstable main contrib non-free

# mirror additional architectures
#deb-alpha http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-amd64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-armel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-hppa http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-i386 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-ia64 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-m68k http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mips http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-mipsel http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-powerpc http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-s390 http://ftp.us.debian.org/debian unstable main contrib non-free
#deb-sparc http://ftp.us.debian.org/debian unstable main contrib non-free

clean http://ftp.us.debian.org/debian

在执行 apt-mirror 后会等待非常久的时间,第一次以为失败了,ctrl+C 终止掉了。再次执行发现原本需要 102G 的变成了 92G,说明他虽然慢,但确实是在下载的

花了 1 天时间终于是下载完成,之后需要部署 apach 代理,供内网设备进行访问

root@raspberrypi:/#apt-get install apache2
root@raspberrypi:/#ln -s /opt/sda1/pve_mirror/mirror/ftp.cn.debian.org/debian /var/www/html/proxmox

软连接创建完成后,就可以通过浏览器访问到这台树莓派上的软件源。访问地址为http://ip/pve

当然也有其他方法如果配置 apache2 或者 nginx 不行还有别的方法。
安装 nodejs,通过使用开源文件服务系统进行搭建链接也是可行的,例如 github 上的hope-server

root@raspberrypi:/#apt install nodejs
root@raspberrypi:/#apt install npm
root@raspberrypi:/#apt install yarn
root@raspberrypi:/#yarn global install hope-server
root@raspberrypi:/#hope-server -d <filepath> -p 8080 -o localhost
#filepath为软件源目录,可以是磁盘挂载路径,也可以是磁盘挂载后的软链接路径
#但是这种方式有一定的缺陷,开启hope-server的时候回占用控制台,所以可以采取nohup+crontab的方式
root@raspberrypi:~# nohup hope-server -d /var/www/proxmox/ -p 8080 -o localhost &
[3] 6843
---------------------------------------------------
root@raspberrypi:~# crontab -e
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2
#在编辑器最后一行加入
@reboot nohup hope-server -d /var/www/proxmox/ -p 8080 -o localhost &
#这样每次重启都会执行一遍

如何修改 pve 源#

部署好本地源后,接下来在客户端,也就是内网里的 pve 系统下修改源地址,路径为 /etc/apt/sources.list

root@proxmox:/#nano /etc/apt/sources.list
#在编辑器中加入类似下方的源地址
deb [trusted=yes] http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian bullseye main contrib

deb [trusted=yes] http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian bullseye-updates main contrib

deb [trusted=yes] http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian bullseye-backports main contrib

deb [trusted=yes] http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian bullseye-security main contrib

虽然到这里就已经算成功了。但是还是毫不意外地报错了😅

Reading package lists... Done
E: Release file for http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian/dists/bullseye/InRelease is not valid yet (invalid for another 33d 17h 53min 33s). Updates for this repository will not be applied.
E: Release file for http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian/dists/bullseye-updates/InRelease is not valid yet (invalid for another 130d 21h 52min 40s). Updates for this repository will not be applied.
E: Release file for http://172.20.0.2:80/proxmox/mirror/mirrors.tuna.tsinghua.edu.cn/debian/dists/bullseye-backports/InRelease is not valid yet (invalid for another 130d 21h 52min 40s). Updates for this repository will not be applied.

大致是时间上的问题,估计安装 pve 系统的时候选的时区不对?先看一下当前 pve 系统的时间吧

root@proxmox:~# date
Mon 08 Aug 2022 12:29:02 AM CST

修改

root@proxmox:/etc# vim /etc/systemd/timesyncd.conf
#设置ntp地址为软路由
root@proxmox:/etc# systemctl restart systemd-timesyncd.service
root@proxmox:/etc# systemctl enable systemd-timesyncd.service
root@proxmox:/etc# date
Sat 17 Dec 2022 07:21:07 AM CST

最后可以正常 apt 更新了

参考资料#

1、Apt 本地源 / 仓库
2、创建 APT 本地源
3、# apt 本地源离线安装
4、# Ubuntu 搭建离线 APT 仓库
5、# 利用 apt-mirror 制作 Ubuntu 本地源
6、# 使用 APT-mirror 四步配置 Ubuntu 本地软件仓库
7、# pve 更新源(Proxmox VE (PVE) 系统修改设置为国内源教程)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.