2/26/2020

Yocto 의 WIC Partition 설정

1.  Yocto 의 Image Partition 수정 

Yocto에서 Image 의 Partition은 일반적으로 WIC기반으로 WKS FILE로 관리되어지고 있다.

1.1 WIC/WKS 사용파악 

아래와 같이 WKS FILE을 찾아 파일을 알고 검색하여 각각의 Partition의 구성을 알고 관련부분을 변경하자.

$ bitbake -e | grep ^IMAGE_FSTYPES=    // 현재 사용중인 IMAGE 구성방법확인   
IMAGE_FSTYPES="wic.bmap wic.bz2 tar.bz2"
// wic.bmap (bmap은 XML로 bmaptool을 이용하여 write가능 or dd도 가능) 
// wic.bz2 로 wic로 구성되고 압축됨 

  • WKS_FILE 검색
$ bitbake -e | grep ^WKS_FILE=    // 현재 사용중인 WKS FILE 검색 
WKS_FILE="imx-uboot-bootpart.wks.in"

1.2 WIC 의 Partition 수정방법 

WKS file은 종류로 나누어지며, *.wks 와 *.wks.in 으로 나누어진다.
*.wks는 일반적인 kickstart files로 wic list images 를 하면 각 wks의 short-description 정보를 확인가능.
*.wks.in는 내부에서 사용되어지는 FILE로 아래와 같이 kickstart files로 보여지지 않는다.

  • WKS FILE 수정방법의 예 
$ find . -name imx-uboot-bootpart.wks.in  // WKS_FILE= 정보
$ cat ./meta-freescale/wic/imx-uboot-bootpart.wks.in         // 상위 wks script로 반드시 아래의 Manual을 읽어야함 
# short-description: Create SD card image with a boot partition
# long-description:
# Create an image that can be written onto a SD card using dd for use
# with i.MX SoC family
# It uses u-boot
#
# The disk layout used is:
#  - --------- -------------- --------------
# | | u-boot  |     boot     |    rootfs   |
#  - --------- -------------- --------------
# ^ ^         ^              ^
# | |         |              |
# 0 1kiB    4MiB          16MiB + rootfs + IMAGE_EXTRA_SPACE (default 10MiB)
#

##
## rawcopy로 이용하며, uboot가 저장이 되어지며, --no-table 때문에 아래의 partition에서는 잡히지는 않는다. 
## 
part u-boot --source rawcopy --sourceparams="file=${UBOOT_BINARY}" --ondisk mmcblk --no-table --align 1

##
## 상위 Layout의 boot에 해당하며, FAT로 구성되고, Kernel 과 DTB 와 TEE 정보 (mmcblkxp1)
##
## --size 16         : data를 넣고 여부으로 16M byte 넣어 매번 가변적 
## --fixed-size 16   : 고정 크기에 data들을 넣음 (고정크기)  ** Upgrade를 위해 추후 변경 
##
part /boot --source bootimg-partition --ondisk mmcblk --fstype=vfat --label boot --active --align 4096 --size 16
#part /boot --source bootimg-partition --ondisk mmcblk --fstype=vfat --label boot --active --align 4096 --fixed-size 16

##
## 상위 Layout의 rootfs에 해당하며 Main Filesystem 이다. 
## Linux Main Filesystem 으로 보면 별도로 size를 정의하지 않지만 아래의 비율로 계산하여 여부발생 ((mmcblkxp2)
## du command 사용 
## IMAGE_OVERHEAD_FACTOR="1.3"
## IMAGE_ROOTFS_EXTRA_SPACE="0"
## IMAGE_ROOTFS_SIZE="65536"
## --size            : 언급이 없으므로, 관련 data를 넣은 후에 상위 설정된 여부의 계산으로 설정하여 변경 (가변적)
## --fixed-size 16   : 고정 크기에 data들을 넣음 (고정크기) ** Upgrade를 위해 고정적으로 변경 
## 
part / --source rootfs --ondisk mmcblk --fstype=ext4 --label root --align 4096
#part / --source rootfs --ondisk mmcblk --fstype=ext4 --label root --align 4096 --fixed-size 256

##
## 상위 Layout에는 존재하지 않으며, 별도로 설정을 변경하고 직접 수정하여 변경 
## VOLATILE_LOG_DIR= no 설정 후 아래와 같이 추가 (mmcblkxp3)
## /var/log를 사용 (256Mbyte)
part /var/log  --ondisk mmcblk --fstype=ext4 --label root --align 4096 --size 256

