Tuesday, July 11, 2017

Formatting a USB Drive with ext4 for Use with Linux

I wish to use an external USB drive exclusively on a Linux system (in my case, Debian).  I want to use the ext4 filesystem. Note that this method wipes out the filesystem on the drive. 
Install hotplug-type package such that upon plugging the USB drive in the device is attached to somewhere in the system. I use KDE and it automatically prepares such an environment.  When a USB drive is plugged in, I should see a message like this one in the system log:
$ sudo tail /var/log/messages
Dec 16 02:07:51 kernel: [ 3000.544361] scsi11 : usb-storage 3-2:1.0
...
Dec 16 02:07:56 mochi kernel: [ 3005.882879]  sdb: sdb1
...
or I may see something like this instead (I’m using USB 3.0 adapter now):
$ sudo tail -n 100 /var/log/messages | grep sd
...
May  3 12:38:33 mochi kernel: [   12.773057] sd 6:0:0:0: [sdb] Spinning up disk...
May  3 12:38:39 mochi kernel: [   18.782284] sd 6:0:0:0: [sdb] 3906963456 512-byte logical blocks: (2.00 TB/1.81 TiB)
May  3 12:38:39 mochi kernel: [   18.783052] sd 6:0:0:0: [sdb] Write Protect is off
May  3 12:38:39 mochi kernel: [   18.794337]  sdb: sdb1
May  3 12:38:39 mochi kernel: [   18.795569] sd 6:0:0:0: [sdb] Attached SCSI disk
Here, I find the device is at /dev/sdb and there is only one partition, /dev/sdb1, for example. I can also see all the disk devices as follows:
$ sudo fdisk -l
Disk /dev/sda: 80.0 GB, 80026361856 bytes
240 heads, 63 sectors/track, 10337 cylinders
Units = cylinders of 15120 * 512 = 7741440 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xbd3cc0bb
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       10338    78149633    5  Extended
/dev/sda5               1          46      340992   83  Linux
/dev/sda6              46          52       48128   83  Linux
/dev/sda7              52         698     4881408   83  Linux
/dev/sda8             698        1086     2928640   83  Linux
/dev/sda9            1086        1861     5858304   83  Linux
/dev/sda10           1861        2830     7323648   83  Linux
/dev/sda11           9305       10338     7812096   82  Linux swap / Solaris
/dev/sda12           2830        9304    48949248   83  Linux
Partition table entries are not in disk order
Disk /dev/sdb: 2000.4 GB, 2000365289472 bytes
255 heads, 63 sectors/track, 243197 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005f107
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      243198  1953480704    7  HPFS/NTFS
When the USB drive is new, the partition is most likely formatted in FAT or NTFS unless the drive is preformatted specifically for OS X.  Now, prepare the partition for Linux:
$ sudo fdisk /dev/sdb
See help for detail by pressing “m” on the fdisk command shell.  For example, press “p” to see the list of partitions in the disk.  To create a new one press “n” (probably after deleting one or more partitions by pressing “d”).  Finally press “w” to actually make changes to the drive.  To use ext4 filesystem, do:
$ sudo mkfs.ext4 /dev/sdb1
after exiting fdisk.
If I always mount this drive on boot, add a line like this to /etc/fstab:
/dev/sdc1 /media/usbdrive ext4 defaults 0 0
assuming /media/usbdrive is the mount point.  If I use KDE, adding an entry to /etc/fstabis no longer necessary; I can easily manage external drives with Device Notifier.

No comments:

Post a Comment

  How to Change Instance Type & Security Group of EC2 in AWS By David Taylor Updated April 29, 2023 EC2 stands for Elastic Compute Cloud...