В началоUnix Toolbox → 3. FILE SYSTEM
Gentoo-doc HOME Пред.: 2. PROCESSESВ началоУровень выше: Unix ToolboxСлед.: 4. NETWORK

3. 3. FILE SYSTEM

3.1. 3.1 Permissions

Change permission and ownership with chmod and chown. The default umask can be changed for all users in /etc/profile for Linux or /etc/login.conf for FreeBSD. The default umask is usually 022. The umsak is subtracted from 777, thus umask 022 results in a permission 0f 755.

1 --x execute # Mode 764 = exec/read/write | read/write | read

2 -w- write # For: |-- Owner --| |- Group-| |Oth|

4 r-- read

ugo=a u=user, g=group, o=others, a=everyone

# chmod [OPTION] MODE[,MODE] FILE # MODE is of the form [ugoa]*([-+=]([rwxXst]))

# chmod 640 /var/log/maillog # Restrict the log -rw-r-----

# chmod u=rw,g=r,o= /var/log/maillog # Same as above

# chmod -R o-r /home/* # Recursive remove other readable for all users

# chmod u+s /path/to/prog # Set SUID bit on executable (know what you do!)

# find / -perm -u+s -print # Find all programs with the SUID bit

# chown user:group /path/to/file # Change the user and group ownership of a file

# chgrp group /path/to/file # Change the group ownership of a file

3.2. 3.2 Disk information

# diskinfo -v /dev/ad2 # information about disk (sector/size) FreeBSD

# hdparm -I /dev/sda # information about the IDE/ATA disk (Linux)

# fdisk /dev/ad2 # Display and manipulate the partition table

# smartctl -a /dev/ad2 # Display the disk SMART info

3.3. 3.3 Boot

FreeBSD

To boot an old kernel if the new kernel doesn't boot, stop the boot at during the count down.

# unload

# load kernel.old

# boot

3.4. 3.4 System mount points/Disk usage

# mount | column -t # Show mounted file-systems on the system

# df # display free disk space and mounted devices

# cat /proc/partitions # Show all registered partitions (Linux)

Disk usage

# du -sh * # Directory sizes as listing

# du -csh # Total directory size of the current directory

# du -ks * | sort -n -r # Sort everything by size in kilobytes

# ls -lSr # Show files, biggest last

3.5. 3.5 Who has which files opened

This is useful to find out which file is blocking a partition which has to be unmounted and gives a typical error of:

# umount /home/

umount: unmount of /home # umount impossible because a file is locking home

failed: Device busy

FreeBSD and most Unixes

# fstat -f /home # for a mount point

# fstat -p PID # for an application with PID

# fstat -u user # for a user name

Find opened log file (or other opened files), say for Xorg:

# ps ax | grep Xorg | awk '{print $1}'

1252

# fstat -p 1252

USER CMD PID FD MOUNT INUM MODE SZ|DV R/W

root Xorg 1252 root / 2 drwxr-xr-x 512 r

root Xorg 1252 text /usr 216016 -rws--x--x 1679848 r

root Xorg 1252 0 /var 212042 -rw-r--r-- 56987 w

The file with inum 212042 is the only file in /var:

# find -x /var -inum 212042

/var/log/Xorg.0.log

Linux

Find opened files on a mount point with fuser or lsof:

# fuser -m /home # List processes accessing /home

# lsof /home

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

tcsh 29029 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)

lsof 29140 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)

About an application:

ps ax | grep Xorg | awk '{print $1}'

3324

# lsof -p 3324

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

Xorg 3324 root 0w REG 8,6 56296 12492 /var/log/Xorg.0.log

About a single file:

# lsof /var/log/Xorg.0.log

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

Xorg 3324 root 0w REG 8,6 56296 12492 /var/log/Xorg.0.log

3.6. 3.6 Mount/remount a file system

For example the cdrom. If listed in /etc/fstab:

# mount /cdrom

Or find the device in /dev/ or with dmesg

FreeBSD

# mount -v -t cd9660 /dev/cd0c /mnt # cdrom

