Beginners Guide: Adding USB Storage, Linux Formatting and Permissions - Printable Version +- PINE64 (https://forum.pine64.org) +-- Forum: ROCK64 (https://forum.pine64.org/forumdisplay.php?fid=85) +--- Forum: Rock64 Tutorials (https://forum.pine64.org/forumdisplay.php?fid=90) +--- Thread: Beginners Guide: Adding USB Storage, Linux Formatting and Permissions (/showthread.php?tid=4925) |
Beginners Guide: Adding USB Storage, Linux Formatting and Permissions - Ptheven - 08-15-2017 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/beginner-geek-how-to-start-using-the-linux-terminal/ Confirm that the drive is recognized by Linux The first step is pretty obvious, plug in the USB drive into your Board: 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 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 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: Formatting the drive for Linux use - Ptheven - 08-15-2017 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. 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 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 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. 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 To confirm creation of the ext4 filesystem on the partition, you can use any of the following commands: Code: lsblk -fs Code: sudo parted -l This shows that you now have a ext4 filesystem on the first (and only) partition of the flash drive. Mounting the Partition/Filesystem - Ptheven - 08-15-2017 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 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 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 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 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 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/ You can unmount the FS using the unmount tool: Code: rock64@rock64:~$ sudo umount /dev/sda1 Mounting using fstab will be covered in another guide, later on, I will link it back here. Setting appropriate directory and file permissions - Ptheven - 08-16-2017 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 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. 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/ 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/ 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 RE: Beginners Guide: Adding USB Storage, Linux Formatting and Permissions - helpmerock - 03-06-2021 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. |