Ubuntu - Tự động mount partition khi khởi động

Trên Linux thông thường những phân vùng ổ cứng sẽ không được mount lúc máy tính khởi động, nó chỉ được mount khi bạn click vào biểu tượng của chúng trong file manager lần đầu tiên. Tuy nhiên bạn có thể setting để thay đổi điều này.

Phần mount tự động đượng cấu hình ở file /etc/fstab. Nó chứa nội dung tùy chọn cho mỗi phân vùng và những setting cho việc mount khi khởi động. Bạn có thể xem bằng lệnh cat /etc/fstab nó sẽ có nội dung như sau.
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda6 during installation
UUID=f2623dd4-e633-42d6-a952-8ec9cf488579 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=23e22849-6534-4e13-a4b3-bb4270da380b none            swap    sw              0       0
Mỗi dòng sẽ tương ứng với một partition sẽ được mount khi khỏi động.

Xem các phân vùng trên máy tính

Bạn có thể xem tất cả partition trên máy tính bằng lệnh sau
sudo fdisk -l
dưới đây là nội dung khi bạn thực hiện lệnh trên
Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x938ad46a

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1  *         2048    206847    204800   100M  7 HPFS/NTFS/exFAT
/dev/sda2          206848 204802047 204595200  97.6G  7 HPFS/NTFS/exFAT
/dev/sda3       204802048 414515199 209713152   100G  7 HPFS/NTFS/exFAT
/dev/sda4       414517246 625141759 210624514 100.4G  5 Extended
/dev/sda5       414517248 446515199  31997952  15.3G 82 Linux swap / Solaris
/dev/sda6       446517248 625141759 178624512  85.2G 83 Linux

Partition 4 does not start on physical sector boundary.

Xem UUID của partition

Tiếp theo bạn sử dụng lệnh blkid để lấy UUID partition. UUID là một thông số cần thiết để thêm vào file /etc/fstab, và là một chuỗi ký tự riêng biệt được định nghĩa cho mỗi partition của đĩa cứng hoặc là ổ đĩa USB.
sudo blkid
/dev/sda1: LABEL="System Reserved" UUID="A8C4538BC4535AA0" TYPE="ntfs" PARTUUID="938ad46a-01"
/dev/sda2: UUID="54804E8D804E7590" TYPE="ntfs" PARTUUID="938ad46a-02"
/dev/sda3: LABEL="Data" UUID="01D34C3B56EFBD90" TYPE="ntfs" PARTUUID="938ad46a-03"
/dev/sda5: UUID="23e22849-6534-4e13-a4b3-bb4270da380b" TYPE="swap" PARTUUID="938ad46a-05"
/dev/sda6: UUID="f2623dd4-e633-42d6-a952-8ec9cf488579" TYPE="ext4" PARTUUID="938ad46a-06"
Bạn chỉ cần chú ý đến UUID và TYPE của partition mà bạn muốn mount khi khởi động.

Thêm partition vào file fstab

Bây giờ khi đã có đầy đủ thông tin chúng ta bắt đầu add vào file /etc/fstab, nếu trong file đã có thì bạn chỉ cần chỉnh sửa theo ý mình là được. Bạn có thể ghi chú lại phần mình đã thêm và comment nó lại bằng dấu: #
Ở máy tôi thì tôi sẽ add thêm partition /dev/sda3 vào file /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda6 during installation
UUID=f2623dd4-e633-42d6-a952-8ec9cf488579 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=23e22849-6534-4e13-a4b3-bb4270da380b none            swap    sw              0       0
# Add automount /dev/sda3 on startup
UUID=01D34C3B56EFBD90 /media/thisuser/Data                ntfs    user,errors=remount-ro,auto,exec,rw 0       0
UUID=01D34C3B56EFBD90 : là UUID của partition
/media/thisuser/Data : đường dẫn của partition khi được mount
ntfs : kiểu định dạng của partition
user,errors=remount-ro,auto,exec,rw : tham số tùy chọn
remount-ro means remount partitions incase of read errors.
auto - Automatically mount partitions at startup
exec - Give users permission to execute files on this partition
rw - Give read write permission
user - Allow all non-root users to mount this partition
Lưu file fstab và khởi động lại máy tính chúng ta sẽ thấy thành quả.

Comment