# mount_cd9660 /dev/wcd0c /cdrom # other method

# mount -v -t msdos /dev/fd0c /mnt # floppy

Entry in /etc/fstab:

# Device Mountpoint FStype Options Dump Pass#

/dev/acd0 /cdrom cd9660 ro,noauto 0 0

To let users do it:

# sysctl vfs.usermount=1 # Or insert the line "vfs.usermount=1" in /etc/sysctl.conf

Linux

# mount -t auto /dev/cdrom /mnt/cdrom # typical cdrom mount command

# mount /dev/hdc -t iso9660 -r /cdrom # typical IDE

# mount /dev/sdc0 -t iso9660 -r /cdrom # typical SCSI

Entry in /etc/fstab:

/dev/cdrom /media/cdrom subfs noauto,fs=cdfss,ro,procuid,nosuid,nodev,exec 0 0

Mount a FreeBSD partition with Linux

Find the partition number containing with fdisk, this is usually the root partition, but it could be an other BSD slice too. If the FreeBSD has many slices, they are the one not listed in the fdisk table, but visible in /dev/sda* or /dev/hda*.

# fdisk /dev/sda # Find the FreeBSD partition

/dev/sda3 * 5357 7905 20474842+ a5 FreeBSD

# mount -t ufs -o ufstype=ufs2,ro /dev/sda3 /mnt

/dev/sda10 = /tmp; /dev/sda11 /usr # The other slices

Remount

Remount a device without unmounting it. Necessary for fsck for example

# mount -o remount,ro / # Linux

# mount -o ro / # FreeBSD

Copy the raw data from a cdrom into an iso image:

# dd if=/dev/cd0c of=file.iso

3.7. 3.7 Mount an SMB share

Suppose we want to access the SMB share myshare on the computer smbserver, the address as typed on a Windows PC is \\smbserver\myshare\. We mount on /mnt/smbshare. Warning> cifs wants an IP or DNS name, not a Windows name.

Linux

# smbclient -U user -I 192.168.16.229 -L //smbshare/ # List the shares

# mount -t smbfs -o username=winuser //smbserver/myshare /mnt/smbshare

# mount -t cifs -o username=winuser,password=winpwd //192.168.16.229/myshare /mnt/share

Additionally with the package mount.cifs it is possible to store the credentials in a file, for

example /home/user/.smb:

username=winuser

password=winpwd

And mount as follow:

# mount -t cifs -o credentials=/home/user/.smb //192.168.16.229/myshare /mnt/smbshare

FreeBSD

Use -I to give the IP (or DNS name); smbserver is the Windows name.

# smbutil view -I 192.168.16.229 //winuser@smbserver # List the shares

# mount_smbfs -I 192.168.16.229 //winuser@smbserver/myshare /mnt/smbshare

3.8. 3.8 Mount an image

Linux loop-back

# mount -t iso9660 -o loop file.iso /mnt # Mount a CD image

# mount -t ext3 -o loop file.img /mnt # Mount an image with ext3 fs

FreeBSD

