Chapter 10 Managing a Disk

壹. 如何加入新硬碟

加入新硬碟的方法:

實際的例子:

Before we start
Before we start,我們必須確定兩點:一是現有磁碟中尚存未被使用的空間(Not formated),二是這些空間未在DOS下切割partition(Not Partitioned )。

Build an Ext3 File System on Linux
Step1.
將PC開啟成Linux系統,執行指令 fdisk [/dev/hda]劃分一個ext3的磁區。
***Linux fdisk指令與DOS非常類似,使用時須相當小心,以免將現有系統毀於一旦。

cc201:~# fdisk

Using /dev/hda as default device!                              ##fdisk未加參數,default 為/dev/hda
Command (m for help): p                                          ##顯示目前partition切割情形
Disk /dev/hda: 64 heads, 63 sectors, 620 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Begin Start End Blocks Id System
/dev/hda1 * 1 1 356 717664+ 6 DOS 16-bit >=32M
Command (m for help):n                                           ##Create a new partition
Command action
e extended
p primary partition (1-4)
p                                                                                ##Create a new primary partiton
Partition number (1-4): 2                                            ##/dev/hda1had been set,select create the second partiotion
First cylinder (357-620): 357                                      ##選擇第一個cylynder所在,一般就選剩餘空間的第一個
Last cylinder or +size or +sizeM or +sizeK ([357]-620): +10M      ##選擇新partition size
Command (m for help):p
Disk /dev/hda: 64 heads, 63 sectors, 620 cylinders
Units = cylinders of 4032 * 512 bytes
Device Boot Begin Start End Blocks Id System
/dev/hda5 * 1 1 356 717664+ 6 DOS 16-bit >=32M
/dev/hda2 357 357 362 12096 83 Linux native                                  ##新的partition已經產生
Command (m for help):w                                                               ##將partition資料寫入系統

Step 2.
Reboot系統,讓partition生效。

Step 3. (這個動作類似DOS2的format,目的是將硬碟製作成UNIX格式)
產生ext3 file system
cc201:~# mke2fs -c /dev/hda2
mke2fs 1.04, 16-May-96 for EXT2 FS 0.5b, 95/08/09
3024 inodes, 12096 blocks
604 blocks (4.99%) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2 block groups
8192 blocks per group, 8192 fragments per group
1512 inodes per group
Superblock backups stored on blocks:
8193
Checking for bad blocks (read-only test): done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
cc201:~#

Step 4.
Mount New File System
cc201:~# df (查看已掛載磁碟的總容量、使用容量)
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/hda1      717472    683504 33968      95%        /
cc201:~# mkdir /home1
cc201:~# mount -t ext3 -o rw /dev/hda2 /home1
cc201:~# df
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/hda1   717472     683520 33952        95%      /
/dev/hda2   11709           13     11092          0%      /home1                        ##New file system had been mounted as /home1
cc201:~#

貳. 檔案系統掛載 (Mount)

所有的檔案都存在不同的 partition 裡面﹐但 Linux 的檔案系統和Windows 的不太一樣,在windows裡﹐每一個 partition 都有各自的磁碟代號﹐從 C﹕開始一直到 Z﹕﹐超過了就讀不到了。在每一個磁碟代號裡面﹐都各自有其獨立的根目錄和子目錄﹐您必須明確指定哪一個磁碟代號才能找到相關檔案。

在 Linux 裡面, 沒有磁碟代號﹐一切都從 / 開始﹐/ 必須有自己的獨立的 partition﹐但其它 partition 則一律掛在 / 下面的目錄或子目錄中﹐我們稱這些用來掛載 partition 的目錄為 mount point﹐而將這些 partition 掛入相應的 mount point 的動作稱為 mount (掛載)。在掛入新的 parition 的時候﹐是無需重新開機的﹐任何時候都可以進行。當 partition 不再使用的時候﹐您也可以將之進行卸載(umount)。

在進行掛載的過程中﹐有一些規則我們是必須遵守的﹕

(1). Device Files: /dev

.To mount a file system, you have to specify its device name. The interfaces to devices that may be attached to your system are provided by special files known as device files. The names of these device files are the device names.

.Device files are located in the /dev directories and usually have abbreviated names ending with the number of the device.

     For example, the device name for your floppy drive is fd0 and is located in the directory /dev. /dev/fd0 references your floppy drive. If you have more than one floppy drive, they are represented by fd1, fd2, and so on.

.A complete listing of all device names can be found in the devices file located in the linux/doc/device-list directory at the www.kernel.org Web site. Table 10-1 lists several of the commonly used device names.

Table 10-1: Device Name Prefixes

Device Name

Description

hd

IDE hard drives 1-4 are primary partitions, and 5 and up are logical partitions

sd

SCSI hard drives

scd

SCSI CD-ROM drives (used on Red Hat)

sr

SCSI CD-ROM drives (alternative prefix name, may be used on other distributions)

fd

Floppy disks

st

SCSI tape drives

nst

SCSI tape drives, no rewind

ht

IDE tape drives

tty

Terminals

lp

Printer ports

pty

Pseudoterminals (used for remote logins)

js

Analog joy sticks

midi

Midi ports

ttyS

Serial ports

md

RAID devices

rd/cndn

Directory that holds RAID devices is rd. cn is the RAID controller and dn is the RAID disk for that controller

cdrom

Link to your CD-ROM device file

cdwriter

Link to your CD-R or CD-RW device file

modem

Link to your modem device file

floppy

Link to your floppy device file

tape

Link to your tape device file

scanner

Link to your scanner device file

 

(2). Mounting File Systems Using KDE, Gnome and commands

.Both the Gnome and K Desktop provide easy-to-use desktop icons for mounting and unmounting your file systems. Normally, these are used for manually mounted devices, such as CD-ROMs, tapes, and floppy disks. Hard disk partitions are usually mounted automatically at boot time.

.On Gnome 1.4, you select the CD-ROM to mount from the Desktop menu Disks submenu (right-click anywhere on the desktop). Right-clicking the icon displays a menu from which you can select an entry to mount or unmount the CD-ROM.

.On the K Desktop, if you want to add a new CD-ROM or floppy drive, right-click the desktop and select the entry either for the CD-ROM or floppy device from the Create New entry in the pop-up menu. Enter a name for the desktop file in the General panel. In the Device panel, enter the device name, mountpoint, and file system type. You can also select a mount and unmount icon.

2.1. The mount and umount Commands

You can also mount or unmount any file system using the mount and umount commands.

2.1.1. mount

.Normally, mounting file systems can only be done as the root user (unless the device is user mountable). To mount a file system, be sure to log in as the root user. Table 10-2 lists the different options for the mount command.

Table 10-2: The mount Command

Mount Option

Description

-f

Fakes the mounting of a file system. You use it to check if a file system can be mounted.

-v

Verbose mode. Mount displays descriptions of the actions it is taking. Use with -f to check for any problems mounting a file system, -fv.

-w

Mount the file system with read and write permission.

-r

Mount the file system with only read permission.

-n

Mount the file system without placing an entry for it in the mstab file.

-t type

Specify the type of file system to be mounted.

-a

Mount all file systems listed in /etc/fstab.

-o option-list

Mount the file system using a list of options. This is a comma- separated list of options following -o.

.The mount command takes two arguments, the syntax for the mount command is as follows: :

       # mount device mountpoint

       The mountpoint is the directory on your main directory tree where you want the files on the storage device attached.

       The device is a device file that connects your system to the hardware device.

.For any partition with an entry in the /etc/fstab file, you can mount it using only the mount directory specified in its fstab entry. You needn't enter the device filename. The mount command looks up the entry for it in the fstab file, using the directory to identify the entry and, in that way, find the device name. For example, to mount the /dev/hda1, the mount command only needs to know the directory it is mounted to-in this case, /mnt/dos.

# mount /mnt/dos

 

2.1.2. unmount

.You unmount a file system with the umount command. The umount command can take as its argument either a device name or the directory where it was mounted. Here is the syntax:

    # umount device-or-mountpoint

.The following example unmounts the floppy disk mounted to the /mydir directory:

# umount /dev/fd0

.Using the example where the device was mounted on the /mydir directory, you could use that directory to unmount the file system:

# umount /mydir

.You can never unmount a file system in which you are currently working.

 For example, suppose you mount the CD-ROM on the /mnt/ cdrom directory and then change to that /mnt/cdrom directory. If you decide to change CD-ROMs, you first have to unmount the current one with the umount command. This will fail because you are currently in the directory in which it is mounted. You first have to leave that directory before you can unmount the CD-ROM.

