So Tired !_! 逆水行舟, 不进则退!

20Sep/16

Ubuntu增加新硬盘并扩展LVM

Posted by Nick Xu

使用虚拟机安装Ubuntu LTS 14.04,刚开始只分配了20G的硬盘,采用LVM的方式进行管理。安装了几个数据库应用后发觉空间剩余不多,之能再添加了一个40G的硬盘上去,还好用LVM,扩展不是太麻烦。

1. 查看硬盘是否增加

2. 格式化新硬盘(根据自身情况分区)

3. 增加新的PV

4. 扩展VG

查看VG的名称

扩展VG

验证操作

5. 将新空间分配到LV

查看要增加的LV名称

增加空间

验证操作

 

 

 

 

 

 

LVM一种支持在线增大和减小的分区技术
一、PV物理卷
  由分区架构而成,为卷组提供空间。
二、VG卷组
  由一个或多个物理卷组成,为LV提供空间。
三、LV逻辑卷
  从卷组中获取空间,形成一个新的可用的LVM分区。
四、制作逻辑卷。
1、创建PV(在创建PV之前需要创建一个可用的分区,并且ID为8e)
[root@station19 media]# fdisk /dev/hda 对第一块IDE硬盘进行操作。这取决于你的环境。
The number of cylinders for this disk is set to 14593.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n 新建一个分区
First cylinder (5421-14593, default 5421):
Using default value 5421
Last cylinder or +size or +sizeM or +sizeK (5421-14593, default 14593): +500M 给他500M的空间。
Command (m for help): p 查看一下当前硬盘分区的情况。
Disk /dev/hda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        3837    30716280   83  Linux
/dev/hda3            3838        5112    10241437+  83  Linux
/dev/hda4            5113       14593    76156132+   5  Extended
/dev/hda5            5113        5243     1052226   82  Linux swap / Solaris
/dev/hda6            5244        5308      522081   8e  Linux LVM
/dev/hda7            5309        5370      497983+  83  Linux
/dev/hda8            5371        5420      401593+  8e  Linux LVM
/dev/hda9            5421        5482      497983+  83  Linux
 (这是新建的那快分区)
Command (m for help): t 修改分区的表示符
Partition number (1-9): 9 选择对第9个分区进行操作
Hex code (type L to list codes): 8e 将标识符改为8e(只有将标识符改为8e才能支持LVM)
Changed system type of partition 9 to 8e (Linux LVM)
Command (m for help): p 再次查看一下
Disk /dev/hda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        3837    30716280   83  Linux
/dev/hda3            3838        5112    10241437+  83  Linux
/dev/hda4            5113       14593    76156132+   5  Extended
/dev/hda5            5113        5243     1052226   82  Linux swap / Solaris
/dev/hda6            5244        5308      522081   8e  Linux LVM
/dev/hda7            5309        5370      497983+  83  Linux
/dev/hda8            5371        5420      401593+  8e  Linux LVM
/dev/hda9            5421        5482      497983+  8e  Linux LVM
  (当前第9快分区的类型变成了8e,LVM)
Command (m for help): w 保存退出
[root@station19 media]# partprobe 重新加载分区,不加载或重启的话当前系统无法识别这个新的分区)
[root@station19 media]# pvcreate /dev/hda9  (将hda9这个分区制作成物理卷)
  Physical volume "/dev/hda9" successfully created
[root@station19 media]# pvdisplay  查看PV的内容
  --- Physical volume ---
  PV Name               /dev/hda9
  VG Name              
  PV Size               486.31 MB / not usable 2.31 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              121
  Free PE               121
  Allocated PE          0
  PV UUID               0pE20O-EWqI-JtTY-gmeX-VJck-GUb0-Mxvx5v
       (从这些信息可以看出,/dev/hda9这个物理卷不属于任何VG,它的磁盘空间为486M)
2、创建VG 
[root@station19 media]# vgcreate vg /dev/hda9 
  Volume group "vg" successfully created
 将hda9这个物理卷加入到vg这个组里(vg为自己定义的名字)
 [root@station19 media]# vgdisplay
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               484.00 MB
  PE Size               4.00 MB
  Total PE              121
  Alloc PE / Size       0 / 0   
  Free  PE / Size       121 / 484.00 MB
  VG UUID               6yOerB-EExr-sY0E-KFwO-fg0D-9Zol-RypiT5
  现在查看一下,vg这个卷组就拥有了484M的空间也就是/dev/hda9这个逻辑卷的空间
3、创建LV
[root@station19 media]# lvcreate -L 300M -n lv vg
  Logical volume "lv" created
 创建一个LV,并给他指定拥有300M的空间,vg为指定它属于哪个卷组,也就是由哪个卷组为它提供这300M的空间。
[root@station19 media]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg/lv
  VG Name                vg
  LV UUID                HIc32H-DR98-Ymd1-Flhy-1Z2u-iU1Q-e2znLB
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                300.00 MB
  Current LE             75
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   现在查看一下LV信息,现在这个lv属于vg这个卷组并拥有300M的空间
5、格式化文件系统
[root@station19 media]# mkfs.ext3 /dev/vg/lv 将lv这个逻辑卷格式化为ext3文件系统
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.  
6、挂载
[root@station19 media]# mount /dev/vg/lv /lv 将/dev/vg/lv这个分区挂在到/下的lv下
[root@station19 media]# df -H /lv/   
文件系统               容量   已用  可用 已用% 挂载点
/dev/mapper/vg-lv      305M    11M   279M   4% /lv
查看一下/lv这个分区的大小,300M
 到现在为止,我们已经成功的创建了一个逻辑卷,但别忘了。逻辑卷最大的功能是增大和减小,下面我们来对逻辑卷进行增大和减小。
7、增大逻辑卷
[root@station19 media]# lvextend -L 500M /dev/vg/lv
  Extending logical volume lv to 500.00 MB
  Insufficient free space: 50 extents needed, but only 46 available
 [root@station19 media]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg/lv
  VG Name                vg
  LV UUID                HIc32H-DR98-Ymd1-Flhy-1Z2u-iU1Q-e2znLB
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                300.00 MB
  Current LE             75
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
这是一个失败的例子,因为lv所属的vg只有484M,所以当我将lv扩增到500M的时候会失败,因为vg无法为lv提供500M的空间。查看一下,lv仍然只有300M。
[root@station19 media]# lvextend -L 480M /dev/vg/lv
  Extending logical volume lv to 480.00 MB
  Logical volume lv successfully resized
[root@station19 media]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg/lv
  VG Name                vg
  LV UUID                HIc32H-DR98-Ymd1-Flhy-1Z2u-iU1Q-e2znLB
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                480.00 MB
  Current LE             120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
现在我将lv扩增到480M,480M是vg所允许的空间大小,查看一下现在成功了。Lv被成功的扩增到了480M。
8、增大文件系统
[root@station19 media]# df -H /lv/
文件系统               容量   已用  可用 已用% 挂载点
/dev/mapper/vg-lv      305M    11M   279M   4% /lv
查看一下文件系统大小,lv这个文件系统仍是300M,因为我们之前扩展的只是它的分区大小,文件大小并没有增大,所以我们仍然只能使用300M的空间
[root@station19 media]# resize2fs /dev/vg/lv 480M
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/vg/lv is mounted on /lv; on-line resizing required
Performing an on-line resize of /dev/vg/lv to 491520 (1k) blocks.
The filesystem on /dev/vg/lv is now 491520 blocks long.
[root@station19 media]# df -H /lv
文件系统               容量   已用  可用 已用% 挂载点
/dev/mapper/vg-lv      488M    11M   452M   3% /lv
现在用resize这个工具将/dev/vg/lv这个文件系统扩展到488M,查看一下成功了。
 *注:文件系统是建立在分区之上的,分区为我们提供空间,而我们真正能直接使用的是文件系统,例如windows下我们使用的ntfs文件系统一样。而在linux下使用的是ext3格式的文件系统。所以文件系统只能比分区小,不能比分区大。
9、缩小文件系统和分区
(1)减小文件系统时需要现卸载,因为resize这个工具不支持在线减小。
[root@station19 media]# umount /lv  卸载
[root@station19 media]# e2fsck -f /dev/vg/lv  
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg/lv: 11/121440 files (9.1% non-contiguous), 25872/491520 blocks
在减小文件系统之前需要先检查一下分区的完整性。
[root@station19 media]# resize2fs /dev/vg/lv 100M 将文件系统减小到100M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/vg/lv to 102400 (1k) blocks.
The filesystem on /dev/vg/lv is now 102400 blocks long.
 (2)缩小分区
