基于Ubuntu的linux环境制作嵌入式SD/TF启动卡
生活随笔
收集整理的這篇文章主要介紹了
基于Ubuntu的linux环境制作嵌入式SD/TF启动卡
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于ubuntu在linux環境下對TF/SD卡進行分區、格式化,并完成文件寫入。
1. ubuntu連接TF/SD卡
將讀卡器插入電腦USB端口,點擊虛擬機vmware的如下圖標連接到ubuntu系統
選擇連接(斷開與 主機的連接)(C)。
正常連接ubuntu后,可以通過如下命令查看卡的掛載情況:
我們的卡連接后對應的設備是/dev/sdb1
2. 分區
首先去除卡的掛載
ubuntu16@ubuntu:linux$ sudo umount /dev/sdb1 [sudo] password for ubuntu16: ubuntu16@ubuntu:linux$ df -l Filesystem 1K-bloubuntu Used Available Use% Mounted on udev 1977084 0 1977084 0% /dev tmpfs 401592 16680 384912 5% /run /dev/sda1 122735644 31807784 84670148 28% / tmpfs 2007952 212 2007740 1% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 2007952 0 2007952 0% /sys/fs/cgroup /dev/loop0 113408 113408 0 100% /snap/core/12821 /dev/loop1 224512 224512 0 100% /snap/code/93 /dev/loop2 113280 113280 0 100% /snap/core/12834 /dev/loop3 224512 224512 0 100% /snap/code/94 vmhgfs-fuse 393942012 340255188 53686824 87% /mnt/hgfs tmpfs 401592 76 401516 1% /run/user/1000 ubuntu16@ubuntu:linux$再次查看系統設備情況
ubuntu16@ubuntu:linux$ sudo fdisk -l ......Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: dos Disk identifier: 0x00000000Device Boot Start End Sectors Size Id Type /dev/sdb1 8192 31116287 31108096 14.9G c W95 FAT32 (LBA) ubuntu16@ubuntu:linux$從以上信息可以看出,我們需要操作的設備是/dev/sdb,執行分區命令:
# 注意,此處是sdb,不是sdb1 ubuntu16@ubuntu:linux$ sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.27.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.Command (m for help): p #輸入p查看分區 Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: dos Disk identifier: 0x00000000Device Boot Start End Sectors Size Id Type /dev/sdb1 8192 31116287 31108096 14.9G c W95 FAT32 (LBA)Command (m for help): d #輸入d刪除原有分區 Selected partition 1 Partition 1 has been deleted.Command (m for help): d #輸入d確認分區是否刪除成功 No partition is defined yet! Could not delete partition 1Command (m for help): n #輸入n創建新分區 Partition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions) Select (default p): p #輸入p選擇分區空間 Partition number (1-4, default 1): 1 First sector (2048-31116287, default 2048): 16384 Last sector, +sectors or +size{K,M,G,T,P} (16384-31116287, default 31116287): +3GCreated a new partition 1 of type 'Linux' and of size 3 GiB.Command (m for help): p Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: dos Disk identifier: 0x00000000Device Boot Start End Sectors Size Id Type /dev/sdb1 16384 6307839 6291456 3G 83 LinuxCommand (m for help): n Partition typep primary (1 primary, 0 extended, 3 free)e extended (container for logical partitions) Select (default p): p Partition number (2-4, default 2): 2 First sector (2048-31116287, default 2048): 6307840 Last sector, +sectors or +size{K,M,G,T,P} (6307840-31116287, default 31116287): Created a new partition 2 of type 'Linux' and of size 11.8 GiB.Command (m for help): p Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 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: dos Disk identifier: 0x00000000Device Boot Start End Sectors Size Id Type /dev/sdb1 16384 6307839 6291456 3G 83 Linux /dev/sdb2 6307840 31116287 24808448 11.8G 83 LinuxCommand (m for help): w #輸入w保存分區 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.查看剛才的分區情況,如下:
ubuntu16@ubuntu:linux$ sudo lsblk [sudo] password for ubuntu16: NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT loop1 7:1 0 219.1M 1 loop /snap/code/93 sdb 8:16 1 14.9G 0 disk ├─sdb2 8:18 1 11.8G 0 part └─sdb1 8:17 1 3G 0 part ...... ubuntu16@ubuntu:linux$3.格式化
ubuntu系統自帶了多種格式化工具,如下圖所示:
ubuntu16@ubuntu:linux$ mkfs mkfs mkfs.cramfs mkfs.ext3 mkfs.ext4dev mkfs.minix mkfs.ntfs mkfs.bfs mkfs.ext2 mkfs.ext4 mkfs.fat mkfs.msdos mkfs.vfat將卡的兩個分區格式化成ext3格式:
ubuntu16@ubuntu:linux$ sudo mkfs.ext3 /dev/sdb1 mke2fs 1.42.13 (17-May-2015) Creating filesystem with 786432 4k bloubuntu and 196608 inodes Filesystem UUID: 3fe3147c-e558-46fe-b197-e537e1852122 Superblock backups stored on bloubuntu: 32768, 98304, 163840, 229376, 294912Allocating group tables: 完成 正在寫入inode表: 完成 Creating journal (16384 bloubuntu): 完成 Writing superbloubuntu and filesystem accounting information: 完成 ubuntu16@ubuntu:linux$ sudo mkfs.ext3 /dev/sdb2 mke2fs 1.42.13 (17-May-2015) Creating filesystem with 3101056 4k bloubuntu and 776720 inodes Filesystem UUID: d7d23a79-b91b-413d-be42-903d1fc9e5a2 Superblock backups stored on bloubuntu: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208Allocating group tables: 完成 正在寫入inode表: 完成 Creating journal (32768 bloubuntu): 完成 Writing superbloubuntu and filesystem accounting information: 完成查看格式化結果,如下圖:
4. 寫入文件
將卡掛載,如果不想手動掛載,可以將卡從ubuntu系統中彈出,然后重新連接到ubuntu就會自動掛載。
buntu20@ubuntu:~/rootfs-ubuntu20$ df -l Filesystem 1K-bloubuntu Used Available Use% Mounted on ...... /dev/sdb1 3030800 4676 2868840 1% /media/ubuntu20/3fe3147c-e558-46fe-b197-e537e1852122 /dev/sdb2 12078132 30436 11427488 1% /media/ubuntu20/d7d23a79-b91b-413d-be42-903d1fc9e5a2將文件拷貝到/dev/sdb1
buntu20@ubuntu:~/rootfs-ubuntu20$ df -l Filesystem 1K-bloubuntu Used Available Use% Mounted on ...... tmpfs 398264 36 398228 1% /run/user/1000 /dev/sdb1 3030800 824180 2049336 29% /media/ubuntu20/3fe3147c-e558-46fe-b197-e537e1852122 /dev/sdb2 12078132 30436 11427488 1% /media/ubuntu20/d7d23a79-b91b-413d-be42-903d1fc9e5a2 ubuntu20@ubuntu:~/rootfs-ubuntu20$ ll /media/ubuntu20/d7d23a79-b91b-413d-be42-903d1fc9e5a2 total 24 drwxr-xr-x 3 root root 4096 Apr 20 00:42 ./ drwxr-x---+ 4 root root 4096 Apr 20 01:44 ../ drwx------ 2 root root 16384 Apr 20 00:42 lost+found/ # cp命令加上-d選項,也可以使用dd命令 ubuntu20@ubuntu:~/rootfs-ubuntu20$ sudo cp -d -rf ./* /media/ubuntu20/d7d23a79-b91b-413d-be42-903d1fc9e5a2最后需要執行sync,以免沒有真正寫入到磁盤中。
ubuntu20@ubuntu:~/rootfs-ubuntu20$ sudo syncLinux sync命令用于數據同步,sync命令是在關閉Linux系統時使用的。
Linux 系統中欲寫入硬盤的資料有的時候為了效率起見,會寫到 filesystem buffer 中,這個 buffer 是一塊記憶體空間,如果欲寫入硬盤的資料存于此 buffer 中,而系統又突然斷電的話,那么資料就會流失了,sync 指令會將存于 buffer 中的資料強制寫入硬盤中。
總結
以上是生活随笔為你收集整理的基于Ubuntu的linux环境制作嵌入式SD/TF启动卡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小姐姐笔记:我是如何学习简单源码拓展视野
- 下一篇: Taro+react开发(39)taro