1

Hi guys i follow this How can I resize my / (root) partition? and work .

But it's possible to make an automate bash script?

I have to pass 501760 when ask for start number.

Thank you

robello84
  • 11
  • 1

2 Answers2

1

This is not a direct answer in the sense of "here I wrote a script for you" but it does contain some information that might be helpful to you or someone else who wants to do so.

You can probably automate fdisk via a tool called expect, which is intended to be used in scripts with interactive CLI tools; the basic idea is you tell expect what prompt to expect and what answer to provide using an "expect script". It isn't installed by default most places, and depends on Tcl, which may also not be installed, so you could just do that and have a look at it and play around, or have a look at the man page online (e.g.) and whatever tutorials and examples you can find (remember, search "linux expect tutorial", do not include "raspberry pi" in your search terms).

[Regarding OSMC: I'm not sure OSMC is intended to provide things like this, so you should check that first and if expect isn't available, keep reading, because it is more likely to have sfdisk, which is a better option anyway; if that isn't available give, you are out of luck.]

Using expect is not the ideal answer here, however. The better option is a tool called sfdisk which is sort of like fdisk, but intended for scripting using its own format. This can be fed via stdin, so you could do it with a "here document" in a bash script.

A thing about partition tables is you can screw around with them without destroying any data as long as you don't actually mount and use any of the partitions. The partition table information is written to a part of the device that is separate from the partitions themselves, so you can completely change things, then put it back the way it was -- no harm done. I'm saying this because you could get comfortable with sfdisk by reading the man page and playing around with a spare USB stick (if available). The first thing you'll want to do is make a back-up copy of the existing table that sfdisk can then use to restore things later; see the -d option.

Sfdisk is probably installed by default; try man sfdisk to find out. You want to read that page anyway.

There is a newer version of sfdisk (2.26, from 2015) that has a much better man page (and some additional functionality for GPT disks, but that is irrelevant on the pi). Unfortunately, Raspbian and other Debian derived distros (I think this includes OSMC) are still using a version from 2011. I would guess it is otherwise much the same but the man page is not as friendly. The ones found online are mostly for this old version as well. :( The newer one has a whole section "BACKING UP THE PARTITION TABLE" based on using

sfdisk -d /dev/whatever > backup.file

That you can later use to restore it if need be using:

sfdisk /dev/whatever < backup.file

This should be applicable to the old version, just it is not spelled out in the man page. Note I do not know how agreeable sfdisk is to making changes to a mounted partition containing the root filesystem but if you can do it with fdisk you can probably do it with sfdisk, and it can't hurt to try (unless it actually works and you screw up, lol).

I said earlier that you can do this without harming anything as long as you don't mount and access the partitions but that is not quite true: It's as long as you don't change anything. So you could mount them read-only, or just check it and don't change anything. In the case of expanding a filesystem, this means:

  • Using sfdisk to script expanding the partition.

  • Mounting the partition to check the filesystem is still readable. It won't have changed size; to do that you still have to use resizefs as per the link in your question (s/fdisk just increases the size of the partition so there is room for a bigger filesystem), but you want to make sure it is still readable because you want the starting offset the same. Otherwise I think using resizefs will fail and/or wreck the data on the fs.

    You could try to do this by just sticking/keeping it in the pi and seeing if it works but I advise against that unless you are determined to do all this from the pi itself, in which case I'd still look around for that old USB stick to play with first. And remember, if it doesn't work, you are now stuck having to take the card out and fix it elsewhere anyway.

  • Using resizefs to expand the filesystem on the partition. If you are trying to do this on a running pi several reboots are needed so you can't really do it with one simple script. You could do it with one more complicated script set up to fly via init as per raspi-config; if you think you are up to that, have a look at how that is done ;)

That's all you need to know to write a script to resize a filesystem. IMO very few people really need to do that, and the ones that do should have the ability to read and make use of this information. If it seems beyond your current skills, you probably do not need to be doing this in the first place.

Note that new versions of Raspbian look to have a new GUI tool for expanding the root fs, but of course you are not using Raspbian.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • whit OSMC work this run sudo fdisk /dev/mmcblk0 Then press the keys in order: **d 2** to delete, **n p 2 Enter Enter** to re-create. Verify the prompts to make sure what you are entering is sensible. In particular make sure the same starting sector as the one you just deleted. Reboot and enter:- sudo resize2fs /dev/mmcblk0p2 i need to insert start number is 501760 in automatic :) – robello84 Jun 16 '16 at 11:43
  • Right, so either you get the idea from the above here or you don't. If you don't, forget about doing it automatically. Not everyone can replace a head gasket in 90 minutes but then, people who cannot may have to accept setting aside a weekend instead. – goldilocks Jun 16 '16 at 11:47
1

I am not familiar with the latest OSMC (I run Kodi under Raspbian) but AFAIK it has a simple 2 partition FS and has the normal Linux tools.

If so the following should work:-

To resize:-

run sudo fdisk /dev/mmcblk0

Then press the keys in order:

**d 2** to delete,
**n p 2 Enter Enter** to re-create.

Verify the prompts to make sure what you are entering is sensible. In particular make sure the same starting sector as the one you just deleted.

Reboot and enter:-

sudo resize2fs /dev/mmcblk0p2

You could make a script BUT scripting fdisk is far from straightforward and you will probably never need to repeat.

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • 1
    This is really an abbreviated duplicate of the answer the OP links in the question, but I do agree that s/he is better off sticking with that anyway. ;) – goldilocks Jun 16 '16 at 11:39
  • Yes it's the same answer :) when when digit n p 2 ask for start number and my start number is 501760 i request if it's possible to automate this – robello84 Jun 16 '16 at 11:42
  • @robello84 just press `Enter` twice to accept the default. Yes it is possible to script (I have done this- usually when I want to move things) BUT it is complex and error prone. – Milliways Jun 16 '16 at 11:50