Page 1 of 1

QEmu + Raspbian Stretch + Lite IMG Second Partition Extend Free Space

Posted: Fri Sep 20, 2019 3:42 am
by inukaze
Hi there, look i am trying to virtualization Raspberry Pi and use Raspbian, in resume i do the follow

First Preparing all files (QEmu should be installed before + kvm + arm support)

Code: Select all

#Create a folder to work
mkdir -p "$HOME/raspbian/root" ; cd raspbian

#Download the image (i prefer the old version based on stretch, but i prefer if you do a Rasvuan [Based on devuan] and avoid the use of systemd)
https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-04-09/2019-04-08-raspbian-stretch-lite.zip

#Extract the downloaded file
unzip 2019-04-08-raspbian-stretch-lite.zip

#Download a compatible kernel for QEmu
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/kernel-qemu-4.14.79-stretch

#Download a PTB File to boot properly QEmu with the arquitecture ARM(hf)
wget https://github.com/dhruvvyas90/qemu-rpi-kernel/raw/master/versatile-pb.dtb

#Define environment varibles :
export KERNEL="$HOME/raspbian/kernel-qemu-4.14.79-stretch"
export RASPBIAN="$HOME/raspbian/2019-04-08-raspbian-stretch-lite.img"

SECTOR1=$( fdisk -l $RASPBIAN | grep FAT32 | awk '{ print $2 }' )
SECTOR2=$( fdisk -l $RASPBIAN | grep Linux | awk '{ print $2 }' )
SECTOR1=$(( SECTOR1 * 512 ))
SECTOR2=$(( SECTOR2 * 512 ))

#Mount & Activate SSH
sudo mount "$RASPBIAN" -o offset=$SECTOR1 "$HOME/raspbian/root"
sudo touch "$HOME/raspbian/raiz/ssh"	
sudo umount "$RASPBIAN"

#Fix the partition mount :
sudo mount "$RASPBIAN" -o offset=$SECTOR2 "$HOME/raspbian/root"
echo 'KERNEL=="sda", SYMLINK+="mmcblk0"
KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
KERNEL=="sda2", SYMLINK+="root"' | sudo tee "$HOME/raspbian/root/etc/udev/rules.d/90-qemu.rules"
sudo sed -i -e 's/^/#/' "$HOME/raspbian/raiz/etc/ld.so.preload"
sudo sed -i -e 's/^/#/' "$HOME/raspbian/raiz/etc/ld.so.conf"
sudo sed -i -e 's/^/#/' "$HOME/raspbian/raiz/etc/fstab"
sudo umount "$RASPBIAN"

#Clone the file
cp "$RASPBIAN" raspbian.img

#At this point i can use perfectly the raspbian image file, but the space available is just 1.6GB and i need around +4GB
#Well can be use with the follow line just for test 

qemu-system-arm \
-M versatilepb \
-cpu arm1176 \
-m 256 \
-net nic \
-net user,hostfwd=tcp::5022-:22 \
-dtb ./versatile-pb.dtb \
-kernel ./kernel-qemu-4.14.79-stretch \
-append 'root=/dev/sda2 panic=1' -no-reboot \
-hda raspbian.img
Well, Continuing with the second partition. Get the partition info :

Code: Select all

RASPBIAN="raspbian"
START1=$(sfdisk -l -uS "$RASPBIAN.img" | grep img1 | awk '{ print $2 }')
START2=$(sfdisk -l -uS "$RASPBIAN.img" | grep img2 | awk '{ print $2 }')
SECTORS1=$(sfdisk -l -uS "$RASPBIAN.img" | grep img1 | awk '{ print $4 }')
SECTORS2=$(sfdisk -l -uS "$RASPBIAN.img" | grep img2 | awk '{ print $4 }')

#Extract the partions / Just a preventive backup
dd if="$RASPBIAN.img" of="$RASPBIAN.img1" skip=$START1 count=$SECTORS1
dd if="$RASPBIAN.img" of="$RASPBIAN.img2" skip=$START2 count=$SECTORS2

#Resize the image file
qemu-img resize "$RASPBIAN.img" -f raw +5G

#Here is the point i stuck because don't matter if i expand with qemu-img, parted, or gparted
#Because don't matter the extend of free space, the image ever is stuck to use 1.6GB.
#and that is the point i not had idea how to solve it, and the reason of this post.
#For Example with parted :

Code: Select all

sudo parted raspbian.img

