Create an image of my SD-CARD so someone else can reuse it

Before you begin, you have already done these things.

  • Use JetPack 4.6.1 sd card.
  • Install and setup.

I create my environment on a 32GB sd card to speed up my work.

Start here.

Logging in to Jetson

  • Install sd card capacity expansion script.
  • Delete apt cache.
  • Delete python/jupyter history. (if you need)
  • Disable problem report. (if you need)
  • Disable GUI. (if you need)
  • Delete user command history.
  • Delete super-user command history.
  • Delete system logs.
  • Shutdown.

Install sd card capacity expansion script.

If your JetPack version is not 4.6.x, select the collect branch.

git clone https://github.com/NVIDIA-AI-IOT/jetcard
cd jetcard
git checkout 852475be56ab220283b273f7c320e74b764a3b43
sudo cp scripts/archive/nvresizefs.sh /etc/systemd/
sudo cp scripts/archive/system/nvresizefs.service /etc/systemd/system/
sudo ln -s /etc/systemd/system/nvresizefs.service /etc/systemd/system/multi-user.target.wants/nvresizefs.service

ls -l /etc/systemd/nvresizefs.sh
ls -l /etc/systemd/system/nvresizefs.service
ls -l /etc/systemd/system/multi-user.target.wants/nvresizefs.service

cd ..
rm -rf jetcard

Delete apt cache.

sudo apt-get autoremove -y
sudo apt-get clean
sudo rm -rf /var/lib/apt/lists/*

Delete python/jupyter history. (if you need)

rm -rf ~/.ipynb_checkpoints/
rm -rf ~/.ipython/
rm -rf ~/.python_history

For example, you need to use pythoch pre-trained model in a jupyter notebook.
In such cases, instead of running jupyter notebook, run the python command.
In this case, no cache will be created.
Example of downloading a pre-trained model of pytorch.

python3 -c "import torchvision; \
  model = torchvision.models.alexnet(pretrained=True); \
  model = torchvision.models.resnet18(pretrained=True)"

Disable problem report. (if you need)

sudo sed -i 's/enabled=1/enabled=0/g' /etc/default/apport

Disable GUI. (if you need)

sudo systemctl set-default multi-user.target

Enable GUI (if you want to rollback)

sudo systemctl set-default graphical.target

Delete user command history.

history -c
HISTIGNORE=*;history -d `history|awk '{a=$1;}END{print a;}'`
rm -rf ~/.bash_history

Delete super-user command history.

sudo su
history -c
HISTIGNORE=*;history -d `history|awk '{a=$1;}END{print a;}'`
rm -rf ~/.bash_history

Delete system logs.

For Jetson Nano 4GB/NX JetPack 4.6.x.

rm /var/log/alternatives.log*
rm /var/log/auth.log*
rm /var/log/btmp*
rm /var/log/dpkg.log*
rm /var/log/kern.log*
rm /var/log/lastlog*
rm /var/log/oem-config.log*
rm /var/log/syslog*
rm /var/log/tallylog*
rm /var/log/wtmp*
rm /var/log/Xorg.0.log*
touch /var/log/alternatives.log
touch /var/log/auth.log
touch /var/log/btmp
touch /var/log/dpkg.log
touch /var/log/kern.log
touch /var/log/lastlog
touch /var/log/oem-config.log
touch /var/log/syslog
touch /var/log/tallylog
touch /var/log/wtmp
touch /var/log/Xorg.0.log
chown syslog:adm /var/log/auth.log
chown root:utmp /var/log/btmp
chown syslog:adm /var/log/kern.log
chown root:utmp /var/log/lastlog
chown syslog:adm /var/log/syslog
chown root:utmp /var/log/wtmp
chown root:root /var/log/Xorg.0.log

for CLEAN in $(find /var/log/ -type f)
do
    cp /dev/null  $CLEAN
done

Shutdown.

shutdown -h now

If you want to disable bug reports or disable the GUI, please do so before shutdown.

Ubuntu PC

  • Make sd card image.
  • Partition compression.

Make sd card image.

I use SONY MRW-S1 SD CARD READER.

sudo su
fdisk -l
umount /dev/sda1
dd bs=4M if=/dev/sda of=jetbot-nano-4gb-jp461-2022-04-04.img status=progress

This process sometimes fails in the process.
This is one of the reasons I use a 32GB sd card.
A smaller capacity sd card is less likely to fail.

Partition compression.

sudo su
modprobe loop
export LOOP=`losetup -f`
losetup $LOOP jetbot-nano-4gb-jp461-2022-04-04.img
partprobe $LOOP
echo $LOOP
gparted $LOOP

Edit partition.
Good reference: partitioning - "Re-size" the .img for smaller SD-card. How to SHRINK a bootable SD card image - Ask Ubuntu
After partition have been changed, exit gparted.

Delete loop.

losetup -d $LOOP
unset LOOP

The file size has not yet been reduced.

ls -l jetbot-nano-4gb-jp461-2022-04-04.img 
-rw-r--r-- 1 root root 32010928128 Apr 13 20:14 jetbot-nano-4gb-jp461-2022-04-04.img

Check the image information.

fdisk -l jetbot-nano-4gb-jp461-2022-04-04.img
Disk jetbot-nano-4gb-jp461-2022-04-04.img: 29.83 GiB, 32010928128 bytes, 62521344 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 91FC289A-1E03-495A-9DAD-732401089949

Device                                 Start      End  Sectors  Size Type
jetbot-nano-4gb-jp461-2022-04-04.img1  28672 60024831 59996160 28.6G Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img2   2048     2303      256  128K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img3   4096     4991      896  448K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img4   6144     7295     1152  576K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img5   8192     8319      128   64K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img6  10240    10623      384  192K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img7  12288    13055      768  384K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img8  14336    14463      128   64K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img9  16384    17279      896  448K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img10 18432    19327      896  448K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img11 20480    22015     1536  768K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img12 22528    22655      128   64K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img13 24576    24959      384  192K Linux filesystem
jetbot-nano-4gb-jp461-2022-04-04.img14 26624    26879      256  128K Linux filesystem

Partition table entries are not in disk order.

See the first line.

Device                                 Start      End  Sectors  Size Type
jetbot-nano-4gb-jp461-2022-04-04.img1  28672 60024831 59996160 28.6G Linux filesystem

Reduce file size.

truncate --size=$[(60024831+1)*512] jetbot-nano-4gb-jp461-2022-04-04.img 

Now the file size is smaller.
(This image has a large SWAP size. So it was able to reduce only 1.3 GB.)

ls -l jetbot-nano-4gb-jp461-2022-04-04.img 
-rw-r--r-- 1 root root 30732713984 Apr 13 20:17 jetbot-nano-4gb-jp461-2022-04-04.img

There may be a smarter way to do the part of deleting logs.
This is how I do it when I create a sd card image.

1 Like