1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Working with symlink and learning unlink = rm the hard way

Discussion in 'General' started by tekkon, Aug 21, 2009.

  1. tekkon

    tekkon New Member

    Messages:
    17
    Likes Received:
    0
    I was following this thread to mount ntfs-3g at boot time. The solution involves creating a symlink from ntfs-3g to mount_ntfs which I'm not familiar with.

    I'm on FreeBSD 7.1-RELEASE. I'll describe what i did to my system.

    I tried creating a symlink by putting
    Code:
    ln -s /sbin/mount_ntfs /usr/local/bin/ntfs-3g
    in '/etc/rc.local'. I gotten this error:
    Code:
    ln: /usr/local/bin/ntfs-3g: File exists
    at boot.

    I read the ln man page some more and tried:
    Code:
    ln -sf /sbin/mount_ntfs /usr/local/bin/ntfs-3g
    It worked. Now I learn I've gotten the order of the source and target of the linking wrong. I then issued:
    Code:
    ln -sf /usr/local/bin/ntfs-3g /sbin/mount_ntfs
    which is the correct order I needed.

    I issued the following to check the symlink. I got:
    Code:
    file /sbin/mount_ntfs
    /sbin/mount_ntfs: broken symbolic link to `/usr/local/bin/ntfs-3g'
    Then I checked /usr/local/bin/ntfs-3g and gotten the same error:
    Code:
    file /usr/local/bin/ntfs-3g
    /usr/local/bin/ntfs-3g: broken symbolic link to `/sbin/mount_ntfs'
    It seems I've now linked both files in a circle to each other. So I looked for a tool to unlink them. I issued:
    Code:
    unlink /sbin/mount_ntfs
    and now it appears I had deleted /sbin/mount_ntfs with unlink. :(

    Figuring that until I find a way to replace the deleted /sbin/mount_ntfs, I should just remove the broken symlink of /usr/local/bin/ntfs-3g from /sbin/mount_ntfs at this time. I made a backup this time with:
    Code:
    cp -RP /usr/local/bin/ntfs-3g /usr/local/bin/ntfs-3g_bak
    this time in case the symlink remove procedure goes wrong again.

    After issuing:
    Code:
    rm /usr/local/bin/ntfs-3g
    surely not only the symlink but the file 'ntfs-3g' was also gone. I recovered 'ntfs-3g' from the backup to find that the broken symlink from '/sbin/mount_ntfs' has also been restored.

    That was what happened. Please teach me how to safely remove symlinks between 2 binary files without removing the file itself and how to replace /sbin/mount_ntfs that I've deleted.
     
  2. Alt

    Alt New Member

    Messages:
    726
    Likes Received:
    0
    1. Do not use `-f` flag unless you need to FORCE action. That why you overwrited ur real binary..
    2. If you do `ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs` one time, this will create symlink "/sbin/mount_ntfs" pointing to your binary. It is *no need* to do it every boot. (If your /sbin/ is not a ramdisk lol))
    3. Do not put any stuff to /etc/rc.local or /etc/rc.d or /etc anyway =) Its not linux, user startup scripts must live in /usr/local/etc/rc.d/
    4. If you do `rm /sbin/mount_ntfs` it will remove object "/sbin/mount_ntfs" - no matter symlink or file. Real file not affected. (Caution if you symlink a directory and then `rm -rf` it can destroy real files)
     
  3. tekkon

    tekkon New Member

    Messages:
    17
    Likes Received:
    0
    Thank you for your advices.

    I used -f with ln because using -s only returned the error: 'ln: /usr/local/bin/ntfs-3g: File exists'

    As symlinks are created one time, how do I get a list of the symlinks created besides having to check the individual linked files?

    Since I had symlinked one binary to another, removing the symlink will remove the symlink will remove the linking binary too. Is there a way to remove the symlink only without touching the binary?
     
  4. Alt

    Alt New Member

    Messages:
    726
    Likes Received:
    0
    It protected you, but you forced =)

    dont understood =)

    Read part 4 above =)
     
  5. jb_fvwm2

    jb_fvwm2 Member

    Messages:
    1,563
    Likes Received:
    0
    I've put a working example of symlink creation in /etc/motd (along
    with other commands) so that I can grep the proper usage instantly...
    (Sorry to not answer the question(s)...)
    (ignoring the post immediately above, it was posted as I wrote)
     
  6. tekkon

    tekkon New Member

    Messages:
    17
    Likes Received:
    0
    Alt:
    Okay, I think I learn something new about symlinks. A symlinks is an object and cannot exist together with another object of the same name along the same path.

    Since I had already issued 'ln -sf' on both '/sbin/mount_ntfs' and '/usr/local/bin/ntfs-3g' as targets, that means both mount_ntfs and ntfs-3g now is a symlink instead of the original binary.

    I think reinstalling ntfs-3g can get the ntfs-3g binary back but how do I restore mount_ntfs ?

    From what I had seen, the correct way to symlink the source 'ntfs-3g' to target 'mount_ntfs' is to issue:
    Code:
    mv /sbin/mount_ntfs /sbin/mount_ntfs.bak
    before running
    Code:
    ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs
    am I right?

    I thought the system had a list somewhere keeping track of all the symlinks that exist throughout the system.

    jb_fvwm2:
    Thanks for the good tip.
     
  7. phoenix

    phoenix Moderator Staff Member Moderator

    Messages:
    3,404
    Likes Received:
    0
    The argument order for ln is the same as for cp or for mv. In other words, you link from an existing file to the name of the link.

    The correct ln command is # ln -s /usr/local/bin/ntfs-3g /usr/bin/mount_ntfs.

    To remove a symlink is as simple as removing a file, just rm it: # rm /usr/bin/mount_ntfs After all, a symlink is just a file.

    What happened, in your case, is that you wrote the arguments backwards. And it gave an error. Which you then ignored, and forced the link from a non-existent file overtop of an existing file (which wiped out the contents of ntfs-3g). Then you deleted the non-existent symlink, leaving an empty file behind. :)

    You'll need to re-install the ntfs-3g port to get things working again. And then re-do the symlink, with the arguments in the right order. :D
     
  8. Alt

    Alt New Member

    Messages:
    726
    Likes Received:
    0
    Yes.

    Technically, a symlink is just a file. So, it lives in your filesystem =) Difference is that it have attribute saying it is symlink. In this case file content is just a path to original file which it point to.
    Look this:
    Code:
    > ls -la s.tgz
    lrwxr-xr-x  1 alt  wheel  14 22 авг 09:20 s.tgz -> sitemap.xml.gz
    We do an ls and see "lrwxr-xr-x". 'l' says its symlink. Filesize is 14 cus this file is a symlink and content only path to "sitemap.xml.gz"
     
  9. tekkon

    tekkon New Member

    Messages:
    17
    Likes Received:
    0
    Alt, phoenix:

    Thanks for further explaining. I now have a better understanding of how to work with symlinks.


    One last thing though, how do I replace the '/sbin/mount_ntfs' binary that I had destroyed? Since 'mount_ntfs' is part of the base system, do I need to rebuild my FreeBSD 7.1-RELEASE to get mount_ntfs back?
     
  10. Beastie

    Beastie Member

    Messages:
    1,915
    Likes Received:
    0
    If you still have the ISO/CD, why don't you just get it back from there? It's in the /7.x-RELEASE/base directory. You'll have to merge the chunks and untar the merged file. Check install.sh for more information. You'll find mount_ntfs in /sbin obviously.

    EDIT: if you don't have the ISO anymore, you can also get the base from the ftp: ftp://ftp.freebsd.org/pub/FreeBSD/releases/i386/7.x-RELEASE/base/ (change the x to your release minor version)
     
  11. phoenix

    phoenix Moderator Staff Member Moderator

    Messages:
    3,404
    Likes Received:
    0
    If you have the source tree installed, you should be able to go into /usr/src/sbin/mount_ntfs and run # make; make install to re-install it.