podman 存储位置迁移

发布时间 2023-08-28 21:54:59作者: 清风6661

使用podman容器时,podman会默认使用/var/lib/containers路径作为存储路径,可能会导致根磁盘空间占用过大。迁移过程

一、关闭所有容器及容器服务

注意在修改前需要关闭所有容器,可以使用Cockpit系统在Web中关闭容器及以下几个服务:

  • podman
  • podman-restart
  • podman-auto-update
  • containerd
  • 建议关闭所有snap.microk8s开头的服务

二、复制目录

关闭了容器及服务后,就可以把/var/lib/containers目录移动到有足够空间的地方了。

三、修改配置

podman会根据/etc/containers/storage.conf中的配置来改变podman的存储路径,但是这并不是绝对的,感觉像是BUG一样。

ubuntu系统中/etc/containers下并没有storage.conf文件,需要自己手动创建(如果有则修改)。

内容如下:

[storage]
driver = "overlay"
runroot = "/run/containers/storage"
graphroot = "/var/lib/containers/storage"

上面的配置是podman的默认配置,其中,driver是驱动类型,默认为overlay,需要填写,但是最好不要修改,因为原来的驱动类型就是overlay; graphroot就是podman的存储路径,runroot是运行路径,这些都可以改为自定义的路径。这里主要是修改 graphroot路径。

修改完成后,可以使用sudo podman info命令来查看是否生效:

四、修改数据库

podman在数据库中可能会存储有运行配置,默认是在/var/lib/containers/storage/libpod/bolt_state.db中的,所以如果该数据库中存储了运行配置,则只修改了/etc/containers/storage.conf中的配置是不起作用的,podman会优先使用数据库中存储的配置,这点很坑。所以如果bolt数据库存储了相关配置,那么无论怎么修改/etc/containers/storage.conf中的配置都会被重载而不起作用。
bolt_state.db又是一种小众的数据库,格式为bolt,不能直接查看和修改,需要使用专门的bolt数据库工具才能查看和修改。

https://github.com/evnix/boltdbweb   boltdbweb地址 

go install github.com/evnix/boltdbweb
boltdbweb --db-name=test.db(db文件路径) --port=8008

 再使用浏览器打开地址   查找buckets  名称为:runtime-config的配置,修改graph rootstatic dirvolume path配置,也可以直接删除掉,让podman直接使用/etc/containers/storage.conf中的配置。

/var/lib/containers/storage/libpod/bolt_state.db 数据库文件非常重要,请注意!