Unix System Admin

Download Report

Transcript Unix System Admin

Filesystems
• Objectives
– to understand Unix filesystem concepts
• Contents
–
–
–
–
–
–
–
–
disk terminology
fdisk
raid, lvm
Make filesystem
filesystem utilities
Check filesystems
Repair
Quota
• Practical
– to make and mount a disk filesystem
• Summary
File System Structure
• Single inverted tree structure
– individual disks are hidden from the casual user
• Filesystems define how disks are structured
– different disks can be formatted with different filesystems
– all filesystems have a superblock (disk information)
– all filesystems have the concept of file i-nodes
• Filesystems must be mounted into the tree structure
– a disk/partition can be mounted onto any directory
– directory is referred to as a mount point
• Unix accessed through file interface
– file permissions used to control user access to the system
File System Types
• A filesystem type defines how the disk data is structured
– Unix supports many different filesystem types (typically 12 or more)
– UFS standard across all platforms
– many systems support DOS FAT filesystems
• ext2fs classic Linux filesystem (UFS like)
– Most well known, many proven recovery toolkits
• ext3fs next generation Linux filesystem
– Journalled filesystem for servers with mixed size large number of files
• NFS
– Network file system
• ISO 9660 & Joilet
– CD-Rom
• MSDOS
– DOS filesystem
• VFAT
– Originally to be compatible with Windows95 to overcome 8.3 limit
File System Types, continued.
• Reiserfs alternate filesystem for linux
– Used by many homeusers, fast for many small files
• UFS (or Fast File System) from Berkeley
– long filenames (255), faster and more efficient than many other filesystems
• Journaling filesystems
– Veritas or VFX (HP, SNI, UnixWare), JFS (AIX)
– self repairing filesystems (more reliable)
– may support Logical Volume Management with virtual filesystems, striping
and mirroring
• VFS
– Virtual filesystems belong to device levels
– Managed by LVM, grouped partitions
• RAID
– RAID system work below filesystem level, and belong to device level
– Soft raid, stores data over several disks, based on linux drivers
– Hard raid, like softraid but special hardware controls datastore
Unix filesystems
• Partition and Slices
–
–
–
–
–
Physical partitons, INTEL plateform splits disk in up to 4 pices
Logical partitions, reside inside physical partitions, extended partitions
Slice is an are of the disk described by start and stop cylinder
Cylinder is a number of disk blocks which size can be altered
Disk block is a number of disk sectors which usally is 512 or 1024 bytes
• Physical diskdevices are named
– ATA types:
– SCSI types:
Examples)
/dev/hd[a-z][’ ’,1-16]
/dev/sd[a-z][’ ’,1-16]
/dev/hda
/dev/hda1
/dev/sda1
(Also SATA belong here)
- IDE disk 0 [whole disk]
- IDE disk 0 first partition
- SCSI disk lun 0 part 0
• Linux configurable dynamic device naming support udev
– User defined device name,practicalfor hotpligged devices, will eventually replace
devfs in future.
• If using devfs naming are different (scsi)
/dev/scsi/host0/bus0/target1/lun0/disc
/dev/scsi/host0/bus0/target1/lun0/part6
/dev/scsi/host0/bus0/target1/lun0/generic
[whole disk]
[partition 6]
[sg device for disk]
Working with FDISK, view disk part.
• cfdisk
– General partitioning toolkit, vt100
• DiskDruid
– Guided ANSI/VT100 graphics partition manager
• Parted
– Work with partition and softraid
• FDISK (preferred)
– Create partitions and partition types
– You must allways operate on the whole disk not partitions
– Look on disk partitions /dev/hda (p-rint partititon)
# fdisk /dev/sda
Command (m for help): p
Disk /dev/hda: 255 heads, 63 sectors, 1244 cylinders
Units = cylinders of 16065 * 512 bytes
Device Boot
/dev/sda1
*
/dev/sda2
/dev/sda3
Start
1
18
84
End
17
83
475
Blocks
136521
530145
3148740
Id
83
82
83
System
Linux
Linux swap
Linux
Working with FDISK, +100M partition.
• Add partition to disk
– You must have unallocated disk-space or delete existing part.
– If you have used all 4 primary partition, extended will be activated
Command (m for help): n
Command action
e
extended (5 or over)
p
primary partition (1-4)
– After selecting primary or logical partition you will be asked
Partition number (1-4): 4
First cylinder (1-1244, default 476): Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1244, default 1244): +100M
Using value 489
Working with FDISK, set type 83 linux
• Select partition type to linux (83) for normal partitions
– Fdisk will ask you what partition to change
Command (m for help): t
Partition number (1-4):4
Hex code (type L to list codes):83
Command (m for help): w
. . .
– After writing typ q to exit fdisk
• Make a filesystem on the clean partition
– We select ext3fs!
# mkfs -t ext3 /dev/hdb1
. . .
• Mounting the filesystem
# mkdir /mnt/extradisk
# mount –t ext3 /dev/hdb1 /mnt/extradisk
# ls –1 /mnt/extradisk
lost+found
Raidlevels, RAID 0
• For increasing storage only, no fault tolerance.
Raidlevels, RAID 1
• Data is mirrored to one or more disks.
– Minimum 2 disks
Raidlevels, RAID 3
• Data is striped over disks, parity is kept on separate disk.
– Minimum 2 disks
Raidlevels, RAID 5
• Data and parity is striped over disks.
– Minimum 3 disks
Preparing for software RAID
• Should I Software RAID Partitions Or Entire Disks?
– Both works, but preformance and redundancy is in danger
– Use of a SPARE disk is preventing system stop.
• RAID Partitioning
– Smallest partition set maxim partition size in RL1-5
Example)
(32G) /dev/sdc1
(32G) /dev/sdd2
32G will be maxim size
– Raid level 0 is additative and slices can vary in size
(64G) /dev/sde1
• Determining Available Partitions
# fdisk –l
[long list will follow]
Unmount partitions that should join the raid, if they are mounted!
# umount /dev/sdd2
Filesystem is added on top of the raid, after it has merged.
Preparing the software RAID, part. type.
• Change filesystem typ of partition’s
– You have to change each partition in the RAID set to be of type FD
– FD=(Linux raid autodetect.
# fdisk /dev/sdc1
Command (m for help): t
Partition number (1-5): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): w
– Waring message is normal, if it appairs!
• Edit The RAID Configuration File
RAID 5 on using 3 disks.
The set will be called /dev/md0.
Filename is: /etc/raidtab
Preparing the software RAID, raidtab
• /etc/raidtab
•
raiddev /dev/md0
raid-level
5
nr-raid-disks
3
persistent-superblock
chunk-size
32
parity-algorithm leftsymmetric
device
sdc1
raid-disk
device
sdd2
raid-disk
Create the RAID Set
device
– This is the funny part!sde1
raid-disk
# mkraid /dev/md0
1
/dev/
0
/dev/
1
/dev/
2
analyzing super-block
disk 0: /dev/sdc1, 104391kB,raid superblock at 104320kB
disk 1: /dev/sdd2, 104391kB,raid superblock at 104320kB
disk 2: /dev/sde1, 104391kB,raid superblock at 104320kB
Creating the software RAID filesystem
• Creating filesystem on the New RAID Set
– Almost any filesystem will suit on your RAID set
# mke2fs -j -b 4096 -R stride=8 /dev/md0
. . .
• Load The RAID Driver For The New RAID Set
# raidstart /dev/md0
• Create A Mount Point For The RAID Set
– Can be in any catalog
# mkdir /mnt/raid
• Edit The /etc/fstab File
/dev/md0
/mnt/raid
ext3
defaults
– Make raidset mount every time machine is started
1 2
Maintaining the soft RAID
• Checking raidset status
# cat /proc/mdstat
Personalities : [raid5]
read_ahead 1024 sectors
unused devices: <none>
• Stopping/Starting the raidset
# raidstop /dev/md0
# raidstart /dev/md0
• Manually mounting/unmounting raidset
– Like any filesystem!
# mount /dev/md0 /mnt/raid
# umount /mnt/raid
Virtual File System, LVM devices
• Study LVM admin instruction pages
http://www.gentoo.org/doc/en/lvm2.xml
and
http://tldp.org/HOWTO/LVM-HOWTO/
• A virtual filesystem is built up from
–
–
–
–
Physical Volume’s PV (partitions on disks that form a volume group)
Volume Group’s VG (virtual disk’s)
Logical Volume’s LV (logical volume or mountable partition)
Physical Extent PE (Physical Extent, mini partitions who build up LV)
• Virtual filesystem is flexable
– You can increase/decrease ”disk-space” while running
– Easy manage fast growing virtual disks
– Use dynamic growing virtual disks
• Works like standard disk devices
• Can be raided
• Is not dependent on single disks for storage
Virtual File System, LVM concept
• LVM Concept
One or several disks have one or morepartitions
VG is build up from several partitions on several disks
LV is a part of or a whole VG
The LVcan be mounted as any other disk device
Configuring LVM Devices
• Backup Your Data
– Logout all users!
– Use the tar command or some other method to backup your data in /home. The
LVM process will destroy the data on all physical volumes.
• Going into single user mode
# init 1
• Unmount your /home filesystem
# umount /home
• Determine The Partition Types
# fdisk -l /dev/hde
• Change partition type to 8e for all lvm partitions
# fdisk /dev/hde
Command (m for help): t
Partition number (1-6): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Define LVM physical devices & VG
• Define each Physical Volume PV
# pvcreate /dev/hde5
pvcreate -- physical volume "/dev/hde5" successfully created
# pvcreate /dev/hdf1
pvcreate -- physical volume "/dev/hdf1" successfully created
# fdisk -l /dev/hde
• Run VGscan
# vgscan
vgscan -- reading all physical volumes (this may take a while...)
• Create A VG (VolumeGroup) for the PV’s above
– This is our device containing both physical volumes
# vgcreate vg1 /dev/hdf1 /dev/hde5
Volume group ”vg1" successfully created
Create Logical Volume’s
• Look into the Volume Group
# vgdisplay vg1
...
PE Size
4 MB
Total PE
212
Alloc PE / Size
0/0
Free PE / Size
212 / 848 MB
...
- We have 212 Physical Extents free to create logical volumes with.
• Create LV:s
# lvcreate -l 212 vg1 -n lv1
Logical volume "lv1" created
Make filesystem on the Logical Volume
• Make filesystem
– Almost any filesystem will do, in both examples we use ext3fs
# mkfs -j /dev/vg1/lv1
Or
# mkfs -t ext3 /dev/vg1/lv1
• Create a mount point
# mkdir /mnt/lvm1
• Update /etc/fstab
/dev/vg1/lv1
/mnt/lvm1
ext3
• Mount the logical volume
# mount –a
# ls –1 /mnt/lvm1
lost+found
defaults
12
Making a general Filesystem
• Use the mkfs command
–
–
–
–
–
use -t to override filesystem default
specify raw disk device /dev/hd[a-z] or /dev/sd[a-z]
usually need to state disk size (in 512 byte blocks)
Additional parameters vary according to filesystem type
Like filesystem block size that can vary from 1024 up to 65535
• Creates superblock and i-node list
– inode list fixed in size, cannot be extended
– can override number of i-nodes when filesystem created
# mkfs -t ext2 /dev/sdb3 102400
– Use small block sizes for disk with many small files
– Make large block sizes for disks with large files
– FRAGS is the loss of diskspace in not filled out blocks
Mounting a Filesystem
• Use mount to add a filesystem to the tree structure
– the root file system is mounted by the kernel at boot time
– filesystem can be mounted manually or automatically in multi-user mode
– use the blocked device for mount
• Filesystems can be mounted onto any directory
– will hide files already in directory (usually choose empty directories)
– directory is called the mount point
– /mnt provided as standard temporary mount point
• Unmount filesystems using umount
– specify the mount point to umount
# mount -t ext3 /dev/sdb2 /home2
# ls /home
lost+found
# umount /home
# ls /home
#
Kernel File Cache
• The kernel allocates data space to hold:
– the superblock
– several i-nodes
– several data blocks
• Cache only written back to disk periodically
– the sync command is used to manually flush the cache to disk
• If Unix crashes:
– cached data is lost and file system is left in an inconsistent state
– disk structure is recovered on reboot using fsck utility
– journaling filesystems recover automatically on mount
• Unix should be shutdown cleanly
– disk cache is flushed as part of shutdown process
The lost+found Directory
• Used to save lost files in a corrupt filesystem
– created when file system is made
– created large enough to hold several files as new disk blocks cannot be
allocated from a corrupt filesystem
– some active i-nodes may not be referenced from any directory
• The file system check program (fsck) finds lost files
– lost files stored in lost+found directory
– filename is inode number
• Use the file command to identify the types of lost files
• Recreate lost+found with mklost+found if it is missing
Dealing with Corrupt Filesystems
• Use fsck to repair corrupt filesystems
– e2fsck –o <alternate superblock> /dev/hda4
– fsck –b <alternate superblock> /dev/vg1/lv1
# fsck /dev/hda4
** Phase 1 -- Check blocks and sizes
** Phase 2 -- Check pathnames
** Phase 3 -- Check connectivity
** Phase 4 -- Check reference counts
UNREF FILE I = 1064 OWNER = 318 MODE = 140664
SIZE = 2186 MTIME = Feb 2 16:12 1951
FREE INODE COUNT WRONG IN SUPERBLOCK
** Phase 5 -- Check free list
1763 files 35478 blocks 6432 free
*** FILESYSTEM WAS MODIFIED ***
First alternate superblock is reported when filesystem is created.
It can be, but not allways:
1K blocksize
8192
2K blocksize
16384
4K blocksize 32768
to recover trashed filesystem!
Recover files with image
• Make diskimage before any attempt to run fsck
# dd if=/dev/hda4 of=/mnt/hda4-backup.dd
- This might fail, if there are any hard errors
- Remove the faulty diskt and keep it.
Prepare loopback filesystem
# losetup /dev/loop0 /mnt/hda4-backup.dd
• Try to mount and recover some files
# mkdir /mnt/trashed
# mount /dev/loop0 /mnt/trashed
# umount /dev/loop0
• Try to mount and recover some files
# fsck -b 32768 -f -y /dev/loop0
# fsck.ext2 -f -y /dev/loop0
Or
# fsck -b 32768 -f -y /dev/loop0
- mount disk again and try recover now!
Exercise - Identifying Lost Files
• Given this lost and found directory which command(s)
would you use to identify the contents of each file?
# cd /home/lost+found
# file *
000541: ASCII text
000872: commands text
001065: iAPX 386 executable not stripped
001085: C source code
001461: data
To identify file 000541 ?
To identify file 000872 ?
To identify file 001065 ?
To identify file 001085 ?
To identify file 000461 ?
Filesystem Configuration Files
• Define standard filesystems in /etc/fstab
– used during system startup to mount file systems automatically
• One line per filesystem of form:
– tab separated fields
– use -p option to mount to get fstab lines for current configuration
# cat /etc/fstab
LABEL=/
LABEL=/boot
none
LABEL=/home
none
none
/dev/sda2
/dev/cdrom
/dev/fd0
File system specifier
/
/boot
/dev/pts
/home
/proc
/dev/shm
swap
/mnt/cdrom
/mnt/floppy
mount point
ext3
ext3
devpts
ext3
proc
tmpfs
swap
iso9660
auto
filesystem type
defaults
1 1
defaults
1 2
gid=5,mode=620 0 0
defaults
1 2
defaults
0 0
defaults
0 0
defaults
0 0
noauto,owner,kudzu,ro 0 0
noauto,owner,kudzu 0 0
mount options
dump / fsck order
Free Disk Space
• Use disk free (df) to report on free disk space
– use -h to give sizes in human readable form
– use -l to restrict local file systems only (ignore network)
– works on mounted file systems only
# df –hl
Filesystem
/dev/hda10
/dev/hda1
/dev/md0
/dev/hda7
/dev/hda13
/dev/hda5
/dev/hda8
/dev/hda11
/dev/md2
/dev/md1
/dev/hdd1
/dev/hda6
Size
509M
45M
40G
2.0G
509M
3.9G
2.0G
251M
3.4G
3.4G
56G
3.9G
Used Avail Use% Mounted on
433M
50M 90% /
25M
18M 57% /boot
30G 8.9G 77% /home
413M 1.4G 22% /opt
332k 483M
1% /tmp
2.7G 1.0G 72% /usr
705M 1.1G 37% /usr/local
59M 179M 25% /usr/src
1.6G 1.6G 50% /var
1.3G 1.9G 41% /home/httpd
45G 9.1G 83% /home/ftp/pub
3.1G 682M 83% /usr/local/src
Disk Usage
• Use disk usage (du) to look at files and directories
–
–
–
–
–
–
recursively looks at named file/directories
by default lists only directory sizes
use -s to summarise for named files
use -a to detail all files
linked files only counted once
size given in 512 blocks (use -k option for kilobytes not on all systems)
# du
25
0
5
67
0
15
51
976
1
-s /home/*
/home/frank
/home/hawkeye
/home/henry
/home/hotlips
/home/lost+found
/home/klinger
/home/mulcahy
/home/radar
/home/trapper
Disk Quotas
• Read the instructions for Quota
http://www.linuxnetmag.com/en/issue6/m6quota1.html
and
http://www.gentoo.org/doc/en/security/security-handbook.xml?part=1&chap=5
• Disk quotas under Linux have the following features:
–
–
–
–
–
–
–
–
Per-file-system implementation
Per-user space accounting
Per-group space accounting
Tracks disk block usage
Tracks disk inode usage
Hard limits
Soft limits
Grace periods
• We will limit the users quota on /home
Preparing for Quota
• Enter Single User Mode (actually not nessesary)
# init 1
• Edit Your /etc/fstab File
Old fstab LABEL=/home
New fstab LABEL=/home
/home ext3 defaults 1 2
/home ext3 defaults,usrquota,grpquota 1 2
• Remount the filesystem
# mount -o remount /home
• Create The Partition Quota Configuration Files
# quotacheck -g -u -a
# chmod 600 /home/aquota.user
# chmod 600 /home/aquota.group
• Start the Quota
# quotaon -a
Setting the Quota limit
• Make Linux Read The Quota Config File
# edquota -u mp3user
The command will invoke the vi editor which will allow you to edit a number of
fields.
Disk quotas for user mp3user (uid 593):
Filesystem blocks
soft
hard inodes
/dev/hda3 24
0
0
7
• Change the soft level & hard level
Kalle softlimit of 5Mbyte would be 10000
Kalle hard limit could be set to 15000
• Get out of single user mod
# init 3
soft
0
hard
0
Managing Quota
• Enforcing Quotas
# quotacheck –vagu
- should be added as cron job
• Editing Grace Periods
# edquota –t
Grace period before enforcing soft limits for users:
Time units may be: days, hours, minutes, or seconds
Filesystem Block grace period Inode grace period
/dev/hda3
7days
7days
• Editing Group Quotas (group users)
# edquota –g users
• Getting Quota Reports
# repquota /home
*** Report for user quotas on device /dev/mapper/vg1-lv1
Block grace time: 7days; Inode grace time: 7days
Block limits
File limits
User
used soft hard grace used soft hard grace
----------------------------------------------------------------------------------------root
-- 32828
0
0
3 0
0
steen -4
1000 1500
1 0
0