Linux HDD

From Christoph's Personal Wiki
Jump to: navigation, search

Adding an external USB hard drive (HDD)

Note: This example will assume the HDD is SATA and uses USB to connect with the computer.

A lot of external HDDs are sold pre-formatted and usually as NTFS (for Windows). If you are like me, you will want this HDD formatted for Linux.

  • Step 1: Getting Linux to discover the newly connected external HDD

After connecting the HDD to the computer via USB, Linux may not always automatically recognise it. The easiest way to manually configure this new HDD is to turn it on and then:

$ tail /var/log/messages

One of the lines should have something like the following:

Feb 19 01:04:49 linux: Attached scsi disk sda at scsi6, channel 0, id 0, lun 0

The "sda" is the part we are interested in. This means your new HDD is going to use /dev/sda as the mount point.

Another and simpler method (that doesn't always work) is to execute one of the following commands:

$ grep -Ff <(hwinfo --disk --short) <(hwinfo --usb --short)
$ udevadm monitor --udev
$ dmesg -T|grep -i usb
$ lsinput # part of the "input-utils" package
  • Step 2: Create the new partition(s)

Note: If your drive arrives pre-formatted (e.g., as HPFS/NTFS), you will first need to delete the existing partition(s).

Only do the following if there is a pre-existing partition you wish to delete (note: fdisk may need to be run from /sbin/fdisk):

$ fdisk /dev/sda
d   # delete a partition

The easiest way to create a partition is to use fdisk (note: Important! You must exercise caution here and only partition un-mounted drives). As root, enter the following

$ fdisk /dev/sda
p   # display a list of current partitions
n   # create a new partition (create a "primary partition")
    # external HDD, so one big partition should suffice
w   # write the partition table

Then quit fdisk.

  • Step 3: Format as a Linux filesystem

I recommend using the ext3 filesystem. Note the "/dev/sda1" (not "/dev/sda"). To format (or "make") this, enter the following (as root; note: mkfs.ext3 may need to be run from /sbin/mkfs.ext3):

mkfs.ext3 -v /dev/sda1  # the 'v' is for "verbose" and shows you its progress.

This step will take a while, depending on how large your external HDD is and other factors.

  • Step 4: Create a mount point

It is customary to use the /mnt subdirectory for external mount points. Create a sub-directory here and call it whatever you like (I will call mine "xhdd"):

$ mkdir /mnt/xhdd

You may want to issue the the following command to grant other users access to the mount point:

$ chmod go+x /mnt/xhdd

Now manually mount the drive and test it with:

$ mount -t ext3 /dev/sda1 /mnt/xhdd
  • Step 5: Add new external HDD to fstab

In order for your external HDD to be automatically mounted every time you turn on your machine, you must add the appropriate line to your /etc/fstab file. Something like the following should work for most uses:

/dev/sda1   /mnt/xhdd   ext3   defaults   0 0

To test that the above works, first un-mount the HDD (as root):

$ umount /dev/sda1

Then mount it using the information in your /etc/fstab file:

$ mount -a

This mounts all filesystems (of the given types) mentioned in fstab (unless they are already mounted).

That's it!

Adding an external eSATA hard drive (HDD)

Note: In this example, I will be mounting a 400 GB external HDD (xHDD) using an eSATA port on my 64bit PCI-X bus host adapter card which use the Silicon Image SATA II - 3Gbs SiI3214 chipset. Also note that this card has four eSATA ports.

First, check that your PCI-X card has been recognized:

lspci | grep Silicon

The chipset on my SATA-II card requires the kernel module sata_sil24. Simply load this module (assuming your kernel is 2.6.x+):

modprobe sata_sil24

then,

lsmod | grep sata

to make sure it loaded.

After that, you should be able to mount your xHDD with something like the following:

mount /dev/sdc1 /mnt/xhdd

Note: you might need to tell it which filesystem your xHDD was formatted as (e.g. ext3). However, newer kernels are fairly good at auto-detecting this.

That should do it.

Also note that you can have the module, sata_sil24, automatically loaded at boot time. Simply add the following line to your /etc/modprobe.preload file (it is called /etc/modules on older kernels):

sata_sil24

Finally, if you wish to add a second xHDD (or third or fourth, to use all available ports), you might need to first umount the devices (i.e., umount /dev/sdc1, or whatever it was first mounted as) then,

rmmod sata_sil24     # to first remove the module
modprobe sata_sil24  # then reload this same module

That should automatically detect the second, connected xHDD, add you are set.

Make bootable USB stick

For this example, I will be creating a bootable USB flash drive from an Ubuntu Desktop ISO. I will assume this ISO has already been downloaded to your local machine.

  • Mount the Ubunto ISO:
$ sudo mkdir /mnt/iso/
$ sudo mount -o loop /path/to/ubuntu.iso /mnt/iso
  • Insert your USB flash drive into your machine and note the mount path (e.g., /media/usb0)
$ mount
  • Copy all of the files from /mnt/iso to your mounted USB flash drive (note the dot):
$ cp -a /mnt/iso/. /media/usb0
  • Install the following packages and copy the ldlinux.sys file to your USB flash drive:
$ sudo apt-get install syslinux mtools
$ lsblk # node the device for your USB flash drive
$ sudo syslinux -s /dev/sdb1
  • Make a new directory on your USB flash drive called syslinux and move all of the files from isolinux to this new directory:
$ sudo mkdir -p /media/usb0/syslinux
$ sudo mv /media/usb0/isolinux /media/usb0/syslinux
$ sudo rmdir /media/usb0/isolinux
$ sudo mv /media/usb0/syslinux/isolinux.cfg /media/usb0/syslinux/syslinux.cfg
  • Reboot your machine and set the boot order in BIOS to boot from the USB flash drive first

Miscellaneous HDD commands

  • Find unallocated free space on a HDD:
parted /dev/sda unit TB print free | grep 'Free Space' | tail -n1 | awk '{print $3}' # in TB
parted /dev/sda unit GB print free | grep 'Free Space' | tail -n1 | awk '{print $3}' # in GB
parted /dev/sda unit MB print free | grep 'Free Space' | tail -n1 | awk '{print $3}' # in MB
parted /dev/sda unit B print free | grep 'Free Space' | tail -n1 | awk '{print $3}' # in bytes
parted /dev/sda unit '%' print free | grep 'Free Space' | tail -n1 | awk '{print $3}'
parted /dev/sda unit s print free | grep 'Free Space' | tail -n1 | awk '{print $3}' # free sectors

External links