##
## bootloader 에서 partition table 정보 (MSDOS 이외 설정도가능)
##
bootloader --ptable msdos


WKS FILE 의 part 관련
  https://www.yoctoproject.org/docs/current/mega-manual/mega-manual.html#ref-kickstart

WIC 명령어
  https://www.yoctoproject.org/docs/2.4.2/dev-manual/dev-manual.html#creating-partitioned-images-using-wic
  https://www.yoctoproject.org/docs/2.0/dev-manual/dev-manual.html#creating-partitioned-images

1.3 WIC Python Code 분석 

WKS FILE을 정확하게 설정하고 이해하려면 어쩔수 없이 현재 본인의 WIC 명령어에서 제공되어지는 기능들을 알아보자

  • WIC Python Code 분석
./poky/scripts
  https://github.com/alexandrebelloni/wic/tree/master/scripts
  https://github.com/openembedded/openembedded-core/tree/master/scripts

./poky/scripts/lib/wic/image/help.py
  https://github.com/alexandrebelloni/wic/blob/master/scripts/lib/image/help.py
  https://github.com/openembedded/openembedded-core/blob/master/scripts/lib/wic/help.py

./poky/scripts/wic
  https://github.com/openembedded/openembedded-core/blob/master/scripts/wic

  • WKS Kickstart File 옵션관련내용
  https://github.com/openembedded/openembedded-core/blob/master/scripts/lib/wic/ksparser.py

  • WKS의 File의 part 의 --source 부분
  ./poky/scripts/lib/wic/plugins/source/
  https://github.com/openembedded/openembedded-core/tree/master/scripts/lib/wic/plugins/source


//--sourceparams=${UBOOT_BINARY}
$ bitbake -e | grep ^UBOOT_BINARY
UBOOT_BINARY="u-boot.imx"

  WKS의 FILE part 의 --source 의 bootimage-partition 분석
  https://github.com/openembedded/openembedded-core/blob/master/scripts/lib/wic/plugins/source/bootimg-partition.py

// --source 의 bootimg-partition 부분분석 (상위 python 소스참조) 
//DEPLOY_DIR_IMAGE 위치에서 IMAGE_BOOT_FILES들을 가져와서 넣음 

$ bitbake -e | grep ^DEPLOY_DIR_IMAGE
DEPLOY_DIR_IMAGE="/home/jhlee/yocto/build-fb/tmp/deploy/images/imx6sxsabresd"

//IMAGE_BOOT_FILES 들이 FAT에 저장됨 
$ bitbake -e | grep ^IMAGE_BOOT_FILES
IMAGE_BOOT_FILES="     zImage     imx6sx-sdb.dtb imx6sx-sdb-emmc.dtb imx6sx-sdb-m4.dtb imx6sx-sdb-sai.dtb imx6sx-sdb-lcdif1.dtb imx6sx-sdb-ldo.dtb imx6sx-sdb-reva-ldo.dtb imx6sx-sdb-reva.dtb imx6sx-sdb-btwifi.dtb imx6sx-sdb-mqs.dtb     tee.bin uTee-6sxsdb      "

  WKS의 FILE part 의 --source 의 rootfs 분석
  https://github.com/openembedded/openembedded-core/blob/master/scripts/lib/wic/plugins/source/rootfs.py

// --source 의 rootfs 부분분석 (상위 python 소스참조) 
$ bitbake -e | grep ^IMAGE_ROOTFS
IMAGE_ROOTFS="/home/jhlee/yocto/build-fb/tmp/work/cortexa9hf-neon-poky-linux-gnueabi/defaultpkgname/1.0-r0/rootfs"
IMAGE_ROOTFS_EXTRA_SPACE="0"
IMAGE_ROOTFS_SIZE="65536


1.4 Linux Partition 관련내용 

VOLATILE_LOG_DIR은 일반적으로 no 로 설정이 되어있으며, 이를 tmpdir로 설정되어 ramdisk로 보통 관리하지만,
기록을 하기 원한다면 아래와 같이 확인 한 후 User Configuration에서 설정을 변경 후 상위 WKS File에 적용하면 된다.

  • User Configuration 에서 VOLATILE_LOG_DIR=no 설정
User Configuration 에 환경변수 설정 상위참조

$ bitbake -e | grep ^VOLATILE_LOG_DIR  // default yes
VOLATILE_LOG_DIR="yes"

  • /etc/fstab 수정
yocto 의 sources에서 fstab 관련부분 검색 후 수정

$ find . -name fstab
./poky/meta/recipes-core/base-files/base-files/fstab

