Sometimes its handy to be able to unpackage and rebuild an RPM to remove specific files. In this example I'll rebuild the RPM for Datastax Opscenter's agent without the init files. The tools used to do this are rpm2cpio and FPM. I manage services with daemontools so having stuff start under init is a real issue. I could just install from tarbal but I prefer to keep everything under rpm for simplicity. Plus there is a lot of useful stuff that the rpm does on install that I don't want to reproduce manually.

Just as a fair warning, this isn't a perfect process and is quite hacky. It won't let you rebuild from source or change the source of compiled code, go find the source rpm for that. This just works for removing files and even that's not quite guaranteed. Test your work before you push to prod! haha.

So first we need to unpack our rpm. For this we can use rpm2cpio.

mkdir datastax-agent-5.0.1
cd !$
rpm2cpio ../datastax-agent-5.0.1-1.noarch.rpm | cpio -idmv

This should install our rpm's files into our current directory. After this go ahead and make any adjustments you want, I'm removing ./etc/init.d (note when removing files don't leave empty directories, you don't want your rpm to own /etc/init.d/). Next you'll need to pull out all the install scripts used in the original rpm: rpm -qp --scripts datastax-agent-5.0.1-1.noarch.rpm. Copy your scripts out to your text editor and go through them, break them into their separate sections for %pre, %post, etc (separated by lines that look like preinstall scriptlet (using /bin/sh):) and ensure they aren't using the files you removed. In my case I removed references to the services and chkconfig.

Next rebuild your rpm with FPM.

gem install fpm
fpm -s dir -t rpm --url http://www.datastax.com --description "Datastax-agent sans init file" -m "rbirnie@gmail.com" --rpm-user opscenter-agent --rpm-group opscenter-agent -n mommas-datastax-agent -e -v 5.0.1 --iteration 1 --prefix=/ .

When I rebuild them I usually try to keep versioning the same but rename the rpm to something unique so others know I mucked with it. If you are going to keep the same name, I'd recommend adding an epoch (if it doesn't already have one). This would make it so your custom rpm will appear newer than the default ones but still let you keep their same versioning. Once FPM opens up your new spec file go ahead and copy in your updated scripts from the last step. Save and close the file and FPM should build you a shiny new RPM.

And that's it!