[root@station19 media]# lvreduce -L 100M /dev/vg/lv
  WARNING: Reducing active logical volume to 100.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv? [y/n]: y
  Reducing logical volume lv to 100.00 MB
  Logical volume lv successfully resized
将分区缩小到100M
[root@station19 media]# mount /dev/vg/lv /lv/
[root@station19 media]# df -H /lv/
文件系统               容量   已用  可用 已用% 挂载点
/dev/mapper/vg-lv      102M    11M    88M  11% /lv
[root@station19 media]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg/lv
  VG Name                vg
  LV UUID                HIc32H-DR98-Ymd1-Flhy-1Z2u-iU1Q-e2znLB
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                100.00 MB
  Current LE             25
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
挂载、查看,成功!文件系统和分区都为100M
10、增大与减小VG
这个很简单,VG是由多个物理卷构成的,只要将物理卷加入VG就可以实现增容,移出就能实现减小。
[root@station19 media]# pvcreate /dev/hda10 将一块新的lvm格式的分区做成物理卷
 Physical volume "/dev/hda10" successfully created
[root@station19 media]# vgextend vg /dev/hda10 将hda10这个分区加入到vg这个组
   Volume group "vg" successfully extended
[root@station19 media]# vgdisplay
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               968.00 MB
  PE Size               4.00 MB
  Total PE              242
  Alloc PE / Size       25 / 100.00 MB
  Free  PE / Size       217 / 868.00 MB
  VG UUID               6yOerB-EExr-sY0E-KFwO-fg0D-9Zol-RypiT5
现在查看一下,VG这个卷组拥有了将近1G的空间。
减小、
root@station19 media]# vgreduce vg /dev/hda10
  Removed "/dev/hda10" from volume group "vg"
[root@station19 media]# vgdisplay
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               484.00 MB
  PE Size               4.00 MB
  Total PE              121
  Alloc PE / Size       25 / 100.00 MB
  Free  PE / Size       96 / 384.00 MB
  VG UUID               6yOerB-EExr-sY0E-KFwO-fg0D-9Zol-RypiT5
将hda10这个物理卷从VG中移出,再查看,VG又被减小了。
Tagged as: , Comments Off
8Jul/16

MongoDB 备份(mongodump)与恢复(mongorerstore)

Posted by Nick Xu

MongoDB数据备份

在Mongodb中我们使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。

mongodump命令可以通过参数指定导出的数据量级转存的服务器。

语法

mongodump命令脚本语法如下:

>mongodump -h dbhost -d dbname -o dbdirectory
  • -h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
  • -d:需要备份的数据库实例,例如:test
  • -o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。

实例

在本地使用 27017 启动你的mongod服务。打开命令提示符窗口,进入MongoDB安装目录的bin目录输入命令mongodump:

>mongodump

执行以上命令后,客户端会连接到ip为 127.0.0.1 端口号为 27017 的MongoDB服务上,并备份所有数据到 bin/dump/ 目录中。命令输出结果如下:

MongoDB数据备份

mongodump 命令可选参数列表如下所示:

语法 描述 实例
mongodump --host HOST_NAME --port PORT_NUMBER 该命令将备份所有MongoDB数据 mongodump --host w3cschool.cc --port 27017
mongodump --dbpath DB_PATH --out BACKUP_DIRECTORY mongodump --dbpath /data/db/ --out /data/backup/
mongodump --collection COLLECTION --db DB_NAME 该命令将备份指定数据库的集合。 mongodump --collection mycol --db test

MongoDB数据恢复

mongodb使用 mongorerstore 命令来恢复备份的数据。

语法

mongorestore命令脚本语法如下:

>mongorestore -h dbhost -d dbname --directoryperdb dbdirectory
  • -h:MongoDB所在服务器地址
  • -d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
  • --directoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧!
  • --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

接下来我们执行以下命令:

>mongorestore

执行以上命令输出结果如下:

MongoDB数据恢复

Tagged as: Comments Off
6Jun/16

mini osd 手动接isp引脚刷bootloader

Posted by Nick Xu

发现mini osd很容易出问题, 第一次刷了对应f3飞控的osd后用了几次就没有osd信息了, 只有图像, 后来又刷了一次, 结果还是用几次后又没了, 而且这次还无法用USB2TTL线刷系统, 听说只能用ISP线来刷单片机, 结果发现这MINI OSD根本没有接上ISP的引脚, 又不知道哪个才是ISP引脚, 找了N久, 在即将要放弃时, 终于找到了ISP引脚图(其实最重要的只是这张图, 其它都是次要)

 

MINI OSD ISP 引脚标注图, 触点很小, 接线时小心, 别短路了.

BOOTLOADER FLASH

下图是我的接线, 这是已经刷完BOOTLOADER后测试刷OSD系统了

IMG20160603223727

然后就是刷BOOTLOADER了, 当然你得先有ISP刷写器,我是USBasp刷写器, 装progisp1.72, 懒得打字了, 直接看图吧, 从论坛上找过来的

522F9946-4E0F-4922-991D-28FA57CDB229

第四步, 固件位置在 arduino-1.0.1\hardware\arduino\bootloaders\atmega\ATmegaBOOT_168_atmega328.hex ,根据自己的arduino安装目录找吧

刷完BOOTLOADER就回到 arduino 刷OSD系统吧.

貌似手动刷了BOOTLOADER后变稳定了, 不会出现用几次就无法显示osd信息的情况, 估计是出厂时预刷有问题或者是出厂BOOTLOADER有问题. 还有一种可能是我的USB2TTL前两次刷的时候是跳线到3.3v而不是5v, 虽然接线还是接到5v的位置, 不过目前已无法考究具体是哪个原因导致的了.

 

 

Filed under: 其它, 单片机 Comments Off
18May/16

How To Mount a Remote Directory With SSHFS on a Linux

Posted by Nick Xu

How can I mount a remote directory with ssh on a Linux bases system? How do I use SSHFS to mount remote file systems over SSH on a Ubuntu or Debian/RHEL/CentOS/Arch Linux system?

SSH is a secure protocol and you can use it to mount a directory on a remote server or local laptop with the help of the SSHF service. With SSHFS you can mount remote server file system to your local development workstation/laptop powered by Linux.

More on SSHFS

sshfs is a filesystem based on the SSH file transfer protocol. It is used on a client system i.e. you need to install sshfs package on your local computer/laptop powered by CentOS/RHEL/Ubuntu/Debian/Arch Linux. No need to install anything on server (server1.cyberciti.biz). You only need an openssh server installed on server side. Our sample setup:

Fig.01: Our sample setup

Fig.01: Our sample setup

Installing SSHFS on a Ubuntu/Debian/Mint Linux

Type the following apt-get command:

sudo apt-get install sshfs

Sample outputs:

[sudo] password for nixcraft:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  sshfs
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 41.7 kB of archives.
After this operation, 138 kB of additional disk space will be used.
Get:1 http://mirror.ox.ac.uk/sites/archive.ubuntu.com/ubuntu/ trusty/main sshfs amd64 2.5-1ubuntu1 [41.7 kB]
Fetched 41.7 kB in 1s (27.8 kB/s)
Selecting previously unselected package sshfs.
(Reading database ... 247545 files and directories currently installed.)
Preparing to unpack .../sshfs_2.5-1ubuntu1_amd64.deb ...
Unpacking sshfs (2.5-1ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up sshfs (2.5-1ubuntu1) ...

Installing SSHFS on an Arch Linux

Type the following command:

sudo pacman -S sshfs fuse

Installing SSHFS on a RHEL (Red Hat)/CentOS Linux

First, turn on EPEL repo and then type the following yum command to install FUSE-Filesystem to access remote filesystems via SSH on a CentOS/RHEL:

sudo yum install fuse-sshfs

Sample outputs:

Loaded plugins: auto-update-debuginfo, protectbase, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
0 packages excluded due to repository protections
Resolving Dependencies
--> Running transaction check
---> Package fuse-sshfs.x86_64 0:2.4-1.el6 will be installed
--> Processing Dependency: fuse >= 2.2 for package: fuse-sshfs-2.4-1.el6.x86_64
--> Running transaction check
---> Package fuse.x86_64 0:2.8.3-4.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=======================================================================================================================
 Package                   Arch                  Version                     Repository                           Size
=======================================================================================================================
Installing:
 fuse-sshfs                x86_64                2.4-1.el6                   epel                                 52 k
Installing for dependencies:
 fuse                      x86_64                2.8.3-4.el6                 rhel-x86_64-server-6                 71 k
 
Transaction Summary
=======================================================================================================================
Install       2 Package(s)
 
Total download size: 123 k
Installed size: 115 k
Is this ok [y/N]: y
Downloading Packages:
(1/2): fuse-2.8.3-4.el6.x86_64.rpm                                                              |  71 kB     00:00
(2/2): fuse-sshfs-2.4-1.el6.x86_64.rpm                                                          |  52 kB     00:00
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  173 kB/s | 123 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : fuse-2.8.3-4.el6.x86_64                                                                             1/2
  Installing : fuse-sshfs-2.4-1.el6.x86_64                                                                         2/2
  Verifying  : fuse-sshfs-2.4-1.el6.x86_64                                                                         1/2
  Verifying  : fuse-2.8.3-4.el6.x86_64                                                                             2/2
 
Installed:
  fuse-sshfs.x86_64 0:2.4-1.el6
 
Dependency Installed:
  fuse.x86_64 0:2.8.3-4.el6
 
Complete!

How do I mount the remote file system?

The syntax is

sshfs user@server /path/to/mountpoint
sshfs user@server /path/to/mountpoint options

First, create a directory using mkdir command:

sudo mkdir /mnt/server1

I’m going to mount file system using root user and you need to type root password when prompted:

sudo sshfs root@192.168.1.142:/ /mnt/server1/
 
##  OR use ssh key based login ##
sudo sshfs -o IdentityFile=~/.ssh/keyfile /mnt/server1/

Sample outputs:

Password for root@freebsd10:

Verify it:

sudo df -h

Sample outputs:

Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/wks05-root  487G  114G  350G  25% /
none                    4.1k     0  4.1k   0% /sys/fs/cgroup
udev                     17G  4.1k   17G   1% /dev
tmpfs                   3.4G  1.9M  3.4G   1% /run
none                    5.3M     0  5.3M   0% /run/lock
none                     17G  160k   17G   1% /run/shm
none                    105M   50k  105M   1% /run/user
/dev/sda1               239M   89M  138M  40% /boot
root@192.168.1.142:/     20G   12G  6.8G  64% /mnt/server1

To access and/or to see the remote file system, run:

sudo -s
cd /mnt/server1
ls -l

Sample ouputs:

Fig. 02: sshfs in action

Fig. 02: sshfs in action

Dealing with “Permission denied” error and recommended procedure for mounting the remote directory

If you get an error that read as cannot access server1: Permission denied, add yourself to a group called fuse:

$ sudo gpasswd -a "$USER" fuse
Adding user nixcraft to group fuse

Next, create a mount point inside your own home directory:

$ mkdir $HOME/server1
$ ls -ld !$
ls -ld $HOME/server1
drwxrwxr-x 2 nixcraft nixcraft 4096 Mar  8 04:34 /home/nixcraft/server1

To mount the remote file system, enter:

sshfs -o idmap=user  root@192.168.1.142:/ $HOME/server1
df
ls -l $HOME/server1
Fig.03: Using sshfs without root access on local laptop/desktop

Fig.03: Using sshfs without root access on local laptop/desktop

How do I unmount the remote file system?

The syntax is:

sudo umount /mnt/server1
 
## OR ##
fusermount -u /mnt/server1

Verify it:

df -h

How can I permanently mount the remote file system by updating /etc/fstab?

Edit the /etc/fstab file, enter:

sudo vi /etc/fstat

The syntax is:

userNameHere@FQDN_OR_IP_HERE:/path/to/source/  /local/mountdir/  fuse.sshfs  defaults,_netdev  0  0

Examples

Add the following entry at the bottom of the file:

sshfs#root@192.168.1.142:/ /mnt/server1

Another example with additional options:

sshfs#$root@192.168.1.142:/ /mnt/server1 fuse defaults,idmap=user,allow_other,reconnect,_netdev,users,IdentityFile=/path/to/.ssh/keyfile 0 0

Recommend option for on-demand mounting if you are using systemd:

vivek@server1.cyberciti.biz:/project/www/ /mnt/server1  fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/home/vivek/.ssh/id_rsa,allow_other,reconnect 0 0

Save and close the file. Where,

  1. root@192.168.1.142 : Remote server with sshd
  2. fuse : File system type.
  3. idmap=user : Only translate UID of connecting user.
  4. allow_other : Allow access to other users.
  5. reconnect : Reconnect to server.
  6. _netdev : The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).
  7. users : Allow every user to mount and unmount the filesystem.
  8. IdentityFile=/path/to/.ssh/keyfile – SSH key file.
