Limiting the capacity of SD cards for the pi

Sep 17, 2023 Last reply: 2 years ago 204 Replies

Oh dear. I have zero understanding of that. I'm not going to manage this am I?

Bob.

Now you know how to mount a disk, you need to know how to find Linux commands that can help you do what you want to do with the RPi. For the rest of this info to work, you need to know how to login as the default user AND how to log in as root:

1 the 'man name' command shows you a page (which can be very long) that tells you what 'name' does and how to use it, Name can be anything from a command to a Linux system call or function.

2 the "apropos 'text' command shows you a list of commands whose manual pages have 'text' in their first line of their manual pages.

3 Using the 'apropos' and 'man' commands is the fastest way I know of of finding a standard issue Linux program that will do what you need it to do and finding out how to use it,

4 If you like reading books rather than screens, consider getting a copy of "UNIX in a Nutshell" or "Linux in a Nutshell" - both are concise references to the way UNIX/Linux works and how to use it.

There's also "Unix Systems Programming for SVR4" which, although quite old now, is still a useful guide to writing programs and applications in C.

Using Tar as suggested is one option. If you still want to use raw image files where the partitioning is already done for you, then what I've done for that is create a blank disk image file of the size I want (using dd reading from /dev/zero), then partition that and copy over the system files before writing that image to a real SD card.

I've also had success with simply creating a partition table with blank space at the end of an SD card. If you just copy over as much data as will fit on the smaller SD card, it should still work if the partitions had all ended before the space ran out. Resizing the last partition on the existing card should work too (make a backup first). But doing the work on a file instead of a real card is probably safer and less confusing. Faster too, especially if you create it in /tmp so the write operations all happen in RAM, provided you have enough.

Computer Nerd Kev (and others),

I've got the same problem as the OP, and also thought of the above as the simpelest solution to the problem.

Question : I'm using an OS image (bullseye, bullseye lite) as can be downloaded here :

formatting link
Is there a possibility to edit it in such a way that it will automatically reserve some space (dummy partition or otherwise) at the end of the SD card ?

Regards, Rudy Wieser

Thank you for that information. Scary stuff. I've done what I've done so far by googling for answers and a little experimenting. I'm neither clever nor an academic.

I wanted to learn how to write code for tcpip comms for another platform, I was advised to get a book called "Unix Network Programming". It's a thick book and cost a lot of money. I got to about page 3 before I was out of my depth and beyond rescue. That's why I have not got any linux books.

Thanks again.

Bob.

I think I understand the principle there (just), but dear me. What is dd? What is /dev/zero is that a drive?

I'd love to do it but don't have anywhere near enough knowledge to do it?

No idea how to create a partition table. Wouldn't know where to start.

Thanks for trying to help me but you're way above my level.

Cheers,

Bob.

If you follow the instructions exactly, and it all works (!), you should be able to manage it. If things are different for your setup (quite likely, unfortunately), then some understanding will be needed to figure out how to keep on track...

If you log in to the pi as "pi", then issue commands prefixed with "sudo" so they run as root (administrator), each command runs in its own "shell", which terminates when the command completes. This means the effects of some commands don't persist from one call to another, e.g.:

$ sudo pwd /home/ian

$ sudo cd /tmp

$ sudo pwd /home/ian ## pwd not changed!

If you log in as "root", or get a root shell by calling "sudo bash", things work as expected:

$ sudo bash

# pwd /home/ian

# cd /tmp

# pwd /tmp

(Note the convention of using "$" to indicate commands that are executed as a normal user, and '#' for those executed as root. On sane systems, this reflects the shell prompt).

[Snip]

[Snip]

Ian, fantastic post. Such a lot explained.

Thank you so much.

Bob.

What a wonderful mixed metaphor.. I smell a rat. I feel it in the air. We must nip it in the bud...

Yes you are. One step at a time.

There is a super user account that can do everything. It's called root. There are two easy ways you can get to 'be root'

One is 'sudo bash', and the other is 'su -'

Both will ask for a password - in the first case it will be yours, in the second case it will be root's.

Root doesn't come with a password set by default, so the first thing you need to do is to give it one...

'sudo passwd' will ask for YOUR password, then ask you to type in one for root, twice. To make sure of whatever.

Then 'su - ' will work, every time.

One of the first things I do is set a root password, because I hate using sudo when I have a lot of configuration work to do.

