由於文章字數的設置,該文章的第一部分請訪問RHCE033--硬體配置與管理1 - 順便在這裡把一個邏輯分區的文件系統類型改為82,即swap類型
[root@51cto ~]#
fdisk /dev/sdb Command (m for help): t //使用t改變分區的文件系統類型標識 Partition number (1-6): 6 //選擇第六個分區號,即一個交換分區 Hex code (type L to list codes): 82 //82是swap類型的文件系統類型號碼 Changed system type of partition 6 to 82 (Linux swap / Solaris)
Command (m for help): p //查看分區信息 Disk /dev/sdb: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot
Start
End
Blocks
Id
System /dev/sdb1
122
261
1124550
83
Linux /dev/sdb2
1
121
971901
5
Extended /dev/sdb5
62
121
481950
83
Linux
/dev/sdb6 1 61 489919 82 Linux swap / Solaris //交換分區ok Partition table entries are not in disk order
Command (m for help):
w //保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
- 到這裡上面的分區結果已經生效,但是分區設置需要reboot系統才生效.如果不希望
reboot馬上生效可以使用partprobe命令使內核更新分區表 [root@51cto ~]#[root@51cto ~]#
partprobe 接下來就是格式化操作了,沒格式化的分區是不能存儲任何信息的.
格式化的命令有:mkfs tune2fs mkswap 三個命令 如下把第一個主分區格式化為ext3格式: [root@51cto ~]# mkfs -t ext3 /dev/sdb1 //使用-t選項指定格式為ext3,
如果直接mkfs /dev/sdb1的話就格式化為ext2類型 mke2fs 1.39 (29-May-2006)
Filesystem label= //分區卷標,這裡為空 OS type: Linux
Block size=4096 (log=2) //塊大小為4KB Fragment size=4096 (log=2) 140832 inodes, 281137 blocks
14056 blocks (5.00%) reserved for the super user //保留5%的空間為root用戶 First data block=0 Maximum filesystem blocks=289406976 9 block groups 32768 blocks per group, 32768 fragments per group 15648 inodes per group Superblock backups stored on blocks:
32768, 98304, 163840, 229376 Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override. //可以用tune2fs修改已經格式化的分區的參數 如果格式化的時候指定參數,把分區的塊大小設為2KB,保留2%的空間給root用戶,
分區卷標為sample [root@51cto ~]# mkfs -t ext3 -b 2048 -m 2 -L sample /dev/sdb1 //具體命令 mke2fs 1.39 (29-May-2006)
Filesystem label=sample //分區卷標為sample OS type: Linux
Block size=2048 (log=1) //分區的塊大小設為2KB Fragment size=2048 (log=1) 141120 inodes, 562274 blocks
11245 blocks (2.00%) reserved for the super user //保留2%的空間給root用戶 First data block=0 Maximum filesystem blocks=537919488 35 block groups 16384 blocks per group, 16384 fragments per group 4032 inodes per group Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368 Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 36 mounts or 180 days, whichever comes first.
Use tune2fs -c or -i to override.
當然,我們可以tune2fs命令來調整已經格式化的分區參數,參數項目和
mkfs命令相同, 但不是所有參數都可以調整,如塊大小不能調整.如果要改變分區塊大小就必須重新格式化. [root@51cto ~]#
tune2fs -r 5000 -L sample24 /dev/sdb1 //使用tune2fs保留給root空間不用百分比,而是分配塊,5000個,即5000*2KB, 大概10M左右.同時把卷標改為sample24 tune2fs 1.39 (29-May-2006) Setting reserved blocks count to
5000 [root@51cto ~]# tune2fs -l /dev/sdb1 tune2fs 1.39 (29-May-2006) Filesystem volume name:
sample24 Last mounted on:
<not available> Filesystem UUID:
95c34d9d-c661-46ad-ab1c-f75a2441af80 Filesystem magic number:
0xEF53 Filesystem revision #:
1 (dynamic) Filesystem features:
has_journal resize_inode dir_index filetype sparse_super Default mount options:
(none) Filesystem state:
clean Errors behavior:
Continue Filesystem OS type:
Linux Inode count:
141120 Block count:
562274 Reserved block count:
5000 …...
如果希望分區格式化為FAT格式,則: [root@51cto ~]# mkfs -t vfat /dev/sdb5 //把邏輯分區格式化為fat格式 mkfs.vfat 2.11 (12 Mar 2005)
對於交換分區就不可以使用mkfs命令格式化了,而是mkswap,如下格式化/dev/sdb6 [root@51cto ~]#
mkswap /dev/sdb6 Setting up swapspace version 1, size = 501669 kB 格式化話啟用交換分區,
查看啟用情況: [root@51cto ~]#
free -m total
used
free
shared
buffers
cached Mem:
408
130
277
0
44
50 -/ buffers/cache:
35
372
Swap: 745
0 745 //發現還沒啟用,是屬於/dev/sda5的 [root@51cto ~]# swapon /dev/sdb6 //通過swapon啟用交換分區 [root@51cto ~]#
free -m total
used
free
shared
buffers
cached Mem:
408
130
277
0
44
50 -/ buffers/cache:
35
372
Swap: 1223 0 1223 //啟用了,增加了500M 如果要是的已經載入的交換分區失效,使用swapoff命令 [root@51cto ~]#
swapoff /dev/sdb6 [root@51cto ~]#
free -m total
used
free
shared
buffers
cached Mem:
408
130
277
0
44
50 -/ buffers/cache:
35
372
Swap: 745 0 745 //恢復到原來的大小了 - 如上已經對各個分區格式化完成了,接下來就是掛載文件系統使用了
對一個磁碟格式化完成後並不能直接使用分區進行存取,必須藉助mount命令掛載 [root@51cto ~]# mount //直接使用
mount查看掛載情況 /dev/sda2 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/sda3 on /home type ext3 (rw) /dev/sda1 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) nfsd on /proc/fs/nfsd type nfsd (rw) 掛載點可看成是訪問文件系統的入口,以上可以看出有哪些文件系統被掛載,是否可讀
創建目錄作為掛載點:
[root@51cto ~]# mkdir /51cto //創建目錄作掛載點 [root@51cto ~]# mkdir /51cto/a /51cto/b /51cto/c //創建子目錄 [root@51cto ~]# touch /51cto/1 /51cto/2 /51cto/3 //創建文件 [root@51cto ~]# ls /51cto/ //查看目錄和文件 1
2
3
a
b
c
[root@51cto ~]# mount /dev/sdb1 /51cto/ //將/dev/sdb1掛載到/51cto/
[root@51cto ~]# mount //查看掛載情況 ……
/dev/sdb1 on /51cto type ext3 (rw) [root@51cto ~]# ll /51cto/ //發現作為掛載點后,裡頭本來有的目錄文件都沒了 總計 16 drwx------ 2 root root 16384 09-30 18:48 lost found
我們不要使用重要的目錄作為掛載點,否則掛載后原有的內容無法訪問了
[root@51cto ~]# mkdir /51cto/d //在掛載點裡頭創建目錄 [root@51cto ~]# touch /51cto/4 //在掛載點裡頭創建文件 [root@51cto ~]# ls /51cto/ //發現新創建的目錄文件是存在的 4 d lost found
[root@51cto ~]# cd /51cto/ //切換到掛載點 [root@51cto 51cto]# ls
//查看裡頭的目錄文件 4
d
lost found
[root@51cto 51cto]# umount /dev/sdb1 //卸載掛載點 umount: /51cto: device is busy //提示無法卸載,必須切換到其他目錄 umount: /51cto: device is busy
[root@51cto 51cto]# cd //切換到/root [root@51cto ~]# umount /dev/sdb1 //卸載掛載點 [root@51cto ~]# ls /51cto/ //可以看到原本創建的目錄,但在掛載點創建的就丟失了 1
2
3
a
b
c 當然,如果你重新掛載上去,還是可以看到新創建的目錄文件的 [root@51cto ~]#
mount /dev/sdb1 /51cto/ [root@51cto ~]#
ls /51cto/ 4
d
lost found
- 介紹mount命令的選項,-o選項是mount命令最強大的選項,如下:
[root@51cto ~]# mount -o ro /dev/sdb1 /51cto/ //以只讀屬性掛載 [root@51cto ~]# touch /51cto/24 //無法執行寫操作 touch: 無法觸碰 「/51cto/24」: 只讀文件系統
[root@51cto ~]# umount /dev/sdb1 //卸載 [root@51cto ~]# mount /dev/sdb1 /51cto/ //重新普通掛載 [root@51cto ~]# touch /51cto/24 //可以執行寫操作了 [root@51cto ~]# cp /bin/ls /51cto/
//copy命令ls到/dev/sdb1 [root@51cto ~]# /51cto/ls //複製過去的ls命令可以正常使用 anaconda-ks.cfg install.log install.log.syslog ……
[root@51cto ~]# umount /dev/sdb1 //卸載 [root@51cto ~]#
mount -o noexec /dev/sdb1 /51cto/ //以noexec不可執行屬性掛載文件系統 [root@51cto ~]# /51cto/ls //ls命令無法執行 -bash: /51cto/ls: 許可權不夠 [root@51cto ~]#
umount /dev/sdb1 [root@51cto ~]#
mount /dev/sdb1 /51cto/ 到這裡,分區、格式化、掛載使用的步驟就OK了 八、開機自動掛載文件系統
上面的操作在系統reboot后就要重新掛載,可以通過編輯/etc/fstab實現自動掛載 [root@51cto ~]#
vim /etc/fstab [root@51cto ~]#
cat /etc/fstab LABEL=/
/
ext3
defaults
1 1 LABEL=/home
/home
ext3
defaults
1 2 LABEL=/boot
/boot
ext3
defaults
1 2 tmpfs
/dev/shm
tmpfs
defaults
0 0 devpts
/dev/pts
devpts
gid=5,mode=620
0 0 sysfs
/sys
sysfs
defaults
0 0 proc
/proc
proc
defaults
0 0 LABEL=SWAP-sda5
swap
swap
defaults
0 0 /dev/sdb1
/51cto
ext3
defaults
0 0 添加的紅色的部分,對於這一行作如下介紹:
- 第一列表示掛載的設備名 :/dev/sdb1
- 第二列表示對應的掛載點: /51cto
- 第三列表示掛載的文件系統: ext3,如果不清楚就用auto類型
- 第四列表示掛載屬性: 如上面的ro、noexec等等...defaults是默認值,
- 第五列表示指明是否要備份:0表示不備份,1表示備份,一般只有根分區要備份
- 第六列表示指明自檢順序:0不自檢,1或2要自檢,一般根分區設1,其他分區設2
對於新增加的內容如果不reboot就生效,可以使用mount -a 命令 [root@51cto ~]# mount -a //重讀/etc/fstab文件載入那些尚未生效的行
[root@51cto ~]#
mount ……
/dev/sdb1 on /51cto type ext3 (rw) 如果在/etc/fstab中對已經掛載的文件系統對應的行進行了修改,可以使用mount -o remount 命令是的系統不用reboot就可以是的修改後的內容生效,比如: [root@51cto ~]#
vim /etc/fstab [root@51cto ~]# cat /etc/fstab //紅色為修改的內容 …... /dev/sdb1
/51cto
ext3
ro,noexec 0 0 [root@51cto ~]#
mount ……
/dev/sdb1 on /51cto type ext3 (rw) //發現沒有改變 [root@51cto ~]# mount -o remount /51cto/ //對掛載點進行操作 或者也可以對設備名進行操作: [root@51cto ~]#
mount -o remount /dev/sdb1 [root@51cto ~]#
mount /dev/sdb1 on /51cto type ext3 (ro,noexec)
//發現修改成功了 備註:對於磁碟配額、raid、lvm等部分的內容放到Linux進階二本文出自 「twenty_four」 博客,請務必保留此出處http://twentyfour.blog.51cto.com/945260/399997