Manage Partitions and Disk on Linux

Linux Partition

 

I. Manage Partitions on Linux

  • Every hard drive requires at least one partition. In fact, we can devide a standard hard drive into 16 different partitions.
  • You can configure 3 types of partitions on a hard drive
    • Primary partition : We can create up to 4 different primary partitions on an IDE or a SCSI hard drive. One primary partition must be active; it will include a bootloader such as GRUB or LILO, or Windows boot loaders.
    • Extended partition : If you need more partitions, you can convert one primary partition to extended partition. The extended partition then can be further subdivided into logical partitions.
    • Logical partition : An extended partition can be devided into logical partitions. We can have up to 12 logical partitions on a hard drive.
  • We can add and manage partitions with fdisk command
    • Show disk drive by fdisk command. We are having 1 hard disk and 3 partitions.
[root@localhost ~]# fdisk -l
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 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: 0x0005401dDevice Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
/dev/sda2 39 1706 13388800 83 Linux
/dev/sda3 1706 1959 2031616 82 Linux swap / Solaris
[root@localhost ~]#
    • Right now, we are having 3 partitions with SCSI hard disk so it’s showing /dev/sda1, /dev/sda2, /dev/sda3. If it is IDE, it should be /dev/hda1, /dev/hda2.
    • Use fdisk to create new partition for new disk in your system. Right now we need create a new partition for new disk /dev/sdb.
[root@localhost ~]# fdisk -l
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 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: 0x0005401dDevice Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 1706 13388800 83 Linux
/dev/sda3 1706 1959 2031616 82 Linux swap / Solaris
Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000
[root@localhost ~]#
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x202c3d30.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)Command (m for help): nCommand action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261):
Using default value 261Command (m for help):
Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
[root@localhost ~]# fdisk -l
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 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: 0x0005401dDevice Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 1706 13388800 83 Linux
/dev/sda3 1706 1959 2031616 82 Linux swap / SolarisDisk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x202c3d30Device Boot Start End Blocks Id System
/dev/sdb1 1 261 2096451 83 Linux
[root@localhost ~]#
  • Major Linux Filesystem Formats
    • ext2 : The seconde extended filesystem, which was the stardard for Linux OS. If you have older system with ext2 partitions, you can easyly to convert to ext3.
    • ext3 : The third extended filesystem, which is current default on Linux System. It includes a journal, which save or records all pending changes, such as data to be written to disk.
    • jfs : a journaling filesystem developed by IBM.
    • reiserfs : The Reiser filesystem, is based on different designs from the Linux extended filesytems.
    • xfs : The filesystem developed by Silicon Graphics, it can support extremely hard drives.
  • Formatting a Partition
    • Use mkfs command to format Linux partitions. We can choose the type of filesystem by -t switch. You can check bad blocks before formatting with -c switch.
[root@localhost ~]# mkfs -t ext2 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524112 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912Writing inode tables: done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
[root@localhost ~]# mkfs -t ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524112 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
    • We also can format partition to ext3 filesystem by -j switch
[root@localhost ~]# mkfs -j /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524112 blocks
26205 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
  •  We can convert an older format ext2 to ext3 by tune2fs command. The only difference between ext2 and ext3 is journal. Therefore, if you create a journal for ext2 filesystem, it’s ext3.
[root@localhost ~]# tune2fs -j /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Creating journal inode: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#

II. Manage Disk on Linux

  •  du and df commands are disk management commands on Linux. du –> directory usage and df –> disk free.
[root@localhost kevin]# du -h
4.0K ./.mozilla/plugins
4.0K ./.mozilla/extensions
12K ./.mozilla
4.0K ./.gnome2
32K .
[root@localhost kevin]#
[root@localhost kevin]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 13G 4.6G 7.4G 39% /
tmpfs 495M 72K 495M 1% /dev/shm
/dev/sda1 291M 55M 221M 20% /boot
[root@localhost kevin]#
  • Labels for Linux partition is very important . The default /etc/fstab uses disk labels. We also can find disk lablels in GRUB configuration. But when we create a new partition with fdisk and format with mkfs, it will not have label. So you should have label for that.
[root@localhost kevin]# e2label /dev/sdb1 /home/kevin
    • Show label
[root@localhost kevin]# e2label /dev/sdb1
/home/kevin
    • We also can get more informatin about partition and lable by dump2fs command.
