Skip to content

Instantly share code, notes, and snippets.

@wspurgin
Last active April 4, 2016 14:31
Show Gist options
  • Save wspurgin/44836be355a11703019d to your computer and use it in GitHub Desktop.
Save wspurgin/44836be355a11703019d to your computer and use it in GitHub Desktop.
How to mount a hard drive on Ubuntu (and most Linux systems)

Mounting a hard drive on Ubuntu (and most Linux distros)

Note:

If you're adding a partition with an existing file structure that you don't want to delete, skip steps 1 and 2 as the partitions will already be created. If you're mounting a partition to replace your encrypted home directory, then see the Sources tab for the Help Ubuntu link where you can find a section on how to re-encrypt your home directory files.

Create a new partition with fdisk

  • Ex: sudo fdisk /dev/sdb (creates sdb1)

Give the partition a file system with mkfs-ext4

  • Ex: sudo mkfs-ext4 /dev/sbd1
  • Go with the defaults (just hit enter for the defaults) unless you know what your doing.

Get the partition's UUID

  • sudo blkid this lists all the UUIDs of all paritions
    • Look for the parition you just made (or added) and copy it's UUID (or leave this terminal window up)

Backup the /etc/fstab file (where we specify our filesystem paritions)

  • sudo cp /etc/fstab /etc/fstab.$(date +%Y-%m-%d) this creates the backup of your fstab file with todays date
    • You do not want to f-up this file

Edit the /etc/fstab file

  • gksu gedit /etc/fstab this prompts the gedit text editor to open the fstab file, but you can choose whichever editor you like
  • Add a new line for your parition (seen below)
  • You'll replace the ?s with the UUID you found with blkid. You'll have to specify a directory for mounting.
  • If you are using this partition to replace an existing directory, assign a temporary directory as the mounting location (which you'll make in the next step)
UUID=???????? /path/to/directory/for/mounting   ext4  defaults  0 2

Create the directory of the partition if it doesn't already exist.

  • Ex: [sudo] mkdir /media/home

Now mount the partition to the directory.

  • mount -a

Setting up the partition to replace an existing directory

Copy the files from an existing directory over to the partition's directory with rsync

  • [sudo] rsync -av <source> <destination>
  • Ex: sudo rsync -av /home/. /media/home/.
    • Specifying the . is important, otherwise you'll copy a new directory home inside /media/home/ (i.e. you'll make /media/home/home)

After you have the partition set up, you can replace the new partition's mounting location with the existing directory location

  • Ex: UUID=???????? /home ext4 defaults 0 2

Important to be safe, you should back up the existing directory before remounting the new partition to overwrite it.

  • Ex: [sudo] mv /home /old-home && [sudo] mkdir /home

Mount the new partition to replace the existing directory

  • mount -a

diff the partition and the old dir

  • sudo diff -r /home /old-home
  • You can optionally add the -x ".gvfs/*" to avoid warnings when diffing files in your home directory.

rm the old dir if the new partition has the correct files

  • rm -rf /old-home

And you're done!

Sources

Help Ubuntu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment