Chris Feng
Chris Feng

Reputation: 199

How to keep config after reinstall ipk on OpenWrt

In makefile, I use INSTALL_DATA to copy config file to /etc/config. And config file will be changed during running.

I found that, after re-install application, the config file will restore to default one packed in ipk.

I want to know how to keep config file after re-install. Anyone can help me?

Makefile:

define Package/zm_control/install
    $(INSTALL_DIR) $(1)/etc/config
    $(INSTALL_DATA) ./config/$(PKG_NAME).json $(1)/etc/config/$(PKG_NAME).json
endef

Upvotes: 2

Views: 1270

Answers (2)

nemesifier
nemesifier

Reputation: 8539

As far as I know, there's a simpler solution, just add the config files used by your package to a "conffiles" section in your Makefile:

define Package/zm_control/conffiles
/etc/config/config_file1
/etc/config/config_file2
# ecc...
endef

Eg: openwisp-config Makefile

Upvotes: 2

Vasily G
Vasily G

Reputation: 939

Your Package/zm_control/install target gets executed during package building process, i.e. on your host machine, not on the OpenWrt device. It copies the config file to staging dir that will be embedded into the firmware image file and the .ipk file.

The configs in /etc/config/ folder are preserved automatically when you execute sysupgrade without -n flag. So, if you re-flash the device with a newly generated image, your config will not be lost.

However, if you want to install a new version of your package using opkg install command, you need to define your custom preinst and postinst targets in the Makefile. Like this:

define Package/$(PKG_NAME)/preinst
#!/bin/sh
# check if we are on real system
if [ -z "$${IPKG_INSTROOT}" ]; then
        #Backup config file
        cp /etc/config/$(PKG_NAME).json /tmp/$(PKG_NAME).json.bak
fi
exit 0
endef

define Package/$(PKG_NAME)/postinst
#!/bin/sh
if [ -z "$${IPKG_INSTROOT}" ]; then
        #Restore config file
        mv /tmp/$(PKG_NAME).json.bak /etc/config/$(PKG_NAME).json
fi
exit 0
endef

Upvotes: 1

Related Questions