- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
28 December 2009
This mini-tutorial goes through the steps needed to add a USB external disk drive as a permanent disk on a Linux computer.
This is something that I did recently on my home server computer. It already has two internal disk drives, but due to the size of my photo and video collection needed some more storage. I therefore added a 250GB external disk drive that I had available and moved my photo files onto that disk instead.
These steps were all performed on Ubuntu 9.10 Karmic Koala Linux distribution. These are all standard Linux commands so should work whatever your distribution. If you distribution does not have cfdisk by default then you can use fdisk, but it is much easier to use cfdisk if that is available.
Trust me the backup is really important. I know from personal experience. Even when you know what you are doing it is easy to make a mistake and lose data. You have been warned.
Note that during this tutorial I have removed some superfluous entries from the command output to shorten this and to only show the important information.
The following commands show the state of the system before the install. The internal disks have drive letters reflecting their install on the internal IDE system. Drive sda is the first drive and sdb is the second. The root file system / is on disk sda5, this is the first logical partition as when originally installed the first partition was used by Windows. Windows has since been removed and I now have a physical partition spare on the first hard disk, but that is no big enough for the amount of storage I need. For the purposes of this sda1 is ignored.
Note that I've already moved /data/music onto the second internal drive, so that is under /dev/sdb1 and the home directory is under /dev/sdb1. It's not quite so easy with an external drive as the drive name can change so we are going to use the UUID when we actually configure the hard disk drive to mount in fstab, this is the way that Ubuntu configures the internal drives as well which makes sense as it means that on a server with swappable disk drives it may mean that the dev letters change, but the UUID is always unique to the disk.
We can see the existing files with the mount command.
$ mount /dev/sda5 on / type ext3 (rw,relatime,errors=remount-ro) /dev/sdb1 on /data/music type ext3 (rw,relatime) /dev/sdb2 on /home type ext3 (rw,relatime)
We can see the disk space in use using the df command.
$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda5 19307400 15075600 3251036 83% / /dev/sdb1 38448276 25272824 11222352 70% /data/music /dev/sdb2 40330060 33747364 4534008 89% /home
When we connect the external device it is detected and mounted. The additional entry on the mount command is shown below:
/dev/sdc1 on /media/6048-2458 type vfat (rw,nosuid,nodev,uhelper=devkit,uid=1000,gid=1000,shortname=mixed,dmask=0077,utf8=1,flush)
Note that in this case we are going to completely remove the data on this drive and replace it with the ext3 filesystem. The ext3 filesystem type will have better support for file permissions, but if there is a requirement to access the disk from another operating system (such as Windows) then vfat may be a better choice.
First unmount the disk - close any file browser window that may have automatically opened and then
sudo umount /dev/sdc1
This could also be done using /media/6048-2458, but I will be using the sdc1 later on. Make sure that you use the correct device name as listed in the mount command.
Now edit the partition of the existing disk using cfdisk. This is not needed if you are keepin the same partition type, but in this case we are changing from vfat to ext3.
sudo cfdisk /dev/sdc
Note that this used /dev/sdc. I have dropped the last digit which refers to the partition - sdc refers to the entire disk.
cfdisk (util-linux-ng 2.16) Disk Drive: /dev/sdc Size: 250059350016 bytes, 250.0 GB Heads: 255 Sectors per Track: 63 Cylinders: 30401 Name Flags Part Type FS Type [Label] Size (MB) ------------------------------------------------------------------------------ sdc1 Boot Primary W95 FAT32 250056.74 [ Bootable ] [ Delete ] [ Help ] [ Maximize ] [ Print ] [ Quit ] [ Type ] [ Units ] [ Write ] Toggle bootable flag of the current partition
At the moment it shows a single FAT32 partition. This can be deleted and then a new file system created using the new command. I selected primary which is fine if you only want a single partition (or even up to 4). The default partition size is the entire disk so accept that.
cfdisk (util-linux-ng 2.16) Disk Drive: /dev/sdc Size: 250059350016 bytes, 250.0 GB Heads: 255 Sectors per Track: 63 Cylinders: 30401 Name Flags Part Type FS Type [Label] Size (MB) ------------------------------------------------------------------------------ sdc1 Primary Linux 250056.74 [ Bootable ] [ Delete ] [ Help ] [ Maximize ] [ Print ] [ Quit ] [ Type ] [ Units ] [ Write ] Toggle bootable flag of the current partition
As you can see this shows as file type Linux (which is what we want for ext 3) and it has been given the name sdc1.
Then choose write to update the disk. It will ask for confirmation before you lose all the data that is on the disk.
This has not actually formatted the disk it has just set the partition to the correct type.
Now format the disk with the mkfs command. The easiest way is
sudo mkfs.ext3 /dev/sdc1
It will take a short while to create the file system then it should finish with the following (or something similar).
Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Next create a directory where the disk will be mounted (known as a mount point). Note that unlike some other operating systems the disk does not get a letter, but instead can be integrated anywhere into the filesystem. In this case I already have a data directory and I am creating the folder /data/photos (previous contents have already been moved elsewhere).
sudo mkdir /data/photos
Linux uses the concept of file and group owner for files and directories. If you are not familiar with this then read Linux Tutorial on file access permissions, before proceeding.
If the disk you were mounting was fat32 then this is where you set permissions for the entire drive, but for a linux filesystem you need to change the permissions once mounted, although there is no hard in changing permissions of the mount point to the normal user that owns the file system.
sudo chown stewart:stewart /data/photos
You could now mount the filesystem using the mount command, but my preferred way is to edit /etc/fstab as this is where we will be permanently mounting it from and we can test the configuration of the fstab this way.
Before we update the fstab we need to identify the UUID for the disk. As previously mentioned we cannot guarantee it will always be called /dev/sdc1 and if the wrong disk was mounted in the wrong place it could have undesired results.
The UUID can be found using the blkid command.
/dev/sdc1: UUID="a3fc2846-9ed3-4a38-9f79-35a7cb57b2c1" SEC_TYPE="ext2" TYPE="ext3"
Note that the SEC_TYPE shows ext3 and the TYPE shows ext3. This is due to the way that ext3 is just a journalled ext2, but you can ignore that for now. As long as the device name matches you can just copy the UUID.
The UUID is now entered into the fstab file
sudo vi /etc/fstab
UUID=a3fc2846-9ed3-4a38-9f79-35a7cb57b2c1 /data/photos ext3 defaults,relatime 0 0
The disk can now be mounted using
sudo mount -a
The -a option means that it will attempt to mount all filesystems listed in fstab. You can check that this was mounted correctly with the mount command without any options.
/dev/sdc1 on /data/photos type ext3 (rw,relatime)
Note that although we used the UUID in the fstab file the mount command show it as /dev/sdc1.
You can also check that the disk that is mounted has the correct space etc using the df command (I've used the -m option to show it in MB).
Filesystem 1M-blocks Used Available Use% Mounted on /dev/sdc1 234730 188 222619 1% /data/photos
The only file / directory that should show in the /data/photos directory is the Lost+Found directory which is an indication that there is a disk mounted at this point.
drwx------ 2 root root 16384 2009-12-28 15:30 lost+found
The directory file permissions should now be set as appropriate
sudo chown stewart:stewart /data/photos/
At this point it is done. The disk is mounted and it is setup to mount back to the same point when the system is next rebooted. It may however be a good idea to reboot the system at this point. Although this is not neccessary it will act as an additional check to see if you've made any mistakes.
sudo shutdown -r now
Assuming all is well from the reboot you can now use this as a part of the normal system. Use either your preferred file manager or the cp command to copy your data onto the new drive and then use as normal.