$ vi ./poky/meta/recipes-core/base-files/base-files/fstab  // 추가 /var/log filesystem 설정 
....
/dev/mmcblk3p3       /var/log             auto       defaults              1  1
...


  https://stackoverflow.com/questions/49912475/adding-a-partition-in-yocto-generated-image
  https://www.digi.com/resources/documentation/digidocs/90001546/concept/yocto/c_emmc_layout_yocto.htm
  https://developer.ridgerun.com/wiki/index.php?title=IMX8/iMX8MEVK/Yocto/Installing_an_Image


2. WIC Command 사용 

  • wic list images 로 kickstart files 확인 
// bitbake가 동작할 경우 같이 동작
$ wic list images //*wks 들의 (short-description 정보)
  imx-uboot                                     Create SD card image with a boot partition
  mkhybridiso                                   Create a hybrid ISO image
  sdimage-bootpart                              Create SD card image with a boot partition
  directdisk                                    Create a 'pcbios' direct disk image
  qemux86-directdisk                            Create a qemu machine 'pcbios' direct disk image
  directdisk-gpt                                Create a 'pcbios' direct disk image
  systemd-bootdisk                              Create an EFI disk image with systemd-boot
  directdisk-multi-rootfs                       Create multi rootfs image using rootfs plugin
  directdisk-bootloader-config                  Create a 'pcbios' direct disk image with custom bootloader config
  mkefidisk                                     Create an EFI disk image


  • wic list images ( kickstart list 추가 )
