Working with symlink and learning unlink = rm the hard way

General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

Working with symlink and learning unlink = rm the hard way

Postby tekkon » 21 Aug 2009, 17:12

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: Select all
ln -s /sbin/mount_ntfs /usr/local/bin/ntfs-3g

in '/etc/rc.local'. I gotten this error:
Code: Select all
ln: /usr/local/bin/ntfs-3g: File exists

at boot.

I read the ln man page some more and tried:
Code: Select all
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: Select all
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: Select all
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: Select all
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: Select all
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: Select all
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: Select all
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.
tekkon
Junior Member
 
Posts: 17
Joined: 19 Nov 2008, 04:32

Postby Alt » 21 Aug 2009, 18:29

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)
User avatar
Alt
Member
 
Posts: 726
Joined: 18 Nov 2008, 12:22
Location: Mother Russia

Postby tekkon » 21 Aug 2009, 19:32

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?
tekkon
Junior Member
 
Posts: 17
Joined: 19 Nov 2008, 04:32

Postby Alt » 21 Aug 2009, 19:47

tekkon wrote:I used -f with ln because using -s only returned the error: 'ln: /usr/local/bin/ntfs-3g: File exists'
It protected you, but you forced =)

tekkon wrote:As symlinks are created one time, how do I get a list of the symlinks created besides having to check the individual linked files?
dont understood =)

tekkon wrote: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?
Read part 4 above =)
User avatar
Alt
Member
 
Posts: 726
Joined: 18 Nov 2008, 12:22
Location: Mother Russia

Postby jb_fvwm2 » 21 Aug 2009, 19:49

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)
jb_fvwm2
Senior Member
 
Posts: 1503
Joined: 16 Nov 2008, 19:19

Postby tekkon » 22 Aug 2009, 01:12

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: Select all
mv /sbin/mount_ntfs /sbin/mount_ntfs.bak

before running
Code: Select all
ln -s /usr/local/bin/ntfs-3g /sbin/mount_ntfs

am I right?

Alt wrote:dont understood =)

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.
tekkon
Junior Member
 
Posts: 17
Joined: 19 Nov 2008, 04:32

Postby phoenix » 22 Aug 2009, 01:45

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 [cmd=#]ln -s /usr/local/bin/ntfs-3g /usr/bin/mount_ntfs[/cmd].

To remove a symlink is as simple as removing a file, just rm it: [cmd=#]rm /usr/bin/mount_ntfs[/cmd] 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
Freddie

Help for FreeBSD: Handbook, FAQ, man pages, mailing lists.
User avatar
phoenix
MFC'd
 
Posts: 3349
Joined: 17 Nov 2008, 05:43
Location: Kamloops, BC, Canada

Postby Alt » 22 Aug 2009, 05:22

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

Yes.

tekkon wrote:I thought the system had a list somewhere keeping track of all the symlinks that exist throughout the system.
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: Select all
> 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"
User avatar
Alt
Member
 
Posts: 726
Joined: 18 Nov 2008, 12:22
Location: Mother Russia

Postby tekkon » 22 Aug 2009, 15:36

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?
tekkon
Junior Member
 
Posts: 17
Joined: 19 Nov 2008, 04:32

Postby Beastie » 22 Aug 2009, 16:17

If you still have the ISO/CD, why don't you just get it back from there? It's in the [file]/7.x-RELEASE/base[/file] directory. You'll have to merge the chunks and untar the merged file. Check install.sh for more information. You'll find [file]mount_ntfs[/file] in [file]/sbin[/file] 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)
May the source be with you!
Beastie
Senior Member
 
Posts: 1862
Joined: 28 Mar 2009, 11:27
Location: /dev/earth0

Postby phoenix » 22 Aug 2009, 17:10

If you have the source tree installed, you should be able to go into /usr/src/sbin/mount_ntfs and run [cmd=#]make; make install[/cmd] to re-install it.
Freddie

Help for FreeBSD: Handbook, FAQ, man pages, mailing lists.
User avatar
phoenix
MFC'd
 
Posts: 3349
Joined: 17 Nov 2008, 05:43
Location: Kamloops, BC, Canada


Return to General

Who is online

Users browsing this forum: TeoEnMing, yayix and 0 guests