Beginners Guide: Adding USB Storage, Linux Formatting and Permissions
#1
Information 
In this guide, I will be going through how to add USB storage to your Board: 

Confirm that the drive is recognized by Linux 

Formatting the drive for Linux use 

Mounting the Partition/FS

Setting appropriate directory and file permissions. 





Prerequisites: 

Rock64 running Debian/Ubuntu 

Local access or SSH access

USB Drive  

I would highly suggest reading through this to understand the basics of using the Linux terminal: 

https://www.howtogeek.com/140679/beginne...-terminal/



Confirm that the drive is recognized by Linux 




The first step is pretty obvious, plug in the USB drive into your Board: 

[Image: CB3Uoyp.jpg]

I know my ethernet cable termination is horrendous, I'm getting it fixed soon. 

Hopefully by now you'll have some idea how to SSH into your device. SSH into the device or open up a terminal. 


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: 

Code:
lsblk

Running it on your board should show something similar to the following: 
Code:
rock64@rock64:~$ 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 /

[Image: 9yATeZM.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: 
Code:
sudo fdisk -l /dev/sda

Code:
rock64@rock64:~$ 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 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. 




You dont need to list your block device as an argument, it just makes the output less cluttered. If you simply used "fdisk -l" it would print out information about all recognized devices: 
[Image: N6wkkua.png]
  Reply
#2
Question 
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!


Invoke the fdisk command with the drive (sda) as an argument:

Code:
sudo fdisk /dev/sda



Type in "m" and press enter to get an extended menu. 


[Image: uCqBg1I.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" 


Code:
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: 


Code:
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. 

[Image: R32YT5D.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)


Code:
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: 
Code:
rock64@rock64:~$ 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: 

Code:
lsblk -fs
or 

Code:
sudo parted -l
[Image: DVZZA8p.png]


This shows that you now have a ext4 filesystem on the first (and only) partition of the flash drive.

  Reply
#3
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. 

Code:
rock64@rock64:~$ 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 /


it 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: 


Code:
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

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

Confirm its been unmounted: 

Code:
rock64@rock64:~$ 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: 

Code:
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: 

Code:
rock64@rock64:~$ 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/ . 


Code:
rock64@rock64:~$ 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: 

Code:
rock64@rock64:~$ 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: 

Code:
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/ 


Code:
sudo mkdir /media/USB-test2 


Use "ls" to confirm creation: 


Code:
rock64@rock64:~$ sudo mkdir /media/USB-test2
rock64@rock64:~$ 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: 


Code:
rock64@rock64:~$ sudo mount -t auto -o rw /dev/sda1 /media/USB-test2/ 
sudo mount -t auto /dev/sda1 /media/USB-test2/ 
rock64@rock64:~$ 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: 


Code:
rock64@rock64:~$ sudo umount /dev/sda1
rock64@rock64:~$ 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 /



Mounting using fstab will be covered in another guide, later on, I will link it back here.



  Reply
#4
Information 
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. 

Code:
Code:
rock64@rock64:~$ pmount /dev/sda1 /media/USB-test3
rock64@rock64:~$ 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: 

Code:
Code:
rock64@rock64:~$ 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: 

Code:
Code:
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. 

[Image: Rciuk5h.png]

Navigate up a directory 

Code:
Code:
cd ..

To give permission to the current user (rock64 in this case), you will need to use the chown command: 
Code:
Code:
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: 
Code:
Code:
sudo chown -R rock64:rock64 USB-test3/


[Image: 6VXZVae.png]
After changing the permissions, you should be able to navigate back into the mounted filesystem and write to files. 
Code:
Code:
rock64@rock64:/media$ cd USB-test3/
rock64@rock64:/media/USB-test3$ touch test
rock64@rock64:/media/USB-test3$ ls
lost+found  test
rock64@rock64:/media/USB-test3$ touch test2
rock64@rock64:/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: 
Code:
Code:
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: 
Code:
Code:
rock64@rock64:/media$ sudo chmod -R a+rwX USB-test3/


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
  Reply
#5
Thanks for Guide. It's very well written.

I have a question - how to use pmount to mount USB hard drive automatically after boot?

********* This is after boot *********
root@rock64:~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda          8:0    0 223.6G  0 disk
└─sda1        8:1    0 223.6G  0 part
mtdblock0    31:0    0    16M  0 disk
mmcblk0    179:0    0  7.4G  0 disk
└─mmcblk0p1 179:1    0  7.2G  0 part /
zram0      252:0    0  489M  0 disk [SWAP]
zram1      252:1    0    50M  0 disk /var/log
*********************************************

Now, after pmount command - folder is mounted & I can access folder from network using samba
*************************
root@rock64:~# pmount /dev/sda1 /media/USB3HDD
root@rock64:~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda          8:0    0 223.6G  0 disk
└─sda1        8:1    0 223.6G  0 part /media/USB3HDD
mtdblock0    31:0    0    16M  0 disk
mmcblk0    179:0    0  7.4G  0 disk
└─mmcblk0p1 179:1    0  7.2G  0 part /
zram0      252:0    0  489M  0 disk [SWAP]
zram1      252:1    0    50M  0 disk /var/log
******************************************

So, the question is How to mount USB3HDD mounting point every time it boots?

Thanks in advance.
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginners Guide: Migrating to SSD rontant 18 39,667 01-19-2022, 09:30 AM
Last Post: Ellesar Dragon
  Step by step guide PXE diskless configuration. burglar_ot 13 35,639 11-01-2020, 11:26 PM
Last Post: michael.gruner
Thumbs Up A guide for how I made RetroPie, RetroArch, and EmulationStation Work on the Rock64 Mrfixit2001 4 16,029 12-17-2018, 03:52 AM
Last Post: va88
  Guide - XRDP - Debian Stretch / Ubuntu Xenial / OMV S3phi40T 3 13,962 05-05-2018, 06:08 AM
Last Post: S3phi40T
Information Guide - Raid Array (Raid 0) Ptheven 0 4,810 10-07-2017, 09:22 AM
Last Post: Ptheven
Information Guide - Setting up a NFS Share Ptheven 0 5,463 09-26-2017, 04:44 AM
Last Post: Ptheven
  Guide - Setting up a SMB(Windows) file server Ptheven 0 12,571 08-21-2017, 08:54 AM
Last Post: Ptheven
Information Beginners Guide: Locating your board, connecting to it. Ptheven 5 15,847 08-21-2017, 04:12 AM
Last Post: Ptheven

Forum Jump:


Users browsing this thread: 1 Guest(s)