且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux系统CentOS8 检测USB、创建挂载点、挂载、存取USB数据、取消挂载

更新时间:2022-06-18 11:27:00

Linux系统CentOS8 检测USB、创建挂载点、挂载、存取USB数据、取消挂载

当主机接入 usb 设备时, linux 系统会在 /dev 路径下增加一个新的 block设备。在和 usb 设备进行数据传输之前,需要将 usb 文件系统 进行挂载

1. 检测:
#查看磁盘信息,根据磁盘文件系统类型和大小查找 usb 驱动器
[arthur@localhost ~]$ sudo fdisk -l
Disk /dev/sdb: 119.2 GiB, 128035676160 bytes, 250069680 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x522664d6

Device     Boot   Start       End   Sectors   Size Id Type
/dev/sdb1  *       2048   2099199   2097152     1G 83 Linux
/dev/sdb2       2099200 250068991 247969792 118.2G 8e Linux LVM
...
Disk /dev/sdc: 29.3 GiB, 31457280000 bytes, 61440000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x50886156

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1  *       63 61439999 61439937 29.3G  c W95 FAT32 (LBA)
2. 创建挂载点: 挂载点可以是主机文件系统内已经存在的或新创建的路径
[arthur@localhost ~]$ sudo mkdir /media/usbC
[arthur@localhost ~]$ ll  /media/usbC
total 0
3. 挂载:
[arthur@localhost ~]$ sudo mount /dev/sdc1 /media/usbC/
#查看挂载后 usb disk 内的目录文件信息
[arthur@localhost ~]$ ll /media/usbC/
total 926512
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
#查看挂载信息
[arthur@localhost ~]$ mount |  grep sdc1
/dev/sdc1 on /run/media/arthur/disk type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
/dev/sdc1 on /media/usbC type vfat (rw,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro)
4. 存取数据
4.1 写入数据:
[arthur@localhost usbC]$ echo Hello,Aliyun! >test.txt
[arthur@localhost usbC]$ ll
total 926528
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40  test.txt
[arthur@localhost usbC]$ cat test.txt 
Hello,Aliyun!
4.2 拷贝数据至主机:
[arthur@localhost usbC]$ cp test.txt  /home/arthur/Downloads/ 
[arthur@localhost usbC]$ ll /home/arthur/Downloads/ 
total 926500
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00 hg19.fa.gz
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40 test.txt
4.3 查看磁盘使用情况
[arthur@localhost usbC]$ df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             4.6G     0  4.6G   0% /dev
...
/dev/sdc1             30G  905M   29G   4% /media/usbC
5. 取消挂载
#确保没有进程正在使用或者占用挂载点路径,否则会出现设备占用的警告
umount:  target is busy
#命令`umount`取消挂载
[arthur@localhost ~]$ sudo umount /media/usbC 
[sudo] password for arthur: 
#查看当前系统挂载文件系统
##手动创建的挂载点已无设备,此处出现的挂载信息为系统检测到的 usb disk,当手动弹出 usb disk 之后,该条信息消失
[arthur@localhost ~]$ mount | grep sdc1
/dev/sdc1 on /run/media/arthur/disk type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2) 
[arthur@localhost ~]$ ll /media/usbC/
total 0
[arthur@localhost ~]$ sudo umount /media/usbC
umount: /media/usbC: not mounted.
#未手动弹出 usb disk 
[arthur@localhost ~]$ ll  /run/media/arthur/disk/
total 926528
-rw-r--r--. 1 arthur arthur 948731419 Mar 10 04:00  hg19.fa.gz
drwxr-xr-x. 2 arthur arthur     16384 Aug  3 07:15 'System Volume Information'
-rw-r--r--. 1 arthur arthur        14 Sep  2 04:40  test.txt
#弹出 usb disk 之后
[arthur@localhost ~]$ ll /run/media/arthur/
total 0
[arthur@localhost ~]$ mount | grep sdc1
[arthur@localhost ~]$ 
6. 在 Linux 中永久性添加挂载点

编辑 fstab 文件, 可以使系统重启后仍然保留此文件挂载点

[arthur@localhost ~]$ tail /etc/fstab 
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=bf580455-80bf-4ca2-98fc-1a663e9a73e0 /boot                   ext4    defaults        1 2
/dev/mapper/cl-home     /home                   xfs     defaults        0 0
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

添加如下:

/dev/sdc1    /media/usbC    vfat    default    0    0
拓展:

etc/fstab获取的 block device 名字来指代 usb 设备可能并不是一个好的长久的方案。随着 linux 系统中 USB drives 数量的变化, block device 名称可能会发生改变。尽管使用 UUID raw block device name 同样是临时性方案,但 使用 UUID 会相对更佳。

reference:
[1]How to mount usb drive in linux.linuxconfig