Guide:Adding USB storage to your Device and Formatting it for linux

From PINE64
Jump to: navigation, search

Preface

This guide will show you various ways to view connected USB storage devices under linux. It will also show you how to format the device for use under linux (Recommended but not necessary, most linux distros are happy to read/write to Windows filesystems).

We will also be going through how to "mount" the volume to allow the system and user (you) to modify and read from it. We will also briefly go over setting file and directory permissions for the newly created volume.

Prerequisites

ROCK64 board running Debian or Ubuntu

Access to terminal, either locally or via SSH

USB Drive

Some basic understanding of using the linux terminal: Howtogeek

Confirm that the drive is recognized by Linux

Plug in your USB device into your board and open up a terminal, either locally or via SSH.

R64 USB 1.jpg

There are two quick ways we can confirm that the USB drive is "recognized" by the system. The first is issuing the command lsblk - List Block devices:

lsblk

Running it on your board should show something similar to the following:

[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part 
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part /

Lsblk 1.png

If you had ran the lsblk command before plugging in the USB device, you'd have only seen entries under "mmcblk1". This shows the boot MicroSD card and its various partitions.

Usually a newly plugged in USB drive shows up as sdX with X being a letter. In this case it's sda but it could also be sdb or sdc depending on your configuration. The numbers after the letters demarcate the various partitions present on the device.

To see more details about the device and partition, invoke the "fdisk -l" command with the block device (sda in this case) as an argument. You will need superuser access for this, simply insert your password to give it access:

sudo fdisk -l /dev/sda

The output would look like:

[email protected]:~$ sudo fdisk -l /dev/sda
Disk /dev/sda: 14.9 GiB, 16008609792 bytes, 31266816 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: gpt
Disk identifier: 7C4E0E7E-888C-48C6-AC55-A3F6D3F37E2B

Device     Start      End  Sectors  Size Type
/dev/sda1   2048 31264767 31262720 14.9G Microsoft basic data

This output shows that our device (the flashdrive) has a capacity of 14.9 GiB (or 15.99 GB). The sector size is 512 bytes long, and it has one partition, "Microsoft basic data" which is know is a FAT partition.

We used the fdisk command with an argument to make the output less cluttered and more relevent.

If you had used the fdisk command without the block device as an argument, it would have simply printed out the information for all recognized devices.

Fdisk -l 1.png

Formatting the drive for Linux use

You could use the flash drive right now, as is, but using a windows file system on linux is suboptimal. Instead, we will be making a new partition table and a linux partition.

Caution: Doing the following will erase all data from the drive. Please make sure you backup any important data on the drive!

After you've made sure that you're ready to erase the drive, proceed by invoking the fdisk command with the drive as the argument:

sudo fdisk /dev/sda

Type in "m" and hit enter to go to the extended menu

Fdisk 1.png

You have the option to create a new partition table. DOS is the most compatible, but larger(equal or greater than 4TB) devices, GPT is used. For a flash drive, either would suffice.

In this case, I will go with a GPT Table. So I type in "g" and hit enter

Command (m for help): g

Created a new GPT disklabel (GUID: 99E3E866-3783-46DE-9FDA-FE25587DB875).

Now we can add a new partition: Press n

The tool will then ask you to choose the partition number, I chose the default (1).

It then asks you what sector you want the partition to start and end (and how bit it will be by extension). I simply hit enter to select the default.

The tool then confirms that a new partition is created. You can type in p to Print out the device and partition information:

Command (m for help): n
Partition number (1-128, default 1): 1
First sector (2048-31266782, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-31266782, default 31266782): 

Created a new partition 1 of type 'Linux filesystem' and of size 14.9 GiB.

Command (m for help): p
Disk /dev/sda: 14.9 GiB, 16008609792 bytes, 31266816 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: gpt
Disk identifier: 99E3E866-3783-46DE-9FDA-FE25587DB875

Device     Start      End  Sectors  Size Type
/dev/sda1   2048 31266782 31264735 14.9G Linux filesystem 

We are almost done making the partition. Type in w to write the changes to disk, and hit enter. It will write the changes to disk and exit the tool. You can rescan the device with "sudo fdisk -l /dev/sda" to confirm this.

Fdisk 2.png

We now have a partition but its empty! We need to format it with a filesystem. I personally have used ext4 for many years without drama, and thats what I will use in this tutorial, but there is really nothing stopping you from using other filesystems like xfs or btrfs.

You will need invoke the ext4 filesystem creation tool and tell it to format our newly created partition (/dev/sda1)

sudo mkfs.ext4 /dev/sda1

The filesystem tool might warn you about a prior filesystem, press y and hit enter to ignore the warning and proceed with the formatting. This warning happens sometimes when you don't completely format a drive before adding a new table and partitions. It will take a moment and it will verify creation of the filesystem in the partition:

[email protected]:~$ sudo mkfs.ext4 /dev/sda1 
mke2fs 1.42.13 (17-May-2015)
/dev/sda1 contains a vfat file system
Proceed anyway? (y,n) y
Creating filesystem with 3908091 4k blocks and 977280 inodes
Filesystem UUID: 8ed85af6-dc70-42a5-ba49-c5bdda0a426e
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208 

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done  

To confirm creation of the ext4 filesystem on the partition, you can use any of the following commands:

lsblk -fs

Or

sudo parted -l

Parted 1.png

Those commands show that you now have an ext4 filesystem on the first (and only) partition of the flash drive.

Mounting the Partition/FileSystem

In order for the filesystem to be written to and read off, linux needs to "mount" the filesystem. Most Distros mount partitions automatically on boot, so if you reboot your board, you should see your new partition mounted after the next boot.

[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part /media/rock64/8ed85af6-dc70-42a5-ba49-c5bdda0a426e
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part / 

sda1 is mounted here according to its partition UUID. However if you want more granularity, you can mount the filesystem/partition manually.

Firstly, unmount the mounted FS:

<tt>

sudo umount /dev/sda1

You can also use the mountpoint as the argument to unmount it, I just find using "/dev/" easier to type or remember

sudo umount /media/rock64/8ed85af6-dc70-42a5-ba49-c5bdda0a426e

Confirm its been unmounted:

[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part 
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part /

For manually mounting and unmounting, there are two tools that can be used: mount and pmount.

pmount is a wrapper for the standard mount program and simplifies mounting and unmounting and allows a normal user to mount and unmount without root privileges.

Make sure you have internet connection and install pmount with the following command:

sudo apt install pmount

Confirm by typing in y followed by the enter key to install pmount.

After it is installed, invoke it to show current mounts and confirm that it is installed:

[email protected]:~$ pmount 
Printing mounted removable devices:
/dev/mmcblk1p7 on / type ext4 (rw,relatime)
/dev/mmcblk1p6 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-
1,shortname=mixed,errors=remount-ro)
 To get a short help, run pmount -h

Mounting using pmount is exceptionally easy, you simply invoke the command with the first argument being the partition, and the second one being the mount location. In this case, lets say we want to mount it in a directory in "/media/".

[email protected]:~$ pmount /dev/sda1 /media/USB-test

.... And thats it! The filesystem on /dev/sda1 has been mounted on /media/USB-test. I didnt have to make a "USB-test" directory or anything, pmount did everything for me.

We can confirm it's been mounted using the lsblk tool:

[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part /media/USB-test

Unmounting is done using the pumount command:

sudo pumount /dev/sda1

If you want to use the standard mount command that comes with almost all linux distributions, you'll need to do some preparation before mounting the partition.

Before you mount the partition, you will need to make sure that the mount directory exists. Create a new directory inside "/media/"

sudo mkdir /media/USB-test2 

Use "ls" to confirm creation:

[email protected]:~$ sudo mkdir /media/USB-test2
[email protected]:~$ ls /media/
pm1  rock64  USB-test2

Mount the FS to the mount point ( "-t auto" means that we will let the mount tool automatically sort out the filesystem complexities) with the Partition as the first argument and the mount point as the second argument:

[email protected]:~$ sudo mount -t auto -o rw /dev/sda1 /media/USB-test2/ 
sudo mount -t auto /dev/sda1 /media/USB-test2/ 
[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part /media/USB-test2
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part /

You can unmount the FS using the unmount tool:

[email protected]:~$ sudo umount /dev/sda1 
[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part 
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part /

That concludes the brief rundown on how to use two different tools to manage manually mounting/unmounting filesystems.

Setting appropriate directory and file permissions

We are almost done!

Since the mkfs.ext4 ran under superuser privileges, the permissions in the filesystem needs to be modified if you wish to write to it with any user other than root.

Mount the ext4 filesystem using any of the means mentioned above.

[email protected]:~$ pmount /dev/sda1 /media/USB-test3
[email protected]:~$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    1 14.9G  0 disk 
└─sda1        8:1    1 14.9G  0 part /media/USB-test3
mmcblk1     179:0    0 29.8G  0 disk 
├─mmcblk1p1 179:1    0  3.9M  0 part 
├─mmcblk1p2 179:2    0   64K  0 part 
├─mmcblk1p3 179:3    0    4M  0 part 
├─mmcblk1p4 179:4    0    4M  0 part 
├─mmcblk1p5 179:5    0    4M  0 part 
├─mmcblk1p6 179:6    0  112M  0 part /boot/efi
└─mmcblk1p7 179:7    0 29.7G  0 part /

Navigate to the mount point using cd:

[email protected]:~$ cd /media/USB-test3/

We will try making a file using the current user (rock64). Use the touch command to try to make a file with that user:

touch test

You will see that you will be unable to make a file. However if you try again using the root user, it should work, showing you need to address the permissions.

Chown 1.png

Navigate up a directory

cd ..

To give permission to the current user (rock64 in this case), you will need to use the chown command:

chown -R user:user directory/

"-R" stands for recursive, it means the mountpoint and all directories/files within it. We need root privileges to change permissions so in our case, the command would be:

sudo chown -R rock64:rock64 USB-test3/

Chown 2.png

After changing the permissions, you should be able to navigate back into the mounted filesystem and write to files.

[email protected]:/media$ cd USB-test3/
[email protected]:/media/USB-test3$ touch test
[email protected]:/media/USB-test3$ ls
lost+found  test
[email protected]:/media/USB-test3$ touch test2
[email protected]:/media/USB-test3$ ls
lost+found  test  test2

If you want to give full permissions to ANY user to that directory, you need to use the chmod command to do that:

chmod -R a+rwX directory/

We need to invoke superuser for that command, and after navigating out of the directory, in our case it would look like:

[email protected]:/media$ sudo chmod -R a+rwX USB-test3/

That concludes a brief overview about setting appropriate permissions to your filesystem, as well as concludes this overall guide.

Its absolutely not required at this level, but if you'd like to learn more about linux file permissions, I've found the following writeups quite handy:

https://www.digitalocean.com/community/t...ermissions https://www.linux.com/learn/understandin...ermissions

References