文章目錄
- 一、linux 檔案系統及磁盤磁區
- 1.1 磁盤簡介
- 1.2 linux 磁盤與檔案系統簡介
- 1.3 mount 掛載
- 二、案例演示
- 2.1 gdisk 給磁盤磁區
- 2.2 parted 創建檔案系統
- 2.3 創建大檔案做 swap 磁區
- 總結
一、linux 檔案系統及磁盤磁區
1.1 磁盤簡介
-
硬碟是計算機主要存盤媒介之一,由一個或者多個鋁制或者玻璃制的碟片組成,碟片外覆寫有鐵磁性材料,硬碟內部由磁道、柱面、扇區、磁頭等部件組成
-
Linux系統中硬體設備相關組態檔存放在
/dev/下,常見硬碟命名:/dev/hda、/dev/sda、/dev/sdb、/dev/sdc、/dev/vda,不同硬碟介面,在系統中識別的設備名稱不一樣, -
IDE硬碟介面在Linux中設備名為/dev/hda,SAS、SCSI、SATA硬碟介面在Linux中設備名為sda,高效云盤硬碟介面會識別為/dev/vda等, -
檔案儲存在硬碟上,
硬碟的最小存盤單位叫做Sector(扇區),每個Sector儲存512位元組,作業系統在讀取硬碟的時候,不會逐個Sector的去讀取,這樣效率非常低,為了提升讀取效率,作業系統會一次性連續讀取多個Sector,即一次性讀取多個Sector稱為一個Block(塊, -
由多個Sector組成的Block是檔案存取的最小單位,Block的大小常見的有
1KB、2KB、4KB,Block在Linux中常設定為4KB,即連續八個Sector組成一個Block, -
/boot磁區Block一般為1KB,而/data/磁區或者/磁區的Block為4K -
linux 系統查看磁區的Block大小方法:
[root@hbs ~]# stat anaconda-ks.cfg |grep -i "block" Size: 1511 Blocks: 8 IO Block: 4096 regular file [root@hbs ~]# [root@hbs ~]# stat /boot/|grep "IO Block" Size: 4096 Blocks: 8 IO Block: 4096 directory [root@hbs ~]#
1.2 linux 磁盤與檔案系統簡介
1.3 mount 掛載
-
Mount命令工具主要用于將設備或者磁區掛載至Linux系統目錄下,Linux系統在磁區時,也是基于mount機制將/dev/sda磁區掛載至系統目錄,將設備與目錄掛載之后,Linux作業系統方可進行檔案的存盤,
mount [-Vh] mount -a [-fFnrsvw] [-t vfstype] mount [-fnrsvw] [-o options [,...]] device | dir mount [-fnrsvw] [-t vfstype] [-o options] device dir -V: 顯示mount工具版本號; -l: 顯示已加載的檔案系統串列; -h: 顯示幫助資訊并退出; -v: 輸出指令執行的詳細資訊; -n: 加載沒有寫入檔案/etc/mtab中的檔案系統; -r: 將檔案系統加載為只讀模式; -a: 加載檔案/etc/fstab中配置的所有檔案系統; -o: 指定mount掛載擴展引數,常見擴展指令:rw、remount、loop等,其中-o相關指令如下: -o atime: 系統會在每次讀取檔案時更新檔案時間; -o noatime: 系統會在每次讀取檔案時不更新檔案時間; -o defaults: 使用預設的選項 rw,suid,dev,exec,auto,nouser等; -o exec 允許執行檔被執行; -o user、-o nouser: 使用者可以執行 mount/umount的動作; -o remount: 將已掛載的系統磁區重新以其他再次模式掛載; -o ro: 只讀模式掛載; -o rw: 可讀可寫模式掛載; -o loop 使用loop模式,把檔案當成設備掛載至系統目錄, -t: 指定mount掛載設備型別,常見型別nfs、ntfs-3g、vfat、iso9660等,其中-t相關指令如下: iso9660 光碟或光碟鏡像; msdos Fat16檔案系統; vfat Fat32檔案系統; ntfs NTFS檔案系統; ntfs-3g 識別移動硬碟格式; smbfs 掛載Windows檔案網路共享; nfs Unix/Linux檔案網路共享, -
MOUNT 常用案例
mount /dev/sdb1 /data 掛載/dev/sdb1磁區至/data/目錄 mount /dev/cdrom /mnt 掛載Cdrom光碟至/mnt目錄; mount -t ntfs-3g /dev/sdc /data1 掛載/dev/sdc移動硬碟至/data1目錄; mount -o remount,rw / 重新以讀寫模式掛載/系統; mount -t iso9660 -o loop centos7.iso /mnt 將centos7.iso鏡像檔案掛載至/mnt目錄; mount -t fat32 /dev/sdd1 /mnt 將U盤/dev/sdd1掛載至/mnt/目錄; mount -t nfs 192.168.10.11:/data/ /mnt 將遠程192.168.10.11:/data目錄掛載至本地/mnt目錄,
二、案例演示
2.1 gdisk 給磁盤磁區
-
使用 gdisk 創建一個 500M 的 xfs 檔案系統,并將它掛載到 /data/test目錄下
# 1.列出所有磁盤 [admin@rivers~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot ├─sda3 8:3 0 10G 0 part / ├─sda4 8:4 0 5G 0 part /home └─sda5 8:5 0 1G 0 part [SWAP] sr0 11:0 1 4.2G 0 rom /run/media/admin/CentOS 7 x86_64 # 2. 查看磁盤磁區表型別 [admin@rivers~]$ sudo parted /dev/sda print [sudo] admin 的密碼: Model: VMware, VMware Virtual S (scsi) # (廠商)模塊名稱 Disk /dev/sda: 42.9GB # 磁盤總容量 Sector size (logical/physical): 512B/512B # 物理扇區容量 Partition Table: gpt # 這里顯示是gpt格式 Disk Flags: pmbr_boot Number Start End Size File system Name 標志 1 1049kB 3146kB 2097kB bios_grub 2 3146kB 1077MB 1074MB xfs 3 1077MB 11.8GB 10.7GB xfs 4 11.8GB 17.2GB 5369MB xfs 5 17.2GB 18.3GB 1074MB linux-swap(v1) [admin@rivers~]$ # 3.使用gdisk 創建磁區 [admin@rivers~]$ sudo gdisk /dev/sda GPT fdisk (gdisk) version 0.8.6 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): ? # ? 命令幫助,想到fdisk中的m b back up GPT data to a file c change a partition's name d delete a partition # 洗掉一個磁區 i show detailed information on a partition l list known partition types n add a new partition # 增加一個磁區 o create a new empty GUID partition table (GPT) p print the partition table #列印出磁區表 (常用) q quit without saving changes # 不保存磁區直接離開 r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit #保存磁區后離開 x extra functionality (experts only) ? print this menu Command (? for help): p Disk /dev/sda: 83886080 sectors, 40.0 GiB # 磁盤檔案名/扇區總數與總容量 Logical sector size: 512 bytes # 單一扇區大小為512 bytes Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5 #磁盤的GPT標識碼 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 83886046 Partitions will be aligned on 2048-sector boundaries Total free space is 48230333 sectors (23.0 GiB) # 下面為完整的磁區資訊 Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 # 第一個磁區資訊 2 6144 2103295 1024.0 MiB 0700 3 2103296 23074815 10.0 GiB 0700 4 23074816 33560575 5.0 GiB 0700 5 33560576 35657727 1024.0 MiB 8200 Command (? for help): n # 新增加一個 Partition number (6-128, default 6): # 識別符號 First sector (34-83886046, default = 35657728) or {+-}size{KMGTP}: # 開始扇區 Last sector (35657728-83886046, default = 83886046) or {+-}size{KMGTP}: +500M Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): #在磁區內可能的檔案系統型別 8300 Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sda: 83886080 sectors, 40.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 83886046 Partitions will be aligned on 2048-sector boundaries Total free space is 47206333 sectors (22.5 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 2 6144 2103295 1024.0 MiB 0700 3 2103296 23074815 10.0 GiB 0700 4 23074816 33560575 5.0 GiB 0700 5 33560576 35657727 1024.0 MiB 8200 6 35657728 36681727 500.0 MiB 8300 Linux filesystem # 8300 linux 檔案系統 # 8200 swap 檔案系統 Command (? for help): w # 保存 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y # 按Y 退出 OK; writing new GUID partition table (GPT) to /dev/sda. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot. The operation has completed successfully. [admin@rivers~]$ [admin@rivers~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot ├─sda3 8:3 0 10G 0 part / ├─sda4 8:4 0 5G 0 part /home └─sda5 8:5 0 1G 0 part [SWAP] sr0 11:0 1 4.2G 0 rom /run/media/admin/CentOS 7 x86_64 # 4. 更新linux 內核的磁區表資訊 [admin@rivers~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot ├─sda3 8:3 0 10G 0 part / ├─sda4 8:4 0 5G 0 part /home ├─sda5 8:5 0 1G 0 part [SWAP] └─sda6 8:6 0 500M 0 part sr0 11:0 1 4.2G 0 rom /run/media/admin/CentOS 7 x86_64 [admin@rivers~]$ # 5.格式化剛建立的磁區 [admin@rivers~]$ sudo mkfs.xfs /dev/sda6 meta-data=/dev/sda6 isize=512 agcount=4, agsize=32000 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=128000, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=855, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [admin@rivers~]$ # 6.查看/dev/sda6的UUID [admin@rivers~]$ sudo blkid|grep "/dev/sda6" /dev/sda6: UUID="fc584ce9-b4ff-4c54-8c2c-9ed6cf54dc1d" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="1aa87728-47cd-428d-b5bd-32a076c8f537" [admin@rivers~]$ # 7.創建掛載點,且掛載 [admin@rivers~]$ sudo mount UUID="fc584ce9-b4ff-4c54-8c2c-9ed6cf54dc1d" /data/test [admin@rivers~]$ df -h 檔案系統 容量 已用 可用 已用% 掛載點 /dev/sda3 10G 4.6G 5.5G 46% / devtmpfs 1.8G 0 1.8G 0% /dev tmpfs 1.8G 0 1.8G 0% /dev/shm tmpfs 1.8G 9.1M 1.8G 1% /run tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup /dev/sda4 5.0G 37M 5.0G 1% /home /dev/sda2 1014M 158M 857M 16% /boot tmpfs 367M 4.0K 367M 1% /run/user/42 tmpfs 367M 36K 367M 1% /run/user/1000 /dev/sr0 4.3G 4.3G 0 100% /run/media/admin/CentOS 7 x86_64 /dev/sda6 497M 26M 472M 6% /data/test [admin@rivers~]$ # 8.洗掉剛建立的 6號磁區,先卸載,再說洗掉 [admin@rivers~]$ sudo umount /data/test/ [admin@rivers~]$ sudo gdisk /dev/sda GPT fdisk (gdisk) version 0.8.6 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): p # 列印下以存在的磁區 Disk /dev/sda: 83886080 sectors, 40.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 83886046 Partitions will be aligned on 2048-sector boundaries Total free space is 47206333 sectors (22.5 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 2 6144 2103295 1024.0 MiB 0700 3 2103296 23074815 10.0 GiB 0700 4 23074816 33560575 5.0 GiB 0700 5 33560576 35657727 1024.0 MiB 8200 6 35657728 36681727 500.0 MiB 8300 Linux filesystem Command (? for help): d # 按 d 洗掉磁區 Partition number (1-6): 6 Command (? for help): p Disk /dev/sda: 83886080 sectors, 40.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 00B9BA4D-D71C-462B-82F4-091B4C43A1D5 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 83886046 Partitions will be aligned on 2048-sector boundaries Total free space is 48230333 sectors (23.0 GiB) Number Start (sector) End (sector) Size Code Name 1 2048 6143 2.0 MiB EF02 2 6144 2103295 1024.0 MiB 0700 3 2103296 23074815 10.0 GiB 0700 4 23074816 33560575 5.0 GiB 0700 5 33560576 35657727 1024.0 MiB 8200 Command (? for help): w # 保存 Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): Y OK; writing new GUID partition table (GPT) to /dev/sda. Warning: The kernel is still using the old partition table. The new table will be used at the next reboot. The operation has completed successfully. [admin@rivers~]$ sudo lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 40G 0 disk ├─sda1 8:1 0 2M 0 part ├─sda2 8:2 0 1G 0 part /boot ├─sda3 8:3 0 10G 0 part / ├─sda4 8:4 0 5G 0 part /home └─sda5 8:5 0 1G 0 part [SWAP] sr0 11:0 1 4.2G 0 rom /run/media/admin/CentOS 7 x86_64 [admin@rivers~]$
2.2 parted 創建檔案系統
-
使用parted 創建一個swap 磁區
# 0.檢查是否有parted 工具,沒有就安裝一個這里我有,不需安裝 [admin@rivers~]$ which parted /usr/sbin/parted #[admin@rivers~]$ yum -y install parted # 1. 先列出 磁盤磁區表型別,及磁區情況 [admin@rivers~]$ sudo parted /dev/sda print Model: VMware, VMware Virtual S (scsi) Disk /dev/sda: 42.9GB Sector size (logical/physical): 512B/512B Partition Table: gpt #2. 這里看到的是GPT型別,使用gdisk或者parted磁區 Disk Flags: pmbr_boot Number Start End Size File system Name 標志 1 1049kB 3146kB 2097kB bios_grub 2 3146kB 1077MB 1074MB xfs 3 1077MB 11.8GB 10.7GB xfs 4 11.8GB 17.2GB 5369MB xfs 5 17.2GB 18.3GB 1074MB linux-swap(v1) # 2. 將 原本gpt格式的 磁盤變為 mbr 磁區表,(很危險,請勿亂用!無法恢復!) @ 這條命令我變了一個磁盤/sdb ,新添加的磁盤,請不要用/sda 去弄,! [admin@rivers~]$ sudo parted /dev/sdb mklabel msdos [admin@rivers~]$parted /dev/sdb print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos # 現在這里變了,變成MBR了, Disk Flags: Number Start End Size Type File system Flags 使用parted 磁區,格式化為 vfat 型別大小為 500M [admin@rivers~]$ sudo parted /dev/sda mkpart primary ext4 18.3GB 18.8GB [sudo] admin 的密碼: 資訊: You may need to update /etc/fstab. [admin@rivers~]$ sudo partprobe /dev/sda parted [設備名] [命令] [引數] # 固定以 MB 格式顯示 parted /dev/sdb unit mb print # 將GPT格式的磁區表,將它改變為 MBR格式 parted /dev/sdb mklabel msdos # 在建立一個 500M的 ext4 格式的 parted /dev/sda mkpart primary ext4 18.3GB 18.8GB
2.3 創建大檔案做 swap 磁區
-
使用dd 命令,創建一個大檔案,格式化做swap磁區,并掛載
# 1. 創建一個 大檔案,塊大小為1M ,共計512個塊 [admin@rivers~]$ dd if=/dev/zero of=/tmp/dvd bs=1M count=512 記錄了512+0 的讀入 記錄了512+0 的寫出 536870912位元組(537 MB)已復制,30.7426 秒,17.5 MB/秒 # 2. 查看,并格式化檔案 [admin@rivers~]$ sudo ls -lih /tmp/dvd 16838973 -rw-rw-r--. 1 admin admin 512M 12月 29 20:09 /tmp/dvd [admin@rivers~]$ sudo mkswap /tmp/dvd mkswap: /tmp/dvd: warning: wiping old swap signature. 正在設定交換空間版本 1,大小 = 524284 KiB 無標簽,UUID=79a3af17-1c7a-4025-a596-c34782619b7a [admin@rivers~]$ #3. 啟動swap ,利用swapon 將/tmp/dvd 啟動(swapoff 關閉 swap file) [admin@rivers~]$ sudo swapon /tmp/dvd # 4. 使用swapoff 關閉,并設定自啟動 [admin@rivers~]$ sudo swapoff /tmp/dvd [admin@rivers~]$ tail -1 /etc/fstab /tmp/dvd swap swap defaults 0 0 [admin@rivers~]$
總結
-
一個 可以被掛載的資料 通常 稱為 檔案系統,不是硬碟磁區
-
linux 傳統檔案系統為 ext2,該檔案系統主要包含資訊有:
- 超級區塊:包含inode/區塊總量,使用量、剩余量,檔案系統格式等資訊
- inode:檔案屬性,一個檔案一個inode,同時記錄檔案資料所在區塊號碼
- 資料區塊:實際記錄檔案的內容,若檔案太大,會占用多個資料區塊
-
ext2 檔案系統的資料存取為 索引式檔案系統
-
硬鏈接:只是多了一個檔案名 對該inode 號碼的連接,不能跨檔案連接、不能連接目錄
-
符號鏈接:快捷方式,
-
磁盤使用 必須經過 磁區、格式化與掛載,常用命令 為 parted、gdisk、fdisk、mount、partproe等
-
啟動自動掛載,可以撰寫/etc/fstab 檔案,然后使用 mount -a測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/398565.html
標籤:其他
上一篇:在線運行 Linux,真滴牛逼。



