USB Gadget - Mass Storage
http://www.linux-usb.org/gadget/file_storage.html http://linux-sunxi.org/USB_Gadget/Mass_storage https://www2.informatik.hu-berlin.de/~iks/Studienarbeit/NewTitelblatt.html https://wiki.kldp.org/wiki.php/DocbookSgml/Loopback-Root-FS-HOWTO
위 두개의 링크를 이용하여 USB Mass Storage Device 동작을 테스트한다. Odroid XU3 를 이용하였으며, Odroid XU3의 전용 ubuntu image가 설치되어 있다.
Device Image 생성.
USB Mass Storage gedget을 이용하기 위해서는 storage device파일이나, 가상의 디스크 이미지(파티션이 포함된), 램디스크 등을 이용할 수 있다. 여기서는 가상의 디스크 이미지를 생성하여 mass storage gadget에 등록할 예정이다.
blank image 생성.
비어있는 파일 /root/mass_storage.img (1GB) 생성.
root@odroid:~# dd bs=1M count=1024 if=/dev/zero of=./mass_storage.img
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 44.4027 s, 24.2 MB/s
partition 생성.
위 가이드 내용과 달리 단순 disk 파티션생성하듯이 진행함.
root@odroid:~# fdisk ./mass_storage.img
Welcome 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.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xbf455473.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-2097151, default 2097151):
Created a new partition 1 of type 'Linux' and of size 1023 MiB.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): b
Changed type of partition 'Linux' to 'W95 FAT32'.
Command (m for help): w
The partition table has been altered.
Syncing disks.
root@odroid:~# fdisk -lu ./mass_storage.img
Disk ./mass_storage.img: 1 GiB, 1073741824 bytes, 2097152 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: 0xbf455473
Device Boot Start End Sectors Size Id Type
./mass_storage.img1 2048 2097151 2095104 1023M b W95 FAT32
loopback device에 등록.
losetup을 이용하여 /dev/loop0 device file에 mass_storage.img를 연결한다. 파티션의 시작지점을 기준으로 /dev/loop0에 연결하여야 한다. start block 0을 /dev/loop0에 에 연결 한 후 format등 작업을 진행 할 경우 생성된 파티션이 삭제될 수 있다.
mass_storage.img 파일의 파티션 시작지점은 2048 block 이고, losetup의 -o 옵션을 이용하여 loop0 의 시작 offset을 지정할 수 있다. -o 옵션은 byte offset이므로, 2048block * 512 byte 를 입력한다.
root@odroid:~# losetup -o `expr 2048 \* 512` /dev/loop0 ./mass_storage.img
root@odroid:~# losetup
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
/dev/loop0 0 1048576 0 0 /root/mass_storage.img
format
mkdosfs 명령을 이용하여 /dev/loop0 를 vfat 으로 포멧한다.
root@odroid:~# mkdosfs /dev/loop0
mkfs.fat 3.0.28 (2015-05-16)
Loop device does not match a floppy size, using default hd params
여기 까지 진행 후 loop device 연결을 해제하고, mass_storage 가젯을 이용하여 장치파일을 연결 할 수 있다. 여기서는 동작확인을 위해 loop device를 특정 폴더에 mount 하고, 파일 생성 후 windows에서 생성된 file을 확인할 것이다.
mount & creating test file
/dev/loop0 장치를 mount하여 파일 생성하였다.
root@odroid:~# mount /dev/loop0 ./mnt
root@odroid:~# mount | grep loop0
/dev/loop0 on /root/mnt type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
root@odroid:~# cd mnt
root@odroid:~/mnt# echo "loopback device and usb gadget mass storage test" > test_file.txt
root@odroid:~/mnt# ls -al
total 12
drwxr-xr-x 2 root root 4096 Feb 11 19:03 .
drwx------ 4 root root 4096 Feb 11 19:00 ..
-rwxr-xr-x 1 root root 49 Feb 11 19:03 test_file.txt
root@odroid:~/mnt# cat ./test_file.txt
loopback device and usb gadget mass storage test
usb gadget mass_storage 에 disk image 등록.
root@odroid:~# modprobe g_mass_storage file=./mass_storage.img
[ 9436.055938] [c6] g_mass_storage gadget: Mass Storage Function, version: 2009/09/11
[ 9436.062099] [c6] g_mass_storage gadget: Number of LUNs=1
[ 9436.067391] [c6] lun0: LUN: file: /root/mass_storage.img
[ 9436.072773] [c6] g_mass_storage gadget: Mass Storage Gadget, version: 2009/09/11
[ 9436.080136] [c6] g_mass_storage gadget: userspace failed to provide iSerialNumber
[ 9436.087622] [c6] g_mass_storage gadget: g_mass_storage ready
등록 후 odroid XU3의 USB 3.0 micro port를 windows pc와 연결 하여 파일 내용을 확인 하였다.
정상적으로 파일이 생성 되었음을 알 수 있다. windows에서 파일을 생성한 후 odroid console에서 확인.
root@odroid:~/mnt# ls
test_file.txt
linux 에서 mount 된상태로 windows pc에 usb를 연결 파일을 변경 하였으나 linux에서는 변경안된 상태로 남아있다. 정확한 이유 좀 더 분석 해야 한다. mass storage gadget이 파일에 lock을 걸었기 때문일 것으로 추정된다. windows pc에서 연결을 해제 하고 loop0 장치를 다시 마운트 한 후 확인하면 다음과 같이 나타난다.
root@odroid:~/mnt# cd ..
root@odroid:~# ls
mass_storage.img mnt
root@odroid:~# umount ./mnt
root@odroid:~# mount /dev/loop0 ./mnt
root@odroid:~# cd mnt
root@odroid:~/mnt# ls
test_file2.txt test_file.txt
root@odroid:~/mnt# cat ./test_file2.txt
test file that created from windows pc
windows pc에서 생성된 파일이 확인되었다.