10

I am trying to resize a fat 32 partition in my linux machine. Following are the specs of the system.

  1. This is not a PC hence UI based tools are not applicable.
  2. This memory partition which I am trying to resize is already having data in the form of files. I can't lose them.
  3. I am trying to shrink the partition to used space alone and move the remaining space to another partition.
  4. Preferably without rebooting of system, unmounting of partition would be fine.

Edit: Backing up data from this partition is not what I am looking for. I am trying to see if there is a way that I can try without losing data. Its more like an analysis rather than task.

What I tried till now: Tried using parted tool as gparted needs an UI. Even though I am able to shrink this partition, it is leading to unnecessary problems due to file fragmentation. For example, after shrinking when I run file system check I am getting errors like chain not found for few files.

I cant use tools like resizee2fs as it supports only ext file systems (correct me if I am wrong here).

I tried using tool fatresize, even though I am able to successfully get it install it in my debian I am not able to get an installable for arm, whatever link I am getting is throwing an error on line 1 statement itself.

Can someone help me with any information they could?

  • 4
    "Backing up data from this partition is not what I am looking for" and "I can't lose them" are incompatible statements. Data without a back-up is by definition unimportant. You really need a back-up even if you didn't try to resize the file system. – David Foerster Oct 08 '17 at 21:51
  • @DavidFoerster My point was I need data to be retained, but without somebody to do backup. There is a reason for me asking this question. I am trying to run it as a solo script without any manual intervention and no internet connection in lesser available space. – Rengasami Ramanujam Oct 09 '17 at 11:19
  • Possible duplicate of How do I resize the partition of a Ubuntu-server? One answer suggests the use of parted which can resize FAT32 file systems. – David Foerster Oct 16 '17 at 19:30

3 Answers3

5

fat32 part resize

NOTE: first of all, backup your data, just in case.

a test of resize sda1 from 28G to 10GB.

0. orig size

//check partition size

ubuntu$ sudo mount /dev/sda1  /media/chen/test

ubuntu$ lsblk /dev/sda NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 1 28.9G 0 disk └─sda1 8:1 1 28.9G 0 part /media/chen/test

//check fs size

sudo fatresize -i /dev/sdXX

1. change fs: fatresize

ubuntu$ sudo fatresize /dev/sda1 -s 10G -p -v
fatresize 1.0.2 (06/16/18)
.Error: Partition /dev/sda1 is being used.  You must unmount it before you modify it with Parted.

ubuntu$ sudo umount /dev/sda1

ubuntu$ sudo fatresize /dev/sda1 -s 10240M -p -v fatresize 1.0.2 (06/16/18) .FAT: fat32 Size: 30985420800 Min size: 2483422208 Max size: 30989615104

NOTE

// gparted seems not change fs size, without this step, will got a broken fs, says: fs large than volume:

ubuntu$ sudo dosfsck /dev/sda1
fsck.fat 4.1 (2017-01-24)
Seek to 30985420288:Invalid argument

ubuntu$ sudo fatresize /dev/sda1 -s 9G -p -v fatresize 1.0.2 (06/16/18) .Error: The file system is bigger than its volume!

2. change partition: gparted OR partx

resize sda1 part size in gparted GUI // partx cli also can do this.

ubuntu$ lsblk /dev/sda
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    1 28.9G  0 disk 
└─sda1   8:1    1   10G  0 part

ubuntu$ sudo blkid /dev/sda* /dev/sda: PTUUID="d45b4904" PTTYPE="dos" /dev/sda1: UUID="0403-0201" TYPE="vfat" PARTUUID="d45b4904-01"

3. check result

ubuntu$ sudo mount /dev/sda1  /media/chen/test

ubuntu$ df -h /media/chen/test Filesystem Size Used Avail Use% Mounted on /dev/sda1 10G 2.4G 7.7G 24% /media/chen/test

ubuntu$ sudo dosfsck /dev/sda1 ...


update1:

seems gparted will resize fat fs in step 1.
// but if fs mounted, it may fail, but not suspend

here is normal log and result:

gparted_resize_fat.png

yurenchen
  • 441
  • If OP really needs to defragment the files before the resize operation, he could use defragfs: https://github.com/ThomasCX/defragfs/blob/master/defragfs – emk2203 Oct 03 '21 at 12:29
  • 1
    The fatresize command in step 1 didn't do anything, the -i flag caused it to only list info and (quietly) exit. – Xen2050 Mar 26 '23 at 10:46
  • @Xen2050 Thanks for the feedback, the answer has been updated – yurenchen Mar 26 '23 at 16:05
3

Well, first if you "already having data in the form of files. I can't lose them" your very first step is to back them up before you do anything else.

Resizing and moving partitions does not often result in data loss, and if all else fails you can almost always revover files with photorec - http://www.cgsecurity.org/wiki/PhotoRec - it is a pain to do so.

