Storage Expansion
RDK X5 Automatic Storage Expansion on Boot
To make it easier for developers to use, we added an automatic storage expansion script to the image.
Create the auto_resize.sh Script:
sudo nano /usr/local/bin/auto_resize.sh Add the following content to the file:
#!/bin/bash
# Automatically detect the root partition and expand it to the maximum available space
# Log file
LOG_FILE="/var/log/auto_resize.log"
RESIZE_FLAG="/var/lib/resize_done"
# Logging function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Check whether resizing has already run
if [ -f "$RESIZE_FLAG" ]; then
log "Expansion already complete, skipping execution"
exit 0
fi
log "Starting the automatic resize script"
# Get the root partition device
ROOT_DEVICE=$(findmnt -n -o SOURCE /)
if [ -z "$ROOT_DEVICE" ]; then
log "Error: cannot find the root partition device"
exit 1
fi
log "Detected root partition device: $ROOT_DEVICE"
# Extract the device name and partition number
if [[ $ROOT_DEVICE =~ /dev/mmcblk[0-9]+p[0-9]+ ]]; then
# SD card type (mmcblk)
DISK_DEVICE=$(echo "$ROOT_DEVICE" | sed 's/p[0-9]*$//')
PARTITION_NUM=$(echo "$ROOT_DEVICE" | grep -o '[0-9]*$')
elif [[ $ROOT_DEVICE =~ /dev/sd[a-z][0-9]+ ]]; then
# SATA/USB type (sda, sdb, etc.)
DISK_DEVICE=$(echo "$ROOT_DEVICE" | sed 's/[0-9]*$//')
PARTITION_NUM=$(echo "$ROOT_DEVICE" | grep -o '[0-9]*$')
else
log "Error: unsupported device type $ROOT_DEVICE"
exit 1
fi
log "Disk device: $DISK_DEVICE"
log "Partition number: $PARTITION_NUM"
# Check whether the disk exists
if [ ! -b "$DISK_DEVICE" ]; then
log "Error: disk device $DISK_DEVICE does not exist"
exit 1
fi
# Get the current partition information
CURRENT_END=$(parted "$DISK_DEVICE" unit s print | grep "^ *$PARTITION_NUM " | awk '{print $3}' | sed 's/s$//')
DISK_SIZE=$(parted "$DISK_DEVICE" unit s print | grep "Disk $DISK_DEVICE:" | awk '{print $3}' | sed 's/s$//')
log "Current partition end sector: $CURRENT_END"
log "Total disk sectors: $DISK_SIZE"
# Calculate the last available sector (reserve some sectors)
AVAILABLE_END=$((DISK_SIZE - 1))
# Check whether resizing is needed
if [ "$CURRENT_END" -ge "$AVAILABLE_END" ]; then
log "Partition is already at its maximum size, no expansion needed"
touch "$RESIZE_FLAG"
exit 0
fi
log "Expansion needed: current end sector $CURRENT_END -> $AVAILABLE_END"
# Back up the partition table
log "Backing up the partition table..."
sfdisk -d "$DISK_DEVICE" > "/tmp/partition_backup_$(date +%Y%m%d_%H%M%S).sfdisk"
# Use parted to expand the partition
log "Starting expansion of partition $PARTITION_NUM..."
# Use parted to repair the partition table and expand
parted "$DISK_DEVICE" --script -- resizepart "$PARTITION_NUM" 100% 2>&1 | tee -a "$LOG_FILE"
if [ $? -ne 0 ]; then
log "Error: partition expansion failed"
exit 1
fi
log "Partition expanded successfully"
# Sync the disk
sync
sleep 1
# Expand the file system
log "Starting file system expansion..."
# Detect the file system type
FS_TYPE=$(blkid -o value -s TYPE "$ROOT_DEVICE")
log "Detected file system type: $FS_TYPE"
case "$FS_TYPE" in
"ext2"|"ext3"|"ext4")
log "Expanding ext file system..."
resize2fs "$ROOT_DEVICE" 2>&1 | tee -a "$LOG_FILE"
if [ $? -eq 0 ]; then
log "ext file system expanded successfully"
else
log "Error: ext file system expansion failed"
exit 1
fi
;;
"xfs")
log "Expanding XFS file system..."
xfs_growfs / 2>&1 | tee -a "$LOG_FILE"
if [ $? -eq 0 ]; then
log "XFS file system expanded successfully"
else
log "Error: XFS file system expansion failed"
exit 1
fi
;;
"btrfs")
log "Expanding Btrfs file system..."
btrfs filesystem resize max / 2>&1 | tee -a "$LOG_FILE"
if [ $? -eq 0 ]; then
log "Btrfs file system expanded successfully"
else
log "Error: Btrfs file system expansion failed"
exit 1
fi
;;
*)
log "Warning: unsupported file system type $FS_TYPE, skipping file system expansion"
;;
esac
# Create the completion flag file
touch "$RESIZE_FLAG"
log "Resize script finished"
Grant Execute Permission to the Script:
Create a System Startup Service
To ensure that the script runs automatically when the system starts, create a systemd service so that the script executes at system startup. Create the systemd service file:
Add the following content to the file:[Unit]
Description=Auto Resize Root Partition
Before=local-fs.target
After=systemd-remount-fs.service
Wants=systemd-remount-fs.service
DefaultDependencies=no
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/bin/auto_resize.sh
StandardOutput=journal+console
StandardError=journal+console
TimeoutSec=300
[Install]
WantedBy=local-fs.target
sudo systemctl daemon-reload // Reload the systemd configuration
sudo systemctl enable auto_resize.service // Enable the service so it runs automatically at startup
sudo systemctl start auto_resize.service // Start the service to perform the first expansion
sudo rm -f /var/lib/resize_done // If you need to repackage the image, delete the flag file before packaging
Set Up Logging
root@ubuntu:~# sudo nano /usr/local/bin/auto_resize.sh
root@ubuntu:~# sudo chmod +x /usr/local/bin/auto_resize.sh
root@ubuntu:~# sudo nano /etc/systemd/system/auto_resize.service
root@ubuntu:~# sudo systemctl daemon-reload
root@ubuntu:~# sudo systemctl enable auto_resize.service
Created symlink /etc/systemd/system/multi-user.target.wants/auto_resize.service → /etc/systemd/system/auto_resize.service.
root@ubuntu:~# sudo systemctl start auto_resize.service
root@ubuntu:~#