$ find . -name *wks*  // wks kickstart file 검색 
...
sources/meta-freescale/wic/imx-uboot.wks // imx-uboot ,BSP 전용 WKS FILE 
poky/scripts/lib/wic/canned-wks/*.wks  // 아래 일반적인 kickstart file로 아래의나열된것들 
....
....                                   // wks.in 파일은 생략 

$ cp sources/meta-freescale/wic/imx-uboot.wks sources/meta-freescale/wic/upgrade.wks  // wks kickstart file 추가 

$ vi sources/meta-freescale/wic/upgrade.wks //short-description 수정

$ wic list images         //*wks 들의 (short-description 정보 재확인)
  upgrade                                      Create SD card image with a boot partition (Jeonghun) //추가확인
  imx-uboot                                     Create SD card image with a boot partition
  mkhybridiso                                   Create a hybrid ISO image
  sdimage-bootpart                              Create SD card image with a boot partition
  directdisk                                    Create a 'pcbios' direct disk image
  qemux86-directdisk                            Create a qemu machine 'pcbios' direct disk image
  directdisk-gpt                                Create a 'pcbios' direct disk image
  systemd-bootdisk                              Create an EFI disk image with systemd-boot
  directdisk-multi-rootfs                       Create multi rootfs image using rootfs plugin
  directdisk-bootloader-config                  Create a 'pcbios' direct disk image with custom bootloader config
  mkefidisk                                     Create an EFI disk image


  • 각 kickstart 세부정보파악 
$ wic list imx-uboot help  // 상위 imx-uboot 정보 (long-description 정보)

Create an image that can be written onto a SD card using dd for use
with i.MX SoC family
It uses u-boot

The disk layout used is:
 - --------- --------------
| | u-boot  |    rootfs    |
 - --------- --------------
^ ^         ^              ^
| |         |              |
0 1kiB    4MiB + rootfs + IMAGE_EXTRA_SPACE (default 10MiB)


  • Cooked Mode 기반으로 Image 생성
존재하는 kickstart file 기반으로 Image 생성

$ wic create upgrade -e core-image-base 
Summary: There was 1 WARNING message shown.
INFO: Creating image(s)...

INFO: The new image(s) can be found here:
  ./upgrade-202005191127-mmcblk.direct  // 생성되는 FILE fdisk -l 로 확인 

The following build artifacts were used to create the image(s):
  ROOTFS_DIR:                   /home/jhlee/yocto/build-fb/tmp/work/imx6sxsabresd-poky-linux-gnueabi/core-image-base/1.0-r0/rootfs
  BOOTIMG_DIR:                  /home/jhlee/yocto/build-fb/tmp/work/imx6sxsabresd-poky-linux-gnueabi/core-image-base/1.0-r0/recipe-sysroot/usr/share
  KERNEL_DIR:                   /home/jhlee/yocto/build-fb/tmp/deploy/images/imx6sxsabresd
  NATIVE_SYSROOT:               /home/jhlee/yocto/build-fb/tmp/work/cortexa9hf-neon-poky-linux-gnueabi/wic-tools/1.0-r0/recipe-sysroot-native

INFO: The image(s) were created using OE kickstart file:
  /home/jhlee/yocto/imx-yocto-bsp/sources/meta-freescale/wic/upgrade.wks

$ wic create imx-uboot -e core-image-base
$ wic create mkefidisk -e core-image-minimal

$ ls
bitbake-cookerdaemon.log cache  conf  sstate-cache  tmp  upgrade-202005191127-mmcblk.direct

$ wic ls upgrade-202005191127-mmcblk.direct // not working 
$ cp upgrade-202005191127-mmcblk.direct upgrade-202005191127-mmcblk.direct.wic

$ wic ls upgrade-202005191127-mmcblk.direct.wic // 확장자만 변경하면 동작 
Num     Start        End          Size      Fstype
 1       4194304    193363967    189169664  ext4

$ fdisk -l upgrade-202005191127-mmcblk.direct  
Disk upgrade-202005191127-mmcblk.direct: 184.4 MiB, 193363968 bytes, 377664 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: 0x2944b746

Device                              Boot Start    End Sectors   Size Id Type
upgrade-202005191127-mmcblk.direct1       8192 377663  369472 180.4M 83 Linux

  • wic는 두가지 방법으로 생성
//Cooked Mode (kickstart File 기반)
$ wic create wks_file -e IMAGE_NAME
// Raw Mode 
$ wic create wks_file options ( -e IMAGE_NAME , .. 모든부분을 다 설정해야함)

일반적으로 최종 Image가 다 합쳐져서 나오므로, 특정 Image들을 별도로 분리할 생각이 있다면
별도의 WKS FILE을 만들어서 Kickstart FILE로 등록한 후 생성하자 (Coocked Mode)
처음부터 Raw Mode는 비추천하며, 차라리 Cooked Mode에서 Raw Mode 처럼 사용하는 방법을 사용하자

  • bitbake로 생성된 Image 분석 (wks.in기반)
$ cp tmp/deploy/images/imx6sxsabresd/core-image-base-imx6sxsabresd.wic.bz2 .

$ bzip2 -d ./core-image-base-imx6sxsabresd.wic.bz2

$ wic ls core-image-base-imx6sxsabresd.wic
Num     Start        End          Size      Fstype
 1       4194304     27491327     23297024  fat16
 2      29360128    259118079    229757952  ext4
 3     260046848    528482303    268435456  ext4

$ wic ls core-image-base-imx6sxsabresd.wic:2  // Num 2 Partition 

$ wic rm core-image-base-imx6sxsabresd.wic:2/mnt  // Num 2 Partition 


2.1 WIC Image 의 정보분석 

wic ls 를 이용하여 확인해도 되지만, wic는 yocto가 설정이 되었을 경우에만 사용가능하므로, 가능하면 fdisk를 이용하여 확인하자.

  • fdisk 를 이용하여 wic image 기본정보확인
$ fdisk -l core-image-base-imx6sxsabresd.wic
Disk core-image-base-imx6sxsabresd.wic: 504 MiB, 528482304 bytes, 1032192 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: 0x304dec68

Device                             Boot  Start     End Sectors   Size Id Type
core-image-base-imx6sxsabresd.wic1 *      8192   53859   45668  22.3M  c W95 FAT32 (LBA)
core-image-base-imx6sxsabresd.wic2  57344  503031  445688 217.6M 83 Linux
core-image-base-imx6sxsabresd.wic3      507904 1032191  524288   256M 83 Linux

$ fdisk -l core-image-base-imx6sxsabresd.wic
Disk core-image-base-imx6sxsabresd.wic: 504 MB, 528482304 bytes, 1032192 sectors
64 cylinders, 255 heads, 63 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device                           Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
core-image-base-imx6sxsabresd.wic1 *  64,0,1      420,3,4           8192      53859      45668 22.2M  c Win95 FAT32 (LBA)
Partition 1 has different physical/logical start (non-Linux?):
     phys=(64,0,1) logical=(0,130,3)
Partition 1 has different physical/logical end:
     phys=(420,3,4) logical=(3,89,58)
core-image-base-imx6sxsabresd.wic2    448,0,1     1023,3,32        57344     503031     445688  217M 83 Linux
Partition 2 has different physical/logical start (non-Linux?):
     phys=(448,0,1) logical=(3,145,15)
Partition 2 has different physical/logical end:
     phys=(1023,3,32) logical=(31,79,40)
core-image-base-imx6sxsabresd.wic3    1023,3,32   1023,3,32       507904    1032191     524288  256M 83 Linux
Partition 3 has different physical/logical start (non-Linux?):
     phys=(1023,3,32) logical=(31,156,62)
Partition 3 has different physical/logical end:
     phys=(1023,3,32) logical=(64,63,63)



  • Sector 정보기반으로 byte로 변환
  1. Start Sector ( 57344 * 512 = 29360128 )
  2. End Sector ( 503031 * 512 = 257551872 )
  3. Sectors or End Sector - Start Sector +1  (503031-57344+1 = 445688 * 512 = 228192256 )


2.2  WIC Image mount 방법

  • wic image loop mount-1
$ losetup -v -f -o 29360128 --sizelimit 228192256 core-image-base-imx6sxsabresd.wic
or 
$ losetup -f -o 29360128 core-image-base-imx6sxsabresd.wic  // busybox는 상위옵션 미지원 

$ losetup -a   // show loop device status 
/dev/loop0: 29360128 core-image-base-imx6sxsabresd.wic

$ blkid /dev/loop0
/dev/loop0: LABEL="root" UUID="051f84f1-7fe9-4adc-abcb-6b50651f66ed" TYPE="ext4"

$ mount -o ro /dev/loop0 /mnt  // loop mount 
$ ls /mnt
bin         etc         lost+found  proc        sys         var
boot        home        media       run         tmp
dev         lib         mnt         sbin        usr

$ losetup -d  /dev/loop0 // detach 

  https://www.computerhope.com/unix/losetup.htm
  https://dustymabe.com/2012/12/15/mounting-a-partition-within-a-disk-image/

  • wic image loop mount-2
$ mount  -o ro,offset=$((512*57344)) core-image-base-imx6sxsabresd.wic /mnt
$ umount /mnt 


2.3 WIC Image Write 방법 과 분할 

  • mmcblk 정보확인
$ fdisk -l /dev/mmcblk3
Disk /dev/mmcblk3: 30 GB, 31826378752 bytes, 62160896 sectors
971264 cylinders, 4 heads, 16 sectors/track
Units: sectors of 1 * 512 = 512 bytes

Device       Boot StartCHS    EndCHS        StartLBA     EndLBA    Sectors  Size Id Type
/dev/mmcblk3p1 *  64,0,1      420,3,4           8192      53859      45668 22.2M  c Win95 FAT32 (LBA)
/dev/mmcblk3p2  448,0,1     1023,3,32        57344     503031     445688  217M 83 Linux
/dev/mmcblk3p3    1023,3,32   1023,3,32       507904    1032191     524288  256M 83 Linux

Start Sector 와 Sector 갯수 정보로 dd를 이용하여 write 진행

  • wic image mmblk3p2 write 방법
//skip : input  offset n bs 
//seek : output offset n bs 
//Sectors 445688 =  EndLBA-StartLBA+1 $((503031-57344+1))

$ dd if=core-image-base-imx6sxsabresd.wic of=/dev/mmcblk3 bs=512 skip=57344 seek=57344 count=$((503031-57344+1)) conv=sync,noerror 
$ dd if=core-image-base-imx6sxsabresd.wic of=/dev/mmcblk3 bs=512 skip=57344 seek=57344 count=445688 conv=sync,noerror 
or 
$ dd if=core-image-base-imx6sxsabresd.wic of=/dev/mmcblk3p2 bs=512 skip=57344 count=$((503031-57344+1)) && sync 
$ dd if=core-image-base-imx6sxsabresd.wic of=/dev/mmcblk3p2 bs=512 skip=57344 count=445688 && sync 


  https://software.intel.com/en-us/node/721474
  https://developer.ridgerun.com/wiki/index.php?title=IMX8/iMX8MEVK/Yocto/Installing_an_Image

  • wic image 분할 저장 
//bs   : 1 block bytes 
//skip : input  offset n bs 
//seek : output offset n bs 
$ dd if=core-image-base-imx6sxsabresd.wic of=partition1_img   bs=512   skip=8192    count=45668  conv=sync,noerror 
$ dd if=core-image-base-imx6sxsabresd.wic of=partition2_img   bs=512   skip=57344   count=445688 conv=sync,noerror 

Image 와 함께 md5sum 혹은 다른 hashsum을 이용하여 이를 같이 보관하는 것이 좋다.

dd option 설명
  https://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html

댓글 1개 :

익명 :

wic만으로도 며칠은 공부해야할 내용이네요.
너무 좋은 자료 잘 보고 갑니다.