Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Updating unmodified config files automatically
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Thu Aug 29, 2002 4:26 am    Post subject: Updating unmodified config files automatically Reply with quote

When I emerged the new baselayout and was presented with 24 config files to update, I became a little bit tired of updating files that I have not modified since the emerge. I wrote a little script that will find these files by checking md5sums from ebuilds and then updates them automatically or one-by-one.

You can run this before running etc-update to save yourself some work.

NB.
1. This needs gentoolkit to work
2. You need to do this before the old package that installed the config file is cleaned (either through an emerge clean or autoclean)
3. Please make sure that copy/cut-and-paste to the script text file does not remove spaces or modify line-ends.
Code:

#!/bin/bash

# This script checks config files in directories with config files and
# checks whether their checksum matches known checksums in /var/db.
# If a checksum matches, the file has not been modified since the emerge
# and can be overwritten safely.  These files are presented and queried
# for update individually or in one shot
#
# You can run this prior to running etc-update to save some work in updating
# files that you have not touched

strip_config()
{
    echo ${1//._cfg????_/}
}

# check for root
if [ $UID != 0 ]; then
    echo "Got root?"
    exit 1
fi

# check for gentoolkit
needed=gentoolkit
if [ ! -f /usr/bin/qpkg ]; then
    echo "You need to emerge $needed to run this"
    exit 2
fi

# get dir list from config
sourcefiles="/etc/make.globals /etc/profile"
for sourcefile in $sourcefiles; do
    source $sourcefile
    for dir in $CONFIG_PROTECT; do
        [ -d $dir ] && dirs="$dirs $dir"
    done
done

# check for config files with matching checksums
for nf in `find $dirs -iname '._cfg*'`; do
    of=`strip_config $nf`
    for eb in `qpkg -v -nc -f $of`; do
        if grep $of /var/db/pkg/$eb/CONTENTS |
           grep -v ^sym |
           sed 's/obj \([^ ]*\) \([^ ]*\) .*$/\2  \1/g' |
           md5sum -c --status; then
            can_update="$can_update $nf"
        fi
    done
done

# exit if nothing to do
[ -z "$can_update" ] && echo "No files found" && exit 0

# make sure files are listed only once in the update
can_update=`echo $can_update | sed 's/ /\n/g' | sort -u`

# blurb
can_update="`echo $can_update | sort -u`"
echo "The following files have not been modified by you:"
echo
echo `strip_config "$can_update"` | tr ' ' '\n'
echo
echo "You can update the files one by one by typing \"y/n\" or update"
echo "all the remaining by typing \"a\""

# update one by one or all
for nf in $can_update; do
    of=`strip_config $nf`
    [ -z "$all" ] && echo -n "Update ${of}? [Y|n|q|a] " && read ans
    case $ans in
    n|N)
        ;;
    q|Q)break
        ;;
    a|A)echo Moving $nf to $of
        mv -f $nf $of
        all=y
        ans=y
        ;;
    y|Y|*)echo Moving $nf to $of
        mv -f $nf $of
        ;;
    esac
done
exit 0

EDIT (rac): incorporated puddpunk's suggestion of checking UID against 0 instead of USER against root.
EDIT(Naan Yaar):03/12/04: Updated to include Yak's suggestion and previous fix for missing pkglist.


Last edited by Naan Yaar on Sat Mar 13, 2004 4:53 am; edited 5 times in total
Back to top
View user's profile Send private message
kirill
Apprentice
Apprentice


Joined: 01 Aug 2002
Posts: 183
Location: Finland

PostPosted: Thu Aug 29, 2002 8:32 pm    Post subject: Re: Updating unmodified config files automatically Reply with quote

Naan Yaar wrote:
When I emerged the new baselayout and was presented with 24 config files to update, I became a little bit tired of updating files that I have not modified since the emerge. I wrote a little script that will find these files by checking md5sums from ebuilds and then updates them automatically or one-by-one.

You can run this before running etc-update to save yourself some work.

NB.
1. This needs gentoolkit to work
2. Lightly tested. YMMV.


The script is not quite working here:

Code:
 * GNU info directory index is up-to-date.
 * IMPORTANT: 22 config files in /etc need updating.
 * Type emerge --help config to learn how to update config files.


Code:
# ./etc-force-update
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
md5sum: standard input: no properly formatted MD5 checksum lines found
No files found


exactly as many times as many new config files I have

