I have my pi 3 B+ in a case, and in the event I need to completely reinstall it, I'd rather not unscrew the case. Is there a way I can get at it through USB? Ideally I'd love the whole pi board to act as a pseudo-sd-reader and display itself as a mass storage device. Is that possible?
Yes, it is possible to rewrite the SD card without removing it, but not with the normal install procedures. Copy the running root filesystem to a USB device. Change the root device in the SD card /boot/cmdline.txt and the USB device /etc/fstab to the USB root partition. Reboot and running on the root filesystem in the USB device, download the new OS. Remove the root filesystem from the SD card and copy the new OS root filesystem to the SD card root partition. Remove the boot filesystem from the SD card and copy the new OS boot filesystem to the SD card boot partition. Check and correct the root device in both /boot/cmdline.txt and /etc/fstab on the SD card to the root partition on the SD card. Reboot and both new filesystems are running on the SD card.
The first vfat partition of the SD card contains the boot instructions and the the Linux kernel (unless NOOBS or PINN is installed, see below). The boot partition can not be reformatted on the running Pi system as the kernel is running, if it is reformatted, it will only change on reboot and may not boot up at all.
This procedure assumes a single OS on the Pi in two partitions with only a single USB drive to be formatted and used. Adjust if different as any mistakes may cause data lost and require rebuilding the SD card. Format a USB device to a Linux format and create an ext4 filesystem. Copy the running root filesystem to the USB device.
THIS PROCEDURE WILL DESTROY ALL DATA ON /DEV/SDA
sudo umount /dev/sda* 2>/dev/null # unmout /dev/sda if mounted echo -e "o\nn\np\n1\n\n\nw" | sudo fdisk /dev/sda # reformat /dev/sda sudo mkfs.ext4 -L root /dev/sda1 # makes Linux filesystem sudo mkdir /mnt/d1 /mnt/d2 # makes mount points for copying sudo mount /dev/sda1 /mnt/d2 # mounts USB filesystem sudo rsync -axvHAXW / /mnt/d2/ # copies root filesystem to USB device # If no rsync cmd, sudo apt-get install rsync
Change root device. Edit
/boot/cndline.txtwith sudo vi or sudo nano and change
root=/dev/sda1. Also edit
/mnt/d2/etc/fstaband change the
??? / ext4to
/dev/sda1 / ext4. Note that the
???could be in the form of
Verify that root (/) is on /dev/sda1 with the
lsblkcommand. Download new OS and copy the images back to the SD card.
wget https://downloads.raspberrypi.org/raspbian_latest #mv 2017-11-29-raspbian-stretch.img stretch.img mv *stretch.img stretch.img sudo fdisk -l stretch.img # ... # stretch.img1 8192 98045 89854 43.9M c W95 FAT32 (LBA) # stretch.img2 98304 6651903 6553600 3.1G 83 Linux # Root filesystem # Use the Start number from the above cmd in next and 10th step below. sudo mount -o loop,offset="$((512 * 98304))" stretch.img /mnt/d1 sudo umount /media/pi/* 2>/dev/null # owners are different on sudo mount /dev/mmcblk0p2 /mnt/d2 # /media/pi and /mnt sudo rm -R /mnt/d2/* # remove old root fileststem sudo rsync -axv /mnt/d1/ /mnt/d2/ # copy new root filesystem sudo blkid /dev/mmcblk0p|tr -d '"' # boot & root PARTUUID number # Replace the boot and root filesystem PARTUUID number with the # results of the sudo blkid cmd above. sudo vi /mnt/d2/etc/fstab # Edit new fstab, vi or nano # To prevent resizing of the root filesystem. sudo rm /mnt/d2/etc/init.d/resize2fs_once sudo umount /mnt/d # Boot filesystem sudo mount -o loop,offset="$((512 * 8192))" stretch.img /mnt/d1 sudo rm -R /boot/* # removes old boot filesystem sudo rsync -axv /mnt/d1/ /boot/ # copies new new boot filefilesystem sudo blkid /dev/mmcblk0p2|tr -d '"' # root PARTUUID number # Replace the PARTUUID number or the root=/dev/mmcblk0p2 with the PARTUUID # number from the above sudo blkid cmd (root=PARTUUID=????????-02). # Also remove the following text: init=/usr/lib/raspi-config/init_resize.sh sudo vi /boot/cmdline.txt # Edit root device, vi or nano sudo reboot # Running on new OS # raspi-config
If NOOBS or PINN is on the SD Card, then the first vfat partition(/dev/mmcblk0p1)/filesystem is NOOBS or PINN boot and /dev/mmcblk0p5 partition/filesystem(SETTINGS) is used to select which OS is to boot. Use the
lsblk cmd to locate the partitions of the
/ (root) filesystems. If there is only one OS on the SD card, use the procedures above and adjust for the boot and root partition (probably /dev/mmcblk0p6 for boot and /dev/mmcblk0p7 for root). If multiple Linux OS's are on the SD card, boot to a different Linux OS and use the above procedure as a guide to rewrite to boot and root partitions/filesystems of the OS to be replaced.