hard links - Re: [TriLUG] mkfs vs mke2fs

Jeremy Portzer jeremyp at pobox.com
Fri May 14 12:03:33 EDT 2004


On Fri, 2004-05-14 at 11:16, Tanner Lovelace wrote:
> Jeremy Portzer said the following on 5/14/04 10:49 AM:
> > Actually, now would be a good time to remember what we talked about last
> > night about hard links.  Note that there is a "3" next to "root" in the
> > the long listing.  This means that there are three (hard) links to the
> > file.  I would guess that these files really are the same (hard
> > linked).  The way to determine this for sure is to run "ls -li".
> 
> Interesting.  On the system I initially checked, all the links were
> one and the inodes were different.   This was on a whitebox system.
> I just now checked on my Mandrake 10.0 system and they are hardlinked.
> Weird.
> 
> Whitebox:
> 
> % ls -li /sbin/mkfs.ext* /sbin/mke2fs
>   325884 -rwxr-xr-x    1 root     root        32056 Dec 12 01:12 /sbin/mke2fs*
>   325887 -rwxr-xr-x    1 root     root        32056 Dec 12 01:12 /sbin/mkfs.ext2*
>   325888 -rwxr-xr-x    1 root     root        32056 Dec 12 01:12 /sbin/mkfs.ext3*
> 
> Mandrake 10.0:
> 
> % ls -li /sbin/mkfs.ext* /sbin/mke2fs
> 58720523 -rwxr-xr-x  3 root root 30668 Feb 26 03:54 /sbin/mke2fs*
> 58720523 -rwxr-xr-x  3 root root 30668 Feb 26 03:54 /sbin/mkfs.ext2*
> 58720523 -rwxr-xr-x  3 root root 30668 Feb 26 03:54 /sbin/mkfs.ext3*
> 

One thing that can sometimes happen is hard links can be "broken" if not
careful when copying or moving things around (perhaps some component of
the RPM install script or similar in the Whitebox system).  For example,
take a look at the following:

$ touch a; ln a b; ln a c; ls -li
total 0
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 a
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 b
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 c

Remember, the above means we have one file with three names/directory
entries/hard links.
Now, let's copy them:

$ for i in a b c; do cp -a $i ${i}.2; done; ls -li
`a' -> `a.2'
`b' -> `b.2'
`c' -> `c.2'
total 0
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 a
 263949 -rw-rw-r--    1 jeremy   jeremy          0 May 14 11:48 a.2
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 b
 263950 -rw-rw-r--    1 jeremy   jeremy          0 May 14 11:48 b.2
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 c
 263954 -rw-rw-r--    1 jeremy   jeremy          0 May 14 11:48 c.2

Note that the files a.2, b.2, and c.2 are all different, not linked to
each other, nor are they linked to the originals.  This is really what
you normally want, but can cause issues in the above demonstration of
mke2fs and mkfs.ext2.  cp doesn't know how to 'preserve' these hard
links, even with the -a option.

Some programs like rsync do have an option to try to preserve hard
links.   I removed a.2, b.2, and c.2, and put a, b, and c into a
directory, files:

$ ls -li files/
total 0
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 a
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 b
 263948 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 c

Now let's make a backup of files/ with rsync, telling it to preserve
hard links with -H:

$ rsync -aH files/ files.bak/
$ ls -li files.bak
total 0
 263950 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 a
 263950 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 b
 263950 -rw-rw-r--    3 jeremy   jeremy          0 May 14 11:48 c

Now, you see that the files.bak/ has the copies of a, b, and c, linked
to each other (all are inode 263950).  But it's a different inode from
the original (263948).  So right now we actually have two distinct
files, each with three names/directory entries.  

Note the following from rsync's man page:

       -H, --hard-links
              This tells rsync to recreate hard  links  on the  remote 
system
              to   be  the  same as the local system. Without this
option hard
              links are treated like regular files.
                                                                                            
              Note that rsync can only detect hard links if both parts
of  the
              link are in the list of files being sent.
 
              This option can be quite slow, so only use it if you need
it.


Keep this in mind if you ever have to move your snapshot backup folders
around.  You wouldn't do this in normal practice, but if you were
reorganizing things, the best bet would be to use rysnc with the -H
option.  

For Bonus points:  Think about why the -H option is slow.  It should
make sense.

Hope this helps,

Jeremy

-- 
/---------------------------------------------------------------------\
| Jeremy Portzer        jeremyp at pobox.com      trilug.org/~jeremy     |
| GPG Fingerprint: 712D 77C7 AB2D 2130 989F  E135 6F9F F7BC CC1A 7B92 |
\---------------------------------------------------------------------/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://www.trilug.org/pipermail/trilug/attachments/20040514/71384619/attachment.pgp>


More information about the TriLUG mailing list