[root@localhost kevin]# dumpe2fs /dev/sdb1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name: /home/kevin
Last mounted on: <not available>
Filesystem UUID: 998ad678-6cc2-4646-9eef-7f3198c6284e
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype s parse_super large_file
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 131072
Block count: 524112
Reserved block count: 26205
Free blocks: 506907
Free inodes: 131061
First block: 0
Block size: 4096
Fragment size: 4096
Reserved GDT blocks: 127
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 8192
Inode blocks per group: 512
Filesystem created: Sat Oct 5 11:54:43 2013
Last mount time: n/a
Last write time: Sat Oct 5 12:31:21 2013
Mount count: 0
Maximum mount count: 28
Last checked: Sat Oct 5 11:54:43 2013
Check interval: 15552000 (6 months)
Next check after: Thu Apr 3 11:54:43 2014
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 256
Required extra isize: 28
Desired extra isize: 28
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: 9fcc02c2-4587-49d0-9e04-15fc4f6965f7
Journal backup: inode blocks
Journal features: (none)
Journal size: 32M
Journal length: 8192
Journal sequence: 0x00000001
Journal start: 0

1. Mount Directories

  •  Before we can use a Linux partition, we need mount it into the Linux system.
  • We need to specify the partition, the directory being mouted, the format with the partition.
  • Syntax

mount -t format partition directory

[root@localhost kevin]# mount -t ext3 /dev/sdb1 /home/kevin
  • Normally we only required this command, because it will take filesystem format from disk partition
[root@localhost kevin]# mount /dev/sdb1 /home/kevin
  • We also can add that partition to mount to Linux System for permanently. We can add this line  to /etc/fstab file.
/dev/sdb1           /home/kevin            ext3        defaults            1 2
  • After done /etc/fstab, you can activate your mouting by
mount /dev/sdb1

or

mount /home/kevin
  • Sometimes you need umount directory on Linux System, you can use umount command
umount /home/kevin

2. Filesystem Troubleshooting

  • Filesystem problems normally require troubleshooting during boot.
  • fsck commands is very important tool. Linux uses it periodically to automatically check most of the partitions on Linux System.
  • fsck command checks and repairs Linux filesystems such as fsck.ext2, fsck.ext3, fsck.reiserfs … If the filesystem is unknown, you can only use fsck command.
    • -a switch : automatically repair target filesystems without prompts. Should be used only within /etc/rc.sysinit.
    • -b superblock : uses a differents superblock.
    • -A switch : Checkes all filesystems lists in /etc/fstab.
    • -R switch : When -A used, skips the root (/) directory filesystem.
    • -y : automatically say “yes” to suggest a solution.
  • If you suspect a problem, you can run fsck on any umounted partition.
  • Automated partition checks: fsck command is no longer run periodically by default, you can change that by using command as: tune2fs -c count /dev/partition . To find the mount count information for specific partition, use tune2fs command.

3. Understand /etc/fstab file

  • /etc/fstab is used during Linux booting process. It will mount partitions and directories to Linux System.
[root@localhost ~]# cat /etc/fstab
# /etc/fstab
# Created by anaconda on Fri Jan 4 10:36:43 2013
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk’
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=5239d6c9-2742-4a94-b628-f71e0e442ea7 / ext4 defaults,usrquota,grpquota 1 2
UUID=3bbdbf55-e9e1-4d5c-a18f-941aa122a7a9 /boot ext4 defaults 1 2
UUID=3eb80b0d-6b3d-4ea7-b5a6-d410cd987d26 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
    • Column 1 : Label : The filesystem , such as /usr, or partition, such as /dev/sdb1, to be mounted.
    • Column 2 : Mount point : The directory where the partition or filesystem is to be mounted.
    • Column 3 : Format : filesytem format as ext2, ext3, ext4 …
    • Column 4: Mount Options : The defaults option includes rw (read,write), suid (SUID permissions), dev (terminals and block devices such as drives), exec (binary files), auto (automatically mounted), nouser (only root can mount), and async (data is read and written asynhronously).
    • Column 5 : Dump value : If it’s 1, filesystem will automatically written to disk.
    • Column 6 : Filesystem Check Order : Filesystems that need fsck. The root / filesystem should be 1; others on local should be 2, swap, virtual, CD and remote directories should be 0.
  •  Others mount options are availabe such as usrquota and grpquota (for set quotas), noauto (to make sure Linux doesn’t look for CD or floppy when it boots), and user (to let any user mount filesystem, such as CD-rom).

