1. 程式人生 > >Linux 基本命令(七)

Linux 基本命令(七)

1、磁碟分割槽

   Linux系統預設所有裝置檔案都在/dev下面

   /dev/sda --硬碟1   sda1 第一個分割槽 sda2 第二個分割槽 sda3 第三個分割槽

[[email protected] dev]# ll |grep 'sd'
lrwxrwxrwx. 1 root root           4 Sep 30 22:49 root -> sda3
brw-rw----. 1 root disk      8,   0 Sep 30 22:49 sda
brw-rw----. 1 root disk      8,   1 Sep 30 22:49 sda1
brw-rw----. 1 root disk      8,   2 Sep 30 22:49 sda2
brw-rw----. 1 root disk      8,   3 Sep 30 22:49 sda3

   /dev/sdb --硬碟2 sdb1 第一個分割槽 sdb2 第二個分割槽  sdb3 第三個分割槽

   /dev/sdc  --硬碟3

  2、# fdisk -l 檢視系統所有硬碟的分割槽情況

(1)系統一共有幾塊硬碟,每個硬碟的容量大小

(2)每個硬碟的分割槽情況(硬碟空間是否還有剩餘)

[[email protected] dev]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x0009d896

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39         543     4046848   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             543        5222    37587968   83  Linux

3、新增硬碟,先關機後新增

[[email protected] ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x0009d896

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39         543     4046848   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             543        5222    37587968   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: 0x00000000

4、分割槽操作

分割槽步驟:

 (1) fdisk 裝置名稱(/dev/sdb)

  (2)partx -a /dev/sdb     使系統重新識別分割槽

  (3)mkfs.ext4 /dev/sdb6   格式化     分割槽完必須要格式化後才能用哈

  (4)# mount   /dev/sdb5 /mnt     掛載  (臨時生效)

                  永久生效需要就修改/etc/fstab  (例如:將  /dev/sdb5  /wwwroot  ext4 defaults  0 0 按列填寫至後面)

檔案系統:ext2  ext3  ext4 xfs  (如windows有NTFS)

block:

         --Linux 作業系統管理檔案的最小(邏輯)單位

         --大小有1k 2k  4k

[[email protected] dev]# mkfs.ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
348816 inodes, 1393630 blocks
69681 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1430257664
43 block groups

如用ext4下block大小為4k,如果一個檔案是1k,會用一個block儲存它,剩餘3k會浪費掉。

注:記住哈,hadoop中還會見面的。

----------------------------------------------------------------------------分割槽步驟---------------------------------------------------------------------------------

新增分割槽:

[[email protected] dev]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd2d759dc.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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

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: 0xd2d759dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1959    15735636   83  Linux

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

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: 0xd2d759dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1959    15735636   83  Linux
/dev/sdb2            1960        2610     5229157+   5  Extended

再分擴充套件分割槽為邏輯分割槽:

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: 0xd2d759dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1959    15735636   83  Linux

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

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: 0xd2d759dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1959    15735636   83  Linux
/dev/sdb2            1960        2610     5229157+   5  Extended

Command (m for help): m^H
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n^H
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1960-2610, default 1960): 
Using default value 1960
Last cylinder, +cylinders or +size{K,M,G} (1960-2610, default 2610): 2500

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: 0xd2d759dc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1959    15735636   83  Linux
/dev/sdb2            1960        2610     5229157+   5  Extended
/dev/sdb5            1960        2500     4345551   83  Linux

寫入退出儲存:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] dev]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x0009d896

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39         543     4046848   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             543        5222    37587968   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: 0x0a4a3d6d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        2610    10474380    5  Extended
/dev/sdb5            1307        2000     5574523+  83  Linux

格式化:

[[email protected] dev]# mkfs.ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
348816 inodes, 1393630 blocks
69681 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1430257664
43 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] dev]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655776 inodes, 2622603 blocks
131130 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8096 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] dev]# mkfs.ext4 /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
	filesystem with 0 blocks, specify higher inode_ratio (-i)
	or lower inode count (-N).

掛載:

[[email protected] dev]# mount   /dev/sdb5 /mnt
[[email protected] dev]# cd /mnt
[[email protected] mnt]# ls
lost+found
[[email protected] mnt]# cd lost+found/
[[email protected] lost+found]# ls
[[email protected] lost+found]# cd ..
[[email protected] mnt]# cd ..
[[email protected] /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        36G  3.5G   30G  11% /
tmpfs           1.9G   72K  1.9G   1% /dev/shm
/dev/sda1       283M   65M  204M  25% /boot
/dev/sdb5       5.2G   11M  4.9G   1% /mnt

解除安裝-->裝載:

[[email protected] /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        36G  3.5G   30G  11% /
tmpfs           1.9G   72K  1.9G   1% /dev/shm
/dev/sda1       283M   65M  204M  25% /boot
/dev/sdb5       5.2G   11M  4.9G   1% /mnt
[[email protected] /]# umount /mnt
[[email protected] /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        36G  3.5G   30G  11% /
tmpfs           1.9G   72K  1.9G   1% /dev/shm
/dev/sda1       283M   65M  204M  25% /boot
[[email protected] /]# mkdir /wwwroot
[[email protected] /]# mount /dev/sdb5 /wwwroot
[[email protected] /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        36G  3.5G   30G  11% /
tmpfs           1.9G   72K  1.9G   1% /dev/shm
/dev/sda1       283M   65M  204M  25% /boot
/dev/sdb5       5.2G   11M  4.9G   1% /wwwroot
[[email protected] /]# mount /dev/sdb1  /mnt
[[email protected] /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        36G  3.5G   30G  11% /
tmpfs           1.9G   72K  1.9G   1% /dev/shm
/dev/sda1       283M   65M  204M  25% /boot
/dev/sdb5       5.2G   11M  4.9G   1% /wwwroot
/dev/sdb1       9.8G   23M  9.3G   1% /mnt

目錄是分割槽的訪問入口,如果在目錄下建立檔案後解除安裝分割槽,目錄下的檔案就不存在了哈,不信可以試試~~~