Formatting and Mounting a Flash Drive ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 15Apr20 Everett Lipman ------------------------------------------------------------------------------- Formatting: 1. cat /proc/partitions | grep sd Take note of what you see in the output, which may be nothing. You will be looking for this to change. You can use the 'lsblk' command rather than 'cat /proc/partitions' if you prefer. 2. Insert the flash drive into a USB port on your RPi. Click on 'Cancel' if you are asked whether you want to open the File Manager. 3. cat /proc/partitions | grep sd You should now see two new lines for devices sda and sda1. If sda was present when you first did this, your flash drive will be assigned a different device name, such as sdb. Here I assume it is sda. You will see one entry for the whole drive (sda), and one for each existing partition (sda1, sda2, etc.). 4. df | grep sd If you get some output showing your flash drive's device, for example /dev/sda1 15126544 16 15126528 1% /media/pi/KINGSTON that means the RPi automatically mounted it. We want to do this manually after we have formatted the drive. If there is no output, skip the next step. 5. sudo umount /dev/sda1 Repeat this for every mounted partition on your flash drive, as listed in the output from the previous step. 6. df | grep sd There should be no output that corresponds to your flash drive this time. 7. sudo fdisk /dev/sda Be extremely careful with this command! You can wipe out your SD card or other storage if you type the wrong device name or make mistakes below. If your flash drive was not assigned the sda device, be sure to use the device it is on! 8. At the Command prompt, type 'p' (without the quotes) This should display the present partition table for the drive. fdisk does not actually change anything until you write the table to disk with the 'w' command. So you can always quit in the middle with 'q' and there will be no changes. 9. Type 'd' to delete any existing partitions, until the 'p' command returns an empty partition table. 10. Type 'n' to create a new partition, 'p' to use a primary partition, and number the new partition '1'. Choose the default First and Last sectors to use all available space. 11. Type 'a'. It should make partition 1 bootable. We will not use this now, but if you decide later to install a bootable system on this drive, it will be ready. 12. Type 'p' You should see a single partition that looks like this: Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 30277631 30275584 14.4G 83 Linux 13. Type 'w' to write the partition to disk. 14. sudo mkfs.ext4 -O ^has_journal /dev/sda1 The option is a capital letter 'O', not a zero. This creates a Linux ext4 filesystem on partition sda1 with no journaling. If you are using a traditional hard drive (not flash memory), you would leave out the '-O ^has_journal' so that the filesystem would use journaling. There are similar mkfs commands for other filesystems, for example mkfs.vfat. Other filesystems may not preserve permissions or other Linux information, however. 15. sudo fsck /dev/sda1 This checks the new filesystem, and should tell you it is clean. Your drive is now formatted, but is not yet mounted. Please note: formatting the drive wipes out all existing data, so this is typically done only once, unless you want to destroy the data and reuse the drive for something else. ------------------------------------------------------------------------------- Mounting: 1. df | grep sd You should not see any partitions listed that correspond to your flash drive (for example, /dev/sda*). 2. sudo mount /dev/sda1 /mnt 3. df | grep sd You should now see something like this /dev/sda1 14900144 36944 14089928 1% /mnt showing that partition 1 on your flash drive (the only partition on the drive) is now mounted on /mnt. This means that anything you copy to or change in /mnt will be on the flash drive until you unmount the partition. 4. sudo rsync -av /home/pi/ /mnt/pi_13Apr16 You should substitute today's date for '13Apr16'. This command backs up your home directory to the specified directory on the flash drive. Note that the trailing slash is necessary immediately following '/home/pi'. You should run backups like this often. If you now try to write a file to /mnt without using sudo, permission will be denied. 5. ls -alF /mnt You will see output that looks like this: total 28 drwxr-xr-x 4 root root 4096 Apr 12 13:27 ./ drwxr-xr-x 21 root root 4096 Dec 31 1969 ../ drwx------ 2 root root 16384 Apr 12 13:10 lost+found/ drwxr-xr-x 28 pi pi 4096 Apr 12 12:30 pi_13Apr16/ Notice that the directory ./ (which in this case corresponds to /mnt) is owned by root, and the permissions indicate that others (the rightmost r-x column) do not have write permission. 6. sudo chown pi:pi /mnt 7. ls -alF /mnt Now you should see this: total 28 drwxr-xr-x 4 pi pi 4096 Apr 12 13:27 ./ drwxr-xr-x 21 root root 4096 Dec 31 1969 ../ drwx------ 2 root root 16384 Apr 12 13:10 lost+found/ drwxr-xr-x 28 pi pi 4096 Apr 12 12:30 pi_13Apr16/ Notice that the owner and group for ./ are both pi. You should now be able to edit and create files in /mnt without using sudo. 8. When you are done using the drive, cd sudo umount /mnt Equivalently, you can run sudo umount /dev/sda1 If you get this error message: umount: /mnt: target is busy (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1).) It means that some file in /mnt is being used by a program, or some program, such as bash, has its current working directory set to /mnt. Make sure you cd out of /mnt in all of your open shell windows and try again. 9. df | grep sd There should be no longer be output that corresponds to your flash drive. 10. ls -alF /mnt You should see something like this: total 8 drwxr-xr-x 2 root root 4096 Mar 18 01:05 ./ drwxr-xr-x 21 root root 4096 Dec 31 1969 ../ indicating that the directory is empty. Once again /mnt is owned by root:root (user:group). This is because the flash drive is unmounted and /mnt once again refers to the original empty directory in the root filesystem on your sd card. If there were any files in /mnt before you mounted your flash drive, they should reappear there now. Once you confirm that your drive is no longer mounted, it is safe to remove it from the USB socket. You may ignore any "Drive was removed without ejecting" message from the RPi desktop. ------------------------------------------------------------------------------- To use the flash drive in the future: 1. Insert the flash drive into a USB port on your RPi. Click on 'Cancel' if you are asked whether you want to open the File Manager. 2. df | grep sd If your drive has been mounted automatically, for example if you see /dev/sda1 14900144 755780 13371092 6% /media/pi/ab478080-df49-47b4-8a56-c7563b13ab7c type sudo umount /dev/sda1 3. sudo mount /dev/sda1 /mnt or use whatever mount point you choose instead of /mnt. 4. When you are done, sync sudo umount /dev/sda1