With memory device (do # kldload md.ko if necessary):

# mdconfig -a -t vnode -f file.iso -u 0

# mount -t cd9660 /dev/md0 /mnt

# umount /mnt; mdconfig -d -u 0 # Cleanup the md device

Or with virtual node:

# vnconfig /dev/vn0c file.iso; mount -t cd9660 /dev/vn0c /mnt

# umount /mnt; vnconfig -u /dev/vn0c # Cleanup the vn device

Solaris and FreeBSD

with loop-back file interface or lofi:

# lofiadm -a file.iso

# mount -F hsfs -o ro /dev/lofi/1 /mnt

# umount /mnt; lofiadm -d /dev/lofi/1 # Cleanup the lofi device

3.9. 3.9 Create and burn an ISO image

This will copy the cd or DVD sector for sector. Without conv=notrunc, the image will be smaller if there is less content on the cd. See below and the dd examples (page 38).

# dd if=/dev/hdc of=/tmp/mycd.iso bs=2048 conv=notrunc

Use mkisofs to create a CD/DVD image from files in a directory. To overcome the file names restrictions: -r enables the Rock Ridge extensions common to UNIX systems, -J enables Joliet extensions used by Microsoft systems. -L allows ISO9660 filenames to begin with a period.

# mkisofs -J -L -r -V TITLE -o imagefile.iso /path/to/dir

On FreeBSD, mkisofs is found in the ports in sysutils/cdrtools.

Burn a CD/DVD ISO image

FreeBSD

FreeBSD does not enable DMA on ATAPI drives by default. DMA is enabled with the sysctl command and the arguments below, or with /boot/loader.conf with the following entries:

hw.ata.ata_dma="1"

hw.ata.atapi_dma="1"

Use burncd with an ATAPI device (burncd is part of the base system) and cdrecord (in sysutils/

cdrtools) with a SCSI drive.

# burncd -f /dev/acd0 data imagefile.iso fixate # For ATAPI drive

# cdrecord -scanbus # To find the burner device (like 1,0,0)

# cdrecord dev=1,0,0 imagefile.iso

Linux

Also use cdrecord with Linux as described above. Additionally it is possible to use the native ATAPI interface which is found with:

# cdrecord dev=ATAPI -scanbus

And burn the CD/DVD as above.

Convert a Nero .nrg file to .iso

Nero simply adds a 300Kb header to a normal iso image. This can be trimmed with dd.

# dd bs=1k if=imagefile.nrg of=imagefile.iso skip=300

Convert a bin/cue image to .iso

The little bchunk program2 can do this. It is in the FreeBSD ports in sysutils/bchunk.

# bchunk imagefile.bin imagefile.cue imagefile.iso

3.10. 3.10 Create a file based image

For example a partition of 1GB using the file /usr/vdisk.img.

FreeBSD

# dd if=/dev/random of=/usr/vdisk.img bs=1K count=1M

# mdconfig -a -t vnode -f /usr/vdisk.img -u 1 # Creates device /dev/md1

# bsdlabel -w /dev/md1

# newfs /dev/md1c

# mount /dev/md1c /mnt

# umount /mnt; mdconfig -d -u 1; rm /usr/vdisk.img # Cleanup the md device

Linux

# dd if=/dev/zero of=/usr/vdisk.img bs=1024k count=1024

# mkfs.ext3 /usr/vdisk.img

2.http://freshmeat.net/projects/bchunk/

# mount -o loop /usr/vdisk.img /mnt

# umount /mnt; rm /usr/vdisk.img # Cleanup

Linux with losetup

/dev/zero is much faster than urandom, but less secure for encryption.

# dd if=/dev/urandom of=/usr/vdisk.img bs=1024k count=1024

# losetup /dev/loop0 /usr/vdisk.img # Creates and associates /dev/loop0

# mkfs.ext3 /dev/loop0

# mount /dev/loop0 /mnt

# losetup -a # Check used loops

# umount /mnt

# losetup -d /dev/loop0 # Detach

# rm /usr/vdisk.img

3.11. 3.11 Create a memory file system

A memory based file system is very fast for heavy IO application. How to create a 64 MB partition mounted on /memdisk:

FreeBSD

# mount_mfs -o rw -s 64M md /memdisk

# umount /memdisk; mdconfig -d -u 0 # Cleanup the md device

md /memdisk mfs rw,-s64M 0 0 # /etc/fstab entry

Linux

# mount -t tmpfs -osize=64m tmpfs /memdisk

3.12. 3.12 Disk performance

Read and write a 1 GB file on partition ad4s3c (/home)

# time dd if=/dev/ad4s3c of=/dev/null bs=1024k count=1000

# time dd if=/dev/zero bs=1024k count=1000 of=/home/1Gb.file

# hdparm -tT /dev/hda # Linux only

Пред.: 2. PROCESSESВ началоУровень выше: Unix ToolboxСлед.: 4. NETWORK
В началоUnix Toolbox → 3. FILE SYSTEM