20150901日课程作业(挂载硬盘,手动创建SWAP分区,链接文件,磁盘及文件系统相关管理命令)

一、作业(练习)内容:

1、复习本次课程所讲的内容;

硬链接ln SRC DEST

   2个文件路径指向同一个Inod

   不能跨文件系统,目录不支持硬链接,创建硬链接会增加inod引用计数

软链接:ln -s SRC DEST

    软链接是独立的文件,指向被链接的文件,其大小不是被链接文件的大小,而是指向被链接文件的文件路径的字符串字节大小,可以看作是原文件的快捷方式

硬盘挂载

     列出当前磁盘情况:fdisk -l

     分区:fdisk /dev/DEVICE

     通知内核更新分区信息:partx -a /dev/DEVICE

     格式化:mkfs.FILETYPE /dev/DEVICE,mke2fs -t FILETYPE /dev/DEVICE

     挂载:mount /dev/DEVICE MOUNT_POINT

     添加开机自动挂载:/etc/fstab

e2label,tune2fs,dumpe2fs,fsck,df,du,fuser,free,blkid

手动创建SWAP分区

     swap有两种:

         1.使用分区做为SWAP

         2.使用文件做为SWAP

         使用分区做为SWAP比较好,速度比文件的快,也不容易产生磁盘碎片所有应该尽量使用分区作为SWAP

2、总结整理磁盘管理及文件系统管理中设计的各种命令的使用,并附注一定的示例

  ①、新建一个2G的交换分区

使用分区作为SWAP分区:

[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          981M       178M       802M       240K        10M        53M
-/+ buffers/cache:       114M       867M 
Swap:         2.0G         0B       2.0G 
[root@localhost ~]# fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00004c84

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        3889    30720000   83  Linux
/dev/sda3            3889        4150     2097152   82  Linux swap / Solaris
/dev/sda4            4150       13055    71527424    5  Extended
/dev/sda5            4150       13055    71526400   83  Linux

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc5a5780c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-2610, default 1307):    
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +2G

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): l

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xc5a5780c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        1568     2104515   82  Linux swap / Solaris

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  104857600 sda
   8        1     512000 sda1
   8        2   30720000 sda2
   8        3    2097152 sda3
   8        4          1 sda4
   8        5   71526400 sda5
   8       16   20971520 sdb
   8       17   10490413 sdb1
   8       18    2104515 sdb2
[root@localhost ~]# 
[root@localhost ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
no label, UUID=0acec160-247f-4d03-91ee-dc7129c708aa
[root@localhost ~]# file /dev/sdb2
/dev/sdb2: block special
[root@localhost ~]# swapon /dev/sdb2
[root@localhost ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2097148 0       -1
/dev/sdb2                               partition       2104508 0       -2
[root@localhost ~]# free
             total       used       free     shared    buffers     cached
Mem:       1004772     185608     819164        240      11756      55404
-/+ buffers/cache:     118448     886324 
Swap:      4201656          0    4201656 
[root@localhost ~]# echo "UUID=69465a2e-7ab3-4d44-a755-10b3ffde416c swap swap defaults 0 0">>/etc/fstab 
[root@localhost ~]# mount -a
[root@localhost ~]#

使用文件作为swap:

[root@localhost ~]# dd if=/dev/zero of=/tmp/swapfile bs=1024 count=2048000 
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 8.52182 s, 246 MB/s
[root@localhost ~]# file /tmp/swapfile 
/tmp/swapfile: data
[root@localhost ~]# mkswap /tmp/swapfile 
mkswap: /tmp/swapfile: warning: don't erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=9c57a77d-03d8-4eed-adc6-27b6404dd70f
[root@localhost ~]# file /tmp/swapfile 
/tmp/swapfile: Linux/i386 swap file (new style) 1 (4K pages) size 511999 pages
[root@localhost ~]# swapon /tmp/swapfile 
[root@localhost ~]# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda3                               partition       2097148 0       -1
/dev/sdb2                               partition       2104508 0       -2
/tmp/swapfile                           file            2047996 0       -3
[root@localhost ~]# free -h
             total       used       free     shared    buffers     cached
Mem:          981M       915M        66M       240K       5.2M       768M
-/+ buffers/cache:       141M       839M 
Swap:         6.0G         0B       6.0G 
[root@localhost ~]# blkid /tmp/swapfile 
/tmp/swapfile: UUID="9c57a77d-03d8-4eed-adc6-27b6404dd70f" TYPE="swap" 
[root@localhost ~]# echo "UUID=9c57a77d-03d8-4eed-adc6-27b6404dd70f swap swap defaults 0 0" >>/etc/fstab 
[root@localhost ~]# mount -a
[root@localhost ~]#

  ②df,du,fuesr,

[root@localhost ~]# df -TH
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4    31G  2.8G   27G  10% /
tmpfs          tmpfs  515M     0  515M   0% /dev/shm
/dev/sda1      ext4   500M   38M  436M   8% /boot
/dev/sda5      ext4    72G  2.2G   67G   4% /usr
/dev/sdb1      ext4    11G   24M  9.9G   1% /mydata
[root@localhost ~]# df -ia
Filesystem      Inodes IUsed   IFree IUse% Mounted on
/dev/sda2      1921360  9287 1912073    1% /
proc                 0     0       0     - /proc
sysfs                0     0       0     - /sys
devpts               0     0       0     - /dev/pts
tmpfs           125596     1  125595    1% /dev/shm
/dev/sda1       128016    38  127978    1% /boot
/dev/sda5      4472832 79188 4393644    2% /usr
/dev/sdb1       655776    12  655764    1% /mydata
none                 0     0       0     - /proc/sys/fs/binfmt_misc

[root@localhost ~]# du -sh ~
160K    /root
[root@localhost ~]# du -a ~ 
4       /root/mq
8       /root/install.log.syslog
4       /root/.tcshrc
4       /root/.bash_logout
0       /root/?a
0       /root/2dev
0       /root/4dev
4       /root/test/test.file
8       /root/test
0       /root/a
0       /root/aaa
0       /root/,a
0       /root/1dev
4       /root/11
36      /root/install.log
4       /root/anaconda-ks.cfg
0       /root/5dev
4       /root/.bashrc
4       /root/.cshrc
48      /root/.bash_history
4       /root/.bash_profile
4       /root/install.sh
12      /root/.nihao.swp
0       /root/3dev
8       /root/.viminfo
160     /root
[root@localhost ~]# 
[root@localhost ~]# umount /mydata/
umount: /mydata: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@localhost ~]# fuser -v /mydata
                     USER        PID ACCESS COMMAND
/mydata:             root       2253 ..c.. bash
[root@localhost ~]# ps aux|grep 2253
root       2253  0.0  0.1 108432  1884 pts/1    Ss+  11:41   0:00 -bash
root       2282  0.0  0.0 103308   844 pts/0    S+   11:43   0:00 grep 2253
[root@localhost ~]# fuser -k /mydata 
/mydata:              2253c

你可能感兴趣的:(linux)