Showing posts with label Hard Drive. Show all posts
Showing posts with label Hard Drive. Show all posts

Wednesday, October 24, 2012

Partitioning and Formatting Second Hard Drive in Linux - (ext3 and ext4)

SkyHi @ Wednesday, October 24, 2012

Recently, I bought an external hard disk formatted with NTFS. Not that there is something really wrong with NTFS but I prefer using ext4.
First, I deleted the existing partition and created a new Linux partition using fdisk:
?
1
# fdisk /dev/sdb
Assuming /dev/sdb is the external hard disk. Use d to delete the partition and use n to create a new partition. 83 is the ID of the native Linux partition.
Them, I use mkfs.ext4 to format the partition with ext4:
?
1
# mkfs.ext4 /dev/sdb1
Note that mkfs.ext4 expects a partition as its argument.
Finally, I use tune2fs to adjust some parameters: // don't need this
?
1
2
# tune2fs -m 0 /dev/sdb1
# tune2fs -L bakap01 /dev/sdb1
The -m option is for adjusting the percentage of reserved blocks. The reserved blocks are used by privileged processes which is by default 5% of the hard disk size. Since I’m using the external hard disk solely as a storage, I set this to 0 so I can also use those 5% for storage. The -L option is for labeling the filesystem.



Introduction

This article presents the commands used to partition and format a second hard drive in Linux using the ext3 file system. For the purpose of this example, I installed a second hard drive in a Red Had Linux system where 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@racnode1 ~]# 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 themkfs.ext3 script:
[root@racnode1 ~]# 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.
 
You will first need to create the /db directory before mouting the disk! (e.g. mkdir /db)
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@racnode1 ~]# mount /db

[root@racnode1 ~]# 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



  237  dmesg
  238  df -h
  239  fdisk -l
  240  fdisk /dev/sdb (n -> p-> default to rest)
  247  mkfs.ext4 /dev/sdb1
  261  mount -t ext4 /dev/sdb1 /opt


REFERENCES
http://blag.borap.net/2011/08/10/formatting-an-external-hard-disk-with-ext4/
http://ashok-linux-tips.blogspot.ca/2011/05/createconvert-ext2-or-ext3-or-ext4.html
http://www.idevelopment.info/data/Unix/Linux/LINUX_PartitioningandFormattingSecondHardDrive_ext3.shtml

Monday, October 22, 2012

Hard Disk: BadCRC errors from dma_intr on bootup

SkyHi @ Monday, October 22, 2012

If dma is enabled on a controller that is not well supported, these errors can appear. ( I had it on a VIA KT266a with kernel 2.2. Upgrading to kernel 2.4 fixed it beautifully.
If you are sure that the IDE controller is supported, the drive is on its way out. You can run fsck with the badblock option turned on to mark these blocks as bad... As a rule, once these errors start, we throw the disk away(This is a high availability production environment).
If you dont mind that the disk can crash in the near future, make a backup and continue using it, it might work for a long time to come.
If the disk is under guarantee... take it back, it is not worth risking data loss if the drive can be replaced for free.
This is how you hunt for and fix badblocks.
# e2fsck -c /dev/hda1
Make sure that you have a backup, badblock scans can destroy data running with certain switches.
# man badblocks && man e2fsck (And read them carefully)
To turn of dma per drive
# hdparm -d0 /dev/hd[a-d]
To list dma settings
# hdparm -d /dev/hd[a-d]
To turn dma on
# hdparm -d1 /dev/hd[a-d]
Where hd[a-d] is hda, hdb, hdc, hdd.

REFERENCES
http://linuxgazette.net/issue76/tag/10.html

Friday, June 1, 2012

Unable to enumerate USB device

SkyHi @ Friday, June 01, 2012

Some hardware just don’t work with ehci_hcd on Karmic Koala. My memory stick from transcend refused to work no matter what I did. After plugging the device nothing happened, doing dmesg showed me the following error:
Apr 18 10:59:04 dpac-laptop kernel: [73668.388060] usb 1-2: new high speed USB device using ehci_hcd and address 5
Apr 18 10:59:04 dpac-laptop kernel: [73668.473034] hub 1-0:1.0: unable to enumerate USB device on port 2
After searching a lot, I came to a conclusion that my device doesn’t work with USB 2.0. So I disabled the ehci_hcd to make it work.
Since Karmic doesn’t use ehci_hcd as a module, modprobe -r ehci_hcd no longer works. The module is compiled into kernel. To disable it execute the following commands in terminal:
1
2
cd /sys/bus/pci/drivers/ehci_hcd
ls
You will see a file with 0000:00:xx.x format. Execute the following command:
1
sudo sh -c 'echo -n "0000:00:xx.x" > unbind'
Replace the xx.x with the numbers displayed on your file. It should disable the ehci_hcd.
I plugged in my memory stick, and it worked.
Please note that you’ll have to do all this each time you restart.
UPDATE
You can now use the following script to disable ehci_hcd. It is far more simpler since it just needs you copy pasting the commands instead of a manual action. Here it goes:
1
2
cd /sys/bus/pci/drivers/ehci_hcd/
sudo sh -c 'find ./ -name "0000:00:*" -print| sed "s/\.\///">unbind'



At some moment, my USB WLAN stick stopped working, and the log was polluted by messages like:
usb 6-1: new high speed USB device using ehci_hcd and address 62
hub 6-0:1.0: unable to enumerate USB device on port 1
hub 6-0:1.0: unable to enumerate USB device on port 1
At first, I decided that the stick had broken after years of work, but out of curiousity tried to connect it to every USB port I found. It worked. The stick worked again when plugged into the keyboard, which in term was connected to the KVM.
What is funny, I did nothing, but after a few days the stick worked again correctly in its original port. I forgot about the problem. But after yet another vacations, the problem appeared again, and again disappeared in a few days.
I have no idea why.
But I finally found a solution here: [Solved] Unable to enumerate USB device (Disabling ehci_hcd). I need to disable ehci_hcd. In my case, the commands is:
# cd /sys/bus/pci/drivers/ehci_hcd
# echo -n "0000:00:1a.7" > unbind
As a side effect, I do not have USB 2.0 on this bus anymore, but it is not a problem.

24.02.2010, update

To issue the command automatically on boot, one could edit/etc/rc.local, but in my case I need to have network loaded before. The best solution (I hope) is based on advice here:
https://bugs.launchpad.net/ubuntu/+source/linux/+bug/354832
You can disable this on boot by creating a /etc/udev/rules.d/disable-ehci.rules file containing:
ACTION=="add", SUBSYSTEM=="pci", DRIVER=="ehci_hcd", \
        RUN+="/bin/sh -c 'echo -n %k > %S%p/driver/unbind'"





REFERENCES
http://www.geekdevs.com/2010/04/solved-unable-to-enumerate-usb-device-disabling-ehci_hcd/
http://uucode.com/blog/2011/01/18/solved-unable-to-enumerate-usb-device-on-port-1/

Monday, January 16, 2012

Rebooting When Filesystem Fails

SkyHi @ Monday, January 16, 2012
Recently I found myself stuck with a system that allowed me to ssh into it but the root filesystem was read-only and all the binaries in /sbin, /bin, or /usr/bin were simply not accessible.
I attempted to run shutdown -r now and reboot without any success. I was really stuck as I did not have physical or KVM access to the server which was located in our office and no one were going to be there until 9 AM. Needless to say, I was also trying to avoid going to the office in the middle of the night as I had a early morning drive to the airport to drop off my business partner.
So I decided to explore how I could reboot the system while the disk subsystem was not responding well. I wondered if I could somehow reboot the system by setting some values in the kernel memory via /proc filesystem. My research led me to the following commands:

echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

The system rebooted immediately and after automatically running a fsck check on the filesystems, it came back as if nothing had happened, which was a very very lucky thing. It reminded myself that every now and then a little bit of good luck can be great blessing.

REFERENCES

Thursday, December 15, 2011

How should I run fsck on a Linux file system

SkyHi @ Thursday, December 15, 2011

Force fsck on boot using /forcefsck

By creating /forcefsck file you will force the Linux system (or rc scripts) to perform a full file system check.
Login as the root:
$ su -
Change directory to root (/) directory:
# cd /
Create a file called forcefsck:
# touch /forcefsckNow reboot the system:
# reboot


Scenario / Question:

I need to check file system for errors using fsck. Can I run fsck on a mounted file system ?

Solution / Answer:

Running fsck on a mounted file system can result in data corruption. The two options are:
1) Change the running state of the system to single user mode and unmount the file system
What if you need to run fsck on the root / file system ?
2) Boot the computer into Rescue Mode using the installation CD

1) Single User Mode and umount the file system

Issue command to change run level and umount the /home file system that is mounted on /dev/sda2
# init 1
# umount /home
Run fsck:
# fsck /dev/sda2

2) Rescue Mode using installation CD ( to run fsck on root /)

Insert the Installation CD into the drive and reboot your system:
# shutdown -r now
After booting from the Installation CD and presented with the installation command prompt type:
linux rescue nomount
Once you are at the system command prompt you need to run mknod. Because we started Rescue Mode with the “nomount” option, no file systems were initialized and no device files were created. If we try to run fsck on a file system it will fail. We need to use mknod to create the block or character special file.
To use mknod we need to know the Minor and Major numbers of the device.
# ls -l /dev/sda
8 0
# ls -l /dev/sda2
8 2
# mknod /dev/sda b 8 0
# mknod /dev/sda2 b 8 2
Run fsck and force the check and attempt to automatically repair:
-y — cause the fs-specific fsck to always attempt to fix any detected filesystem corruption automatically.
-f — force a check even if reported in a clean state
-v — Produce verbose output, including all file system-specific commands that are executed.
# fsck -yvf /dev/sda2
LVM Partitions
In order to be able to run fsck on lvm partitions we need to find the pv’s, vg’s, lv’s and activate them.
# lvm pvscan
# lvm vgscan
# lvm lvchange -ay /dev/VolGroup00/LogVol_home
# lvm lvscan

# fsck -yfv /dev/VolGroup00/LogVol_home
LUKS Partition
In order to be able to access an encrypted LUKS partition user cryptsetup.
cryptsetup luksOpen
– is the device path
– is the name of the unencrypted mount that can be accessed by /dev/mapper/
# cryptsetup luksOpen /dev/VolGroup00/LogVol_home home
# Ener LUKS passphrase for /dev/VolGroup00/LogVol_home
# fsck -yvf /dev/mapper/home


REFERENCES