4. Logical Volume Management (LVM)

  •  Without LVM (Logical Volume Management), once drives are partitioned, there are no easy way to expand the available space.
  • Example, your company had 20 users and you created 20 users in /home directory, but right now your company increased more than 20 users. So with LVM, you can expand the size of /home directory.
  • LVM also allows you to reallocate chunks of disk space between different filesystems. So with LVM, if you have extra room in a filesystem as /var, you can reallocate that space to /home.
  • We can create LVM in installation process. You also can create LVM after installation.

Basic Fundamentals about LVM

  • LVM is essentially a mapping of different physical sections of a hard drive. Once collected into a logical volume, filesystems such as /home and /usr can be mouted on that volume. We can reorganize logical volumes to include additional hard drive space.
  • Some definitions about LVM
    • PV (Physical Volume), a PV usually corresponds to a standard primary or logical partition on a hard drive.
    • PE (Physical Extent) a PE is a chunk of disk space. Physical Volumes are divided up into a number of equal sized PEs.
    • LE (Logical Extent) a LE is a chunk of disk space. The size of an LE in an LVM system is same size of PEs on that systems. Every LE corresponds to a specifi PE.
    • LV (Logical Volume) a LV is a collection of LEs. You can mount filesystems such as /usr and /boot on an LV.
    • VG (Volume Group) LVs on our sytem, collected together form a VG (Volume Group). When we configure an LVM system, most commands are applied to a VG.
  • Create LVM on Linux
    • Step 1: Create a Physical Volume
[root@localhost ~]# pvcreate /dev/sdc
Physical volume “/dev/sdc” successfully created
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume “/dev/sdb1” successfully created
[root@localhost ~]#
    • Step 2: Create Volume Group
[root@localhost ~]# vgcreate Data /dev/sdc /dev/sdb1
Volume group “Data” successfully created
[root@localhost ~]#
    • Step 3: Display Volume Group
[root@localhost ~]# vgdisplay Data
— Volume group —
VG Name Data
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 6.99 GiB
PE Size 4.00 MiB
Total PE 1790
Alloc PE / Size 0 / 0
Free PE / Size 1790 / 6.99 GiB
VG UUID ZUPFSm-6uP4-NkPx-baWU-hn9X-9P9R-vFGsmS
    • Step 4: Add more disk for Volume Group with another parition
[root@localhost ~]# pvcreate /dev/sdd
Physical volume “/dev/sdd” successfully created
[root@localhost ~]# vgextend Data /dev/sdd
Volume group “Data” successfully extended
[root@localhost ~]# vgdisplay Data
— Volume group —
VG Name Data
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 7.99 GiB
PE Size 4.00 MiB
Total PE 2045
Alloc PE / Size 0 / 0
Free PE / Size 2045 / 7.99 GiB
VG UUID ZUPFSm-6uP4-NkPx-baWU-hn9X-9P9R-vFGsmS[root@localhost ~]#
    • Step 5: Create a Logical Volume

Create a Logical Volume with 200Mb

Syntax:

lvcreate -l number_of_PEs Data -n LogicalVolume

Note: each PE is 4MB in size.

[root@localhost ~]# lvcreate -l 50 Data -n LogicalVolume01
Logical volume “LogicalVolume01” created
[root@localhost ~]# mkfs -j /dev/Data/LogicalVolume01
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 13G 4.6G 7.4G 39% /
tmpfs 495M 72K 495M 1% /dev/shm
/dev/sda1 291M 55M 221M 20% /boot
[root@localhost ~]# mount -t ext3 /dev/Data/LogicalVolume01 /tmp
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 13G 4.6G 7.4G 39% /
tmpfs 495M 72K 495M 1% /dev/shm
/dev/sda1 291M 55M 221M 20% /boot
/dev/mapper/Data-LogicalVolume01
194M 5.6M 179M 4% /tmp
[root@localhost ~]#
    • Step 6: Increate /dev/Data/LogicalVolume01 to 300M
[root@localhost ~]# lvextend -L300M /dev/Data/LogicalVolume01
Extending logical volume LogicalVolume01 to 300.00 MiB
Logical volume LogicalVolume01 successfully resized
[root@localhost ~]#

Thanks for using IThelpblog.com.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Go to top