I also ran this:
Code:
# qpkg -c -v baselayout
sys-apps/baselayout-1.8.2 *
/etc/devfsd.conf !md5! !mtime!
/etc/fstab !md5! !mtime!
/etc/group !md5! !mtime!
/etc/hosts !md5! !mtime!
/etc/inputrc !md5! !mtime!
/etc/profile !md5! !mtime!
/etc/rc.conf !md5! !mtime!
/etc/services !md5! !mtime!
/etc/conf.d/local.start !md5! !mtime!
/etc/conf.d/net !md5! !mtime!
/etc/init.d/bootmisc !md5! !mtime!
/etc/init.d/checkfs !md5! !mtime!
/etc/init.d/checkroot !md5! !mtime!
/etc/init.d/consolefont !md5! !mtime!
/etc/init.d/halt.sh !md5! !mtime!
/etc/init.d/keymaps !md5! !mtime!
/etc/init.d/modules !md5! !mtime!
/etc/init.d/net.eth0 !md5! !mtime!
/etc/init.d/net.ppp0 !md5! !mtime!
/etc/init.d/netmount !md5! !mtime!
/etc/skel/.bash_profile !md5! !mtime!
/etc/skel/.bashrc !md5! !mtime!
22/236


is that my configuration or the script which b0rks?:(
_________________
--kirill
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Thu Aug 29, 2002 8:42 pm    Post subject: Reply with quote

kirill,

I'll follow up with PM's and post the resolution to the forum since I don't want to clutter up the documentation thread.

[EDIT]PM exchanges determined that the cut-and-paste to a file changed the two spaces between \2 and \1 above into a single space, causing md5sum to fail. Adding warning to original post above regarding this.
Thanks, kirill[/EDIT]
Back to top
View user's profile Send private message
puddpunk
l33t
l33t


Joined: 20 Jul 2002
Posts: 681
Location: New Zealand

PostPosted: Wed Nov 20, 2002 6:28 am    Post subject: Reply with quote

The script doesn't work if I'm SU'd :\

I just changed the line:
Code:
if [ $USER != root ]; then

to
Code:
if [ $UID != 0 ]; then


and it worked fine.

Great script, thanks man :)
Back to top
View user's profile Send private message
ales
n00b
n00b


Joined: 20 Nov 2002
Posts: 19

PostPosted: Thu Nov 21, 2002 8:20 am    Post subject: Reply with quote

how exactly is this script suppose to be run?

" (either through an emerge clean or autoclean) " ?

maybe an example please...

(BTW.. i just finished the Stage2 and am about to do the stage 3)

plus if I copy and paste it... i get a bunch of these at the end of each line (viewing it in nano)..

Code:
^M


which didn't make the script work... had to delete each one of those.. for it to properly tell me that "gentoolkit is required"

but after emerging gentoolkit, (before going to stage 3) I now get the following:

Code:
 
: command not found
: command not found
: command not found
: command not found
: command not found
No files found


I'm assuming it probably has something to do with the fact that I'm still at stage 2 ?

this is probably baby sh*t to those who know what they're doing... however, i'm *just* starting out... so any help would be cherished :P


-ales
Back to top
View user's profile Send private message
puddpunk
l33t
l33t


Joined: 20 Jul 2002
Posts: 681
Location: New Zealand

PostPosted: Thu Nov 21, 2002 10:59 pm    Post subject: Reply with quote

yup, finish your install first! :)
Back to top
View user's profile Send private message
sebest
Apprentice
Apprentice


Joined: 03 Jul 2002
Posts: 163
Location: Paris - France

PostPosted: Sun Dec 29, 2002 12:09 am    Post subject: Reply with quote

Hi.

I tryed your script, but i think that the new default behavior of emerge, to clean after emerging brook it.

Because it can removes the old CONTENT file containing the md5 checksum of a current file in /etc. So when i run you script he thinks that i modified the file because there is no CONTENT file containing his checksum.
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Sun Dec 29, 2002 5:11 am    Post subject: Reply with quote

Autoclean is on by default now. You can change this behavior by putting in this line in your /etc/make.conf file:
Code:

AUTOCLEAN="no"


NB. Portage seems to strongly recommend not doing this.
sebest wrote:
Hi.

I tryed your script, but i think that the new default behavior of emerge, to clean after emerging brook it.

Because it can removes the old CONTENT file containing the md5 checksum of a current file in /etc. So when i run you script he thinks that i modified the file because there is no CONTENT file containing his checksum.
Back to top
View user's profile Send private message
Yama
Tux's lil' helper
Tux's lil' helper


Joined: 21 Jul 2002
Posts: 135
Location: Sydney, Australia

