0

I don't have another computer to go in and mess around with the SD card. Is it possible to change operating systems from within the Pi itself?

2
  • your profile name implies that you already know the answer ..... lol – jsotola Mar 6 '19 at 17:32
  • It's a joke name haha – God Himself Mar 6 '19 at 17:34
3

Yes, if you have a second SD card, you can write that one from the running OS, then shut down the running OS and swap the newly-created card into the SD card slot. You will probably need a "USB SD adapter" device that can hold SD cards.

Once you've got the SD adapter and card plugged into the Pi you can use the dd command to copy the .IMG file to the new SD card (dd if=inputfile.img of=/dev/sdx iflag=fullblock oflag=direct status=progress), where /dev/sdx is the name of the device that you found from journalctl.

1

dd is the Unix utility to clone disks. (You can view the Linux "manual" page for it by typing man dd in a terminal window.) It can a physical disk (including a memory card) to another. I can clone a disk to a file image. I can clone a file image to a disk, etc. It operates at the block level of the device and doesn't care about partitions or filesystems (which means it can even clone filesystem types that Linux doesn't recognize).

The most basic version of the command needs an input file (source specified by if=___) and output file (destination). The "input file" needs to be the name of your downloaded disk image file (the .img file). The "output file" needs to be the raw device.

In Linux, the devices are always special files in the /dev folder. If you peek inside /dev you'll see lots of them. But there is a naming convention. The device will typically have a name of /dev/sd__. The "sd" stands for "SCSI device" (some would say "SCSI disk"). Technically your device isn't a SCSI device but that convention has expanded and now all sorts of USB attached devices are reported as if they are SCSI devices.

There will be at least one more letter... e.g. /dev/sda, /dev/sdb, /dev/sdc, etc. The "a", "b", "c" is used to differentiate which device in the event that you have several of them connected at the same time. Your device will probably show up as /dev/sdb (but that's not a guarantee... check it). lsblk will show you a device tree of all connected block devices. (Alternatively you can type dmesg and just the last few lines of output will show the device that it just attached. You want the last device it found.)

Here is an example and how to interpret the output:

pi@timspi:/dev $ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb           8:16   1  29.8G  0 disk 
├─sdb1        8:17   1   319M  0 part 
└─sdb2        8:18   1  29.5G  0 part 
mmcblk0     179:0    0 119.3G  0 disk 
├─mmcblk0p1 179:1    0  43.2M  0 part /boot
└─mmcblk0p2 179:2    0 119.2G  0 part /

I have two devices ...

One is "mmcblk0". This is actually the card that my Pi is currently using. You can see it has two partitions and they are mounted as "/boot" and "/".

The other is "sdb". This is the device that only showed up when I inserted an extra microSD card (via a card reader) into a USB port on the Pi. It also has two partition (sdb1 and sdb2) but they are not mounted (notice the "MOUNTPOINT" column is empty).

If I wanted to overwrite this card with a new OS image, then I would set the dd "of" (output file) parameter to of=/dev/sdb (not sdb1 or sdb2 ... those only address individual partitions or "slices" on the disk).

If your microSD card was previously formatted with an image that the Pi recognizes, it may try to mount any filesystems it finds. You should unmount them before issuing the dd command.

e.g. sudo umount /dev/sdb1; sudo umount /dev/sdb2

Once you know the device you need to use as the output file, use the dd command suggested by bls in the answer above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.