120 likes | 209 Views
System Administration – Part 2. Devices in UNIX. Devices in UNIX are files: A device can be accessed with different file names All device files are stored in /dev or its sub-directories Device files can be grouped in to 2 main categories:
E N D
Devices in UNIX • Devices in UNIX are files: • A device can be accessed with different file names • All device files are stored in /dev or its sub-directories • Device files can be grouped in to 2 main categories: • b (block devices) include floppy, CD-ROM, disk drives, DVD • All data is read & written in blocks an uses a buffer cache. • Example file listing: brw-rw-rw- fd0 • c (character devices) include terminals, printers and tape • drives. Also known as raw devices. • Example file listing: crw-rw-rw- tty1A • Device file do not contain any data.
How the UNIX File System Is Organized on Disk Drives • Each drive is organized in the form of a directory structure with own sub-root • Each disk must have at least 1 file system on it • UNIX usually resides on multiple file systems. Mounting combines them into one logical system • Root File System: Contains bare-bones UNIX - /root,/bin, /etc,/dev,/lib • UNIX often has a swap file system where the kernel temporarily moves processes out of memory while it is waiting, e.g., I/O transfers, to swap in once process is ready to run again.
Mounting Disk Drives in UNIX • Secondary Disk drives must be mounted to be known to the kernel: • When a new file system is created, root does not know of it. • mount attaches all secondary file systems to the root file system. • Enables root to be main file system with the root directory the directory of a unified file system. • Example: mount –F ufs /dev/dsk/ct08d0s1 /oracle • mount command by it self lists all mounted drives. • A mount point is the location in the operating system's directory structure where a mounted file system appears by the name provided, i.e., a child directory of root. • Example: /oracle is the device’s mount-point in the above
Unmounting Disk Drives in UNIX • The umount(note spelling!) command disassociates and detaches a • secondary file system . Example (system-dependent, like mount): • umount /oracle # linux • umount /dev/hda3 /oracle # linux • umount /dev/dsk/ct08d0s1 # solaris • If a mounted file system is being used by a user, i.e., the user has cd’ to /oracle, the umountcommand will fail. • # umount: /oracle busy • To use umount, the user must be in a directory closer to root than the file system being detached.
What’s in a UNIX File System? Boot block: Referred to as Master Boot Record (MBR). Contains small boot program and the partition table. Superblock: Contains global info about the file sytem, includes a list of free inodes and data blocks. inode blocks: Contains the inode value for each file of the file system. When file is created, its inode entry is allocated here. Contains array of disk block addresses. Data blocks: Contains all user created data and programs. Although disk blocks are numbered consecutively, the file’s data may be arranged in non-contiguous blocks on the drive. Note: Blocks are usually 512 bytes (or 1024 bytes in Linux).
When Good File Systems Go Bad • Every 30 seconds the update daemon writes copies of the superblock to disk using sync. But what happens if power is lost before a sync is performed? • The file system can lose its integrity in ways like: • two inodes can claim the same disk block • a used block is marked free • a free block is not listed in the superblock • There is a periodic need to check (and sometimes repair) a file system that may have gone bad.
Checking the File System • fsck– File System Consistency Check • Used if file system fails to mount. Checks and repairs damaged file system (dirty, not clean) . • Damage often occurs from abnormal shutdown due to hardware failure, power failure or switching off without proper shutdown. • If cannot be repaired, then reinstallation of system may be required. • Example: # fsck /dev/da0s1a • ** phase 1 - Check Blocks and Sizes • ** phase 2 - Check Pathnames • ** phase 3 - Check Connectivity • ** phase 4 - Check Reference Counts • ** phase 5 - Check Free List • # (checks out ok, otherwise answer questions to fix)
Monitoring Free Disk Space df - reports amount of free space for each file system (separately). $ df -k Filesystem 1024-blocks Free %Used Iused %Iused Mounted on /dev/hd4 32768 16016 52% 2271 14% / /dev/hd2 4587520 1889420 59% 37791 4% /usr /dev/hd9var 65536 12032 82% 518 4% /var /dev/hd3 819200 637832 23% 1829 1% /tmp /dev/hd1 524288 395848 25% 421 1% /home /proc - - - - - /proc /dev/hd10opt 65536 26004 61% 654 4% /opt
Monitoring Disk Space Used • du - reports used disk space for each subordinate directory (separately). • Example: directory usage listed in kilobytes: • $ du -k * • 152304 ./junk1 • 1856548 ./junk2 • Example: directory usage in human-readable format: • $ du -h * • 149M ./junk1 • 1.8G ./junk2 • 1.3K ./junk3 • Example: report usage of all subdirectories and files including • hidden files, sorted by filesize : • $du -k .[A-z]* * | sort -n
Backing Up Files Using tar • tar – backup and restore files (tape archive, but other devices are OK too) • Example: # cd /home • # tar –cvf /dev/rdsk/f0q18dt ./rick • -c creates a new archive • -v displays the progress of the backup (verbose mode) • -f use the specified backup device • The files being backed up are specified using a relative pathname • so they can be restored in a different directory if needed.
Restoring Files Using tar Use the –t option to display the table of contents. Use –x to restore the files. Example: Create an incremental backup and then restore it. # tar –cvf /dev/rct0 `find /home/rick –newer .lt –print` # touch .lt # tar –tvf /dev/rct0 rw-r--r-- 203/50 470 Jun 4 09:35 2010 ./grades rwxr-xr-x 203/50 470 Jun 4 10:46 2010 ./test.sh rwxr-xr-x 203/50 470 Jun 3 02:35 2010 ./a.out # tar –xvf /dev/rct0 x /home/rick/grades 169 bytes, 1 tape blocks x /home/rick/test.sh 4855 bytes, 10 tape blocks x /home/rick/a.out 7505 bytes, 15 tape blocks