[Linux] How to get USB-stick path and use it?
First, make sure your privileged level:
- #: requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $: requires given linux commands to be executed as a regular non-privileged user
List USB partition
lsblk
You’ll see a response similar to:
or,
fdisk -l
You’ll see a response similar to:
Here, I want to use sda1 for example.
Create mount point
Before you are able to use mount
command to mount the USB partition, you need to create a mount point. It can be a new or a existing directory within your host filesystem.
I create new directory to be the mount point to mount my USB device by using mkdir
command:
mkdir UsbStick
Mount USB Drive
Use mount
command to mount /dev/sda1
into UsbStick
mount point:
mount /dev/sda1 UsbStick/
You’ll see a response similar to:
sda1 is mounted to /home/root/UsbStick
from /run/media/sda1
To check whether the USB drive has been mounted correctly execute mount
command again without any arguments and use grep
to search for USB block device name:
mount | grep sda1
You’ll see a response similar to:
Accessing USB Data
You can access the USB data simply by navigating to the previously created mount point /UsbStick
:
# cd /home/root/UsbStick
Unmount
Make sure that no process is using or accessing the mount point directory.
Execute the following command to unmount your USB drive:
# umount /home/root/UsbStick
Permanent Mount
If you want to mount the USB drive permanently, you can use vi
command to modify the config file:
vi /etc/fstab
You’ll see a response similar to:
Add the following line into fstab config file:
/dev/sda1 /home/root/UsbStick vfat defaults 0 0
If fail, you can use partition UUID
instead of the device name. Now, locate a UUID of your USB drive:
# ls -l /dev/disk/by-uuid/*
You’ll see a response similar to:
The UUID of device sda1
is FDFA-40E0
thus our /etc/fstab
mount line will be:
/dev/disk/by-uuid/FDFA-40E0 /home/root/UsbStick vfat 0 0
Run mount -a
command to mount all not yet mounted devices.
Partitioning the SD/MMC card if you need:
sudo fdisk /dev/sd<X> ,where <X> is the SD card you used#Type the following parameters (each followed by):
p [lists the current partitions]
d [to delete existing partitions. Repeat this until no unnecessary partitions are reported by the 'p' command to start fresh.]
n [create a new partition]
p [create a primary partition - use for both partitions]
1 [the first partition]
2048 [starting at offset sector]
102400 [size for the first partition to be used for the boot images]
p [to check the partitions]n
p
2
122880 [starting at offset sector, which leaves enough space for the kernel, the bootloader and its configuration data]
<enter> [using the default value will create a partition that extends to the last sector of the media]
p [to check the partitions]
w [this writes the partition table to the media and fdisk exits]