新聞中心
在Linux系統(tǒng)中,使用fdisk命令可以對磁盤進行分區(qū)操作。分區(qū)是將一個硬盤分成多個邏輯分區(qū),可以分別存儲不同類型的數(shù)據(jù),以便更好地管理和利用磁盤空間。本篇文章將以Ubuntu系統(tǒng)為例,介紹如何使用fdisk命令添加分區(qū)。

一、準備工作
在使用fdisk命令前,需要獲取待分區(qū)的硬盤名稱??梢酝ㄟ^以下命令查看系統(tǒng)中已有的磁盤和其對應的名稱:
“`
sudo fdisk -l
“`
我們以/dev/sda為例進行操作。在分區(qū)之前,需要確保硬盤上沒有重要數(shù)據(jù),因為分區(qū)會擦除硬盤上的數(shù)據(jù)。如果需要保存數(shù)據(jù),應先備份。
二、使用fdisk分區(qū)
1. 打開分區(qū)工具
在終端中輸入以下命令:
“`
sudo fdisk /dev/sda
“`
其中,/dev/sda為待分區(qū)的硬盤名稱。打開分區(qū)工具后,會出現(xiàn)命令行交互界面。
2. 查看分區(qū)信息
輸入m命令,查看分區(qū)操作的幫助信息。輸入p命令,查看當前硬盤的分區(qū)信息。如果硬盤尚未分區(qū),則無任何分區(qū)信息。
3. 創(chuàng)建新分區(qū)
輸入n命令,創(chuàng)建新的分區(qū)。在下面的交互界面中,需要輸入新分區(qū)的類型、起始扇區(qū)和結(jié)束扇區(qū)。根據(jù)自己的需求輸入相應的參數(shù)即可。
“`
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (contner for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2023-524287, default 2023):
Last sector, +sectors or +size{K,M,G,T,P} (2023-524287, default 524287):
Created a new partition 1 of type ‘Linux’ and of size 256 MiB.
“`
在本例中,我們創(chuàng)建了一個主分區(qū),起始扇區(qū)為2023,結(jié)束扇區(qū)為524287,大小為256MB。
4. 查看分區(qū)信息
輸入p命令,查看新分區(qū)的信息。
“`
Command (m for help): p
Disk /dev/sda: 256 MiB, 268435456 bytes, 524288 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xa1d922fe
Device Boot Start End Sectors Size Id Type
/dev/sda1 2023 524287 522240 256M 83 Linux
“`
可以看到,新分區(qū)已經(jīng)成功創(chuàng)建,其編號為/dev/sda1。
5. 保存分區(qū)信息
當所有分區(qū)信息已輸入完畢后,使用w命令將分區(qū)信息寫入硬盤。輸入w后會提示確認。
“`
Command (m for help): w
The partition table has been altered.
Syncing disks.
“`
至此,新分區(qū)信息已成功保存。
三、格式化分區(qū)
新分區(qū)需要格式化后才能正常使用。以下命令將/dev/sda1格式化為ext4文件系統(tǒng)。
“`
sudo mkfs.ext4 /dev/sda1
“`
四、使用新分區(qū)
將新分區(qū)掛載到系統(tǒng)中,就可以使用了。以下命令將/dev/sda1掛載到/mnt目錄中。
“`
sudo mount /dev/sda1 /mnt
“`
如果需要永久掛載,可以編輯/etc/fstab文件,將分區(qū)信息添加到其中。
使用fdisk命令添加分區(qū)比較簡單,但是需要注意分區(qū)前備份有重要數(shù)據(jù),并慎重輸入分區(qū)信息,否則有可能造成數(shù)據(jù)丟失。分區(qū)完成后需要格式化才能使用,并且掛載到系統(tǒng)中。希望本文對讀者有所幫助。
成都網(wǎng)站建設公司-創(chuàng)新互聯(lián),建站經(jīng)驗豐富以策略為先導10多年以來專注數(shù)字化網(wǎng)站建設,提供企業(yè)網(wǎng)站建設,高端網(wǎng)站設計,響應式網(wǎng)站制作,設計師量身打造品牌風格,熱線:028-86922220linux磁盤重新掛載?
linux 重新掛載需要先卸載再重新掛載,如果要一條命令可以通過&&符號將兩個命令組合到一個命令里,例如:
umount -l /mnt/test && mount -o ro /dev/hda1 /mnt/test
linux中掛載命令是mount,卸載是umount,在命令行中使用&&可以將多條命令組合成一條命令并順序執(zhí)行,以達到快速執(zhí)行的目的。
擴展資料
linux掛載命令mount的用法介紹:
1、語法
mount device dir
2、參數(shù)說明:
-V:顯示程序版本
-h:顯示輔助訊息
-v:顯示較訊息,通常和 -f 用來除錯。
-a:將 /etc/fstab 中定義的所有檔案系統(tǒng)掛上。
-F:這個命令通常和 -a 一起使用,它會為每一個 mount 的動作產(chǎn)生一個行程負責執(zhí)行。在系統(tǒng)需要掛上大量 NFS 檔案系統(tǒng)時可以加快掛上的動作。
-f:通常用在除錯的用途。它會使 mount 并不執(zhí)行實際掛上的動作,而是模擬整個掛上的過程。通常會和 -v 一起使用。
-n:一般而言,mount 在掛上后會在 /etc/mtab 中寫入一筆資料。但在系統(tǒng)中沒有可寫入檔案系統(tǒng)存在的情況下可以用這個選項取消這個動作。
VMware虛擬機中,Linux系統(tǒng)下掛載、卸載新硬盤的方法
裝載:
1、啟動vm,選擇vm->Settings,按向?qū)砑右粔KSCSI磁盤。進入Linux系統(tǒng)。
2、 fdisk -l 會看到有一塊新的設置,如果你先前有一塊硬盤(sda1, sda3…),新加的這塊應該是(/dev/sdb)。 (如果添加的第二塊硬盤是IDE硬盤,應該看到hdb,如果是SCSI硬盤,看到的就是應該sdb)
# fdisk -l
Disk /dev/sda: 8589 MB,bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of* 512 =bytes
Device Boot StartEnd Blocks Id System
/dev/sda1 * Linux
/dev/sda+ 83 Linux
Disk /dev/sdb: 8589 MB,bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of* 512 =bytes
Disk /dev/sdb doesn’t contain a valid partition table
3、分區(qū):
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won’t be recoverable.
The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): m ##在這里按m ,就會輸出幫助;
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition ##這是刪除一個分區(qū)的動作;
l list known partition types ##:l是列出分區(qū)類型,以供我們設置相應分區(qū)的類型;
m print this menu
n add a new partition ##添加一個分區(qū);
o create a new empty DOS partition table
p print the partition table ##p列出分區(qū)表;
q quit without saving changes ##不保存退出;
s create a new empty Sun disklabel
t change a partition’s system id ##t 改變分區(qū)類型;
u change display/entry units
v verify the partition table
w write table to disk and exit ##把分區(qū)表寫入硬盤并退出;
x extra functionality (experts only) ##擴展應用,專家功能;
Command (m for help): n
Command action
e extended ##擴展分區(qū)
p primary partition (1-4) ##選p建主分區(qū)
p
Partition number (1-4): 1
First cylinder (, default 1): ##注:這個就是分區(qū)的Start 值;這里更好直接按回車,如果你輸入了一個非默認的數(shù)字,會造成空間浪費;
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (, default 17849): 8920
Using default value 500
##這個是定義分區(qū)大小的,+200M 就是大小為200M ;當然你也可以根據(jù)上面提示的單位cylinder的大小來算,然后來指定 End的數(shù)值,在fdisk –l命令中可以看到Units = cylinders of* 512 =bytes,這個就是單位cylinder的大小,我這里選的End的數(shù)值是8920,正好是總大小的一半,71G左右
##然后再來建一個主分區(qū)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (, default 8921):8921
Using default value 8921
Last cylinder or +size or +sizeM or +sizeK (, default 17849):17849
Using default value 17849
##最后保存并退出,切記,一定要保存,不然不會生效的。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
##使用fdisk –l命令來查看磁盤狀況:
# fdisk -l
Disk /dev/sda: 8589 MB,bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of* 512 =bytes
Device Boot StartEnd Blocks Id System
/dev/sda1 * Linux
/dev/sda+ 83 Linux
Disk /dev/sdb: 8589 MB,bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of* 512 =bytes
Device Boot StartEnd Blocks Id System
/dev/sdb+ 83 Linux
/dev/sdb+ 83 Linux
##磁盤/dev/sdb已經(jīng)被劃分為2個分區(qū),每個分區(qū)大小大概是71G左右。
##在掛載文件系統(tǒng)之前,需要將/dev/sdb1和/dev/sdb2重新用mkfs -t ext3 命令格式化一下,否則在掛載時會報錯。
4、格式化分區(qū):使用mkfs.ext3 /dev/sdb命令格式化磁盤 注:也可以使用mkfs -t ext3 /dev/sdb命令
# mkfs -t ext3 /dev/sdb1 ##指將該磁盤格式化成ext3文件系統(tǒng)
mke2fs 1.39 (29-May-2023)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
inodes,blocks
blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=
64 block groups
32768 blocks per group,fragments per group
16384 inodes per group
Superblock backups stored on blocks:
, 98304,,,,,,
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
補充:掛載
掛載文件系統(tǒng),目前有兩種方法:
一是通過 mount 來掛載,使用mount掛載系統(tǒng),一旦系統(tǒng)重啟之后就需要重新掛載。
二是通過/etc/fstab文件來開機自動掛載。
我把/dev/sdb1掛載到/test下
# mkdir /test ## 首先建立掛載的目錄test
(或者# cd /
# mkdir test
)
# df –h ##掛載前的分區(qū)
FilesystemSize Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
.7G 5.8G 576M 92% /
/dev/sda 99M 12M 82M 13% /boot
tmpfs 233MM 0% /dev/shm
# mount /dev/sdb1 /test
# df –h ##掛載后的分區(qū)
FilesystemSize Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
.7G 5.8G 576M 92% /
/dev/sda 99M 12M 82M 13% /boot
tmpfs 233MM 0% /dev/shm
/dev/sdb7.9G 147M 7.4G 2% /test
6、通過/etc/fstab文件來開機自動掛載(不然重啟電腦后系統(tǒng)將不會保存之前的掛載操作):
# vi /etc/fstab
/dev/VolGroup00/LogVol00 /ext3 defaults
LABEL=/boot/boot ext3 defaults
devpts /dev/pts devpts gid=5,mode=
tmpfs /dev/shm tmpfs defaults
proc /proc proc defaults
sysfs /sys sysfs defaults
/dev/VolGroup00/LogVol01 swap swap defaults
/dev/sdb/testextdefaults1 1
/dev/sdb/testextdefaults1 1
# Beginning of the block added by the VMware software
.host:/ /mnt/hgfs vmhgfs defaults,ttl=
# End of the block added by the VMware software
之一字段:設備名,在這里表示是文件系統(tǒng); 有時我們把掛載文件系統(tǒng)也說成掛載分區(qū);
第二字段:文件系統(tǒng)的掛載點;
第三字段:文件系統(tǒng)類型;
第四字段:mount 命令的選項,和mount 中的-o 同理;defaults包括這些選項 rw, suid, dev, exec, auto, nouser, async;
第五字段:表示文件系統(tǒng)是否需要dump 備份,1是需要,0 是不需要;
第六字段: 是否在系統(tǒng)啟動時,通過fsck磁盤檢測工具來檢查文件系統(tǒng),1是需要,0是不需要,2是跳過;
卸載:
# umount /dev/sdb1
# df -h
FilesystemSize Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
.7G 5.8G 576M 92% /
/dev/sda 99M 12M 82M 13% /boot
tmpfs 233MM 0% /dev/shm
補充知識:
1. mount可以用來掛載什么:
不同的操作系統(tǒng)使用不同的文件系統(tǒng)格式。MS-DOS支持FAT16文件系統(tǒng),Windows98支持FAT16、FAT32文件系 統(tǒng),WindowsNT支持FAT16、NTFS文件系統(tǒng),Windows2023則支持FAT16、FAT32、NTFS三種文件系統(tǒng)格式,現(xiàn)在的xp 可以支持FAT32,NTFS,現(xiàn)在最新版本的windows 7 引入了新的WinFS文件系統(tǒng)。 而 Linux差不多支持所有的文件系統(tǒng)格式,但一般使用ext2或ext3文件系統(tǒng)。很多用戶使用的是windows操作系統(tǒng),如果想在運行的Linux下 訪問其它文件系統(tǒng)中的資源的話,就要用Linux mount命令來實現(xiàn)。
2. mount使用格式:
mount命令
說明:
必須是一個已經(jīng)存在的目錄,這個目錄可以不為空,但掛載后這個目錄下以前的內(nèi)容將不可用,umount以后會恢復正常。
可以是一個分區(qū),一個u設備,光驅(qū),軟盤,網(wǎng)絡共享等。
常見參數(shù)說明:
Mount 掛載文件系統(tǒng)使用幫助
參數(shù) 說明
掛載指定的設備類型:adfs, affs, autofs, coda, coherent, cramfs,
devpts, efs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix,
msdos, ncpfs, nfs, nfs4, ntfs, proc, qnx4, ramfs, reiserfs,
romfs, bfs, sysv, tmpfs, udf, ufs, umsdos, vfat, xenix, xfs,
xiafs
一般文件類型,可以不需要指定就可以有相同自動檢測(adfs, bfs,
cramfs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, ntfs,
qnx4, reiserfs, romfs, udf, ufs, vxfs, xfs, xiafs 等文件系統(tǒng)),如
果探測失敗,就將訪問/etc/filesystems ,以指定文件系統(tǒng)探測。說
明:如果清楚文件系統(tǒng),更好指定具體類型。探測錯誤將帶來災難性
的。
多個文件系統(tǒng)可以用“,”分割開來.
-t vfstype mount -a -t nomsdos,ext
將掛載fstab文件指定所有的文件系統(tǒng),除了類型為 msdos 和 ext 的
那一些。
1. 將 /usr 里的所有文件 完全 復制到 空分區(qū)中。注意軟硬鏈接文件的問題。 可使用 cp -a 命令。 如果新分區(qū)夠大,建議 采取壓縮再釋放的方法。cd /usr ; tar cvf (/新分區(qū)掛在目錄/usr.tar) . ;cd /新分區(qū)掛在目錄 ;tar xvf usr.tar -C .
2.安全起見,不要刪除原來的 /usr 目錄,建議改名,比如usr_ ,新建 /usr 文件夾
3.修改 /etc/fstab 文件,新建 掛載規(guī)則,/usr 新分區(qū) ……….(詳細的可參考 fstab 的幫助文件)
還有一個偏門方法:
使用 aufs 或者 unionfs 綁定一個新的分區(qū)。這樣新的文件就自動被轉(zhuǎn)到新分區(qū)上建立。舊文件還在原來的分區(qū)上。
但玩這個需要一定的技術。如果樓主有閑心,可以試試。
我建議轉(zhuǎn)換分區(qū)的時候。直接用 mv 把 /usr 的老文件轉(zhuǎn)移到新的分區(qū)上。轉(zhuǎn)移的時候可以先把新的 usr 分區(qū)掛載到一個臨時的目錄上,比如 /mnt/temp 。
注意這么操作之前先修改好 /etc/fstab 。之后到純文本狀態(tài)下(如果可以用 LiveCD )進行移動,之后立即重啟。不然系統(tǒng)可能會找不到重要的文件而掛掉。
linux fdisk add的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關于linux fdisk add,Linux下使用fdisk添加分區(qū),linux磁盤重新掛載?的信息別忘了在本站進行查找喔。
成都創(chuàng)新互聯(lián)科技公司主營:網(wǎng)站設計、網(wǎng)站建設、小程序制作、成都軟件開發(fā)、網(wǎng)頁設計、微信開發(fā)、成都小程序開發(fā)、網(wǎng)站制作、網(wǎng)站開發(fā)等業(yè)務,是專業(yè)的成都做小程序公司、成都網(wǎng)站建設公司、成都做網(wǎng)站的公司。創(chuàng)新互聯(lián)公司集小程序制作創(chuàng)意,網(wǎng)站制作策劃,畫冊、網(wǎng)頁、VI設計,網(wǎng)站、軟件、微信、小程序開發(fā)于一體。
分享文章:Linux下使用fdisk添加分區(qū)(linuxfdiskadd)
當前網(wǎng)址:http://m.5511xx.com/article/cdossih.html


咨詢
建站咨詢
