<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<HTML>
<HEAD>
  <META NAME="generator" CONTENT=
  "HTML Tidy for Linux/x86 (vers 1st August 2003), see www.w3.org">

  <TITLE>RPM - Rebuilding a RPM database NFAQ</TITLE>
  <META HTTP-EQUIV="Content-Type" CONTENT=
  "text/html; charset=iso-8859-1">
  <META NAME="author" CONTENT="Pascal J. Bourguignon">
  <META HTTP-EQUIV="Reply-to" NAME="Reply-to" CONTENT=
  "pjb@informatimago.com">
  <META HTTP-EQUIV="Description" NAME="description" CONTENT=
  "A HOWTO file describing a procedure to rebuild a RPM database from scratch, populating with packages already installed on the Linux system.">
  <META NAME="keywords" CONTENT=
  "RPM,RedHat,Mandrake,Linux,failed to open //var/lib/rpm/packages.rpm">
  <link rel="stylesheet" type="text/css" href="../default.css"></HEAD>

<BODY>
  <!--TOP-BEGIN-->
<!-- This section is automatically generated by html-update, -->
<!-- from data in 'node.el'.    Please, do not edit it here. -->
<DIV CLASS="TOP"></DIV>
<!--TOP-END-->
<!--MENU-BEGIN-->
<!-- This section is automatically generated by html-update, -->
<!-- from data in 'node.el'.    Please, do not edit it here. -->
<DIV CLASS="MENU"><HR><P>|
 <A CLASS="button" HREF="../toc.html">Contents</a> |
 <A CLASS="button" HREF="../index.html">Home</a> |
 <A CLASS="button" HREF="../develop/pic-merge-diff3/index.html">Previous</a> |
 <A CLASS="button" HREF="../develop/sms/index.html">Up</a> |
 <A CLASS="button" HREF="chrooted-ssh-cvs.html">Next</a> |
</P><HR></DIV>
<!--MENU-END-->


  <H1>RPM - Rebuilding a RPM database
  NFAQ</H1>

  <P>On one of my Mandrake Linux systems, I had once this problem:</P>
  <PRE>
     [pascal@hermes pascal]$ su
     Password:
     [root@hermes pascal]# rpm -q -a
     failed to open //var/lib/rpm/packages.rpm

     rpmQuery: rpmdbOpen() failed
     [root@hermes pascal]# rpm --rebuilddb
     failed to open //var/lib/rpm/packages.rpm

     [root@hermes pascal]# rpm --initdb
     [root@hermes pascal]# rpm -q -a
     failed to open //var/lib/rpm/packages.rpm

     rpmQuery: rpmdbOpen() failed
     [root@hermes pascal]# rpm --version
     RPM version 3.0.3
</PRE>

  <P>Well, clearly the RPM database was completely out of usage. I had
  to rebuild it. You may think that it&#39;s simply a matter of using
  :</P>
  <PRE>
   rpm --rebuilddb