#Inside parted
print			# I Observer "Disk : .../raspbian.img: 12,5GB"
resizepart 2		# I Resize
End? 12,5G		# I Specify the 12,5G
print			# I Notice "End 12,5GB & Size 12,4GB"
q

#Using qemu with both images

Code: Select all

qemu-system-arm -M versatilepb -cpu arm1176 -m 256 -hda 2019-04-08-raspbian-stretch-lite.img -net nic -net user,hostfwd=tcp::5022-:22 -dtb ./versatile-pb.dtb -kernel ./kernel-qemu-4.14.79-stretch -append 'root=/dev/sda2 panic=1' -no-reboot -hdb raspbian.img

#Login with "pi" user and password "raspberry"
sudo fdisk /dev/sdb

#Inside fdisk
p		#Print the info about partition i write down the "Start" of "/dev/sdb2"
d		#Delete Partition
2		#Delete The Partition 2
n		#Add Partition
p		#Primary Type
2		#Add Second Partition
98304		#The sector must be the same begiing of the delete before partition
24494079	#Indicate the quantity of sectors must be use [Automatic Value should work]

Partition #2 contains a ext4 signature
Do you want to remove the signature? [Y]es/[N]o: N
q
#Outside fdisk
sudo umount /dev/sdb{1,2}
sudo e2fsck -f -y /dev/sdb2

sudo resize2fs /dev/sdb2
	resize2fs the filesystem is already (4k) blocks long. nothing to do

sudo umount /dev/sdb{1,2}
sudo e2fsck -f -y /dev/sdb2
sudo poweroff

I try with the modify image :
qemu-system-arm \
-M versatilepb \
-cpu arm1176 \
-m 256 \
-net nic \
-net user,hostfwd=tcp::5022-:22 \
-dtb ./versatile-pb.dtb \
-kernel ./kernel-qemu-4.14.79-stretch \
-append 'root=/dev/sda2 panic=1' -no-reboot \
-hda raspbian.img

#But is the same available space 1.6GB
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Another method :

Code: Select all

sudo kpartx -v -a "$RASPBIAN.img"
	add map loop1p1 (253:0): 0 87851 linear 7:1 8192
	add map loop1p2 (253:1): 0 13912064 linear 7:1 96256

#Backup Partition Table
sfdisk -d /dev/loop0 >~/loop0.parts	#Can be restore with the command -> sfdisk /dev/loop0 <~/loop0.parts
sfdisk -d /dev/loop1 >~/loop1.parts	#Can be restore with the command -> sfdisk /dev/loop0 <~/loop1.parts

#Using parted 

#Print information
parted /dev/loop1 print

#Delete second partition
sudo parted /dev/loop1 rm 2

#Create a new primary partition ext4 start after the end of first partition until 5GB
sudo parted /dev/loop1 mkpart primary ext4 50M 5GB

#Umount
sudo kpartx -d "$RASPBIAN.img" ; sudo kpartx -d /dev/loop1p{1,2} ; sudo kpartx -d /dev/loop1

#Mount again
sudo kpartx -v -a "$RASPBIAN.img"
	add map loop0p1 (253:0): 0 87851 linear 7:0 8192
	add map loop0p2 (253:1): 0 3424256 linear 7:0 98304

#Print mapper loop0 information
parted /dev/loop0 print	

#Check & Fix the second partition
sudo e2fsck -f -y /dev/mapper/loop0p2

#Try to resize :
sudo resize2fs /dev/mapper/loop0p2
	resize2fs the filesystem is already (4k) blocks long. nothing to do

#Mount & Check
sudo mkdir /media/prueba
sudo mount /dev/mapper/loop0p2 /media/prueba/
sudo df -h /media/prueba/

#Well still says is 1.6G /root , nothing changed.

#For example trying to use gparted :

Code: Select all

sudo gparted "$RASPBIAN.img"
#Resize/Move
Image

#I set total the new free space to the current
Image

#I Apply
Image

#The Grey Triangle says "You should check this partition" i use the "Check"
Image

#I execute it
Image

#I Accept to run the check
Image

#And i had new free space i can't use
Image

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

After of that and try another methods i don't find a way to expand free space available on the second partition (/root) of that image of raspbian stretch, for i do the test i want to try using qemu, someone had idea what i miss to solve that?

Thanks for any useful help and commentary

Re: QEmu + Raspbian Stretch + Lite IMG Second Partition Extend Free Space

Posted: Mon Oct 14, 2019 2:15 pm
by HypnoToad
Once the partition is resized, try using this command:

Code: Select all

sudo resize2fs /dev/sda2
where /dev/sda2 is the filesystem you need to resize