Advertisement
plirof2

Linux mount same partition to 2 paths

Aug 13th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ------------------------------------
  2. Use a Bind Mount: A bind mount allows you to mount an already mounted filesystem at a new location. This does not require the device to be unmounted.
  3.  
  4. To perform a bind mount, you can use the following command:
  5.  
  6. sudo mount --bind /mnt/sda3 /mnt/sdk3
  7.  
  8. After this command, both /mnt/sda3 and /mnt/sdk3 will point to the same filesystem, and changes made in either will be reflected in both locations.
  9.  
  10. ------------------------------------
  11.  
  12.  
  13. ------------------------------------
  14. #!/bin/bash
  15.  
  16. # Label of the partition to search for
  17. PARTITION_LABEL="wd1TBd_ext2"
  18.  
  19. # Mount point for the bind mount
  20. MOUNT_POINT="/mnt/sdk3"
  21.  
  22. # Find the partition by its label
  23. DEVICE=$(lsblk -o NAME,LABEL | grep "$PARTITION_LABEL" | awk '{print $1}')
  24.  
  25. if [ -z "$DEVICE" ]; then
  26.     echo "Partition with label '$PARTITION_LABEL' not found."
  27.     exit 1
  28. fi
  29.  
  30. # Get the full device name (e.g., /dev/sda1)
  31. DEVICE="/dev/$DEVICE"
  32.  
  33. # Check if the partition is mounted
  34. if mount | grep "on $MOUNT_POINT " > /dev/null; then
  35.     echo "$MOUNT_POINT is already mounted."
  36.     exit 0
  37. fi
  38.  
  39. # Create the mount point if it does not exist
  40. if [ ! -d "$MOUNT_POINT" ]; then
  41.     echo "Creating mount point '$MOUNT_POINT'."
  42.     mkdir -p "$MOUNT_POINT"
  43. fi
  44.  
  45. # Bind mount the partition to the mount point
  46. mount --bind "$DEVICE" "$MOUNT_POINT"
  47.  
  48. # Check if the bind mount was successful
  49. if [ $? -eq 0 ]; then
  50.     echo "Successfully bind mounted '$DEVICE' to '$MOUNT_POINT'."
  51. else
  52.     echo "Failed to bind mount '$DEVICE' to '$MOUNT_POINT'."
  53.     exit 1
  54. fi
  55.  
  56.  
  57. ------------------------------------
  58.  
  59. A bind mount and a hard link are related concepts in file systems, but they serve different purposes and have distinct behaviors. Let's break down the differences and discuss their advantages and potential problems:
  60.  
  61. ### Bind Mount
  62. A bind mount allows you to mount an existing directory (or file) to another location in the file system. This means that the same underlying data can be accessed from multiple paths. It is created using the `mount` command in Linux.
  63.  
  64. **Example:**
  65. ```bash
  66. mount --bind /mnt/sda3 /mnt/sdk3
  67. ```
  68.  
  69. **Characteristics:**
  70. - Both the original mount point (`/mnt/sda3`) and the bind mount (`/mnt/sdk3`) refer to the same inode (the internal representation of the file or directory in the filesystem).
  71. - Changes made in one location are immediately reflected in the other.
  72. - Unmounting the bind mount does not affect the original location.
  73. - This can be useful for organizing file systems or making certain locations accessible at different paths.
  74.  
  75. ### Hard Link
  76. A hard link creates an additional directory entry for an existing file. Unlike a bind mount, a hard link does not involve the filesystem mount system; it is purely a file system operation. You create a hard link using the `ln` command without the `-s` option.
  77.  
  78. **Example:**
  79. ```bash
  80. ln /mnt/sda3/file.txt /mnt/sdk3/file.txt
  81. ```
  82.  
  83. **Characteristics:**
  84. - A hard link points to the same inode as the original file. Such links share the same file data.
  85. - Files must reside on the same filesystem for hard links.
  86. - Both links are indistinguishable; deleting either one does not delete the actual data until all links to it are removed.
  87. - Hard linking can only be done on files, not on directories, except by the root user under specific circumstances.
  88.  
  89. ### Symbolic Link vs Hard Link
  90. Note that in your example, you've used `ln -s`, which creates a symbolic (soft) link instead of a hard link. Here's a brief look at symbolic links:
  91.  
  92. - A symbolic link points to the path of the target file or directory and can link across different filesystems.
  93. - If the target file is deleted, the symbolic link becomes broken (dangling).
  94.  
  95. ### Advantages and Problems
  96.  
  97. #### Bind Mounts
  98. **Advantages:**
  99. - Can mount entire directories to another location in the file system.
  100. - Works even across different directories (unlike hard links).
  101. - Useful for various administrative tasks or for creating a uniform directory structure.
  102.  
  103. **Problems:**
  104. - Unmounting can be overlooked, as it doesn't affect the original mount.
  105. - May complicate the filesystem hierarchy if not managed properly.
  106.  
  107. #### Hard Links
  108. **Advantages:**
  109. - Can help save space by avoiding data duplication.
  110. - The actual data only gets deleted once the last link is removed (data integrity).
  111.  
  112. **Problems:**
  113. - Cannot span across different filesystems.
  114. - Not applicable to directories (except in special cases).
  115. - Can create confusion, as you may have multiple directory entries pointing to the same data, making tracking harder.
  116.  
  117. ### Summary
  118. - Use **bind mounts** when you want to refer to the same directory (or file) at multiple paths in the same filesystem.
  119. - Use **hard links** when you want to create multiple directory entries for the same file within the same filesystem.
  120. - If you meant to compare bind mounts to symbolic links, remember that symbolic links can point to files across different filesystems, while bind mounts cannot.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement