Pages

Friday 16 September 2011

Post titleBasis Filesystem Management in Linux


Basis Filesystem Management in Linux

Basis Filesystem Management in Linux

In this doc I am trying to present base for Linux Filesystem. I tried to cover Theoratical and Pratical portition where-ever it is necessary.

What is Filesystem

In simple term you can say a FS is a medium to access your files/directories in any Unix Flavour. In Unix each and every thing is File or Directory. In order to standardize Files or Directories structure Filesystem are used.

Now in depth you can say a FS is a portion of your harddisk (or complete harddisk) which is used to create files and directories which are world-wide accessiable.

Linux Filesystem routemap

1.) Identify Device –> which may be your internal harddisk or external storage. (/dev/sd{a,b,c,….} —> SCSI , /dev/hd{a,b,c,…} —> IDE)

2.) Partition Device –> Disks are partitioned to make seperate Filesystems as per requirement.

3.) Make Filesystem –> Partitions are used to make Filesystems so that they can be recognised by the OS.

4.) Mountpoint –> Mountpoints are nothing but just are simple directories where Filesystems are mapped. Its easy to remember simple directory names as compare to complete FS name.

5.) Make FS permanent after reboot –> The FS entry should be made under a special file called /etc/fstab which is used by init phase to mount all the filesystems listed under /etc/fstab.

Practical Example

1.) Identify Device — I have three internal disk in the server, you can check that by fdisk -l & cat /proc/partition

Here sda —>; is my root disk
sdb & sdc are newly attached disk which I will be using for FS creation.

yogesh-Linux#fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1305 10377990 8e Linux LVM
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdc doesn’t contain a valid partition table
yogesh-Linux#cat /proc/partitions
major minor #blocks name
8 0 10485760 sda
8 1 104391 sda1
8 2 10377990 sda2
8 16 1048576 sdb
8 32 1048576 sdc
253 0 9306112 dm-0
253 1 1048576 dm-1

2.) Partition Device –> We will partition the disks to create FS on them. In Linux on SCSI drives you can create 3 Primary Partitions and 1 Extended Partition. The extended partion is further used to create Logical partitions which can be treated as Primary partition by dependable to extended partition. The maximum supportable no. of partitions in SCSI drivers are 15 and IDE are 63.

In the below example I will create both primary and extended partitions for handson practice. I am using /dev/sdc disk and creating Primary partition first.

Primary Partition

yogesh-Linux#fdisk /dev/sdc
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.

Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)

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): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-130, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): 40

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System /dev/sdc1 1 40 321268+ 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
yogesh-Linux#

yogesh-Linux#cat /proc/partitions
major minor #blocks name

8 0 10485760 sda
8 1 104391 sda1
8 2 10377990 sda2
8 16 1048576 sdb
8 32 1048576 sdc
8 33 321268 sdc1
253 0 9306112 dm-0
253 1 1048576 dm-1

yogesh-Linux#partprobe /dev/sdc1
Why Partporbe in Linux?

At system bootup, the kernel makes its own in-memory copy of the partition tables from the disks. Most tools like
fdisk edit the on-disk copy of the partition tables. To update the in-memory copies, run partprobe.

Extended Partition

yogesh-Linux#fdisk /dev/sdc

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): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (41-130, default 41):
Using default value 41
Last cylinder or +size or +sizeM or +sizeK (41-130, default 130): 100

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 40 321268+ 83 Linux
/dev/sdc2 41 100 481950 5 Extended ———> this extended partition can be divided into Logical partitions between 41 to 100 cylinders

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (41-100, default 41):
Using default value 41
Last cylinder or +size or +sizeM or +sizeK (41-100, default 100): 60

Command (m for help): p

Disk /dev/sdc: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdc1 1 40 321268+ 83 Linux
/dev/sdc2 41 100 481950 5 Extended
/dev/sdc5 41 60 160618+ 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
yogesh-Linux#

yogesh-Linux#cat /proc/partitions

major minor #blocks name

8 0 10485760 sda
8 1 104391 sda1
8 2 10377990 sda2
8 16 1048576 sdb
8 32 1048576 sdc
8 33 321268 sdc1
8 34 1 sdc2
8 37 160618 sdc5
253 0 9306112 dm-0
253 1 1048576 dm-1

yogesh-Linux#partprobe /dev/sdc5
yogesh-Linux#

3.) Make Filesystem —> Next we have to proceed with filesystem creation in Linux, with Linux RHEL version 5 the default Filesystem type in ext3 so we will take ext3 throughout over practical session. Though you can use ext2 or raiserFS or any other Linux supported filesystem.

mkfs is the command used to create FS in Linux, for more details kindly refer man for mkfs.
yogesh-Linux#mkfs -t ext3 /dev/sdc1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
80320 inodes, 321268 blocks
16063 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
40 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
yogesh-Linux#

yogesh-Linux#mkfs -t ext3 /dev/sdc5
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
40160 inodes, 160616 blocks
8030 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
20 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
yogesh-Linux#

4.) Mountpoint —> Mountpoint is just a directory on to which the FS is mapped to make the FS accessiable.

yogesh-Linux#mkdir /yogesh-primary-test

yogesh-Linux#mkdir /yogesh-extended-test

yogesh-Linux#mount /dev/sdc1 /yogesh-primary-test

yogesh-Linux#mount /dev/sdc5 /yogesh-extended-test

yogesh-Linux#df -h /yogesh*
Filesystem Size Used Avail Use% Mounted on
/dev/sdc5 152M 5.6M 139M 4% /yogesh-extended-test
/dev/sdc1 304M 11M 279M 4% /yogesh-primary-test
yogesh-Linux#

yogesh-Linux#cd /yogesh-extended-test
yogesh-Linux#ls -lrt
total 12
drwx—— 2 root root 12288 Aug 15 21:15 lost+found

yogesh-Linux#touch yogesh-test
yogesh-Linux#cp /var/log/messages /yogesh-extended-test/yogesh-test
cp: overwrite `/yogesh-extended-test/yogesh-test’? y

yogesh-Linux#ls -lrt
total 51
drwx—— 2 root root 12288 Aug 15 21:15 lost+found
-rw-r–r– 1 root root 37057 Aug 15 21:22 yogesh-test
yogesh-Linux#

5.) Make FS permanent after reboot (significance of /etc/fstab) –> Till this point of time the Filesystem’s are not permnenly mounted. For eg if your system crashed the above listed FS will not come up as mounted FS. To make them visible across reboots we have to make their entries in /etc/fstab.

Below is the example:

yogesh-Linux#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.6G 2.2G 6.1G 27% /
/dev/sda1 99M 13M 82M 14% /boot
tmpfs 250M 0 250M 0% /dev/shm
/dev/sdc1 304M 11M 279M 4% /yogesh-primary-test
/dev/sdc5 152M 5.6M 139M 4% /yogesh-extended-test

yogesh-Linux#umount /yogesh-primary-test

yogesh-Linux#umount /yogesh-extended-test

yogesh-Linux#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.6G 2.2G 6.1G 27% /
/dev/sda1 99M 13M 82M 14% /boot
tmpfs 250M 0 250M 0% /dev/shm

yogesh-Linux#mount -a

yogesh-Linux#df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9014656 2250392 6298960 27% /
/dev/sda1 101086 12512 83355 14% /boot
tmpfs 255296 0 255296 0% /dev/shm
yogesh-Linux#

See the FS which we have created are not coming up once they are umounted. This is bzu once we execute mount -a command kernel start reading entries from /etc/fstab.

It will mount all the FS listed in /etc/fstab. Below is the current /etc/fstab where their is no entry for the FS which are newly created. lets add them and then check the results.

yogesh-Linux#more /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
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
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
LINUX1:/yogesh /nfstest nfs defaults 0 0
yogesh-Linux#

Amend the entries using vi /etc/fstab as shown below:

yogesh-Linux#more /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
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
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
LINUX1:/yogesh /nfstest nfs defaults 0 0
/dev/sdc1 /yogesh-primary-test ext3 defaults 0 0
/dev/sdc5 /yogesh-extended-test ext3 defaults 0 0
yogesh-Linux#mount -a

yogesh-Linux#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.6G 2.2G 6.1G 27% /
/dev/sda1 99M 13M 82M 14% /boot
tmpfs 250M 0 250M 0% /dev/shm
/dev/sdc1 304M 11M 279M 4% /yogesh-primary-test
/dev/sdc5 152M 5.6M 139M 4% /yogesh-extended-test
yogesh-Linux#
All FS are visible and persists across reboots now.

No comments:

Post a Comment

Twitter Bird Gadget