Second you can not resize or move partitions that are in use. This is the main reason people advise you do these operations from a live usb.

So , no matter what, you can not move or resize your root partition without booting to a live USB (an exception would be adding to LVM).

So long as the partitions in question are not in use, ie unmounted, you can move and resize them :

See How do I resize partitions using command line without using a GUI on a server?

Otherwise your question is likely to be closed due to :

  1. You are running Debian

  2. Lack of information - You did not post your partitioning scheme, tell us what you want to move, what partitions are mounted, and what command and error message you are getting.

Panther
  • 102,067
  • 1
    he's running arm not debian --- "Although I can install on my debian, I can not get installable on my arm" – ravery Oct 07 '17 at 15:06
  • Yes I am not running debian, I am running ARM. Actually backing up data was not an option that I am looking for. But I can unmount that partition. – Rengasami Ramanujam Oct 07 '17 at 15:39
  • @RengasamiRamanujam, You were not looking for backup, but we recommend it anyway. We don't want you to lose valuable data, and resizing a partition is risky. – sudodus Oct 07 '17 at 15:46
  • PhotoRec has been never a reliable tool. – user6039980 Oct 25 '17 at 14:05
  • @Kais - Do you have a better one ? PhotoRec has worked for me as well as any other I have tried. – Panther Oct 25 '17 at 15:21
  • @Panther Of course, partition recovery tools - there are plenty, and the good ones are commercial. No one wants to get his data chunked. – user6039980 Oct 25 '17 at 16:01
  • OK, use what you wish I suppose, not sure the point of your comments other than to start a debate. If you have better advice , add an answer already. I will say testdisk and photorec "works for me" , never had a problem that an alternate tool could resolve, and unless you have some specific advice best of luck to you and use the tools you prefer. – Panther Oct 25 '17 at 16:32
1

You had better copy the data in the form of files to a safe place (call it backup if you wish), but in this case also a temporary location, because I think you have to accept that you cannot shrink the FAT file system with linux tools. You might be able to do it with Windows tools (FAT32 is a Microsoft file system.)

So after you have copied the data in the form of files to a safe place, you can

  • boot from another drive and unmount all partitions in this drive, that you want to change

  • edit the partition (easiest to remove the FAT32 partition and create a new partition with the size you want.

  • create a FAT32 file system in the new partition

  • do the other modifications to the partition table ...

  • copy the files from the safe place to the new FAT32 partition.

sudodus
  • 46,324
  • 5
  • 88
  • 152
  • You most certainly can resize FAT partitions from linux https://www.gnu.org/software/parted/manual/html_chapter/parted_6.html#SEC54 – Panther Oct 07 '17 at 15:19
  • OK, @bodhi.zazen, I failed one week ago in an up to date 16.04 LTS system with gparted. But when you say, that other tools can do it, I'm happy :-) – sudodus Oct 07 '17 at 15:38
  • @Rengasami Ramanujam, You can try to shrink the FAT32 file system and partition, but I do not rely on the process after a failure last week, so I recommend that you copy the data in the form of files to a safe place before you start. – sudodus Oct 07 '17 at 17:28
  • @bodhi.zazen Will parted do file defragmentation within a partition before resizing it? In other words, will parted make distributed file storage blocks to contiguous error? – Rengasami Ramanujam Oct 08 '17 at 11:10
  • @RengasamiRamanujam - It is considered poor form to ask a new question in the comments. You should ask a new question. Parted does not defragment but it should move files as necessary to resize the partition. I do not know the details of the internals of parted. – Panther Oct 08 '17 at 14:14
  • @bodhi.zazen Agreed and taking your feedback. Wont ask new question. – Rengasami Ramanujam Oct 08 '17 at 14:27
  • @Panther - AFAIK, that manual page is very out-of-date - it refers to parted version 1.6. In version 3.0, all filesystem-specific features were removed. Current as of writing is 3.2, and the manual for 3.2 makes no reference to filesystem resizing. Other linux programs can still resize FAT, but it seems parted is no longer among them – Hamy Jan 07 '18 at 16:54
  • @Hamy what man page are you referring to ? if it is the man page in Ubuntu file a bug report - http://manpages.ubuntu.com/manpages/artful/man8/parted.8.html – Panther Jan 07 '18 at 17:03
  • @Panther - It's a GNU issue. The man linked in your Oct '17 comment is for 1.6. I'm unclear if GNU would take down an old manual, but that would be the right place to go - they have linked the 3.2 manual properly from their landing page for parted - e.g. https://www.gnu.org/software/parted/manual/ - but they are still hosting this old manual for 1.6. I'm not entirely sure how you got to that page as it's not linked from the GNU parted landing page - e.g. note the URI references html_chapter whereas the landing page uses html_node – Hamy Jan 07 '18 at 19:14