在树莓派上缩小系统ext4分区的方法

最近把树莓派的系统ext4分区缩小了一点,然后在系统分区的后面空出一块空闲空间,建立了一个FAT32分区。下面列出操作方法。

重要的数据先备份!重要的数据先备份!重要的数据先备份!

不过如果顺利的话,缩小ext4分区是不会丢失数据的。

过程如下:

【缩小ext4分区大小】

首先,把树莓派系统sd卡插入读卡器,并插到电脑上读取。我的电脑是CentOS。当然用另外一个树莓派的Debian系统来操作也是可以的。不过不能在原来的系统直接缩小ext4分区。 因为

resize2fs: On-line shrinking not supported

好了,先mount看看目前数据占用空间有多少。

[root@centos ~]# mount /dev/sdb2 /mnt
[root@centos ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.7G   16G   10% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.8M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  159M  856M   16% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sdb2                 15G  1.0G   13G    8% /mnt
[root@centos ~]# 

可以看到/dev/sdb2的15G空间只使用了大约1G。

然后unmount之后,再使用resize2fs

[root@centos ~]# umount /mnt
[root@centos ~]# resize2fs /dev/sdb2 2G
resize2fs 1.42.9 (28-Dec-2013)
请先运行 'e2fsck -f /dev/sdb2'.

[root@centos ~]# e2fsck -f /dev/sdb2
e2fsck 1.42.9 (28-Dec-2013)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
rootfs: 39254/940576 files (0.1% non-contiguous), 329297/3877248 blocks
[root@centos ~]# resize2fs /dev/sdb2 2G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdb2 to 524288 (4k) blocks.
The filesystem on /dev/sdb2 is now 524288 blocks long.

[root@centos ~]# 

注意如果有提示先e2fsck的话,要先运行e2fsck,参考上面。

然后使用parted修改分区

[root@centos ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit s print free
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 31116288s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End        Size       Type     File system  标志
        63s     8191s      8129s               Free Space
 1      8192s   96663s     88472s     primary  fat32        lba
        96664s  98303s     1640s               Free Space
 2      98304s  31116287s  31017984s  primary  ext4

(parted) 

先print出目前的分区参数,注意记下上面ext4分区的开始和结束扇区数值。

然后删除该分区,之后再新建一个,保证开始扇区数不变(Start那一列的数值,这一点非常重要!),但是结束扇区数变小。

如果你要空出500M,那就把之前的结束扇区数(是End那一列,而不是Size那一列哦)减掉 500 * 1024 * 2(一个扇区是512byte)以上。  我这里计算后得出的是30092287。

(parted) rm                                                               
分区编号? 2                                                              
(parted) mkpart primary 98304s 30092287s
(parted) unit s print free
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 31116288s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start      End        Size       Type     File system  标志
        63s        8191s      8129s               Free Space
 1      8192s      96663s     88472s     primary  fat32        lba
        96664s     98303s     1640s               Free Space
 2      98304s     30092287s  29993984s  primary  ext4
        30092288s  31116287s  1024000s            Free Space

(parted)         

注意上面mkpart那一步非常重要,数字千万不要打错,不要拷贝我的,要根据你自己的参数来。

然后退出parted,之后resize2fs一下。

(parted) quit                                                             
信息: You may need to update /etc/fstab.

[root@centos ~]# resize2fs /dev/sdb2                                      
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdb2 to 3749248 (4k) blocks.
The filesystem on /dev/sdb2 is now 3749248 blocks long.

注意如果有提示先e2fsck的话,要先运行e2fsck。

mount检查一下,看看运气如何。

[root@centos ~]# mount /dev/sdb2 /mnt
[root@centos ~]# ll /mnt
总用量 92
drwxr-xr-x.  2 root root  4096 6月  27 08:21 bin
drwxr-xr-x.  2 root root  4096 6月  27 09:08 boot
drwxr-xr-x.  2 root root  4096 6月  27 08:13 debootstrap
drwxr-xr-x.  4 root root  4096 6月  27 08:06 dev
drwxr-xr-x. 84 root root  4096 7月  15 18:24 etc
drwxr-xr-x.  3 root root  4096 6月  27 08:17 home
drwxr-xr-x. 16 root root  4096 6月  27 08:30 lib
drwx------.  2 root root 16384 6月  27 09:08 lost+found
drwxr-xr-x.  2 root root  4096 6月  27 08:03 media
drwxr-xr-x.  2 root root  4096 6月  27 08:03 mnt
drwxr-xr-x.  3 root root  4096 6月  27 08:17 opt
drwxr-xr-x.  2 root root  4096 3月  12 22:03 proc
drwx------.  2 root root  4096 6月  27 08:03 root
drwxr-xr-x.  5 root root  4096 6月  27 08:18 run
drwxr-xr-x.  2 root root  4096 6月  27 08:30 sbin
drwxr-xr-x.  2 root root  4096 6月  27 08:03 srv
drwxr-xr-x.  2 root root  4096 3月  12 22:03 sys
drwxrwxrwt.  7 root root  4096 7月  15 22:11 tmp
drwxr-xr-x. 10 root root  4096 6月  27 08:03 usr
drwxr-xr-x. 11 root root  4096 6月  27 09:09 var
[root@centos ~]# 

看来运气还不错,那我们继续。

【建立一个FAT32分区】

unmount掉/dev/sdb2,然后回到parted。

[root@centos ~]# umount /mnt
[root@centos ~]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit s print free
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 31116288s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start      End        Size       Type     File system  标志
        63s        8191s      8129s               Free Space
 1      8192s      96663s     88472s     primary  fat32        lba
        96664s     98303s     1640s               Free Space
 2      98304s     30092287s  29993984s  primary  ext4
        30092288s  31116287s  1024000s            Free Space

(parted)           

可以看到free space是从30092288s开始的,我习惯于空出一部分,我们来加上13312。 30092288 + 13312 = 30105600

30105600也刚好是可以被8192s(4KB)整除的,good!(好吧,其实我上面加13312就是为了4K对齐)

(parted) mkpart primary fat32 30105600s -1s
(parted) unit s print free                                                
Model: Generic- SD/MMC (scsi)
Disk /dev/sdb: 31116288s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start      End        Size       Type     File system  标志
        63s        8191s      8129s               Free Space
 1      8192s      96663s     88472s     primary  fat32        lba
        96664s     98303s     1640s               Free Space
 2      98304s     30092287s  29993984s  primary  ext4
        30092288s  30105599s  13312s              Free Space
 3      30105600s  31116287s  1010688s   primary               lba

(parted) quit                                                             
信息: You may need to update /etc/fstab.

[root@centos ~]#     

然后用mkfs.vfat格式化刚刚建立的分区

[root@centos ~]# mkfs.vfat -n DATA /dev/sdb3
mkfs.fat 3.0.20 (12 Jun 2013)
[root@centos ~]# 

mount一下看看

[root@centos ~]# mount /dev/sdb3 /mnt
[root@centos ~]# ll /mnt
总用量 0
[root@centos ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.7G   16G   10% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.8M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  159M  856M   16% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sdb3                494M     0  494M    0% /mnt
[root@centos ~]# 

嗯,好像没问题,这样一个大约500M的FAT32分区就建立好了。

Tips: Centos等linux系统默认是不安装mkfs.vfat命令的,如果你发现没有该命令,需要先安装dosfstools。

[root@centos ~]# yum -y install dosfstools
【结束】

那么问题来了,我在树莓派的系统SD卡上建立一个FAT32分区干什么呢?




你可能感兴趣的:(在树莓派上缩小系统ext4分区的方法)