Raspberry – Extending the root file system of Raspbian Jessie Lite

Recently I bought a Raspberry Pi 3 to experiment with Raspbian and Domoticz.

I’m starting from scratch with a lite installation of Raspberry Jessie. Downloading and flashing the Raspbian Jessie lite to a SDcard leaves you with two filesystems, a 60 MB /boot and a 1.2 GB root.
A df -h / shows the following:

Filesystem Size Used Avail Use% Mounted on
 /dev/root 1.2G 816M 290M 74% /

Guessing 1.2G is probably too small for a future Domoticz install I will start with extending the root filesystem. In this post I will try to explain some options available to grow the root filesystem.

raspi-config

Probably the easiest one. Fire up raspi-config with sudo raspi-config and choose option 1 Expand Filesystem.
The filesystem will be expanded to encompass all available storage.

???????????? Raspberry Pi Software Configuration Tool (raspi-config) ?????????????
?                                                                                ?
?    1 Expand Filesystem               Ensures that all of the SD card st        ?
?    2 Change User Password            Change password for the default us        ?
?    3 Boot Options                    Choose whether to boot into a desk        ?
?    4 Wait for Network at Boot        Choose whether to wait for network        ?
?    5 Internationalisation Options    Set up language and regional setti        ?
?    6 Enable Camera                   Enable this Pi to work with the Ra        ?
?    7 Add to Rastrack                 Add this Pi to the online Raspberr        ?
?    8 Overclock                       Configure overclocking for your Pi        ?
?    9 Advanced Options                Configure advanced settings               ?
?    0 About raspi-config              Information about this configurati        ?
?                                                                                ?
?                                                                                ?
?                     <Select>                     <Finish>                      ?
?                                                                                ?
??????????????????????????????????????????????????????????????????????????????????

Not the most useful command if you want to reserve some space for future partitions.

gparted

You can use a tool like gparted to expand the filesystem. Gparted is an excellent graphical partition manager.

Installing gparted on Jessie lite requires a lot of extra packages… there goes the “lite” installation.

# apt-get install gparted
 Reading package lists... Done
 Building dependency tree
 Reading state information... Done
 The following extra packages will be installed:
 fontconfig fontconfig-config fonts-dejavu-core hicolor-icon-theme libatk1.0-0 libatk1.0-data libatkmm-1.6-1 libavahi-client3 libcairo2
 libcairomm-1.0-1 libcups2 libdatrie1 libfontconfig1 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-common libglibmm-2.4-1c2a libgraphite2-3 libgtk2.0-0
 libgtk2.0-bin libgtk2.0-common libgtkmm-2.4-1c2a libharfbuzz0b libjasper1 libjbig0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0
 libpangomm-1.4-1 libparted-fs-resize0 libpixman-1-0 libthai-data libthai0 libtiff5 libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1
 libxdamage1 libxfixes3 libxi6 libxinerama1 libxrandr2 libxrender1
 Suggested packages:
 xfsprogs reiserfsprogs reiser4progs jfsutils ntfs-3g mtools yelp kpartx dmraid gpart cups-common librsvg2-common gvfs libjasper-runtime
 libparted-dev
 The following NEW packages will be installed:
 fontconfig fontconfig-config fonts-dejavu-core gparted hicolor-icon-theme libatk1.0-0 libatk1.0-data libatkmm-1.6-1 libavahi-client3
 libcairo2 libcairomm-1.0-1 libcups2 libdatrie1 libfontconfig1 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-common libglibmm-2.4-1c2a libgraphite2-3
 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libgtkmm-2.4-1c2a libharfbuzz0b libjasper1 libjbig0 libpango-1.0-0 libpangocairo-1.0-0
 libpangoft2-1.0-0 libpangomm-1.4-1 libparted-fs-resize0 libpixman-1-0 libthai-data libthai0 libtiff5 libxcb-render0 libxcb-shm0
 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxi6 libxinerama1 libxrandr2 libxrender1
 0 upgraded, 44 newly installed, 0 to remove and 0 not upgraded.
 Need to get 15.0 MB of archives.
 After this operation, 54.7 MB of additional disk space will be used.
 Do you want to continue? [Y/n]

Maybe it is better to pop the SDcard in another Linux box and run gparted on that machine.

Use the already available tools, parted and resize2fs.

Using parted and resize2fs it is quite easy to grow the filesystem with quite a lot of control over the end result. Two steps, first extend the partition with gparted and then resize the filesystem to use all the available space in the partition.

Sound easy right? How to go about this?
Find the partition containing the root filesystem, designated “/”, with mount or with cat /etc/fstab.

# cat /etc/fstab 
proc            /proc           proc    defaults          0       0
/dev/mmcblk0p1  /boot           vfat    defaults          0       2
/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

In the above example the root filesystem is on /dev/mmcblk0p2.

Start parted and use the select command to operate on the device containing the partition you want to expand (/dev/mmcblk0). A quick print command makes sure you are operating on the right device.

parted

(parted) select /dev/mmcblk0
Using /dev/mmcblk0
(parted) print
Model: SD ASTC (sd/mmc)
Disk /dev/mmcblk0: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  67.1MB  62.9MB  primary  fat16        lba
 2      67.1MB  1361MB  1294MB  primary  ext4

Print shows the partitions on the device /dev/mmcblk0 with the / partition being number 2.

It is now time to resize the partition. Enter resizepart 2 and specify the end of the partition in megabytes. In my case I decided to add 4096MB. Give after resizing a quick print command (p) to check the result.

(parted) resizepart 2                                                     
End?  [1361MB]? 4163                                                      
(parted) p                                                                
Model: SD ASTC (sd/mmc)
Disk /dev/mmcblk0: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  67.1MB  62.9MB  primary  fat16        lba
 2      67.1MB  4163MB  4096MB  primary  ext4

Time for the last step, growing the filesystem with resize2fs. It is as easy as resize2fs followed by the device representing the partition. Without the size parameter resize2fs will grow the filesystem to the largest possible size.

# resize2fs /dev/mmcblk0p2 
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/mmcblk0p2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mmcblk0p2 is now 999973 (4k) blocks long.

Finally a df -h shows the root filesystem being around 4GB,

# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.8G  858M  2.8G  24% /

That’s it! Thank you for reading, hope you can use this information in your Linux adventures.

 

(*) A quick note, I start all my admin sesions on debian based systems with “sudo su -” so I don’t have to use sudo all the time.