本文展示將openwrt上的/usr轉移到一個空間更大的硬碟或者閃存之上
  • 列出當前硬碟設備
fdisk -l

輸出

Disk /dev/sda: 14.8 GiB, 15837691904 bytes, 30932992 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
Disklabel type: dos
Disk identifier: 0xc1e0f78c

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1  *       512    33279    32768   16M 83 Linux
/dev/sda2        33792   558079   524288  256M 83 Linux
/dev/sda3       559104 30932991 30373888 14.5G 83 Linux

這裡我的硬碟是一顆16GB的SSD,設備名為/dev/sda,openwrt預裝之後只划走了256M,這樣的空間太小,目前SSD還有一個sda3的分區較大.我們的目標是將sda3刪掉,並將剩下的空間中劃分出8G的空間出來,並將其掛載給/usr

*使用fdisk進入到/dev/sda

fdisk /dev/sda

輸出:

Welcome to fdisk (util-linux 2.32).                                     
Changes will remain in memory only, until you decide to write them.     
Be careful before using the write command.                              
                                                                        
                                                                        
Command (m for help): m                                                 

我們敲出m之後,可以看到fdisk各項指令的用途

Help:                                                             
                                                                  
  DOS (MBR)                                                       
   a   toggle a bootable flag                                     
   b   edit nested BSD disklabel                                  
   c   toggle the dos compatibility flag                          
                                                                  
  Generic                                                         
   d   delete a partition                                         
   F   list free unpartitioned space                              
   l   list known partition types                                 
   n   add a new partition                                        
   p   print the partition table                                  
   t   change a partition type                                    
   v   verify the partition table                                 
   i   print information about a partition                        
                                                                  
  Misc                                                            
   m   print this menu                                            
   u   change display/entry units                                 
   x   extra functionality (experts only)                         
                                                                  
  Script                                                          
   I   load disk layout from sfdisk script file                   
   O   dump disk layout to sfdisk script file                     
                                                                  
  Save & Exit                                                     
   w   write table to disk and exit                               
   q   quit without saving changes                                
                                                                  
  Create a new label                                              
   g   create a new empty GPT partition table                     
   G   create a new empty SGI (IRIX) partition table              
   o   create a new empty DOS partition table                     
   s   create a new empty Sun partition table                     
                                                                  
  • 我們需要刪除第三個分區,因此鍵入d (delete a partition )
Command (m for help): d
Partition number (1-3, default 3):

第三個分區,繼續選擇3

Partition 3 has been deleted.
  • 保存分區信息,鍵入w (write table to disk and exit)

  • 創建新的分區,鍵入 n (add a new partition )

Partition type
   p   primary (2 primary, 0 extended, 2 free)  # windows上叫做主要分區
   e   extended (container for logical partitions) #windows叫做擴展分區

需要創建主要分區,選擇p繼續,

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3):
First sector (33280-30932991, default 559104):   #這裡是你這個分區開始的起始柱面,你需要參考其他分區的值,可以理解為分區開始存東西的開始地址,如果你的分區是連續的,一般默認就好
Last sector, +sectors or +size{K,M,G,T,P} (559104-30932991, default 30932991): +8G #這裡設定結束柱面,或者填寫一個大小

Created a new partition 3 of type 'Linux' and of size 8 GiB.
  • 保存分區狀態 w 再次確認
fdisk -l
Disk /dev/sda: 14.8 GiB, 15837691904 bytes, 30932992 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
Disklabel type: dos
Disk identifier: 0xc1e0f78c

Device     Boot  Start      End  Sectors  Size Id Type
/dev/sda1  *       512    33279    32768   16M 83 Linux
/dev/sda2        33792   558079   524288  256M 83 Linux
/dev/sda3       559104 17336319 16777216    8G 83 Linux

可以看到我們已經成功創建了一個8GB的分區

  • 將分區格式化成ext4
mkfs.ext4 /dev/sda3
  • 將8GB的分區掛載進來
mkdir /usr_new
mount /dev/sda3 /usr_new
  • 無損複製/usr中的文件到新的分區

此處使用cp時候,請一定要-a,才能保留和原來文檔一樣的屬性以及權限!!!

cp -a /usr/* /usr_new

之後使用編輯/etc/fstab或者使用block-mount工具管理開機講/dev/sda3掛載到/usr.大功告成