環境:node
模擬新加一塊磁盤,並分區,掛載,永久掛載,而後寫入數據,而後對分區擴容,測試擴容後分區裏面的內容是否丟失。bash
查看全部分區:編輯器
[root@backup ~]# fdisk -l Disk /dev/sda: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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: 0x00040b96 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 1070 8388608 82 Linux swap / Solaris /dev/sda3 1070 13055 96263168 83 Linux Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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
開始分區:
ide
[root@backup ~]# 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 0xc6e44e94. 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): p #p是打印當前磁盤全部分區 Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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: 0xc6e44e94 Device Boot Start End Blocks Id System Command (m for help): n #n是新建分區 Command action e extended p primary partition (1-4) p #新建主分區 Partition number (1-4): 1 #給主分區編號爲1,從1開始編號 First cylinder (1-130541, default 1): #回車,默認從1開始,也能夠手動輸入1 Using default value 1 #起始分區的位置 Last cylinder, +cylinders or +size{K,M,G} (1-130541, default 130541): #直接回車,默認把磁盤全部空間分配到此分區。這裏支持K,M,G,若是想給此分區分配100M空間,就輸入+100M;若是要分配100G那麼就輸入+100G Using default value 130541 #結束分區的位置 Command (m for help): w #w爲寫入分區信息並保存,而後退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
查看剛剛分區的信息:/dev/sdb1測試
[root@backup ~]# fdisk -l Disk /dev/sda: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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: 0x00040b96 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 1070 8388608 82 Linux swap / Solaris /dev/sda3 1070 13055 96263168 83 Linux Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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: 0xc6e44e94 Device Boot Start End Blocks Id System /dev/sdb1 1 130541 1048570551 83 Linux
格式化分區:mkfs.ext4 /dev/sdb1ui
[root@backup ~]# 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 65536000 inodes, 262142637 blocks 13107131 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 8000 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
掛載分區:spa
[root@backup backup]# mkdir /backup [root@backup backup]# mount /dev/sdb1 /backup/ [root@backup backup]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/sda3 ext4 91G 2.2G 84G 3% / tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/sda1 ext4 190M 38M 142M 21% /boot /dev/sdb1 ext4 985G 72M 935G 1% /backup
掛載磁盤:(永久掛載)
.net
經過vi編輯器編輯/etc/fstab文件,內容以下,(最後一行是剛添加上去的):orm
[root@backup ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Mon May 14 02:01:11 2018 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=7d223255-1b77-4f58-bd72-262928547fcf / ext4 defaults 1 1 UUID=284d6b31-8723-497b-bc7b-3c12ede23ee6 /boot ext4 defaults 1 2 UUID=5d36c1b3-df6e-480d-80ca-72befc66f5e9 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/sdb1 /backup ext4 defaults 0 0
第四列掛載參數:經過查看man mount 來查看blog
第五列是否要備份:(0爲不備份,1爲要備份,通常狀況下不用作備份)
第六列自檢程序 (0爲不自檢,1或2爲要自檢,若是是根分區要設置1,其它分區只能是2)
[root@backup ~]# echo backup >/backup/backup.txt [root@backup ~]# cat /backup/backup.txt backup
此時機器不關機,用vmware vspere client擴容磁盤大小。
[root@backup ~]# fdisk -l /dev/sdb Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes 255 heads, 63 sectors/track, 130541 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: 0xc6e44e94 Device Boot Start End Blocks Id System /dev/sdb1 1 130541 1048570551 83 Linux
此時發現擴容的磁盤容量看不到,因此仍是重啓一下吧,建議關機時候再擴容。
[root@backup ~]# umount /dev/sdb1 #取消掛載 [root@backup ~]# fdisk /dev/sdb 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): p Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes 255 heads, 63 sectors/track, 143595 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: 0xc6e44e94 Device Boot Start End Blocks Id System /dev/sdb1 1 130541 1048570551 83 Linux Command (m for help): d #由於此磁盤只有一個分區sdb1,因此按d刪除時候默認不會讓選擇要刪除的分區,若是有多個分區會提示要刪除的分區。 Selected partition 1 Command (m for help): p #打印當前分區,發現分區已刪除 Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes 255 heads, 63 sectors/track, 143595 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: 0xc6e44e94 Device Boot Start End Blocks Id System Command (m for help): n #新建分區 Command action e extended p primary partition (1-4) p #新建主分區 Partition number (1-4): 1 #和擴容前的分區標號必須同樣 First cylinder (1-143595, default 1): #默認是1,好比要和擴容前的分區其實點同樣,擴容前的分區起始也是1,這個是擴容的重點地方。 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-143595, default 143595): #終止的位置是默認是硬盤柱體的尾部,即便最大值,這個是要擴容的點,回車。 Using default value 143595 Command (m for help): w #w報錯,並退出。 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
三、調整分區
e2fsck -f /dev/sdb1 #檢查分區信息
resize2fs /dev/sdb1 #調整分區大小
[root@backup ~]# e2fsck -f /dev/sdb1 #因爲磁盤容量很大,數據多的狀況下須要等待一段時間 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/sdb1: 12/65536000 files (0.0% non-contiguous), 4164250/262142637 blocks [root@backup ~]# resize2fs /dev/sdb1 #因爲磁盤容量很大,數據多的狀況下須要等待一段時間 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/sdb1 to 288356701 (4k) blocks. The filesystem on /dev/sdb1 is now 288356701 blocks long. [root@backup ~]# mount /dev/sdb1 /backup/ [root@backup ~]# cat /backup/ backup.txt lost+found/ [root@backup ~]# cat /backup/backup.txt backup
發現擴容成功,擴容前的內容沒有丟失。
參考內容:
https://blog.csdn.net/wuweilong/article/details/7538634
https://blog.csdn.net/jackyu651/article/details/53070607
https://blog.csdn.net/a746742897/article/details/52689285