'sudo -i' always works for me.

LOL. Don't ever pick up a book on Philosophy. I think it was Wittgenstein who said 'If you have not bought a book on philosophy, got as far as page 3, and then immediately thrown it into a corner of the room. you have no aptitude for philosophy'

I too have ceased buying Unix and linux books - the Web is by far the quickest way, or asking here.

In general you can learn far more my grabbing 'a simple TCP/IP client for Linux' as source code, compiling and debugging it than a dozen books will teach you.

Just keep muddling along and be grateful that these days you can buy a fully operational multi user multitasking computer that will run a full linux distro for £15 when back in the day it was nearer £15,000...

Brilliant and helpful post. I'm doing well today. So many good teachers generous with their time.

Thanks.

Bob.

It was never £15,000. Back in 1992 a reasonable PC cost about £2000.

Prior to Linux, late 80s, Xenix ran impressively on a 386. The writing was on the wall for minis.

I think it stands for 'direct disk' Unix was written in the days long before even a CRT terminal was common and so its commands were deliberately short.

What it does is ignore the niceties of partitions and files and just copy a disk sector by sector into a file or vice versa. Conventionally the files are *.img extensions and, if copied back onto a fresh disk (or SD card) will recreate not only the files, but the partitions informations was well (and in the case of Intel machines, the boot sector as well)

It is a pseudo file, which is infinitely long and contains an infinite source of binary zeroes.

Whatever you do to it, how ever many characters you read from it, they will all always be zero.

Unix tried to make everything look like a file, so this is a special file that looks like a file, acts like a file but contains no data, or if you prefer an infinite amount of data guaranteed to be 0.

There is another pseudo file called /dev/null, which is an infinite sink of data. A write only file. If you have a program that insists on chattering output to you that yo really don't care about you can 'redirect' its output to /dev/null

If only such a thing existed for my Australian cousin....

...

There is a command called 'parted' (PARTitionEDitor) that allows you to do exactly that.

No, he is not. Only just above your level. One step and a time

Thanks. I've seen that before in the distant past. Didn't understand why it might be different to just sudo.

Bob.

Back in the early 80s, you needed a PDP/11 to run Unix. They were about £15,000 Or a VAX. They were a LOT more. The Internet ran on VAXes to start with.

Xenix was much later . And it wasn't a whole lot of cop. The best 'PC Unix' was SCO and if you wanted BSD you bought a SUN SPARC, but that was long after the PDP era.

The fact that a PI Zero is probably a better computer than a VAX was back in the day is - astonishing.

All it lacks is a hard drive.

'-i' forces a login shell, as in runs target user's .login, .bash_profile or .profile, and .bashrc see: 'man sudo'

On a 'new' copy on an SD card, /boot/cmdline.txt finishes with: init=/usr/lib/raspberrypi-sys-mods/firstboot

If you either remove that bit (not recommended), or edit /usr/lib/raspberrypi-sys-mods/firstboot to remove (or comment out) the bulk of the 'do_resize' function (add return just after the definition) before you start the Pi for the first time, you may be lucky. You can use (g)parted to resize the partition manually (obviously on another machine).

Right. I have got quite a bit further but stumped now. :-)

I typed sudo passwd <ret>

It asked for a new password x2

Never asked for existing password for pi user oddly.

after that su - worked fine.

On stage 3. mkdir /mnt/x I got: Cannot create directory, file exists. I presume that is from my attempts yesterday. How would I remove that file/directory or whatever it is?

Anyway I ignored it and persisted with mount /dev/sda2 /mnt/x

seemed to work, no error.

Stage 4. cd /mnt/x worked fine. tar -czf ~/xxx.tar.gz . this went away for more than 18 minutes, I thought it had crashed. Got 22 lines of this...

tar: ./var/lib/samba/private/msg.sock/ nnnnn socket ignored.

eventually machine came back.

stage 5

cd umount /mnt/x

seemed to go fine.

stage 6 fdisk /dev/sda2

I ASSUME sda2 is correct ???

P

Lots of text and red writing about 'ext4' signatures...

Then a table very similar to your example but after the line: Disk identifier: 0x9b19dd1d

I did not get the 3 lines

Device Boot start End sectors size Id Type /dev... /dev...

Just didn't happen.

Help !

Thanks.

Bob.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required