skip to Main Content

I want to know how can we mount an file as read-only in Linux CentOS 7 Server through Golang. I have tried syscall but that doesn’t work, syscall mounts the file but as read-write i have tried to give ro argument in the data but still it’s mounting as read-write.
Here is my go code:

syscall.Mount(src, dst, "auto", syscall.MS_BIND, "ro")

You can see I have given ro argument in the data, i have also tried to give only r and readonly and also read-only but none of them works, when i compile the go file and execute it and then when i check /etc/mtab then i am getting this output :

cat /etc/mtab | grep "firewall"
/dev/vda1 /root/firewall.txt ext4 rw,relatime,data=ordered,jqfmt=vfsv1,usrjquota=quota.user 0 0

Here you can see that firewall.txt is mounted as rw means read-write

I want to mount it as read-only , can anyone help me how can i do that in Golang?

2

Answers


  1. Read-only mode is defined by the syscall flag MS_RDONLY, which is also defined in the syscall package. So the call should be:

    syscall.Mount(src, dst, "auto", syscall.MS_BIND | syscall.MS_RDONLY, "")
    
    Login or Signup to reply.
  2. I have faced similar issue, It was fixed by setting fstype as bind and flags as MS_REMOUNT MS_RDONLY and MS_BIND . You can use these codes:

    syscall.Mount(source, destination, "bind", syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_BIND, "")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search