# mount /dev/hdc /mnt/cdrom
# cd /mnt/cdrom
# umount /mnt/cdrom
umount: /dev/hdd: device is busy
# cd /root
# umount /mnt/cdrom

.If other users are using a file system you are trying to unmount, you can use the lsof or the fuser commands to find out who they are.

2.1.3. Mounting CD-ROMs

    a.  You see an entry for this in the /etc/fstab file. With such an entry, to mount a CD-ROM, all you have to do is enter the command mount and the directory /mnt/cdrom. Once mounted, you can access the CD-ROM through the /mnt/cdrom directory.

      # mount /mnt/cdrom

    b. You are mounting a particular CD-ROM, not the CD-ROM drive. You cannot just remove the CD-ROM and put in a new one. The mount command has attached those files to your main directory tree, and your system expects to find them on a disc in your CD-ROM drive. To change discs, you must first unmount the CD-ROM already in your CD-ROM drive with the umount command. Your CD-ROM drive will not open until you issue this command. Then, after putting in the new disc, you must explicitly mount that new CD-ROM. You can then remove the CD-ROM and put in the new one. Then, issue a mount command to mount it.

      # umount /mnt/cdrom

    c. If you want to mount a CD-ROM to another directory, you have to include the device name in the mount command. The following example mounts the disc in your CD-ROM drive to the /mydir directory. The particular device name for the CD-ROM in this example is /dev/hdc.

      # mount /dev/hdc /mydir

 

2.1.4. Mounting Hard Drive Partitions: Linux and Windows

     a. You can mount Linux hard drive partitions with the mount command. The next example mounts the Linux hard disk partition on /dev/hda4 to the directory /mnt/mydata:

     # mount -t ext3 /dev/hda4 /mnt/mydata

    b. You can also mount a Windows partition and directly access the files on it. But you have to specify the file system type as Windows. For that, use the -t option, and then type vfat (msdos for MS-DOS). In the next example, the user mounts the Windows hard disk partition /dev/hda1 to the Linux file structure at directory /mnt/windows. The /mnt/windows directory is a common designation for Windows file systems, though you can mount it in any directory (/mnt/dos for MS-DOS). .

    # mount -t vfat /dev/hda1 /mnt/windows

(3). Mount Configuration: /etc/fstab

.You can simplify the process by placing mount information in the /etc/fstab configuration file. Using entries in this file, you can have certain file systems automatically mounted whenever your system boots.

.You can do this by directly and carefully editing the /etc/fstab file to type in a new entry.

.An entry in an fstab file contains several fields, each separated by a space or tab. These are described in the sequence shown here:

      <device> <mountpoint> <filesystemtype> <options> <dump> <fsck>

     1. The first field is the device name to be mounted. This usually begins with /dev, such as /dev/hda3 for the third hard disk partition.

     2. The next field is the directory in your file structure where you want the file system on this device to be attached.

     3. The third field is the type of file system being mounted. Table 10-3 provides a list of all the different types you can mount. The type for a standard Linux hard disk partition is ext3. The next example shows an entry for the main Linux hard disk partition. This entry is mounted at the root directory, /, and has a file type of ext2.

Table 10-3: File System Types

Type

Description

auto

Attempt to automatically detect the file system type

minux

Minux file systems (filenames are limited to 30 characters)

ext

Earlier version of Linux file system, no longer in use

ext2

Standard Linux file system supporting large filenames and file sizes; does not include journaling

ext3

Standard Linux file system supporting large filenames and file sizes; includes journaling

xiaf

Xiaf file system

msdos

File system for MS-DOS partitions (16-bit)

vfat

File system for Windows 95, 98, and Millennium partitions (32-bit)

ntfs

NT and Windows 2000 file systems

hpfs

File system for OS/2 high-performance partitions

proc

Used by operating system for processes

nfs

NFS file system for mounting partitions from remote systems

umsdos

UMS-DOS file system

swap

Linux swap partition or swap file

sysv

UNIX System V file systems

iso9660

