Resizing an SD partition

Often, when loading a Linux operating system onto an SD Card, the partition is set to the size of the ISO file which may only be 2GB.
Of course if the SD card is 16GB you may want to resize the root partition to use the full SD card space.
This can be done fairly simply. (The following was copied from a post on a forum).

The Short Version:

Backup your system using "dd if=/dev/sdx of=/path/to/image bs=1M"
Remove the main and swap partitions using fdisk (leaving the boot partition alone)
Recreate the main partition to utilize the remaining disk space (excluding the boot partiton). Make sure to reuse the same start sector as the original root partition.
reboot the system
resize the new boot root partition to utilize the full partition size using resize2fs.

Step by Step Instructions:

First make a backup of your SD Card in case something goes wrong.
From the command line or a terminal window enter the following
sudo fdisk /dev/mmcblk0
then type p to list the partition table
you should see three partitions. if you look in the last column labeled System you should have
W95 FAT32
Linux Swap

make a note of the start number for partiton 2, you will need this later. though it will likely still be on the screen (just in case).

next type d to delete a partition.

You will then be prompted for the number of the partition you want to delete. In the case above you want to delete both the Linux and Linux swap partitions.

So type 2

then type d again and then type 3 to delete the swap partition.

Now you can resize the main partition.

type n to create a new partition.

This new partition needs to be a primary partition so type p.

Next enter 2 when prompted for a partition number.

You will now be prompted for the first sector for the new partition.
Enter the start number from the earlier step (the Linux partition)

Next you will be prompted for the last sector you can just hit enter to accept the default which will utilize the remaining disk space.

Type w to save the changes you have made.

Next reboot the system with the following command:

sudo reboot

once the system has reboot and you are back at the commandline enter the following command:

sudo resize2fs /dev/mmcblk0p2

Note: this can take a long time (depending on the card size and speed) be patient and let it finish so you do not mess up the file system and have to start from scratch.

Once it is done reboot the system with the following command:

sudo reboot

You can now verify that the system is using the full capacity of the SD Card by entering the following command:

df -h

Why This Works:

The real magic here is that you delete the root and swap partitions, then recreate only the root partition (using the original start sector) before writing the data to the disk. As a result you don't erase the existing data from the root partition.

By removing the swap partition you allow the root partition room to grow beyond its current size and fill the unused portion of the disk (because of the placement of the partitions -the root partition is sandwiched between the boot and swap partitions - it can't simply be resized leaving the swap partition alone).

You then resize (which is safe to run on a mounted disk) the file system to use all the space in the new root partition.

07.07.2018. 04:46

3G USB Modem

Linux is a fun, frustrating, exciting, head scratching operating system for computers. Unfortunately its not well supported by commercial entities. Most equipment suppliers will provide help or even software drivers for Windows or Apple computers but very little for the Linux user. So we have to search for answers that other Linux users have put on web forums or personal blogs like this one.

When I have a task to accomplish with Linux I can usually find some way to do it and in the process my skill level improves. So lately I've been working on using the ubiquitous little Raspberry Pi for various tasks. Using a version of the Debian Linux system called 'Raspbian' it has some amazing features.

One thing I hadn't tackled with the Raspberry Pi was using a 3G USB modem on it. I've used telephone dial-up modem software on Linux years ago but not a 3G modem. Needing an Internet connection on a remote Science Station it seemed like a natural way to go.

I started with a USB modem from Optus, the Huawei E3131 and looked for articles on how to get it running. When the modem is first plugged in, the USB subsystem in Linux talks to it to find out what device it is. The modem responds with a manufacturer code and hardware code, in this case 12d1:14fe and the Raspberry Pi assigns it as an Optical Disk or CD Drive (run 'dmesg' and you should see a scsi device 'sr0'). Now of course these devices are configured to connect to a Windows PC mostly. So when plugged into a Windows computer it mounts as a CD which contains a startup program which installs the Dial-up program for Windows. Not much good to us.

I found an article on the Raspberry Pi Forum that gave me a lot of answers for this device. Huawei E3131 on Wheezy. So I needed to tell the modem to stop being a CD drive and change to being a modem. By running a line of code I could do this, according to the article. I needed to install a program called sg3-utils so I could talk directly to the modem.

On the command line i ran 'sudo apt-get install sg3-utils'

I could now send the modem a command to change mode. So on the command line again I ran
'sudo /usr/bin/sg_raw /dev/sr0 11 06 20 00 00 00 00 00 01 00'
and a few seconds later the modem responded with its buffer ready. Awesome. Thats step one. So now how do I get it to connect to the Internet?

Once the device had changed mode to a modem, Linux realized it was a serial device and assigned it ttyUSB0, ttyUSB1 and ttyUSB2. I could now talk to it using modem 'AT' commands. The AT command set is an old message and control format used with RS232 serial ports to talk to dial-up modems and was just what I needed.

A Linux program I'd used years ago to talk to modems, WVDial, seemed perfect for the job so I installed it on the Raspberry Pi using
'sudo apt-get install wvdial'
and configured the dialler to talk to the modem. When installed, WVDial has a configuration file that needs to be edited to set up the dialling parameters. The 'pre-paid' settings for Optus Australia turned out to be 'preconnect' for the APN (access point name) and the dialled number is *99#. After setting up the wvdial.conf file I added a shell script to the startup rc.d boot list so the script would execute after everything else during bootup. The script needed to look for a device called 'sr0' since this is the CD Drive the modem first came up as.

So, now I'm at the point where on boot-up, if the USB modem is plugged in it gets mode changed by the script to be a serial device. Sure enough - I had three tty devices. To run the modem dialler I had to type in 'sudo wvdial optus &' (the '&' at the end tells Linux to drop out of the shell script and return to command line). Up came the modem response on the screen as it talked to Optus. The Raspberry Pi was assigned an IP address and also the addresses of two DNS Servers. Great! I was online.

I tried a 'ping' to the outside world. It was working. Now at this point I was still in command line mode. I was itching to try a web browser so I started XWindows on the Raspberry Pi. So I just typed 'startx' and up came the very fetching Pi Desktop. I opened the web browser 'Midori' and of course opened this site first!

The response was quick, only a few seconds and there was my website. All I needed was to run 'sudo wvdial optus &' to connect to the Internet and to run 'sudo pkill -f wvdial' to stop wvdial and hang up. For my task that I needed an Internet connection for I just needed a script to call when I needed the Internet, then hang up afterwards. In this case, a file is getting uploaded using the Ethernet port from elsewhere to the FTP server running on the Raspberry Pi. The script gets called every minute using Cron and looks for the file to upload. If the file exists then wvdial is called, and a few lines in the script starts the Linux FTP client. The file get sent and the modem is then disconnected ready for next time.

Well thats it. There should be enough information for the next person looking for a way to connect their Huawei E3131 to a Raspberry Pi and get on the Internet. Thats what we do. We make something and add to it and write about it for the next person.

I love Linux.

06.07.2014. 01:02