2

I build a debian package with cmake/cpack. I want the deb execute a bash script after I install the deb. I install the deb use sudo dpkg -i my.deb . If I install the deb like that, the installed program's user and group is root. so I want the deb auto-execute a bash script to modify the user and group using the current logged user and group.

I search the infomations about this with Google.I find

SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, ./script/postinst)

can do this. So I do like that, but there is no use with it.

I do like this:

  install(PROGRAMS ${CMAKE_SOURCE_DIR}/script/postinst DESTINATION ./script)

  set(CPACK_GENERATOR "DEB")

  set(CPACK_PACKAGE_NAME "mydeb")

  set(CPACK_PACKAGE_VERSION "1.0")

  set(CPACK_DEBIAN_PACKAGE_MAINTAINER "George")

  set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, ./script/postinst)

  set(CPACK_PACKAGING_INSTALL_PREFIX "usr")

  include(CPACK)

the postinst is a bash script to modify the installed program's user and group with the current logged user and group

If some thing is wrong ? I need help

Saeed Masoomi
  • 1,703
  • 1
  • 19
  • 33
George
  • 41
  • 1
  • 5

3 Answers3

2

I do this in the same way. In my case I use the set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, ./script/postinst) to set a systemd service to run at boot.

And similarly I use set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, ./script/prerm) to cleanly remove the service from the boot sequence before actually removing the Debian package.

An important note here is that you do NOT need to install these files seperately. set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA, ./script/postinst) is sufficient, hence install(DIRECTORY ${CMAKE_SOURCE_DIR}/scripts/ DESTINATION ${SCRIPT_DESINATION} is not required. Do make sure the permissions of these additional scripts is set correctly.

Bruno Hendrickx
  • 354
  • 2
  • 14
0

In general, the postinst script shouldn't modify anything directly related to one user, but should complete the installation for the system as a whole. For example, if you need to generate configuration files based on hardware present on the system, you might use postinst scripts. I don't know what your user script could be doing to suggest a possible alternative.

saiarcot895
  • 554
  • 5
  • 16
  • 1
    so,if I install my deb like this:sudo dpkg -i my.deb, the program's user and group is root.I don't want to modify the program's user and group manual after installed.I want the deb to do this.I find the postinst is execute after the deb installed .so I just want the postinst script to modify the user and group with chown – George Feb 19 '14 at 02:46
  • Modify what, and why `chown`? In general, anything a deb installs shouldn't belong to the user; it should belong to root. – saiarcot895 Feb 19 '14 at 02:47
  • but,if it belong to root, it only can run with root.I want the current logged user to run it.I want the deb installed, the program binaries's user is the current logged user. – George Feb 19 '14 at 02:54
  • Ownership and "runnability" are two different things. For example, in Terminal, run `stat /usr/bin/wget` (the tool to make HTTP/FTP requests). Note that the binary is owned by root/root (the user and group). However, you are able to run it. This is determined by the access restrictions, which should be 0755. This means that the owning user can read, write, and run it (the '7'), the owning group can read and run it (the first '5'), and everyone else can read and run it (the second '5') (see also [chmod wiki](https://en.wikipedia.org/wiki/Chmod). – saiarcot895 Feb 19 '14 at 03:00
  • Every package that you download from the repos (`apt-get`, the Software Center, whichever method you use, which is effectively the same) will have the binaries and libraries in that package belong to root, and will be either 0644 (for libraries and other files that aren't executables) or 0755 (for directories (which have to be 0755) and executables). – saiarcot895 Feb 19 '14 at 03:06
  • thanks,I understand you.I know 0755.if I do like this, the program is not perfect.Have you ever installed mysql database.I see mysql's ownership is the current logged user and group.I just want my program can do like this. – George Feb 19 '14 at 03:10
  • Interesting. Now that I think about it, it might be reading the list of users file (which I don't have the location of off the top of my head) and adding any users present in the file as allowed users of the database. – saiarcot895 Feb 19 '14 at 03:15
0

I solved this with

# generate the shell script from template.                            #
CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/contrib/postinst.in" "postinst" @ONLY IMMEDIATE)
# and 
# hook up the event postinst to run after install                       #
SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_BINARY_DIR}/postinst;")

I did not really need to use a template i.e. a .in file but if I ever need something more complex I have the possibility of @varname@ where varname is a cmake variable for now my postinst.in is

#!/usr/bin/env sh
/usr/sbin/ldconfig
# vim: set filetype=sh

basically a shell script there are also postrm and prerm also shell scripts see https://cmake.org/cmake/help/latest/cpack_gen/deb.html but the docs for cmake and cpack are very poor. not sure when postrm and prerm happen but postinst is run after install so perfect time to run ldconfig after installing my .so files