File system for mounting CD-ROM

 

               3.1 The next example shows an entry for the main Linux hard disk partition. This entry is mounted at the root directory, /, and has a file type of ext2.

                      /dev/hda3       /                  ext2        defaults                   0   1

               3.2 The type of file system on a floppy drive could vary, often depending on the type floppy you are trying to mount. For example, you may want to read a Windows formatted floppy disk at one time and a Linux formatted floppy disk at another time. For this reason, the file system type specified for the floppy device is auto. With this option, the type of file system formatted on the floppy disk will be automatically detected and the appropriate file system type used.

            /dev/fd0  /mnt/floppy  auto   defaults,noauto   0 0

          4. The 4th field lists the different options for mounting the file system. You can list specific options next to each other separated by a comma (no spaces).

               4.1 You can specify a default set of options by simply entering defaults. The defaults option specifies that a device is read/write (rw), asynchronous (async), a block device (dev), cannot be mounted by ordinary users (user) , and that programs can be executed on it (exec).

               4.2  CD-ROM only has a few options listed for it: ro and noauto. ro specifies this is read-only; and noauto specifies this is not automatically mounted. The noauto option is used with both CD-ROMs and floppy drives, so they won't automatically mount because you do not know if you have anything in them when you start up.

               4.3 Table 10-4 lists the options for mounting a file system. 

               4.4 An example of CD-ROM and floppy drive entries follows. Notice the type for a CD-ROM file system is different from a hard disk partition, iso9660. The floppy drive entry also has all the default options of the hard disk partitions, with the exception that it is not automatically mounted.

             /dev/hdc /mnt/cdrom   iso9660 ro,noauto          0 0
             /dev/fd0 /mnt/floppy  auto    defaults,noauto    0 0
Table 10-4: Mount Options for File Systems:-o and /etc/fstab

Option

Description

async

All I/O to the file system should be done asynchronously.(當有I/O動作時不立即進行)

auto

Can be mounted with the -a option. A mount -a command executed when the system boots, in effect, mounts file systems automatically.

defaults

Use default options: rw, suid, dev, exec, auto, nouser, and async.

dev

Interpret character or block special devices on the file system.

noauto

Can only be mounted explicitly. The -a option does not cause the file system to be mounted.

exec

Permit execution of binaries.

nouser

Forbid an ordinary (that is, nonroot) user to mount the file system.

remount

Attempt to remount an already mounted file system. This is commonly used to change the mount flags for a file system, especially to make a read-only file system writable.

ro

Mount the file system read-only.

rw

Mount the file system read/write.

suid

Allow set-user-identifier or set-group-identifier bits to take effect.

sync

All I/O to the file system should be done synchronously.

user

Enable an ordinary user to mount the file system. Ordinary users always have the following options activated: noexec, nosuid, and nodev.

nodev

Do not interpret character or block special devices on the file system.

nosuid

Do not allow set-user-identifier or set-group-identifier bits to take effect.

              5. The last two fields consist of an integer value. The first one is used by the dump command to determine if a file system needs to be dumped, backing up the file system. The last one is used by fsck to see if a file system should be checked at reboot and in what order. If the field has a value of 1, it indicates a boot partition, and 2 indicates other partitions. The 0 value means the fsck needn't check the file system.

.Example, A copy of an /etc/fstab file is shown here. Notice the first line is comment. All comment lines begin with a #. The entry for the /proc file system is a special entry used by your Linux operating system for managing its processes, and it is not an actual device. The /proc and swap partition entries are particularly critical.

/etc/fstab
Start example
# <device> <mountpoint> <filesystemtype> <options> <dump><fsck>
/dev/hda3  /            ext3             defaults  0     1
/dev/hdc   /mnt/cdrom   iso9660          ro,noauto 0     0
/dev/fd0   /mnt/floppy  auto             defaults,
                                         noauto    0 
/proc      /proc        proc             defaults
/dev/hda2  none         swap             sw
/dev/hda1  /mnt/windows vfat             defaults  0     0
End example

.You can mount either MS-DOS or Windows 95 partitions onto your Linux file structure. You only have to specify the file type of vfat for Windows 95 and msdos for MS-DOS. To do this, you need to put an entry for your Windows partitions in your /etc/fstab file and give it the defaults option or be sure to include an auto option. The /mnt/windows directory would be a logical choice. The next example shows a standard MS-DOS partition entry for an /etc/fstab file.

         /dev/hda1 /mnt/windows vfat defaults 0 0

