How to format external HDD with shell

From SynologyWiki

Jump to: navigation, search

Overview

This article presents the commands used to partition and format a second hard drive in Linux using the ext3 file system. The drive is recognized as /dev/hdb. I want to make only one partition on this hard drive which will be /dev/hdb1.

fdisk

First, you will need to run the fdisk command in order to partition the disk. For this example, I only want to create one ext3 partition. Here is an example session:

   [root@linux2 etc]# fdisk /dev/hdb
   Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
   Building a new DOS disklabel. Changes will remain in memory only,
   until you decide to write them. After that, of course, the previous
   content won't be recoverable.
   The number of cylinders for this disk is set to 4865.
   There is nothing wrong with that, but this is larger than 1024,
   and could in certain setups cause problems with:
   1) software that runs at boot time (e.g., old versions of LILO)
   2) booting and partitioning software from other OSs
      (e.g., DOS FDISK, OS/2 FDISK)
   Command (m for help): n
   Command action
      e   extended
      p   primary partition (1-4)
   p
   Partition number (1-4): 1
       
   First cylinder (1-4865, default 1): 1
   Last cylinder or +size or +sizeM or +sizeK (1-4865, default 4865): 4865
   
   Command (m for help): t
   Partition number (1-4): 1
   
   Hex code (type L to list codes): 83
   Command (m for help): w
   The partition table has been altered!
   Calling ioctl() to re-read partition table.
   Syncing disks.

Create ext3 File System

The next step is to create an ext3 file system on the new partition. Provided with the distribution is a script named /sbin/mkfs.ext3. Here is an example session of using the mkfs.ext3 script:

   [root@linux2 root]# mkfs.ext3 -b 4096 /dev/hdb1
   mke2fs 1.27 (8-Mar-2002)
   Filesystem label=
   OS type: Linux
   Block size=4096 (log=2)
   Fragment size=4096 (log=2)
   4889248 inodes, 9769520 blocks
   488476 blocks (5.00%) reserved for the super user
   First data block=0
   299 block groups
   32768 blocks per group, 32768 fragments per group
   16352 inodes per group
   Superblock backups stored on blocks:
           32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
           4096000, 7962624
   Writing inode tables: done
   Creating journal (8192 blocks): done
   Writing superblocks and filesystem accounting information: done
   This filesystem will be automatically checked every 36 mounts or
   180 days, whichever comes first.  Use tune2fs -c or -i to override.

Mounting the File System

Now that the new hard drive is partition and formated, the last step is to mount the new drive. For this example, I will be mounting the new hard drive on the directory /db.

NOTE: You will first need to create the /db directory before mouting the disk! (e.g. mkdir /db)

What I typically do is to edit the /etc/fstab file and add an entry for the new drive. For my example, I will create the /dev/hdb1 entry as follows:

   LABEL=/                 /                       ext3    defaults        1 1
   LABEL=/boot             /boot                   ext3    defaults        1 2
   none                    /dev/pts                devpts  gid=5,mode=620  0 0
   none                    /proc                   proc    defaults        0 0
   none                    /dev/shm                tmpfs   defaults        0 0
   /dev/hdb1               /db                     ext3    defaults        1 1
   /dev/hda2               swap                    swap    defaults        0 0
   /dev/cdrom              /mnt/cdrom              iso9660 noauto,owner,kudzu,ro 0 0
   /dev/fd0                /mnt/floppy             auto    noauto,owner,kudzu 0 0

After making the entry in the /etc/fstab file, it is now just a matter of mounting the disk:

   [root@linux2 /]# mount /db
   [root@linux2 /]# df -k
   Filesystem           1K-blocks      Used Available Use% Mounted on
   /dev/hda3             37191660  11016692  24285724  32% /
   /dev/hda1               101089     12130     83740  13% /boot
   none                    515524         0    515524   0% /dev/shm
   /dev/hdb1             38464340     32828  36477608   1% /db
Personal tools