PostPosted: Tue Feb 10, 2004 2:26 am    Post subject: Reply with quote

This script served me flawlessly for quite some time (> 1 year), but now when I run it I receive the following error:
Code:
/usr/local/sbin/etc-autoupdate: line 29: /usr/lib/portage/bin/pkglist: No such file or directory
You need to emerge gentoolkit to run this
This started happening after upgrading my gentoolkit to the latest stable version (0.2.0_pre6). It seems that this version does not have the /usr/lib/portage/bin/pkglist file.
Back to top
View user's profile Send private message
nempo
Guru
Guru


Joined: 16 Apr 2002
Posts: 360
Location: Linkoping, Sweden

PostPosted: Tue Feb 10, 2004 8:45 am    Post subject: Reply with quote

This is a feature that should be in portage IMO. Maybe you should try to commit it ;)
_________________
homeobocks wrote:
superjaded wrote:

would Big Brother become a reality?

Yeah . . . it would be just like the tv show. Except somebody would be watching.
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Tue Feb 17, 2004 6:29 pm    Post subject: Reply with quote

Sorry. I haven't been around the forums a lot recently and have updated to the recent portage version only recently. I have attached the modified script below (it is a simple change that checks for qpkg explicitly rather than through pkglist).

Regards.

NB. Code has been updated in the first post to this thread itself and has been removed from this post.
Yama wrote:
This script served me flawlessly for quite some time (> 1 year), but now when I run it I receive the following error:
Code:
/usr/local/sbin/etc-autoupdate: line 29: /usr/lib/portage/bin/pkglist: No such file or directory
You need to emerge gentoolkit to run this
This started happening after upgrading my gentoolkit to the latest stable version (0.2.0_pre6). It seems that this version does not have the /usr/lib/portage/bin/pkglist file.

EDIT(Naan Yaar): Removed code and put in consolidated version in the first post.


Last edited by Naan Yaar on Sat Mar 13, 2004 4:56 am; edited 2 times in total
Back to top
View user's profile Send private message
Yama
Tux's lil' helper
Tux's lil' helper


Joined: 21 Jul 2002
Posts: 135
Location: Sydney, Australia

PostPosted: Wed Feb 18, 2004 2:09 pm    Post subject: Reply with quote

Thanks Naan. Your new script appears to work well.
Back to top
View user's profile Send private message
Yak
Tux's lil' helper
Tux's lil' helper


Joined: 01 Sep 2002
Posts: 107

PostPosted: Fri Mar 12, 2004 1:34 am    Post subject: Reply with quote

This script has served me well, it's a real time saver. However, awhile back I somehow got about 50 files in /usr/X11R6/lib/X11/xkb that needed updating, and I finally got around to looking at it. It seems that CONFIG_PROTECT is also present in /etc/profile.env which gets read when you source /etc/profile. So I played around a bit and changed the script so it sources /etc/profile too. This worked for what I needed so I thought I would share it. Use it at your own peril.
Code:

# get dir list from config
sourcefiles="/etc/make.globals /etc/profile"
for file in $sourcefiles; do
    echo "List from $file"
    source $file
    for dir in $CONFIG_PROTECT; do
        echo -n "    $dir"
        if [ -d $dir ]; then
            echo "  (directory exists)"
            dirs="$dirs $dir"
        else
            echo "  (not existing dir)"
        fi
    done
done
echo
echo "Dirs list to search: $dirs"

It prints a bunch of extra stuff, I sort of like it so I left it in. I suppose to be complete it should source /etc/make.conf too, but it doesn't seem to work right unless you add an empty CONFIG_PROTECT="" into /etc/make.conf.
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Sat Mar 13, 2004 4:58 am    Post subject: Reply with quote

Yak,

I had noticed that it did not pick up files in .../xkb correctly, but did not investigate further. Thanks for your insight.

I have updated the script in the first post to this thread to reflect this fix.
Yak wrote:
This script has served me well, it's a real time saver. However, awhile back I somehow got about 50 files in /usr/X11R6/lib/X11/xkb that needed updating, and I finally got around to looking at it. It seems that CONFIG_PROTECT is also present in /etc/profile.env which gets read when you source /etc/profile. So I played around a bit and changed the script so it sources /etc/profile too. This worked for what I needed so I thought I would share it. Use it at your own peril.
...
It prints a bunch of extra stuff, I sort of like it so I left it in. I suppose to be complete it should source /etc/make.conf too, but it doesn't seem to work right unless you add an empty CONFIG_PROTECT="" into /etc/make.conf.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum