Showing posts with label hard disk. Show all posts
Showing posts with label hard disk. Show all posts

Monday, November 14, 2022

Creating Partition Using cfdisk

Manipulating and partitioning hardisk can be a daunting task, especially for new sysadmin and if the disk has already contain data. Luckily, there is a tool that make this task easier, and that tool is called cfdisk.


First, we need to identify our disk name. This can be achieved by running lsblk
$ lsblk











Above is a sample output of lsblk. We can see that our disk name is sda (append /dev/ to the name for full path), and it has 130GB size. We can also see that only around 30GB has been used. We should have 100GB worth of free space for our new partition.

To start using cfdisk, just run it against the path of our disk, in this case /dev/sda
$ sudo cfdisk /dev/sda

We should be able to see this interface




















Move the cursor using the down arrow key on our keyboard, so that the cursor lies on the 107374.19MB line




















Make sure your cursor is at "New" button, and press Enter.

Choose "Primary" and press Enter.





















We want to use the whole free space, so just leave the size as it is, and press Enter.




















We now have a partition ready. To actually write the partition to the partition table, move your cursor using the right arrow key on the keyboard until it reached the "Write" button, and press Enter.




We will get  a warning message. Type "yes" and press Enter to confirm this action.



We will get this message once the partitioning is complete.



Quit cfdisk by choosing "Quit" and press Enter





















Eventhough we have created the partition, but sometimes the partition table is not automatically updated. Run "partprobe" to inform the OS of the partition table change.
$ sudo partprobe


Use "lsblk" to see that the new partition has been created
$ lsblk







Thursday, October 6, 2011

Extending LVM logical volume

To extend the lv (logical volume), the command is lvextend. Please check beforehand that you have free disk space to be extended to.

Check which volume group the partition belongs to:

# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.0T 6.0T 20K 100% /var/lib/hwbackup/db
...

# lvdisplay vg01
--- Logical volume ---
LV Name /dev/vg01/db
VG Name vg01
LV UUID 2LvWW6-5TIG-ovrl-GUJt-1y3N-M6ME-3LgtUY
LV Write Access read/write
LV Status available
# open 1
LV Size 5.99 TB
Current LE 1569792
Segments 5
Allocation inherit
Read ahead sectors auto
- currently set to 4096
Block device 253:2

So, our lv belongs to vg01

Now let's check how much space we have for vg01:

# vgdisplay vg01
...
Alloc PE / Size 4059648 / 15.49 TB
Free PE / Size 232788 / 909.33 GB
VG UUID iaFnIb-hKjs-q3fQ-yuTy-tKKl-uqSp-EXGyHF
...

So we know that it got 909.33 GB unused space.

Lets do the extension, let's say we want to extend by 300GB. Run a test run first:

# lvextend -t -v -L +300G /dev/mapper/vg01-db
Test mode: Metadata will NOT be updated.
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Test mode: Skipping archiving of volume group.
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Found volume group "vg01"
Test mode: Skipping volume group backup.
Logical volume db successfully resized
Test mode: Wiping internal cache
Wiping internal VG cache
[root@hitw-gc-backup-1 ~]# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized
where -t is for test mode, -v for verbose and -L is for the size that you want to extend

If we are OK with the test, proceed to the real extension by removing -t:
# lvextend -v -L +300G /dev/mapper/vg01-db
Finding volume group vg01
Using stripesize of last segment 512.00 KB
Archiving volume group "vg01" metadata (seqno 19).
Extending logical volume db to 6.28 TB
Found volume group "vg01"
Found volume group "vg01"
Loading vg01-db table (253:2)
Suspending vg01-db (253:2) with device flush
Found volume group "vg01"
Resuming vg01-db (253:2)
Creating volume group backup "/etc/lvm/backup/vg01" (seqno 20).
Logical volume db successfully resized

Check your new lv size, it is now being extended by 300GB:
# lvdisplay /dev/mapper/vg01-db
...
LV Size 6.28 TB
...

To make your filesystem aware of the extension, run resize2fs (for ext3 filesystem). Please unmount the partition beofre you run resize2fs
# umount /dev/mapper/vg01-db
# resize2fs /dev/mapper/vg01-db

# mount /dev/mapper/vg01-db /var/lib/hwbackup/db
For xfs, you can resize it on the fly without unmounting it:
# xfs_growfs /var/lib/hwbackup/db
Now you can enjoy your newly extended lv:
# df -lh
Filesystem Size Used Avail Use% Mounted on
...
/dev/mapper/vg01-db 6.3T 5.7T 608G 91% /var/lib/hwbackup/db
...

Friday, June 11, 2010

Checking hardisk information on linux

To check hardisk information such as model and serial number, there are a few useful commands like hdparm, sdparm and fdisk.

To check the information about first hardisk for PATA and SATA hardisk:

# hdparm -i /dev/sda

For SCSI disk, a different command is to be used:
# sdparm /dev/sda

To check the hardisk capacity and partitions contained in the hardisk, fdisk can be used:
# fdisk -lu /dev/sda

If your hardisk is smart compatible, you can refer here on how to install and use smart to find information and monitor the health of your hardisk.

Tuesday, September 29, 2009

Restore GRUB

GRUB is a boot loader commonly used with linux operating system. It can be used to managed dual boot environment where linux and windows can coexist easily in a same machine without problem provided you install the windows OS first so that when you install linux, GRUB will overwrite Windows boot loader and automatically detect and manage both operating system the next time you boot your computer. Problems will happen if you alter your partitions outside the knowledge of GRUB, for example, you create new partition in your hard drive using windows. This will cause GRUB to automatically go into GRUB shell when boot. To restore back your GRUB is very simple, just follow easy steps below:

1. find in which partition does GRUB store its configuration file, which is your /boot partition. (hd0,2) means third partition of the first hard drive
grub> find /boot/grub/stage1
(hd0,2)

2. set the root for GRUB to be (hd0,2)
grub> root (hd0,2)

3. write GRUB to the Master boot record(MBR) of your hard drive. Change (hd0) to (hd0,2) to write GRUB to your /boot partition instead
grub> setup (hd0)

4. Reboot machine
grub> reboot

All those steps can also be used using livecd, if let say the grub shell did not come out but you cannot boot your machine or you cannot boot your linux due to messed up GRUB. just boot from livecd, open a terminal, and type "grub" as a superuser to go to GRUB shell

Friday, July 24, 2009

Labeling linux partition

In linux, to label a partition, there are 3 tools that can be used. The tools are e2label, tune2fs and mke2fs.

To use e2label to label the second partition of the first hardisk with label DATA:
# e2label /dev/sda2 DATA

To use tune2fs to do the similar job as above:
# tune2fs -L DATA /dev/sda2

The third tool, mke2fs is actually a tool to build ext2/ext3 filesystem. So, if you want to build the partition's filesystem as ext2/ext3 and at the same time label it, this command can be used. Be careful though, because it will delete all existing data on that particular partition
# mke2fs -L DATA /dev/sda2

To view the label that you have set, there are 3 ways which are using e2label, blkid and viewing /dev/disk/by-label.

To check using e2label:
# e2label /dev/sda2
DATA

blkid tool is even more useful, because it can list out all the partitions that you have in the machine together with their labels,uuid and filesystem type:
# blkid
/dev/sda1: LABEL="/" UUID="1CC08F13C08EF276" TYPE="ext3"
/dev/sda2: LABEL="DATA" UUID="2063f830-fe5d-438e-b727-571b313cb89e" TYPE="ext3"
/dev/sda3: TYPE="swap" LABEL="SWAP" UUID="3e266b53-42e0-4f09-8fe3-d1cf79cb5d37"

To view the /dev/disk/by-label
# ls -l /dev/disk/by-label
total 0
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 / -> ../../sda1
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 DATA -> ../../sda2
lrwxrwxrwx 1 root root 10 2009-07-24 05:38 SWAP -> ../../sda3

Note that the label will stay with the partition although the disk is moved to another computer.

To use it in /etc/fstab:
LABEL=/ / ext3 defaults 1 1
LABEL=DATA /DATA ext3 defaults 1 2
LABEL=SWAP swap swap defaults 0 0

Thursday, February 5, 2009

Unmounting busy device

You have an external drive attached to your linux machine. Then after finishing all your job, you try to unmount it, a message come out saying the device is busy.

# umount /media/disk
umount: /media/disk: device is busy
umount: /media/disk: device is busy

So what could possibly the cause?

1. You are inside the disk. Check your working directory using pwd

# pwd
/media/disk

2. Some files are accessing the disk. Check list of open files using lsof

# lsof | grep /media/disk

3. Some processes areaccessing the disk. Use fuser to check

# fuser -m /media/disk

What to do?

1. For case 1, just go to another directory

# cd
# umount /media/disk

2. For case 2, check the files that are accessing the disk and kill it

# lsof | grep "/media/disk"
vim 2693 pingu cwd DIR 8,4 4096 73729 /media/disk
# kill -9 2693
# umount /media/disk

3. For case 3, find the process that accessing the disk and kill it

# fuser -m /media/disk
/media/disk: 2693

# ps -e | grep 2693
2693 pts/0 00:00:00 vim
# kill -9 2693
# umount /media/disk

4. For case no 3 also, you can use fuser -k to kill the process that bugging the disk directly (Thanx to mr. linuxmalaysia for the comment)

# fuser -k /media/disk
#umount /media/disk


Hope this will be useful

Tuesday, July 22, 2008

Monitoring hard disk with smartmontools

Monitoring your hard disk health is a very important thing. You do not want to wake up one day, turn on your computer and suddenly your hard disk has crash and all your valuable data has gone with the wind. At that time crying would not get your data back. Like some people always say, prevention is better than cure. Apart from backing up your data regularly, monitoring the health of your hard disk is an essential task. It is to make sure any symptoms of bad sector or any failure can be detected earlier and steps to take care of it can be done sooner. One of the tool that can be used to do the job mentioned before is smartmontools. According to yum description, smartmontools are "Tools for monitoring SMART capable hard disks".

To install smartmontools on fedora:
# yum install smartmontools

Make sure your hard disk is smart capable
# smartctl -i /dev/sda
smartctl version 5.37 [i386-redhat-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF INFORMATION SECTION ===
Model Family: Western Digital Caviar SE (Serial ATA) family
Device Model: WDC WD800JD-60LSA5
Serial Number: WD-WMAM9MA75547
Firmware Version: 10.01E03
User Capacity: 80,026,361,856 bytes
Device is: In smartctl database [for details use: -P show]
ATA Version is: 7
ATA Standard is: Exact ATA specification draft version not indicated
Local Time is: Tue Jul 22 10:05:31 2008 MYT
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

Smart support is available for this hard disk and enabled

To monitor your hard disk health
# smartctl -H /dev/sda
smartctl version 5.37 [i386-redhat-linux-gnu] Copyright (C) 2002-6 Bruce Allen
Home page is http://smartmontools.sourceforge.net/

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

To run test on your hard disk
# smartctl -t short /dev/sda

To see the selftest logs of smartctl
# smartctl -l selftest /dev/sda

See all options for smartctl
# smartctl -h

Manual for smartctl
# man smartctl