</PRE>

  <P>However, I learned that --rebuilddb is REALLY AND COMPLETELY
  USELESS. May be the authors and contributors to rpm could take some
  idea from this NFAQ to actually implement a true
  <CODE>--rebuilddb</CODE> option.</P>

  <P>The main idea of the process is to use:</P>
  <PRE>
    rm -f /var/lib/rpm/* ; rpm --initdb
</PRE>

  <P>and:</P>
  <PRE>
    rpm -i -v --nodeps --noscripts --notriggers --excludepath / packages...
</PRE>

  <P>to fill a package database without really installing anything (not
  overriding installed files, not running any script, ...). This is
  done in the seventh step; everything before I made to recover a list
  of installed packages.</P>

  <P>This is not perfect, since some packages I have upgraded or
  modified the sources and upgraded. I should have checked for
  /usr/src/RPMS too; I&#39;ll update the database later by hand to
  synchronize it with the handfull of packages I touched after the
  initial installation.</P>
  <PRE>
+------------------------------------------------------------------+
| COOKBOOK PROCEDURE TO REBUILD A RPM DATABASE DAMAGED BEYOND HOPE |
+------------------------------------------------------------------+

####
#### BEWARE, THIS IS NOT AN AUTOMATIC SCRIPT!
####
#### Use this only as a guideline to rebuild your rpm database.
####


#####
# 1 # Find the files currently installed.
#   # ON THE HOST WHO LOST ITS RPM DATABASE.
#####

find / -print | sort &gt; /tmp/dsk-contents



#####
# 2 # Find all the files installed by the installation RPMs,
#   # with their corresponding RPM.
#####

losetup /dev/loop0  /mnt/usr3/mandrake/mandrake61-1.iso
mount -o ro -t iso9660 /dev/loop0 /mnt/cdrom

cd /mnt/cdrom/Mandrake/RPMS
rm -f /tmp/rpm-contents
for f in *.rpm ; do
    rpm -q -l -p $f \
    | awk -v pn=$f &#39;{printf &quot;%s:%s\n&quot;,pn,$0;}&#39; &gt;&gt; /tmp/rpm-contents
done
sort -t: +1 -o /tmp/rpm-contents /tmp/rpm-contents



#####
# 3 # Obtain the actually installed RPMs.
#####



gzip -d &lt; <A HREF=
"lgetvalue-src.tar.gz">lgetvalue-src.tar.gz</A> | tar xf -
cd lgetvalue-src
make all test


./lgetvalue    --field-separator=: \
               --key-position=2 \
               --value-position=1 \
               /tmp/rpm-contents /tmp/dsk-contents \
| sort -u &gt; /tmp/rpm-installed



#####
# 4 # Reinitialize a new rpm database.
#####

mv /var/lib/rpm /var/lib/rpm-damaged-beyond-hope
mkdir /var/lib/rpm
rpm --initdb



#####
# 5 # Let rpm say what problems there may be
#   # (trying to do a pseudo-installation.
#####

cd /mnt/cdrom/Mandrake/RPMS
rpm -i -v --noscripts --notriggers --excludepath /  `cat /tmp/rpm-installed`



#####
# 6 # Correct the list of installed RPMs.
#####

emacs /tmp/rpm-installed


#####
# 7 # Rebuild the rpm data base, without installing any file
#   # nor running any script.
#####

rpm -i -v --nodeps --noscripts --notriggers --excludepath /  \
    `cat /tmp/rpm-installed `


#####
# 8 # Check you have a valid rpm database.
#####

rpm -q -a


</PRE><!--MENU-BEGIN-->
<!-- This section is automatically generated by html-update, -->
<!-- from data in 'node.el'.    Please, do not edit it here. -->
<DIV CLASS="MENU"><HR><P>|
 <A CLASS="button" HREF="../toc.html">Contents</a> |
 <A CLASS="button" HREF="../index.html">Home</a> |
 <A CLASS="button" HREF="../develop/pic-merge-diff3/index.html">Previous</a> |
 <A CLASS="button" HREF="../develop/sms/index.html">Up</a> |
 <A CLASS="button" HREF="chrooted-ssh-cvs.html">Next</a> |
</P><HR></DIV>
<!--MENU-END-->
  <!--BOTTOM-BEGIN-->
<!-- This section is automatically generated by html-update, -->
<!-- from data in 'node.el'.    Please, do not edit it here. -->
<DIV CLASS="BOTTOM">
<hr><code><small>
 | <a href="http://www.informatimago.com//linux/rpm-rebuilddb.html">Mirror on informatimago.com</a>
 | <a href="http://informatimago.free.fr/i//linux/rpm-rebuilddb.html">Mirror on free.fr</a>
 | </small></code>

<BR><SMALL>
      <a href="http://validator.w3.org/check?uri=referer"><img
          src="http://www.w3.org/Icons/valid-html401"
          alt="Valid HTML 4.01!" height="31" width="88"></a>
   </SMALL>
</DIV>
<!--BOTTOM-END-->
</BODY>
</HTML>
ViewGit