22Mar/16

如何利用NFC伪造一张门禁卡

Posted by Nick Xu

0×00 前言

我租住的杭州一个老小区一年前出现了所谓的“出租房杀人事件”,事件过后民警叔叔们给小区的每个单元都装上了门禁,所有住户都需要在物业处登记,物业的工作人员会让你提供身份证或者公交卡用来注册,注册之后就可以刷卡进门了。

但由于某些原因,我并不想去登记注册一张门禁卡,正好手头有一部nexus5,众所周知nexus5是有nfc功能的,我便想能不能用nexus5的nfc功能伪造一张门禁卡呢? 一番尝试之后,就有了下文的方法。 (从来没接触过无线安全,对Proxmark3,acr122u 等设备也是一窍不通,各位大牛见笑了)

0×01分析

由于身份证,公交卡等很多卡都可以用来当门禁卡,那基本上有九成把握这个门禁只是简单读取卡的id,并不会去解密里面的内容,只要简单模拟一个相同id的卡就可以刷开门禁。

0×02“采样”

我们下载一款名为“TagInfo”的android应用,用它来读取一下现在可以刷开门禁的卡信息。(跟同楼的妹子借的)

如下图:

卡的id为:D2:69:76:5B

接着我们找另一台nexus5看一下手机原来的id,扫描出来这台nexus5的id是在不停变化的,如下图:

原来出于安全考虑,android手机的id是一个4个字节长度的随机id,每次连接都会变化,并且都以 “0×80”开头。

android官方并没有提供任何的api可以指定修改nfc的id,但是我们可以用直接修改配置文件的方式来实现,前提当然是手机必须root过了。

0×03Just Do IT

1、到手机的 /etc/ 目录找一个文件名为libnfc-brcm-20791b05.conf,默认情况下,文件中NFA_DM_START_UP_CFG 的配置项是这样的值:

1
{45:CB:01:01:A5:01:01:CA:17:00:00:00:00:06:00:00:00:00:0F:00:00:00:00:E0:67:35:00:14:01:00:00:10:B5:03:01:02:FF:80:01:01:C9:03:03:0F:AB:5B:01:00:B2:04:E8:03:00:00:CF:02:02:08:B1:06:00:20:00:00:00:12:C2:02:00:C8}

2、通过修改这个值就可以改变id。把该文件下载到电脑上,先在最后面增加一个0×33作为标志位,接着接上要指定的id长度,在当前的情况下就是0×04,最后在后面接上要制定的id:“0xD2,0×69,0×76,0x5B”,接着改变最首的数字,加上我们总共增加的字符串长度,这里我们需要加上6,所以最后配置项变成:

1
{4B:CB:01:01:A5:01:01:CA:17:00:00:00:00:06:00:00:00:00:0F:00:00:00:00:E0:67:35:00:14:01:00:00:10:B5:03:01:02:FF:80:01:01:C9:03:03:0F:AB:5B:01:00:B2:04:E8:03:00:00:CF:02:02:08:B1:06:00:20:00:00:00:12:C2:02:00:C8:33:04:D2:69:76:5B}

3、采用adb的方式覆盖系统中原来的文件,然后重启

adb root
adb remount
adb push libnfc-brcm-20791b05.conf /etc/
adb reboot

重启之后我们发现,这台nexus5的id已经被改成了我们想要的。

 

 

拿着修改好的nexus5去试试看?记得必须在唤醒屏幕的情况下nfc才有效。

Tagged as: , Comments Off
22Mar/16

Mac OS X 下部分Android手机无法连接adb问题之解决方案

Posted by Nick Xu

打开终端,在任意当前路经下都可以直接执行adb命令,通过USB线将Android手机和Mac PC连接后,adb devices,没有相应设备的列表。下面是解决办法:
1. 打开终端,输入:system_profiler SPUSBDataType,查看Mac系统所有USB设备信息,找到相应的厂商Vender ID。
查到相应连有Android手机设备的usb hub,找到相应的厂商Vender ID,比如,我的测试机K-Touch T619天语手机就无法连接adb,查到的相应信息片段如下:
  1. -----------------
  2.  Spreadtrum phone:
  3.               Product ID: 0x5d04
  4.               Vendor ID: 0x1782
  5.               ...
  6.        Serial Number: T619
  7.        ...
  8. -----------------

