labunix's blog

labunixのラボUnix

ESXi6.5にUSB-HDDをVMFS6でフォーマットして、sshfsでマウントする。

■ESXi6.5にUSB-HDDをVMFS6でフォーマットして、sshfsでマウントする。
 ESXiからデータストアとして利用しつつ、Linuxからも利用出来るようにする。
 ただし、sshで暗号化してネットワーク越しにホストのUSB-HDDに書き込むのでLinuxからの速度は期待しないこと。

#PS1="root@\h:\w\#"

root@dummy-host:~#esxcli system version get;vmware -vl
   Product: VMware ESXi
   Version: 6.5.0
   Build: Releasebuild-7388607
   Update: 1
   Patch: 36
VMware ESXi 6.5.0 build-7388607
VMware ESXi 6.5.0 Update 1

■ゲストにUSBデバイスを見せるサービスを停止、および自動起動しないようにする。
 普通のchkconfigの使い方で。

root@dummy-host:~#/etc/init.d/usbarbitrator stop
usbarbitrator is not running

root@dummy-host:~#chkconfig --list | grep usbarbitrator 
usbarbitrator           off

# chkconfig usbarbitrator off

■デバイスが認識されていることを確認

root@dummy-host:~#lsusb 
Bus 004 Device 002: ID 13fd:1340 Initio Corporation Hi-Speed USB to SATA Bridge
Bus 008 Device 001: ID 0e0f:8002 VMware, Inc. 
Bus 007 Device 001: ID 0e0f:8001 VMware, Inc. 
Bus 006 Device 001: ID 0e0f:8001 VMware, Inc. 
Bus 005 Device 001: ID 0e0f:8001 VMware, Inc. 
Bus 004 Device 001: ID 0e0f:8002 VMware, Inc. 
Bus 003 Device 001: ID 0e0f:8001 VMware, Inc. 
Bus 002 Device 001: ID 0e0f:8001 VMware, Inc. 
Bus 001 Device 001: ID 0e0f:8001 VMware, Inc. 

root@dummy-host:~#ls -l /dev/disks/*Gene*
-rw-------    1 root     root     2000398934016 Feb 28 10:47 /dev/disks/t10.Generic_External________

■ラベルを作成

root@dummy-host:~#ls /dev/disks/*Gene* | awk '{print "partedUtil mklabel "$1" gpt"}' | sh

■GPTパーティションを取得

root@dummy-host:~#ls /dev/disks/*Gene* | awk '{print "partedUtil getptbl "$1}' | sh
gpt
243201 255 63 3907029168

■容量計算をしてVMFSパーティションを作成
 「AA31E02A400F11DB9590000C2911D1B8」の文字列は常に固定らしい。

root@dummy-host:~#ls /dev/disks/*Gene* | awk '{print "partedUtil getptbl "$1}' | sh | awk 'END{print $1*$2*$3-1}'
3907024064

root@dummy-host:~#TARGET=$(ls /dev/disks/*Gene*);\
  echo $TARGET | awk '{print "partedUtil getptbl "$1}' | sh | \
  awk 'END{print $1*$2*$3-1}' | \
  awk -v target=$TARGET '{print "partedUtil setptbl "target" gpt \0421 2048 "$1" AA31E02A400F11DB9590000C2911D1B8 0\042"}'
partedUtil setptbl /dev/disks/t10.Generic_External________ gpt "1 2048 3907024064 AA31E02A400F11DB9590000C2911D1B8 0"

root@dummy-host:~#TARGET=$(ls /dev/disks/*Gene*);\
  echo $TARGET | awk '{print "partedUtil getptbl "$1}' | sh | \
  awk 'END{print $1*$2*$3-1}' | \
  awk -v target=$TARGET '{print "partedUtil setptbl "target" gpt \0421 2048 "$1" AA31E02A400F11DB9590000C2911D1B8 0\042"}' | sh
gpt
0 0 0 0
1 2048 3907024064 AA31E02A400F11DB9590000C2911D1B8 0

root@dummy-host:~#ls /dev/disks/*Gene*;unset TARGET
/dev/disks/t10.Generic_External________    /dev/disks/t10.Generic_External________:1

■VMFS6でフォーマット
 「usb_datastore1」として見せる。

root@dummy-host:~#vmkfstools -C vmfs6 -S usb_datastore1 $(ls /dev/disks/*Gene*:1)
create fs deviceName:'/dev/disks/t10.Generic_External________:1', fsShortName:'vmfs6', fsName:'usb_datastore1'
deviceFullPath:/dev/disks/t10.Generic_External________:1 deviceFile:t10.Generic_External________:1
ATS on device /dev/disks/t10.Generic_External________:1: not supported
.
Checking if remote hosts are using this device as a valid file system. This may take a few seconds...
Creating vmfs6 file system on "t10.Generic_External________:1" with blockSize 1048576, unmapGranularity 1048576, unmapPriority default and volume label "usb_datastore1".
Successfully created new volume: 5c77c3d7-0933a404-ea38-000ffec41b13

■書き込み、削除テスト

root@dummy-host:~#touch /vmfs/volumes/usb_datastore1/test
root@dummy-host:~#ls -l /vmfs/volumes/usb_datastore1/test
-rw-r--r--    1 root     root             0 Feb 28 11:21 /vmfs/volumes/usb_datastore1/test
root@dummy-host:~#rm /vmfs/volumes/usb_datastore1/test

root@dummy-host:~#df -h | grep "usb_datastore1\|^File"
Filesystem   Size   Used Available Use% Mounted on
VMFS-6       1.8T   1.4G      1.8T   0% /vmfs/volumes/usb_datastore1

■sshfsマウント用のスクリプトを書く。

 sshfsでESXi6.0U2の内蔵HDD(datastore1)を有効活用する。
 http://labunix.hateblo.jp/entry/20170424/1493039845

$ sed -e 's/root@.*:/root@dummy-host:/' -e 's/labunix/user_group-name/g' myscripts/ssh_usb_datastore1.sh 
#!/bin/bash

if [ "`id -u`" -eq "0" ];then
  echo "Sorry, Not Permit root!" >&2
  exit 1
fi

test -d /media/usb_datastore1/ || sudo mkdir -p /media/usb_datastore1/ && sudo chown user_group-name:user_group-name /media/usb_datastore1/
sshfs root@dummy-host:/vmfs/volumes/usb_datastore1/ /media/usb_datastore1/ \
  -o compression=yes,transform_symlinks,idmap=user

■sshfsマウント

$ ./myscripts/ssh_usb_datastore1.sh
[sudo] labunix のパスワード:

$ ls /media/usb_datastore1/
$ df -h | grep usb_datastore1 | sed -e 's/root@.*:/root@dummy-host:/'
root@dummy-host:/vmfs/volumes/usb_datastore1/   1.9T  1.5G  1.9T    1% /media/usb_datastore1