linux系統中外接硬盤的使用邏輯:
磁盤分區---分區格式化---掛載
linux系統中一台主機上可以有多快硬盤,內核中的udev設備管理器會自動對識別的硬盤進行命名
系統采用a~p來代表16塊不同的硬盤(默認從a開始),如/dev/sda 代表系統識別的同種型號接口的第一塊硬盤,
/dev/sdb代表系統識別的同種型號接口的第二快硬盤,依次類推,直到/dev/sdp。
硬件命名規則知道之后要想使用硬盤,則先需要進行磁盤分區:
1、首先查看硬盤情況:
[root@linuxprobe /]# cd /dev ## 外接硬盤的默認路徑 [root@linuxprobe dev]# find sd* ## 查看幾塊硬盤,一共兩塊,sda 和 sdb(sda1和sda2是sda的兩個分區) sda sda1 sda2 sdb
2、使用fdisk命令進行磁盤分區
使用格式: fdisk + 硬盤路徑
fdisk + 硬盤路徑 使用的是交互式命令
fdisk命令中的參數及其作用(linux就該這么學p125)
測試:
[root@linuxprobe dev]# fdisk /dev/sdb ## 使用fdisk 對 /dev/sdb進行管理 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p ## 這里輸入p,查看分區信息,目前沒有分區 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System Command (m for help): n ## 輸入n,添加新的分區 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p ## 這里輸入p選擇主分區 Partition number (1-4, default 1): 1 ## 選擇編號1 First sector (2048-41943039, default 2048): ## 這里定義起始的扇區位置,直接敲擊回車鍵保留默認設置 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +3G ## 這里選擇結束扇區的位置,實際是分配分區的大小,輸入 +3G 表示分區大小3G Partition 1 of type Linux and of size 3 GiB is set Command (m for help): p ## 這里輸入p查看分區信息,已經出現 /dev/sdb1分區 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xd55cd25f Device Boot Start End Blocks Id System /dev/sdb1 2048 6293503 3145728 83 Linux Command (m for help): w ## 這里輸入w 保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@linuxprobe dev]# find sd* ## 查看硬盤sdb的分區信息,已經多出sdb1 sda sda1 sda2 sdb sdb1 [root@linuxprobe dev]# file /dev/sdb1 /dev/sdb1: block special 注:有時候分區信息未能及時同步內核,在終端輸入:partprobe命令,連續執行兩次,如果仍然沒有同步內核,重啟系統即可
3、使用mkfs命令對磁盤分區進行格式化(指定文件系統)
mkfs命令的使用方式是在終端輸入mkfs,然后連續按兩下Tab鍵,會彈出常用的文件系統。
[root@linuxprobe dev]# mkfs ## 此處輸入兩下Tab鍵 mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat [root@linuxprobe dev]# mkfs.xfs /dev/sdb1 ## rhel7版本,使用 xfs系統 mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs). mkfs.xfs: Use the -f option to force overwrite. [root@linuxprobe dev]# mkfs.xfs -f /dev/sdb1 ## 格式化成功 meta-data=/dev/sdb1 isize=256 agcount=4, agsize=196608 blks = sectsz=512 attr=2, projid32bit=1
= crc=0 data = bsize=4096 blocks=786432, imaxpct=25
= sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
4、對格式話成功的分區進行掛載
[root@linuxprobe dev]# df -h ## 查看當前掛載 Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot [root@linuxprobe dev]# mkdir /mnt/sdb1test ## 創建掛載目錄 [root@linuxprobe dev]# mount /dev/sdb1 /mnt/sdb1test/ ## 掛載 [root@linuxprobe dev]# df -h ## 查看掛載 Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot /dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test
5、進行第二快分區
[root@linuxprobe dev]# fdisk /dev/sdb ## fdisk + 硬盤,進行交互式操作 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n ## 此處輸入n添加分區 Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p ## 此處選擇p,再添加一個主分區 Partition number (2-4, default 2): 2 ## 編號選擇2 First sector (6293504-41943039, default 6293504): ## 這里選擇扇區的起始位置,按回車就行 Using default value 6293504 Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): +5G ## 這里設定分區大小,設為5G Partition 2 of type Linux and of size 5 GiB is set Command (m for help): w ## 這里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@linuxprobe dev]# find sdb* ## 查看分區,沒有!可能是沒有及時同步內核 sdb sdb1 [root@linuxprobe dev]# partprobe ## 輸入partprobe命令 Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@linuxprobe dev]# partprobe ## 同上 Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@linuxprobe dev]# find sdb* ## 再次查看分區,已經同步 sdb sdb1 sdb2 [root@linuxprobe dev]# mkfs ## 這里按兩下Tab鍵 mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat [root@linuxprobe dev]# mkfs.xfs /dev/sdb2 ## 對分區進行格式話 meta-data=/dev/sdb2 isize=256 agcount=4, agsize=327680 blks = sectsz=512 attr=2, projid32bit=1
= crc=0 data = bsize=4096 blocks=1310720, imaxpct=25
= sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@linuxprobe dev]# df -h ## 查看當前分區 Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot /dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test [root@linuxprobe dev]# mkdir /mnt/sdb2test ## 創建掛載目錄 [root@linuxprobe dev]# mount /dev/sdb2 /mnt/sdb2test/ ## 掛載 [root@linuxprobe dev]# df -h ## 查看掛載 Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot /dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test /dev/sdb2 5.0G 33M 5.0G 1% /mnt/sdb2test
6、如何刪除分區
## 首先進行卸載
[root@linuxprobe dev]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot /dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test /dev/sdb2 5.0G 33M 5.0G 1% /mnt/sdb2test [root@linuxprobe dev]# umount /mnt/sdb2test/ ## 卸載 [root@linuxprobe dev]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_linuxprobe-root 18G 3.4G 15G 20% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 84K 994M 1% /dev/shm tmpfs 994M 8.8M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sr0 3.5G 3.5G 0 100% /media/mounttest /dev/sda1 497M 119M 379M 24% /boot /dev/sdb1 3.0G 33M 3.0G 2% /mnt/sdb1test [root@linuxprobe dev]# fdisk /dev/sdb ## 使用fdisk + 硬盤進行交互式操作 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): d ## 這里輸入d Partition number (1,2, default 2): 2 ## 這里刪除編號為2的主分區 Partition 2 is deleted 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@linuxprobe dev]# find sdb* ## 查看,還在 sdb sdb1 sdb2 [root@linuxprobe dev]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@linuxprobe dev]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. [root@linuxprobe dev]# find sdb* ## 再次查看,已經沒有第二個分區 sdb sdb1
##注: 以上操作在/dev 目錄下,如果其他目錄需要指定路徑