2. 终端进入/Users/user/.android,应该有一个adb_usb.ini文件,如果没有就创建,然后将上面的Vender ID “0x1782”单独一行加入到该文件,然后退出。

  1. # ANDROID 3RD PARTY USB VENDOR ID LIST -- DO NOT EDIT.
  2. # USE 'android update adb' TO GENERATE.
  3. 1 USB VENDOR ID PER LINE.
  4. 0x1782

3. 重启adb server进程。

终端依次执行下面命令:
  1. adb kill-server
  2. adb start-server

然后通过adb devices命令就可以查看到以相应设备Serial Number为名字的设备。如:

  1. -MacBook-Pro:.android user$ adb devices
  2. List of devices attached
  3. emulator-5554   device
  4. T619    device

PS:

1. 有时 adb devices不能显示连接设备,需要拔掉数据线,多插几次,并且退出终端,然后重新打开,再输入命令就能发现连接的设备,再无法连接请重启电脑,等待系统初始化环境设置。
需要注意的是并不是所有的android机器都能解决,比如有的手机有供应商ID,缺少产品ID,依然无法识别,这种奇葩形的就只能找其它方法了。
2. Windows(XP、7)下面也可以同样的方法解决。但是前提也是已经指定了adb环境变量并且安装了相应Android手机设备的adb usb驱动,判断有没有装通过将手机和Windows连接,设备管理器里面没有发现显示有问号的设备就说明装了,有就说明没有安装。然后在C:\Users\username\.android目录下面也有一个adb_usb.ini文件,像前面那样别起一行添加相应设备的Vender ID,重启adb server进程即可。
3. 如果发现无法启动ADB SERVER, 运行下面这行查看具体原因:
adb nodaemon server
Tagged as: , , Comments Off
17Mar/16

install Python Imaging Library on mac

Posted by Nick Xu

# download
curl -O -L http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz
# extract
tar -xzf Imaging-1.1.7.tar.gz
cd Imaging-1.1.7
# build and install
ln -s /usr/local/include/freetype2 /usr/local/include/freetype
xcode-select --install
python setup.py build
sudo python setup.py install
# or install it for just you without requiring admin permissions:
# python setup.py install --user
Tagged as: , Comments Off
2Mar/16

在 Mac 下用 Homebrew 安装 MySQL

Posted by Nick Xu

在 Mac 下用 Homebrew 安装 MySQL, 网上的教程倒是很多,不过大多数都很默契地雷同。如果稍有点定制要求,就无从下手了。

我先也不免俗,从基本的开始:

一、首先安装 Homebrew

1
2
3
$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
$ brew install git
$ brew update

二、安装 MySQL

用下面的命令就可以自动安装了:

1
$ brew install mysql

如果想让 MySQL 开机自动启动,可以如下操作:

1
2
3
4
$ mkdir -p ~/Library/LaunchAgents
$ ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
$ find /usr/local/Cellar/mysql/ -name "homebrew.mxcl.mysql.plist" -exec cp {} ~/Library/LaunchAgents/ \;
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

设置 MySQL 用户以及数据存放地址

1
2
$ unset TMPDIR
$ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

好了,可以启动了

1
$ mysql.server start

另外的参数还有 {start|stop|restart|reload|force-reload|status}

大部分的介绍就在此结束了。

三、更详细的设置

配置文件 my.cnf

作为用惯了 Linux 的人, 一定会去 /etc 下找 my.cnf, 让你失望了,这个文件要自己建立。如果看一下帮助

1
$ mysqld --help --verbose

就会发现系统会按这个顺序去找 my.cnf

  1. /etc/my.cnf
  2. /etc/mysql/my.cnf
  3. /usr/local/etc/my.cnf
  4. ~/.my.cnf

一般网上大虾都会这么教小白建立 my.cnf, 其实这个默认的文件里面几乎没什么内容。

1
$ sudo cp $(brew --prefix mysql)/support-files/my-default.cnf /etc/my.cnf

所以,还是自己老老实实参考 linux 下的配置文件吧。

