Linux FileSystem Intro.
Device
character device
echo hello hello > /dev/null
cat music.wav > /dev/dsp
block device
ll /dev | grep '^b'
Disk Geometry
Platters
Tracks
Cylinders
Heads
Sectors
512 Bytes
CHS Addressing
Cylinder x Head x Sector x 512 (cylinder<=1024)
Logical Block Address
LBA
BIOS: 1.INT 13h 2. INT 13h Extension
Capacity calculate
Cylinder=1647, Head=16, Sector=63
Why
Why use Cylinder/Head/Sector to caculator capacity
Master Boot Record
sector 1 of cylinder 0, head 0
MBR
1. Bootloader (446 bytes)
2. Partition Table (64 bytes)
3. Magic Number (2 bytes):0x55AA
MBR Backup
dd if=/dev/hda of=mbr.bin bs=512 count=1
MBR Restore
dd if=mbr.bin of=/dev/hda bs=512 count=1
MBR View
od -xa mbr.bin
Bootloader Cleanup
right way : dd if=ms.mbr
of=/dev/hda bs=446 count=1
wrong way : dd if=/dev/zero of=/dev/hda bs=446 count=1
Bootloader
Boot Loader(Manager) kind :
1.grub 2.lilo 3.ntldr 4.spfdisk
Boot Manager
1. stage1
2. stage1.5
3. stage2 (kernel loader)
Boot Manager Location
1.MBR 2.first block of partition
grub
GRand Unified Bootloader
/boot/grub/menu.lst
grub-install /dev/sda
Partition Table
4 partition : Primary + Extended
Partitions
From x cylinder To y cylinder
Partition ID
82:swap 83:ext2/ext3 0b:fat32
Partition tools
1.fdisk 2.cfdisk
3.sfdisk 4.parted
fdisk
fdisk -l
fdisk -l /dev/sda
HD tools
hdparm -d1 -c1 -X69 /dev/sda
Why
Why fdisk target to /dev/sda not /dev/sda1
OS
Format
ext2 ext3
ReiserFS
JFS
XFS
Format tools
mkfs.[TAB][TAB]
mkfs.ext3 /dev/sda5
mkfs.vfat /dev/sda6
mkfs.reiserfs /dev/sda7
Why
Why mkfs target to /dev/sda1 not /dev/sda
Ext2 allocation
1. file attribute (ls -l)
2. file content (cat)
inode
@inode=128 bytes
1. file attribute(stat)
2. file pointer
block
@block=8 sectors = 4096 Bytes
file content
inode trace
ls -lid / /etc /etc/fstab
inode estimate
x=partition capacity(1G)
y=the size of inode controled block(8K)
inode quantity = x / y
block estimate
x=partition capacity(1G)
y=the size of block(8K)
block amount = x / y
Ext2 filesystem
SuperBlock / Group Description
Block bitmap / Inode bitmap
Inode table / Block area
SuperBlock
total/used/unused amount of inode&block
size of one inode&block
mount/write/check time
[maximum] mount count
Block bitmap
store unused block
Inode bitmap
store unused inode
Inconsistent
e2fsck
fsck.[TAB][TAB]
remember umount filesystem first
Journal
write to journal when start
write attribute&data(inode,block)
write metadata(superblock,...)
write to journal when complete
asynchronous write
umount
sync
mount
attach a filesystem to a mount point,
so the filesystem is accessible.
mount point
a directory, empty or not.
/media
/mnt
How to
# mount -t vfat -o iocharset=utf8 /dev/hde1 /media/hde1
1.fs type 2.option
3.device 4.mount point
umount
umount /mnt/temp
umount: /mnt/temp: device is busy
fs type
kernel modules
$ ls -l /lib/modules/`uname -r`/kernel/fs
probe fs type
probe superblock
1. no -t option
2. -t auto
auto is useful for removable devices
probe fail
when probes fail
1. /etc/filesystems
2. /proc/filesystems
options
# mount -t vfat
-o umask=007,
iocharset=utf8,
uid=1000,gid=46
mount cdrom
# mount -t iso9660 /dev/sr1 /mnt/burner
file ubuntu.iso
# mount -t iso9660 -o loop ubuntu.iso /mnt/cdrom
loop file
dd if=/dev/zero of=hd500.img bs=1048576 count=500
mkfs.ext3 hd500.img
#mount -t ext3 -o loop hd500.img /mnt/temp
loop device
dd if=/dev/zero of=hd500.img bs=1M count=500
#losetup /dev/loop1 hd500.img
#mkfs.ext3 /dev/loop1
loop device
#mount -t ext3 /dev/loop1 /mnt/temp
df -T
#umount /mnt/temp
#losetup -d /dev/loop1
fstab
6 columns per line, separated by white space.
# vim /etc/fstab
# mount -a
defaults=rw,suid,dev,exec,auto,nouser,async
mount NTFS
# mount -t ntfs ...
# mount -t ntfs-3g ...
fs utility
debugfs
debugfs [-w] /dev/sdb1
debugfs: help
debugfs: lsdel
debugfs: undel <inode> SaveAsFilename
debugfs: quit
dumpe2fs
dumpe2fs -h /dev/sda2
tune2fs
tune2fs -l /dev/sda2
badblocks
badblocks -v /dev/sda5
df
df -iT
df -ahT
du
du -a /tmp
dd
dd if=... of=... bs=... count=...
swap fs
fdisk /dev/hda(id=82)
mkswap /dev/hda1
swapon /dev/hda1
swapoff
swap file
dd if=/dev/zero of=/tmp/swap bs=4k count=16382 (64MB)
mkswap /tmp/swap
swapon /tmp/swap
swapoff /tmp/swap
Hard Link
cd /tmp ; cp -a /etc/passwd .
du -sb ; df -i .
ln passwd passwd-ha
du -sb ; df -i .
Symbolic Link
ln -s passwd passwd-sy
du -sb ; df -i .
ls -il passwd*
remove source
rm passwd
cat passwd-ha
cat passwd-sy
why
1. hard link can't cross filesystem
2. hard link can't link directory
3. hard link still work even source file deleted
why
4. symbolic link can't work when source file deleted
5. when make directory will incremental it's link number
tmpfs
mount tmpfs /tmp -t tmpfs -o size=64m
vi /etc/fstab
tmpfs /tmp tmpfs size=64m,mode=1777 0 0
Refrence