包含关键字 xvf 的文章

container 里面没有 shell

今天本地搭建了一个 etcd 的服务器, 然后接下来想用 etcdctl 去测试一些命令, 于是想去用 container 里面的这个命令, 然后执行 docker exec -it etcd sh, 竟然得到没有这个文件, 然后又试了 bash 也没有.

首先 google 一把, 发现确实有人遇到同样的问题:
https://stackoverflow.com/questions/39900369/how-could-i-get-files-from-a-docker-container-running-the-official-etcd-image-if

按照上面的答案, 我们一个个去试一下:

docker export 导出整个container 文件

$ docker export etcd > /tmp/etcd.tar
$ ls -lah /tmp/etcd.tar
-rw-rw-r-- 1 supra supra 193M Aug 27 23:51 etcd.tar
$ mkdir /tmp/etcd
$ tar xvf /tmp/etcd.tar -C /tmp/etcd
$ ls -lah /tmp/etcd/bin/
total 8.0K
drwxr-xr-x  2 supra supra 4.0K Apr  2 04:55 .
drwxrwxr-x 16 supra supra 4.0K Aug 27 23:53 ..

$ find /tmp/etcd/ -name etcdctl
/tmp/etcd/usr/local/bin/etcdctl

可以看到, /bin 目录啥都没有. 但是我们找到了 etcdctl 在哪里.

docker cp 复制出 /bin 目录

$ mkdir /tmp/tmpbin
$ docker cp etcd:/bin /tmp/tmpbin/
$ ls -lah /tmp/tmpbin/bin/

可以看到内部啥也没有.

tmp 目录文件无法执行, 报 Permission denied

今天在执行 aysnc-profiler 的时候, 遇到无法执行的问题: 为了方便文件清除, 把解压后的文件放到了 /tmp 目录, 然后把 owner 和 权限都加好, 之后切换java 进程的用户去执行, 报 Permission denied.

mkdir /tmp/profiler
tar -C /tmp/profiler -xvf async-profiler-1.7.1-linux-x64.tar.gz
sudo chmod -R 755 /tmp/profiler/*
sudo chown -R appuser /tmp/profiler
sudo su appuser
/tmp/profiler/profile.sh -d 60 -o tree -e cpu -f profile0717.log.html 
/tmp/profile/profiler.sh: Permission denied

如果使用 bash 去执行, 则报:

bash /tmp/profiler/profile.sh -d 60 -o tree -e cpu -f profile0717.log.html 
/tmp/profile/profiler.sh: line 67: /tmp/profile/build/jattach: Permission denied

既然文件 owner 和 读和执行权限都加好了, 为什么还报错呢?
原来在 /tmp 目录的挂载方式:

appsuer@test-host:/home/appuser$ mount | grep /tmp
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noexec)

它设置了 noexec 属性. 所以问题就知道出在那里了, 既然这样, 换个目录就解决了.

使用 async-profiler 具体实践步骤

async-profiler 是一个对 Java 应用影响很小的 profiler 工具, 不仅能 sample Java 栈, 还能获取 perf event 的数据.

常见的基本步骤:

  1. 下载最新 async-profiler, 并且复制到远程目标机器

    scp ~/async-profiler-1.7.1-linux-x64.tar.gz  user1@server.tianxiaohui.com:/home/user1
    rsync ~/async-profiler-1.7.1-linux-x64.tar.gz  user1@server.tianxiaohui.com:/home/user1
  2. 解压, 并给运行 Java 应用的用户执行权限

    mkdir /tmp/profiler
    tar -xvf -C /tmp/profiler/  async-profiler-1.7.1-linux-x64.tar.gz
    sudo chown -R app1:app /tmp/profiler/
    sudo chmod -R 755 /tmp/profiler
  3. 执行 profiler

    /tmp/profiler/profiler.sh -e itimer -d 60 -o svg  -f c.log.html 74211
    /tmp/profiler/profiler.sh -e lock  -d 60 -o svg  -f f.log.html --reverse  74211 
    /tmp/profiler/profiler.sh -e com.ebay.configuration.console.CalServlet.service  -d 60 -o svg  -f f.log.html --reverse  74211
    /tmp/profiler/profiler.sh -d 30 -e itimer -o svg -t --reverse -f t.log.html 74211
  4. 展示

    sudo nc -v -4 -l 7070 < f.log.html
    sudo nc -v -l -p 7070 < f.log.html

    http://server.tianxiaohui.com:7070/

https://github.com/jvm-profiling-tools/async-profiler

常见的压缩/解压 jar 文件

有关 jar 文件

解压 jar 文件:

    jar xvf xxx.jar 
    unzip xxx.jar -d ./directoryToExtractTo
    jar tf jar-file  # 仅仅查看文件

//使用 jar 命令只能解压到当前目录

压缩为 jar 文件:

    jar cvf xxx.jar .
    jar cf jar-file input-file(s)

如果是使用 Spring boot big jar 的方式, 要制定 manifest 文件

    jar cfm xxx.jar Manifest.txt MyPackage/*.class
    jar cmf jar-file existing-manifest input-file(s)

更新 jar 文件 使用 input-file(s) 添加或更新 jar 里面的文件

  jar uf jar-file input-file(s)
  # 只更新 my.jar 里的这个文件
  jar -uf my.jar BOOT-INF/classes/application-Production.properties 

more info:

  1. https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
  2. https://docs.oracle.com/javase/tutorial/deployment/jar/index.html
  3. https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jar.html

jar (Java™ Archive) 文件格式是 Java 提供的压缩方式, 和 zip 使用的格式是一样的. 不过里面添加了一些其他特性, 比如使用 MANIFEST.MF 添加了扩展机制, 使使用者能够更方便的扩展.

  1. MANIFEST.MF 作为 jar 文件的元数据, 默认路径是: META-INF/MANIFEST.MF
  2. 可以在 MANIFEST.MF 里添加 Class-Path 来设置依赖的其他 jar 文件, 打包进去;