my.cnf (my.cnf.txt)download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with 
# ticks/quotes escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing 
# the socket location.
[client]
port        = 3306
#socket     = /var/run/mysqld/mysqld.sock

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

# This was formally known as [safe_mysqld]. Both versions 
# are currently parsed.
[mysqld_safe]
#socket     = /var/run/mysqld/mysqld.sock
#nice       = 0

[mysqld]
#
# * Basic Settings
#

#
# * IMPORTANT
#   If you make changes to these settings and your system uses 
#   apparmor, you may also need to also adjust 
#   /etc/apparmor.d/usr.sbin.mysqld.
#

#user       = mysql
#socket     = /var/run/mysqld/mysqld.sock
port        = 3306
#basedir    = /usr
datadir    = /usr/local/var/mysql
#tmpdir     = /tmp
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address        = 127.0.0.1
#
# * Fine Tuning
#
key_buffer          = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size   = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover         = BACKUP
#max_connections       = 100
#table_cache           = 64
#thread_concurrency    = 10
#
# * Query Cache Configuration
#
query_cache_limit   = 1M
query_cache_size    = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1

log_error                = /usr/local/var/mysql/MacBook15.local.err

# Here you can see queries with especially long duration
#log_slow_queries   = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or 
# for replication.
# note: if you are setting up a replication slave, see 
#       README.Debian about other settings you may need 
#       to change.
#server-id          = 1
#log_bin            = /var/log/mysql/mysql-bin.log
expire_logs_days    = 10
max_binlog_size     = 100M
#binlog_do_db       = include_database_name
#binlog_ignore_db   = include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

# Query Caching
query-cache-type = 1

# Default to InnoDB
default-storage-engine=innodb

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]
#no-auto-rehash # faster start of mysql but no tab completition

[isamchk]
key_buffer      = 16M

错误日志

错误日志默认会存在数据目录下,也就是上面所定义的 /usr/local/var/mysql/,如果 Mac 电脑名字是 MacBook,那日志的全路径就是 /usr/local/var/mysql/MacBook.local.err

让别的电脑访问数据库

取消下面两个文件中关于绑定 127.0.0.1 的语句
/etc/my.cnf

1
bind-address = 127.0.0.1

~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

1
<string>--bind-address=127.0.0.1</string>

就我个人而言,不需要 MySQL 自启动,所以只要在 /etc/my.cnf 改一下就好了。

Tagged as: Comments Off
2Mar/16

用gzip/gunzip备份,压缩数据库

Posted by Nick Xu

缘起:公司服务器上数据库越来越大,以前备份数据时将数据库整个dump出来,然后用tar czvf压缩备份,还原时用tar zxvf解压缩,每次都作这种事情的确很烦。

浏览PostgreSQL文档时,看到介绍数据库备份部分的内容时,找到一个不错的办法可以直接将数据库dump出来并自动压缩,还原数据库也可以从压缩文件直接还原到数据库,原来数据库dump出来大概50多M的plain text文件压缩后只有不到7MB,非常利于网络传输,且原理并不复杂,只用到了Linux的gzip/gunzip和管道命令而已。

MySQL:

Ruby代码  收藏代码
  1. # Backup
  2. mysqldump -u username -p dbname | gzip > outfile.gz
  3. # Restore
  4. cat outfile.gz | gunzip | mysql -u username -p dbname

PosgreSQL:

Ruby代码  收藏代码
  1. # Backup
  2. pg_dump dbname | gzip > outfile.gz
  3. # Restore
  4. cat outfile.gz | gunzip | psql dbname

还可以远程备份/还原,拿PosgreSQL举例:

Ruby代码  收藏代码
  1. pg_dump -h host1 dbname | psql -h host2 dbname

这种用法虽然基础,但确让我又一次体会到了Linux/Unix下面的工具,作一件小事,并把他做好这个原则。自己写代码模块的时候,习惯把关联功能集成在一起,比如一个encrypt模块,我会把加密/解密两个方法成对写成一个scope中,且其他人大抵也多是如此操作,合乎常理呀。但是Linux下面就把压缩,解压缩用两个程序来处理(gzip/gunzip),然后用管道把他们串联起来,然后就可以去执行一些匪夷所思的任务。赞叹

Tagged as: , Comments Off
1Mar/16

killing of MySQL processlist(清理mysql所有队列)

Posted by Nick Xu

mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root' and time > 3 into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;
site
site
test