.If your /etc/fstab file ever becomes a line gets deleted accidentally then your system will boot into a maintenance mode, giving you read-only access to your partitions. To gain read/write access so you can fix your /etc/fstab file, you have to remount your main partition. The following command performs such an operation:

         # mount -n -o remount,rw /

.If you would want to make the CD-ROM user mountable, add the user option.

/dev/hdc /mnt/cdrom iso9660 ro,noauto,user 0 0

.The "automatic" mounting of file systems from /etc/fstab is actually implemented by executing a mount -a command in the /etc/rc.d/rc.sysinit file that is run whenever you boot. The mount -a command will mount any file system listed in your /etc/fstab file that does not have a noauto option. The umount -a option will unmount the file systems in /etc/fstab (which is executed when you shut down your system).

 

參. 磁碟空間管理(Disk Quota)

On your Linux system, unused disk space is held as a common resource that each user can access as they need it. As a user creates more files, they take the space they need from the pool of available disk space. In this sense, all the users are sharing this one resource of unused disk space. However, if one user were to use up all the remaining disk space, then none of the other users would be able to create files.

To counter this problem, you can create disk quotas on particular users, limiting the amount of available disk space they can use.

一. Concept

1.什麼是Disk Quota
      DISK Quota是系統管理者可用來管理磁碟使用空間的一種工具,利用quota管理者可以在兩方面控制user不至於濫用磁碟,一是磁碟空間,一是檔案數目。UNIX中quota的設定是以file system為對象,而非整個系統,也就是說你可只針對某一file system設定quota。
 

2. Soft Limit Hard Limit & Grace Period
      Soft Limit、Hard Limit & Grace Period是管理者利用quota觀念,對user使用磁碟所設的三個限制。

    .Soft Limit
          Soft Limit 是user使用所能使用磁碟空間的大小,Soft Limit是一個警戒線,使用者使用磁碟空間超過這個警戒線,就會開始收到系統所給的警告。

    .Hard Limit
          Hard Limit代表的是user對於磁碟空間使用的絕對上限,使用者使用磁碟空間無法超越此一上限。

    .Grace period
          Grace period是user可被容許超過soft limit 的時間,假設Grace period設定為3天,超過soft limit的user在三天內未處理超過limit的檔案的話,user將無法再使用任何磁碟空間。
 

3. Disk Quota on Linux
         Linux支援disk quota,但目前僅限於linux系列的 file system,為了練習disk quota,接下來我們將在現有的linux上建立一個ext3的檔案系統。


二. Setting up Quota on Linux Ext3 File System

Quota Setup: Quotas are enabled using the quotacheck and quotaon programs. On Fedora core, they are executed in the /etc/rc.d/rc.sysinit script, which is run whenever you start up your system.

Step1: To enables quota controls for users and groups

Step 2: To create quota.user and quota.group files, these are the quota databases used to hold the quota information for each user and group.

           For example, create quota record "quota.user" on the base of new file system(/home1)
                                 cc201:/etc#touch  /home1/quota.user
                                 cc201:/etc#chmod  600 /home1/quota.user
 

Step3: To reboot


Enforce disk quota on users

Step 1: 空間設定

Table 10-5: The options edquota and quota

edquota

Description

-u

Edit the user quota. This is the default.

-g

Edit the group quota.

-p

Duplicate the quotas of the prototypical user specified for each user specified. This is the normal mechanism used to initialize quotas for groups of users.

-t

Edit the soft time limits for each file system.

   

quota

Description

-g

Print group quotas for the group of which the user is a member.

-u

Print the user's quota.

-v

Will display quotas on file systems where no storage is allocated.

-q

Print information on file systems where usage is over quota.

Step 2: 時間設定

其他相關的命令

Each partition that has quotas enabled will have its own quota database. You can check the validity of your quota database with the quotacheck command. You can turn quotas on an off using the quotaon and quotaoff commands. When you start up your system, quotacheck is run to check the quota databases and then quotaon is run to turn on quotas.

As the system administrator, you can use the repquota command to generate a summary of memory usage, checking to see what users are approaching or exceeding quota limits. Individual users can use the quota command to check their memory use and how much disk space they have left in their quota.

        cc201:/home1# quotacheck -af
          //加上-f是強制執行。